Skip to content

Commit c5678c2

Browse files
committed
Handle initial values
1 parent 1ba1a7e commit c5678c2

2 files changed

Lines changed: 30 additions & 5 deletions

File tree

lib/prawn_html/attributes.rb

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
# frozen_string_literal: true
22

33
require 'ostruct'
4+
require 'set'
45

56
module PrawnHtml
67
class Attributes < OpenStruct
7-
attr_reader :styles
8+
attr_reader :initial, :styles
89

910
STYLES_APPLY = {
1011
block: %i[align bottom leading left margin_left padding_left position right top],
@@ -19,13 +20,13 @@ class Attributes < OpenStruct
1920
'color' => { key: :color, set: :convert_color },
2021
'font-family' => { key: :font, set: :unquote },
2122
'font-size' => { key: :size, set: :convert_size },
22-
'font-style' => { key: :styles, set: :append_styles },
23-
'font-weight' => { key: :styles, set: :append_styles },
23+
'font-style' => { key: :styles, set: :append_styles, values: %i[italic] },
24+
'font-weight' => { key: :styles, set: :append_styles, values: %i[bold] },
2425
'href' => { key: :link, set: :copy_value },
2526
'letter-spacing' => { key: :character_spacing, set: :convert_float },
2627
'list-style-type' => { key: :list_style_type, set: :unquote },
27-
'text-decoration' => { key: :styles, set: :append_styles },
28-
'vertical-align' => { key: :styles, set: :append_styles },
28+
'text-decoration' => { key: :styles, set: :append_styles, values: %i[underline] },
29+
'vertical-align' => { key: :styles, set: :append_styles, values: %i[subscript superscript] },
2930
'white-space' => { key: :white_space, set: :convert_symbol },
3031
# tag opening styles
3132
'break-before' => { key: :break_before, set: :convert_symbol },
@@ -55,6 +56,7 @@ class Attributes < OpenStruct
5556
def initialize(attributes = {})
5657
super
5758
@styles = {} # result styles
59+
@initial = Set.new
5860
end
5961

6062
# Processes the data attributes
@@ -94,6 +96,11 @@ def remove_value(context_styles, rule)
9496
#
9597
# @return [Hash] the update context styles
9698
def update_styles(context_styles)
99+
initial.each do |rule|
100+
next unless rule
101+
102+
remove_value(context_styles, rule)
103+
end
97104
context_styles
98105
end
99106

@@ -143,6 +150,8 @@ def evaluate_rule(rule_key, attr_value)
143150
end
144151

145152
def apply_rule!(merged_styles:, rule:, value:, options:)
153+
return (@initial << rule) if value == 'initial'
154+
146155
if rule[:set] == :append_styles
147156
val = Utils.normalize_style(value)
148157
(merged_styles[rule[:key]] ||= []) << val if val

spec/units/prawn_html/attributes_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,22 @@
9393
end
9494
end
9595

96+
describe '#update_styles' do
97+
subject(:update_styles) { attributes.update_styles(context_styles) }
98+
99+
let(:context_styles) { { size: 9.6 } }
100+
let(:rule) { { key: :styles, set: :append_styles, values: %i[bold] } }
101+
102+
before do
103+
allow(attributes).to receive_messages(initial: Set.new([rule]), remove_value: nil)
104+
end
105+
106+
it 'asks to the attributes to remove the value from the context styles that match the specified rule' do
107+
update_styles
108+
expect(attributes).to have_received(:remove_value).with(context_styles, rule)
109+
end
110+
end
111+
96112
describe '.merge_attr!' do
97113
context 'with an empty key' do
98114
let(:key) { nil }

0 commit comments

Comments
 (0)