Skip to content

Commit 0b729fb

Browse files
committed
Update normalize_style helper method with an allowlist
1 parent 7d61a1a commit 0b729fb

3 files changed

Lines changed: 8 additions & 4 deletions

File tree

lib/prawn_html/attributes.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ def apply_rule!(merged_styles:, rule:, value:, options:)
154154
return (@initial << rule) if value == 'initial'
155155

156156
if rule[:set] == :append_styles
157-
val = Utils.normalize_style(value)
157+
val = Utils.normalize_style(value, rule[:values])
158158
(merged_styles[rule[:key]] ||= []) << val if val
159159
else
160160
opts = rule[:options] ? options[rule[:options]] : nil

lib/prawn_html/utils.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,13 @@ def copy_value(value, options: nil)
103103
# Normalize a style value
104104
#
105105
# @param value [String] string value
106+
# @param accepted_values [Array] allowlist of valid values (symbols)
106107
#
107108
# @return [Symbol] style value or nil
108-
def normalize_style(value)
109+
def normalize_style(value, accepted_values)
109110
val = value&.strip&.downcase
110-
NORMALIZE_STYLES[val]
111+
ret = NORMALIZE_STYLES[val]
112+
accepted_values.include?(ret) ? ret : nil
111113
end
112114

113115
# Unquotes a string

spec/units/prawn_html/utils_spec.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,9 @@
154154
end
155155

156156
describe '.normalize_style' do
157-
subject(:normalize_style) { described_class.normalize_style(value) }
157+
subject(:normalize_style) { described_class.normalize_style(value, accepted_values) }
158+
159+
let(:accepted_values) { [:bold, :italic] }
158160

159161
context 'with an invalid value (ex. "some_string")' do
160162
let(:value) { 'some_string' }

0 commit comments

Comments
 (0)