Skip to content

Commit 24077d8

Browse files
authored
Merge pull request #168 from railsware/fix-important-without-value
Fix error when parsing values consisting of `!important` only
2 parents a2b77b7 + 037d882 commit 24077d8

4 files changed

Lines changed: 44 additions & 25 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* Removed OffsetAwareRuleSet, it's a RuleSet with optional attributes filename and offset
1111
* Improved performance of block parsing by using StringScanner
1212
* Improve `RuleSet#parse_declarations!` performance by using substring search istead of regexps
13+
* Fix error when parsing values consisting of `!important` only
1314

1415
### Version v1.18.0
1516

lib/css_parser/rule_set.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class RuleSet
3131
SEMICOLON = ';'.freeze
3232
LPAREN = '('.freeze
3333
RPAREN = ')'.freeze
34+
IMPORTANT = '!important'.freeze
3435
class Declarations
3536
class Value
3637
attr_reader :value
@@ -667,7 +668,7 @@ def parse_declarations!(block) # :nodoc:
667668
value = decs[(colon + 1)..]
668669
property.strip!
669670
value.strip!
670-
next if property.empty? || value.empty?
671+
next if property.empty? || value.empty? || value.casecmp?(IMPORTANT)
671672

672673
add_declaration!(property, value)
673674
continuation = nil

test/test_css_parser_misc.rb

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -227,47 +227,58 @@ def test_enumerator_nonempty
227227
end
228228
end
229229

230+
def with_value_exception(&block)
231+
# Raise synthetic exception to test error handling because there is no known way to cause it naturally
232+
CssParser::RuleSet::Declarations::Value.stub :new, -> { raise ArgumentError.new, 'stub' }, &block
233+
end
234+
230235
def test_catching_argument_exceptions_for_add_rule
231-
cp_with_exceptions = Parser.new(rule_set_exceptions: true)
232-
assert_raises ArgumentError, 'background-color value is empty' do
233-
cp_with_exceptions.add_rule!(selectors: 'body', block: 'background-color: !important')
234-
end
236+
with_value_exception do
237+
cp_with_exceptions = Parser.new(rule_set_exceptions: true)
238+
assert_raises ArgumentError, 'stub' do
239+
cp_with_exceptions.add_rule!(selectors: 'body', block: 'background-color: blue')
240+
end
235241

236-
cp_without_exceptions = Parser.new(rule_set_exceptions: false)
237-
cp_without_exceptions.add_rule!(selectors: 'body', block: 'background-color: !important')
242+
cp_without_exceptions = Parser.new(rule_set_exceptions: false)
243+
cp_without_exceptions.add_rule!(selectors: 'body', block: 'background-color: blue')
244+
end
238245
end
239246

240247
def test_catching_argument_exceptions_for_add_rule_positional
241-
cp_with_exceptions = Parser.new(rule_set_exceptions: true)
248+
with_value_exception do
249+
cp_with_exceptions = Parser.new(rule_set_exceptions: true)
250+
251+
assert_raises ArgumentError, 'stub' do
252+
_, err = capture_io do
253+
cp_with_exceptions.add_rule!('body', 'background-color: blue')
254+
end
255+
assert_includes err, "DEPRECATION"
256+
end
242257

243-
assert_raises ArgumentError, 'background-color value is empty' do
258+
cp_without_exceptions = Parser.new(rule_set_exceptions: false)
244259
_, err = capture_io do
245-
cp_with_exceptions.add_rule!('body', 'background-color: !important')
260+
cp_without_exceptions.add_rule!('body', 'background-color: blue')
246261
end
247262
assert_includes err, "DEPRECATION"
248263
end
249-
250-
cp_without_exceptions = Parser.new(rule_set_exceptions: false)
251-
_, err = capture_io do
252-
cp_without_exceptions.add_rule!('body', 'background-color: !important')
253-
end
254-
assert_includes err, "DEPRECATION"
255264
end
256265

257266
def test_catching_argument_exceptions_for_add_rule_with_offsets
258-
cp_with_exceptions = Parser.new(capture_offsets: true, rule_set_exceptions: true)
267+
with_value_exception do
268+
cp_with_exceptions = Parser.new(capture_offsets: true, rule_set_exceptions: true)
269+
270+
assert_raises ArgumentError, 'stub' do
271+
_, err = capture_io do
272+
cp_with_exceptions.add_rule_with_offsets!('body', 'background-color: blue', 'inline', 1)
273+
end
274+
assert_includes err, "DEPRECATION"
275+
end
259276

260-
assert_raises ArgumentError, 'background-color value is empty' do
277+
cp_without_exceptions = Parser.new(capture_offsets: true, rule_set_exceptions: false)
261278
_, err = capture_io do
262-
cp_with_exceptions.add_rule_with_offsets!('body', 'background-color: !important', 'inline', 1)
279+
cp_without_exceptions.add_rule_with_offsets!('body', 'background-color: blue', 'inline', 1)
263280
end
264281
assert_includes err, "DEPRECATION"
265282
end
266-
267-
cp_without_exceptions = Parser.new(capture_offsets: true, rule_set_exceptions: false)
268-
_, err = capture_io do
269-
cp_without_exceptions.add_rule_with_offsets!('body', 'background-color: !important', 'inline', 1)
270-
end
271-
assert_includes err, "DEPRECATION"
272283
end
273284
end

test/test_rule_set.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,12 @@ def test_overriding_specificity
122122
end
123123
end
124124

125+
def test_important_without_value
126+
declarations = 'color: !important; background-color: #fff'
127+
rs = RuleSet.new(selectors: '#content p, a', block: declarations)
128+
assert_equal('background-color: #fff;', rs.declarations_to_s)
129+
end
130+
125131
def test_not_raised_issue68
126132
ok = true
127133
begin

0 commit comments

Comments
 (0)