Skip to content

Commit bee8350

Browse files
committed
feat: implement !important handling in CSS rules
1 parent 0dd911e commit bee8350

File tree

4 files changed

+27
-9
lines changed

4 files changed

+27
-9
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,9 @@ prawn-svg supports CSS, both in `<style>` blocks and `style` attributes.
114114
In CSS selectors you can use element names, IDs, classes, attributes (existence, `=`, `^=`, `$=`, `*=`, `~=`, `|=`)
115115
and all combinators (` `, `>`, `+`, `~`).
116116
The pseudo-classes `:first-child`, `:last-child` and `:nth-child(n)` (where n is a number) also work.
117+
`!important` is supported.
117118

118-
Pseudo-elements and the other pseudo-classes are not supported. Specificity ordering is
119-
implemented, but `!important` is not.
119+
Pseudo-elements and the other pseudo-classes are not supported.
120120

121121
## Not supported
122122

lib/prawn/svg/elements/base.rb

+2-3
Original file line numberDiff line numberDiff line change
@@ -216,9 +216,8 @@ def extract_attributes_and_properties
216216

217217
# Apply stylesheet styles
218218
if (styles = document.element_styles[source])
219-
# TODO : implement !important, at the moment it's just ignored
220-
styles.each do |name, value, _important|
221-
@properties.set(name, value)
219+
styles.each do |name, value, important|
220+
@properties.set(name, value, important: important)
222221
end
223222
end
224223

lib/prawn/svg/properties.rb

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module Prawn::SVG
22
class Properties
3-
Config = Struct.new(:default, :inheritable?, :valid_values, :attr, :ivar)
3+
Config = Struct.new(:default, :inheritable?, :valid_values, :attr, :ivar, :id)
44

55
EM = 16
66
FONT_SIZES = {
@@ -49,6 +49,7 @@ class Properties
4949

5050
PROPERTIES.each do |name, value|
5151
value.attr = name.gsub('-', '_')
52+
value.id = value.attr.to_sym
5253
value.ivar = "@#{value.attr}"
5354
end
5455

@@ -57,9 +58,11 @@ class Properties
5758
ATTR_NAMES = PROPERTIES.keys.map { |name| name.gsub('-', '_') }
5859

5960
attr_accessor(*ATTR_NAMES)
61+
attr_reader :important_ids
6062

6163
def initialize
6264
@numeric_font_size = EM
65+
@important_ids = []
6366
end
6467

6568
def load_default_stylesheet
@@ -70,10 +73,11 @@ def load_default_stylesheet
7073
self
7174
end
7275

73-
def set(name, value)
76+
def set(name, value, important: false)
7477
name = name.to_s.downcase
7578
if (config = PROPERTIES[name])
76-
if (value = parse_value(config, value.strip))
79+
if (value = parse_value(config, value.strip)) && (important || !@important_ids.include?(config.id))
80+
@important_ids << config.id if important
7781
instance_variable_set(config.ivar, value)
7882
end
7983
elsif name == 'font'
@@ -99,14 +103,15 @@ def compute_properties(other)
99103
PROPERTY_CONFIGS.each do |config|
100104
value = other.send(config.attr)
101105

102-
if value && value != 'inherit'
106+
if value && value != 'inherit' && (!@important_ids.include?(config.id) || other.important_ids.include?(config.id))
103107
instance_variable_set(config.ivar, value)
104108

105109
elsif value.nil? && !config.inheritable?
106110
instance_variable_set(config.ivar, config.default)
107111
end
108112
end
109113

114+
@important_ids += other.important_ids
110115
@numeric_font_size = calculate_numeric_font_size
111116
nil
112117
end

spec/sample_svg/important.svg

+14
Loading

0 commit comments

Comments
 (0)