diff --git a/CHANGELOG.md b/CHANGELOG.md index 4b08b72..a4ca7f9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ * Removed OffsetAwareRuleSet, it's a RuleSet with optional attributes filename and offset * Improved performance of block parsing by using StringScanner * Improve `RuleSet#parse_declarations!` performance by using substring search istead of regexps +* Fix error when parsing values consisting of `!important` only ### Version v1.18.0 diff --git a/lib/css_parser/rule_set.rb b/lib/css_parser/rule_set.rb index 82c7a99..8f3224f 100644 --- a/lib/css_parser/rule_set.rb +++ b/lib/css_parser/rule_set.rb @@ -667,7 +667,7 @@ def parse_declarations!(block) # :nodoc: value = decs[(colon + 1)..] property.strip! value.strip! - next if property.empty? || value.empty? + next if property.empty? || value.empty? || value.start_with?(CssParser::IMPORTANT_IN_PROPERTY_RX) add_declaration!(property, value) continuation = nil diff --git a/test/test_css_parser_misc.rb b/test/test_css_parser_misc.rb index 007a742..817c436 100644 --- a/test/test_css_parser_misc.rb +++ b/test/test_css_parser_misc.rb @@ -227,47 +227,58 @@ def test_enumerator_nonempty end end + def with_value_exception(&block) + # Raise synthetic exception to test error handling because there is no known way to cause it naturally + CssParser::RuleSet::Declarations::Value.stub :new, -> { raise ArgumentError.new, 'stub' }, &block + end + def test_catching_argument_exceptions_for_add_rule - cp_with_exceptions = Parser.new(rule_set_exceptions: true) - assert_raises ArgumentError, 'background-color value is empty' do - cp_with_exceptions.add_rule!(selectors: 'body', block: 'background-color: !important') - end + with_value_exception do + cp_with_exceptions = Parser.new(rule_set_exceptions: true) + assert_raises ArgumentError, 'stub' do + cp_with_exceptions.add_rule!(selectors: 'body', block: 'background-color: blue') + end - cp_without_exceptions = Parser.new(rule_set_exceptions: false) - cp_without_exceptions.add_rule!(selectors: 'body', block: 'background-color: !important') + cp_without_exceptions = Parser.new(rule_set_exceptions: false) + cp_without_exceptions.add_rule!(selectors: 'body', block: 'background-color: blue') + end end def test_catching_argument_exceptions_for_add_rule_positional - cp_with_exceptions = Parser.new(rule_set_exceptions: true) + with_value_exception do + cp_with_exceptions = Parser.new(rule_set_exceptions: true) + + assert_raises ArgumentError, 'stub' do + _, err = capture_io do + cp_with_exceptions.add_rule!('body', 'background-color: blue') + end + assert_includes err, "DEPRECATION" + end - assert_raises ArgumentError, 'background-color value is empty' do + cp_without_exceptions = Parser.new(rule_set_exceptions: false) _, err = capture_io do - cp_with_exceptions.add_rule!('body', 'background-color: !important') + cp_without_exceptions.add_rule!('body', 'background-color: blue') end assert_includes err, "DEPRECATION" end - - cp_without_exceptions = Parser.new(rule_set_exceptions: false) - _, err = capture_io do - cp_without_exceptions.add_rule!('body', 'background-color: !important') - end - assert_includes err, "DEPRECATION" end def test_catching_argument_exceptions_for_add_rule_with_offsets - cp_with_exceptions = Parser.new(capture_offsets: true, rule_set_exceptions: true) + with_value_exception do + cp_with_exceptions = Parser.new(capture_offsets: true, rule_set_exceptions: true) + + assert_raises ArgumentError, 'stub' do + _, err = capture_io do + cp_with_exceptions.add_rule_with_offsets!('body', 'background-color: blue', 'inline', 1) + end + assert_includes err, "DEPRECATION" + end - assert_raises ArgumentError, 'background-color value is empty' do + cp_without_exceptions = Parser.new(capture_offsets: true, rule_set_exceptions: false) _, err = capture_io do - cp_with_exceptions.add_rule_with_offsets!('body', 'background-color: !important', 'inline', 1) + cp_without_exceptions.add_rule_with_offsets!('body', 'background-color: blue', 'inline', 1) end assert_includes err, "DEPRECATION" end - - cp_without_exceptions = Parser.new(capture_offsets: true, rule_set_exceptions: false) - _, err = capture_io do - cp_without_exceptions.add_rule_with_offsets!('body', 'background-color: !important', 'inline', 1) - end - assert_includes err, "DEPRECATION" end end diff --git a/test/test_rule_set.rb b/test/test_rule_set.rb index d1bf9aa..1a40c1f 100644 --- a/test/test_rule_set.rb +++ b/test/test_rule_set.rb @@ -122,6 +122,12 @@ def test_overriding_specificity end end + def test_important_without_value + declarations = 'color: !important; background-color: #fff' + rs = RuleSet.new(selectors: '#content p, a', block: declarations) + assert_equal('background-color: #fff;', rs.declarations_to_s) + end + def test_not_raised_issue68 ok = true begin