Skip to content

Commit ae22b92

Browse files
committed
Handle 'inherit' font-family value
1 parent 1219665 commit ae22b92

4 files changed

Lines changed: 36 additions & 4 deletions

File tree

lib/prawn_html/attributes.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class Attributes < OpenStruct
1818
# text node styles
1919
'background' => { key: :callback, set: :callback_background },
2020
'color' => { key: :color, set: :convert_color },
21-
'font-family' => { key: :font, set: :unquote },
21+
'font-family' => { key: :font, set: :filter_font_family },
2222
'font-size' => { key: :size, set: :convert_size },
2323
'font-style' => { key: :styles, set: :append_styles, values: %i[italic] },
2424
'font-weight' => { key: :styles, set: :append_styles, values: %i[bold] },

lib/prawn_html/utils.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,16 @@ def copy_value(value, options: nil)
100100
value
101101
end
102102

103+
# Filter font family
104+
#
105+
# @param value [String] string value
106+
#
107+
# @return [Symbol] unquoted font family or nil if the input value is 'inherit'
108+
def filter_font_family(value, options: nil)
109+
result = unquote(value, options: options)
110+
result == 'inherit' ? nil : result
111+
end
112+
103113
# Normalize a style value
104114
#
105115
# @param value [String] string value
@@ -124,6 +134,6 @@ def unquote(value, options: nil)
124134
end
125135

126136
module_function :callback_background, :callback_strike_through, :convert_color, :convert_float, :convert_size,
127-
:convert_symbol, :copy_value, :normalize_style, :unquote
137+
:convert_symbol, :copy_value, :filter_font_family, :normalize_style, :unquote
128138
end
129139
end

spec/units/prawn_html/attributes_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@
5050
STYLES
5151
end
5252

53-
it 'receives the expected convert messages', :aggregate_failures do
53+
it 'receives the expected messages', :aggregate_failures do
5454
merge_text_styles!
5555

56-
expect(PrawnHtml::Utils).to have_received(:send).with(:unquote, "'Times-Roman'", options: nil)
56+
expect(PrawnHtml::Utils).to have_received(:send).with(:filter_font_family, "'Times-Roman'", options: nil)
5757
expect(PrawnHtml::Utils).to have_received(:send).with(:convert_size, '16px', options: nil)
5858
expect(PrawnHtml::Utils).to have_received(:send).with(:convert_size, '22px', options: nil)
5959
end

spec/units/prawn_html/utils_spec.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,28 @@
153153
end
154154
end
155155

156+
describe '.filter_font_family' do
157+
subject(:filter_font_family) { described_class.filter_font_family(value) }
158+
159+
context 'with any string (ex. "some_string")' do
160+
let(:value) { 'some_string' }
161+
162+
it { is_expected.to eq 'some_string' }
163+
end
164+
165+
context 'with a string with some spaces (ex. " some_string ")' do
166+
let(:value) { ' some_string ' }
167+
168+
it { is_expected.to eq 'some_string' }
169+
end
170+
171+
context 'with "inherit" value' do
172+
let(:value) { 'inherit' }
173+
174+
it { is_expected.to be_nil }
175+
end
176+
end
177+
156178
describe '.normalize_style' do
157179
subject(:normalize_style) { described_class.normalize_style(value, accepted_values) }
158180

0 commit comments

Comments
 (0)