Skip to content

Commit 743e5d1

Browse files
committed
Handle tag extra_styles
Extra styles are additional attributes per tag that need to be merged in the styles. For example: href for A tag.
1 parent 9d1b857 commit 743e5d1

2 files changed

Lines changed: 44 additions & 0 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

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

0 commit comments

Comments
 (0)