Skip to content
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
## Ruby CSS Parser CHANGELOG

### Unreleased
* Speed up `RuleSet::Declarations#to_s` ~20x: its clean-up regexp matched the empty string at every character (`[\n\r\f\t]*`), making `gsub!` replace at every position; the unreachable `\s+$` branch is dropped, output unchanged
* Allocate less in `RuleSet`: skip the downcased copy of already normalized property names, and build dimension shorthands without enumerators

### Version 3.3.0
* Fix Arbitrary local-file read via attacker-controlled `@import`
Expand Down
15 changes: 10 additions & 5 deletions lib/css_parser/rule_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class RuleSet

NUMBER_OF_DIMENSIONS = 4

NORMALIZED_PROPERTY_RX = /\A[-a-z0-9_]+\z/.freeze

DIMENSIONS = [
['margin', %w[margin-top margin-right margin-bottom margin-left]],
['padding', %w[padding-top padding-right padding-bottom padding-left]],
Expand Down Expand Up @@ -205,8 +207,7 @@ def to_s(options = {})
importance = options[:force_important] || value.important ? ' !important' : ''
memo << "#{prop}: #{value.value}#{importance}; "
end
# TODO: Clean-up regexp doesn't seem to work
str.gsub!(/^[\s^({)]+|[\n\r\f\t]*|\s+$/mx, '')
str.gsub!(/^[\s^({)]+|[\n\r\f\t]+/, '')

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this used to remove trailing whitespaces, don't we need that ?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm "since the leftmost alternative always wins, the trailing \s+$ branch can never match"

str.gsub!(/^[\s^({)]+|[\n\r\f\t]*|\s+$/mx, '')

the problem is the middle, it matches every string, so that end never worked

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so yeah this should work and we did not really need the /m or /x either

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly, the * in the middle matched the empty string everywhere, so \s+$ never got a chance; the str.strip! right after is what actually removes the trailing whitespace. And yes, the /m and /x flags are already gone in this version. Thanks a lot for the careful review, and for maintaining css_parser!

str.strip!
str
end
Expand All @@ -226,7 +227,10 @@ def ==(other)
attr_writer :declarations

def normalize_property(property)
property = property.to_s.downcase
property = property.to_s
return property if property.match?(NORMALIZED_PROPERTY_RX)

property = property.downcase
property.strip!
property
end
Expand Down Expand Up @@ -584,10 +588,11 @@ def create_dimensions_shorthand! # :nodoc:
return if declarations.size < NUMBER_OF_DIMENSIONS

DIMENSIONS.each do |property, dimensions|
values = DIMENSION_DIRECTIONS.each_with_index.with_object({}) do |(side, index), result|
values = {}
DIMENSION_DIRECTIONS.each_with_index do |side, index|
next unless (declaration = declarations[dimensions[index]])

result[side] = declaration.value
values[side] = declaration.value
end

# All four dimensions must be present
Expand Down
Loading