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

Allow rule set argument exceptions to be muted #132

Merged
merged 2 commits into from Sep 16, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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