Skip to content

Commit

Permalink
Deprecate add_rule! and add_rule_with_offsets! for append_rule!
Browse files Browse the repository at this point in the history
  • Loading branch information
stoivo committed May 16, 2024
1 parent 94c498f commit 7a29440
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 20 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

### Unreleased

* Deprecate `add_rule!` (positional arguments)and `add_rule_with_offsets!` for `add_rule!` (keyword argument)
* RuleSet initialize now takes keyword argument, positional arguments are still supported but deprecated
* Removed OffsetAwareRuleSet, it's a RuleSet with optional attributes filename and offset

Expand Down
33 changes: 25 additions & 8 deletions lib/css_parser/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -164,32 +164,49 @@ def add_block!(block, options = {})
parse_block_into_rule_sets!(block, options)
end

# Add a CSS rule by setting the +selectors+, +declarations+ and +media_types+.
# Add a CSS rule by setting the +selectors+, +declarations+
# and +media_types+. Optional pass +filename+ , +offset+ for source
# reference too.
#
# +media_types+ can be a symbol or an array of symbols. default to :all
# optional fields for source location for source location
# +filename+ can be a string or uri pointing to the file or url location.
# +offset+ should be Range object representing the start and end byte locations where the rule was found in the file.
def append_rule!(selectors: nil, block: nil, filename: nil, offset: nil, media_types: :all)
rule_set = RuleSet.new(
selectors: selectors, block: block,
offset: offset, filename: filename
)

def add_rule!(selectors, declarations, media_types = :all)
rule_set = RuleSet.new(selectors: selectors, block: 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+ and +media_types+.
#
# +media_types+ can be a symbol or an array of symbols. default to :all
# optional fields for source location for source location
# +filename+ can be a string or uri pointing to the file or url location.
# +offset+ should be Range object representing the start and end byte locations where the rule was found in the file.

def add_rule!(selectors, declarations, media_types = :all)
warn '[DEPRECATION] `add_rule!` is deprecated. Please use `append_rule!` instead.', uplevel: 1

append_rule!(selectors: selectors, block: declarations, media_types: media_types)
end

# Add a CSS rule by setting the +selectors+, +declarations+, +filename+, +offset+ and +media_types+.
#
# +filename+ can be a string or uri pointing to the file or url location.
# +offset+ should be Range object representing the start and end byte locations where the rule was found in the file.
# +media_types+ can be a symbol or an array of symbols.
def add_rule_with_offsets!(selectors, declarations, filename, offset, media_types = :all)
rule_set = RuleSet.new(
selectors: selectors, block: declarations,
offset: offset, filename: filename
warn '[DEPRECATION] `add_rule_with_offsets!` is deprecated. Please use `append_rule!` instead.', uplevel: 1
append_rule!(
selectors: selectors, block: declarations, media_types: media_types,
filename: filename, offset: offset
)

add_rule_set!(rule_set, media_types)
end

# Add a CssParser RuleSet object.
Expand Down
12 changes: 0 additions & 12 deletions test/test_css_parser_loading.rb
Original file line number Diff line number Diff line change
Expand Up @@ -204,16 +204,4 @@ 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
24 changes: 24 additions & 0 deletions test/test_css_parser_misc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -226,4 +226,28 @@ def test_enumerator_nonempty
assert_equal 'color: black;', desc
end
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!('body', 'background-color: !important')
end

cp_without_exceptions = Parser.new(rule_set_exceptions: false)

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

def test_catching_argument_exceptions_for_add_rule_with_offsets
cp_with_exceptions = Parser.new(capture_offsets: true, rule_set_exceptions: true)

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

cp_without_exceptions = Parser.new(capture_offsets: true, rule_set_exceptions: false)

cp_without_exceptions.add_rule_with_offsets!('body', 'background-color: !important', 'inline', 1)
end
end

0 comments on commit 7a29440

Please sign in to comment.