Skip to content

Commit c95cb37

Browse files
authored
Merge pull request #28 from blocknotes/extra-styles
Handle tag extra_styles
2 parents 9d1b857 + ebf4836 commit c95cb37

4 files changed

Lines changed: 49 additions & 4 deletions

File tree

lib/prawn_html/tag.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ def process_styles(element_styles: nil)
4545
attrs.merge_text_styles!(tag_styles, options: options) if respond_to?(:tag_styles)
4646
attrs.merge_text_styles!(element_styles, options: options) if element_styles
4747
attrs.merge_text_styles!(attrs.style, options: options)
48+
attrs.merge_text_styles!(extra_styles, options: options) if respond_to?(:extra_styles)
4849
end
4950

5051
# Styles to apply on tag closing

lib/prawn_html/tags/a.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ module Tags
55
class A < Tag
66
ELEMENTS = [:a].freeze
77

8-
def tag_styles
9-
return unless attrs.href
8+
def extra_styles
9+
attrs.href ? "href: #{attrs.href}" : nil
10+
end
1011

12+
def tag_styles
1113
<<~STYLES
1214
color: #00E;
13-
href: #{attrs.href};
1415
text-decoration: underline;
1516
STYLES
1617
end

spec/units/prawn_html/tag_spec.rb

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,49 @@
4141
end
4242
end
4343

44+
describe '#process_styles' do
45+
subject(:process_styles) { tag.process_styles(element_styles: element_styles) }
46+
47+
let(:element_styles) { nil }
48+
49+
before do
50+
allow(tag.attrs).to receive(:merge_text_styles!)
51+
end
52+
53+
it 'merges the inline styles' do
54+
process_styles
55+
expect(tag.attrs).to have_received(:merge_text_styles!).with('color: #0088ff', options: {})
56+
end
57+
58+
context 'with some additional styles' do
59+
let(:some_tag_class) do
60+
Class.new(described_class) do
61+
def extra_styles
62+
'color: green; text-decoration: underline'
63+
end
64+
65+
def tag_styles
66+
'color: yellow; font-style: italic'
67+
end
68+
end
69+
end
70+
71+
let(:element_styles) { 'color: red; font-weight: bold' }
72+
let(:tag) { some_tag_class.new(:some_tag, attributes: attributes) }
73+
74+
it 'merges the tag styles', :aggregate_failures do
75+
process_styles
76+
77+
expected_styles = 'color: yellow; font-style: italic'
78+
expect(tag.attrs).to have_received(:merge_text_styles!).with(expected_styles, options: {}).ordered
79+
expect(tag.attrs).to have_received(:merge_text_styles!).with(element_styles, options: {}).ordered
80+
expect(tag.attrs).to have_received(:merge_text_styles!).with('color: #0088ff', options: {}).ordered
81+
expected_styles = 'color: green; text-decoration: underline'
82+
expect(tag.attrs).to have_received(:merge_text_styles!).with(expected_styles, options: {}).ordered
83+
end
84+
end
85+
end
86+
4487
describe '#styles' do
4588
subject(:styles) { tag.styles }
4689

spec/units/prawn_html/tags/a_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
end
1212

1313
it "styles doesn't include the link property" do
14-
expect(a.styles).to eq(color: 'ffbb11')
14+
expect(a.styles).to eq(color: 'ffbb11', styles: [:underline])
1515
end
1616
end
1717

0 commit comments

Comments
 (0)