File tree Expand file tree Collapse file tree 3 files changed +19
-3
lines changed Expand file tree Collapse file tree 3 files changed +19
-3
lines changed Original file line number Diff line number Diff line change 2
2
3
3
### Unreleased
4
4
5
+ * Prefer ` !important ` rules over non-` !important ` rules in the same ruleset
5
6
* Minor performance improvements
6
7
7
8
### Version v1.21.0
Original file line number Diff line number Diff line change @@ -89,17 +89,20 @@ def initialize(declarations = {})
89
89
# puts declarations['margin']
90
90
# => #<CssParser::RuleSet::Declarations::Value:0x00000000030c1838 @important=true, @order=2, @value="0px auto">
91
91
#
92
- # If the property already exists its value will be over-written.
92
+ # If the property already exists its value will be over-written unless it was !important and the new value
93
+ # is not !important.
93
94
# If the value is empty - property will be deleted
94
95
def []=( property , value )
95
96
property = normalize_property ( property )
97
+ currently_important = declarations [ property ] &.important
96
98
97
- if value . is_a? ( Value )
99
+ if value . is_a? ( Value ) && ( ! currently_important || value . important )
98
100
declarations [ property ] = value
99
101
elsif value . to_s . strip . empty?
100
102
delete property
101
103
else
102
- declarations [ property ] = Value . new ( value )
104
+ value = Value . new ( value )
105
+ declarations [ property ] = value if !currently_important || value . important
103
106
end
104
107
rescue ArgumentError => e
105
108
raise e . exception , "#{ property } #{ e . message } "
Original file line number Diff line number Diff line change @@ -109,6 +109,18 @@ def test_merging_important
109
109
assert_equal 'black !important;' , merged [ 'color' ]
110
110
end
111
111
112
+ def test_prioritising_important_over_non_important_in_the_same_block
113
+ rs1 = RuleSet . new ( block : 'color: black !important; color: red;' )
114
+ merged = CssParser . merge ( rs1 )
115
+ assert_equal 'black !important;' , merged [ 'color' ]
116
+ end
117
+
118
+ def test_prioritising_two_important_declarations_in_the_same_block
119
+ rs1 = RuleSet . new ( block : 'color: black !important; color: red !important;' )
120
+ merged = CssParser . merge ( rs1 )
121
+ assert_equal 'red !important;' , merged [ 'color' ]
122
+ end
123
+
112
124
def test_merging_multiple_important
113
125
rs1 = RuleSet . new ( block : 'color: black !important;' , specificity : 1000 )
114
126
rs2 = RuleSet . new ( block : 'color: red !important;' , specificity : 1 )
You can’t perform that action at this time.
0 commit comments