Skip to content

Commit 1ba1a7e

Browse files
committed
Permit to remove a specific rule/value from the context styles
1 parent e513698 commit 1ba1a7e

2 files changed

Lines changed: 34 additions & 0 deletions

File tree

lib/prawn_html/attributes.rb

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

79+
# Remove an attribute value from the context styles
80+
#
81+
# @param context_styles [Hash] hash of the context styles that will be updated
82+
# @param rule [Hash] rule from the STYLES_LIST to lookup in the context style for value removal
83+
def remove_value(context_styles, rule)
84+
if rule[:set] == :append_styles
85+
context_styles[rule[:key]] -= rule[:values] if context_styles[:styles]
86+
else
87+
context_styles.delete(rule[:key])
88+
end
89+
end
90+
7991
# Update context styles applying the initial rules (if set)
8092
#
8193
# @param context_styles [Hash] hash of the context styles that will be updated

spec/units/prawn_html/attributes_spec.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,28 @@
7171
end
7272
end
7373

74+
describe '#remove_value' do
75+
subject(:remove_value) { attributes.remove_value(context_styles, rule) }
76+
77+
let(:context_styles) { { size: 9.6, styles: %i[bold italic] } }
78+
79+
context 'with a missing rule' do
80+
let(:rule) { { key: :color, set: :convert_color } }
81+
82+
it "doesn't change the context styles" do
83+
expect { remove_value }.not_to change(context_styles, :values)
84+
end
85+
end
86+
87+
context 'with an applied rule' do
88+
let(:rule) { { key: :styles, set: :append_styles, values: %i[bold] } }
89+
90+
it "changes the context styles" do
91+
expect { remove_value }.to change(context_styles, :values).from([9.6, %i[bold italic]]).to([9.6, %i[italic]])
92+
end
93+
end
94+
end
95+
7496
describe '.merge_attr!' do
7597
context 'with an empty key' do
7698
let(:key) { nil }

0 commit comments

Comments
 (0)