Skip to content

Commit e513698

Browse files
committed
Make update_styles a mandatory method
1 parent bc94102 commit e513698

6 files changed

Lines changed: 37 additions & 15 deletions

File tree

lib/prawn_html/attributes.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,15 @@ def merge_text_styles!(text_styles, options: {})
7676
process_styles(hash_styles, options: options) unless hash_styles.empty?
7777
end
7878

79+
# Update context styles applying the initial rules (if set)
80+
#
81+
# @param context_styles [Hash] hash of the context styles that will be updated
82+
#
83+
# @return [Hash] the update context styles
84+
def update_styles(context_styles)
85+
context_styles
86+
end
87+
7988
class << self
8089
# Merges attributes
8190
#

lib/prawn_html/context.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def merged_styles
5656
@merged_styles ||=
5757
each_with_object(base_styles) do |element, res|
5858
evaluate_element_styles(element, res)
59-
element.update_styles(res) if element.respond_to?(:update_styles)
59+
element.update_styles(res)
6060
end
6161
end
6262

lib/prawn_html/tag.rb

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,17 @@
22

33
module PrawnHtml
44
class Tag
5+
extend Forwardable
6+
57
CALLBACKS = {
68
'Background' => Callbacks::Background,
79
'StrikeThrough' => Callbacks::StrikeThrough
810
}.freeze
11+
912
TAG_CLASSES = %w[A B Blockquote Body Br Code Del Div H Hr I Img Li Mark Ol P Pre Small Span Sub Sup U Ul].freeze
1013

14+
def_delegators :@attrs, :styles, :update_styles
15+
1116
attr_accessor :parent
1217
attr_reader :attrs, :tag
1318

@@ -55,13 +60,6 @@ def tag_close_styles
5560
styles.slice(*Attributes::STYLES_APPLY[:tag_close])
5661
end
5762

58-
# Styles hash
59-
#
60-
# @return [Hash] hash of styles
61-
def styles
62-
attrs.styles
63-
end
64-
6563
# Styles to apply on tag opening
6664
#
6765
# @return [Hash] hash of styles to apply

lib/prawn_html/tags/small.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ module Tags
55
class Small < Tag
66
ELEMENTS = [:small].freeze
77

8-
def update_styles(styles)
9-
size = (styles[:size] || Context::DEF_FONT_SIZE) * 0.85
10-
styles[:size] = size
11-
styles
8+
def update_styles(context_styles)
9+
size = (context_styles[:size] || Context::DEF_FONT_SIZE) * 0.85
10+
context_styles[:size] = size
11+
super(context_styles)
1212
end
1313
end
1414
end

spec/units/prawn_html/context_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,8 @@ def on_context_remove(context)
125125
end
126126

127127
context 'with some elements' do
128-
let(:tag1) { instance_double(PrawnHtml::Tag, styles: { color: 'fb1', size: 12.34 }) }
129-
let(:tag2) { instance_double(PrawnHtml::Tag, styles: { color: 'abc' }) }
128+
let(:tag1) { instance_double(PrawnHtml::Tag, styles: { color: 'fb1', size: 12.34 }, update_styles: nil) }
129+
let(:tag2) { instance_double(PrawnHtml::Tag, styles: { color: 'abc' }, update_styles: nil) }
130130

131131
before do
132132
context << tag1 << tag2
@@ -145,7 +145,7 @@ def update_styles(res)
145145
end
146146
end
147147
end
148-
let(:tag1) { instance_double(PrawnHtml::Tag, styles: { color: 'fb1', size: 12.34 }) }
148+
let(:tag1) { instance_double(PrawnHtml::Tag, styles: { color: 'fb1', size: 12.34 }, update_styles: nil) }
149149
let(:tag2) { some_tag_class.new(:some_tag) }
150150

151151
before do

spec/units/prawn_html/tag_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,21 @@ def tag_styles
106106
it { is_expected.to eq({}) }
107107
end
108108

109+
describe '#update_styles' do
110+
subject(:update_styles) { tag.update_styles(context_styles) }
111+
112+
let(:context_styles) { { size: 9.6 } }
113+
114+
before do
115+
allow(tag.attrs).to receive(:update_styles)
116+
end
117+
118+
it 'asks to the attributes to update the context styles' do
119+
update_styles
120+
expect(tag.attrs).to have_received(:update_styles).with(context_styles)
121+
end
122+
end
123+
109124
describe '.class_for' do
110125
subject(:class_for) { described_class.class_for(tag_name) }
111126

0 commit comments

Comments
 (0)