Skip to content

Commit

Permalink
Merge pull request #132 from markedmondson/swallow-rule-set-exceptions
Browse files Browse the repository at this point in the history
Allow rule set argument exceptions to be muted
  • Loading branch information
grosser committed Sep 16, 2022
2 parents db69609 + b75efe1 commit d6a04b3
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,8 @@

### Unreleased

* `:rule_set_exceptions` option added [#132](https://github.com/premailer/css_parser/pull/132)

### Version 1.11.0

* Do not combine border styles width/color/style are not all present
Expand Down
3 changes: 3 additions & 0 deletions lib/css_parser/parser.rb
Expand Up @@ -39,6 +39,7 @@ def initialize(options = {})
@options = {absolute_paths: false,
import: true,
io_exceptions: true,
rule_set_exceptions: true,
capture_offsets: false}.merge(options)

# array of RuleSets
Expand Down Expand Up @@ -167,6 +168,8 @@ def add_block!(block, options = {})
def add_rule!(selectors, declarations, media_types = :all)
rule_set = RuleSet.new(selectors, declarations)
add_rule_set!(rule_set, media_types)
rescue ArgumentError => e
raise e if @options[:rule_set_exceptions]
end

# Add a CSS rule by setting the +selectors+, +declarations+, +filename+, +offset+ and +media_types+.
Expand Down
12 changes: 12 additions & 0 deletions test/test_css_parser_loading.rb
Expand Up @@ -204,4 +204,16 @@ def test_toggling_not_found_exceptions

cp_without_exceptions.load_uri!("#{@uri_base}/no-exist.xyz")
end

def test_rule_set_argument_exceptions
cp_with_exceptions = Parser.new(rule_set_exceptions: true)

assert_raises ArgumentError, 'background-color value is empty' do
cp_with_exceptions.add_rule!('body', 'background-color: !important')
end

cp_without_exceptions = Parser.new(rule_set_exceptions: false)

cp_without_exceptions.add_rule!('body', 'background-color: !important')
end
end

0 comments on commit d6a04b3

Please sign in to comment.