File tree Expand file tree Collapse file tree 2 files changed +49
-5
lines changed
lib/article_json/export/common/html/elements
spec/article_json/export/html/elements Expand file tree Collapse file tree 2 files changed +49
-5
lines changed Original file line number Diff line number Diff line change @@ -7,24 +7,38 @@ module Image
7
7
include ArticleJSON ::Export ::Common ::HTML ::Elements ::Shared ::Caption
8
8
include ArticleJSON ::Export ::Common ::HTML ::Elements ::Shared ::Float
9
9
10
- # Generate the `<figure>` node containing the image and caption
10
+ # Generate the `<figure>` node containing the image and caption or
11
+ # an `<a>` node containing the `<figure>` node if href is present.
11
12
# @return [Nokogiri::XML::Element]
12
13
def export
14
+ figure_node
15
+ end
16
+
17
+ private
18
+
19
+ # @return [Nokogiri::XML::NodeSet]
20
+ def figure_node
13
21
create_element ( :figure , node_opts ) do |figure |
14
- figure . add_child ( image_node )
22
+ node = @element . href . present? ? href_node : image_node
23
+ figure . add_child ( node )
15
24
if @element . caption &.any?
16
25
figure . add_child ( caption_node ( :figcaption ) )
17
26
end
18
27
end
19
28
end
20
29
21
- private
22
-
23
30
# @return [Nokogiri::XML::NodeSet]
24
31
def image_node
25
32
create_element ( :img , src : @element . source_url )
26
33
end
27
34
35
+ # @return [Nokogiri::XML::NodeSet]
36
+ def href_node
37
+ create_element ( :a , href : @element . href ) do |a |
38
+ a . add_child ( image_node )
39
+ end
40
+ end
41
+
28
42
# @return [Hash]
29
43
def node_opts
30
44
return if floating_class . nil?
Original file line number Diff line number Diff line change 5
5
ArticleJSON ::Elements ::Image . new (
6
6
source_url : '/foo/bar.jpg' ,
7
7
caption : caption ,
8
- float : float
8
+ float : float ,
9
+ href : url
9
10
)
10
11
end
11
12
let ( :float ) { nil }
13
+ let ( :url ) { nil }
12
14
let ( :caption ) { [ ArticleJSON ::Elements ::Text . new ( content : 'Foo Bar' ) ] }
13
15
14
16
describe '#export' do
45
47
let ( :expected_html ) { '<figure><img src="/foo/bar.jpg"></figure>' }
46
48
it { should eq expected_html }
47
49
end
50
+
51
+ context 'when the image has an href' do
52
+ let ( :caption ) { [ ] }
53
+ let ( :url ) { 'http://devex.com' }
54
+ let ( :expected_html ) do
55
+ '<figure><a href="http://devex.com">' \
56
+ '<img src="/foo/bar.jpg"></a></figure>'
57
+ end
58
+ it { should eq expected_html }
59
+ end
60
+
61
+ context 'when the image has an href and a caption with a link' do
62
+ let ( :caption ) do
63
+ [
64
+ ArticleJSON ::Elements ::Text . new (
65
+ content : 'Foo Bar' ,
66
+ href : 'http://foo.io'
67
+ )
68
+ ]
69
+ end
70
+ let ( :url ) { 'http://devex.com' }
71
+ let ( :expected_html ) do
72
+ '<figure><a href="http://devex.com">' \
73
+ '<img src="/foo/bar.jpg"></a>' \
74
+ '<figcaption><a href="http://foo.io">Foo Bar</a></figcaption></figure>'
75
+ end
76
+ it { should eq expected_html }
77
+ end
48
78
end
49
79
end
You can’t perform that action at this time.
0 commit comments