Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error when parsing values consisting of !important only #168

Merged
merged 1 commit into from
Oct 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion lib/css_parser/rule_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class RuleSet
SEMICOLON = ';'.freeze
LPAREN = '('.freeze
RPAREN = ')'.freeze
IMPORTANT = '!important'.freeze
class Declarations
class Value
attr_reader :value
Expand Down Expand Up @@ -667,7 +668,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.casecmp?(IMPORTANT)

add_declaration!(property, value)
continuation = nil
Expand Down
59 changes: 35 additions & 24 deletions test/test_css_parser_misc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 6 additions & 0 deletions test/test_rule_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down