-
Notifications
You must be signed in to change notification settings - Fork 110
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
Preserve rules priority while expanding shorthands #119
Conversation
lib/css_parser/rule_set.rb
Outdated
@@ -113,8 +113,44 @@ def delete(property) | |||
end | |||
alias remove_declaration! delete | |||
|
|||
def replace_declaration!(property, replacements) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was expecting this to be something like:
declarations.fetch(property) # blow up when unknown
declarations.each_with_object({}) do |(k, v), h|
next if replacements.key?(k)
if k == property
h.merge! replacements
else
h[k] = v
end
end
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should obey !important
& declarations order, because we shouldn't override subsequent declarations and declarations before with !important
if we're not, so I don't see easier way to do that. Will add .key?
check for the replaced property though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the idea!
bb456c4
to
d06c5d3
Compare
So yeah, it's more or less RFC at this point and I will add |
e134cc1
to
6cfcca1
Compare
end | ||
|
||
def value=(value) | ||
value = value.to_s.sub(/\s*;\s*\Z/, '').gsub(CssParser::IMPORTANT_IN_PROPERTY_RX, '').strip | ||
value = value.to_s.sub(/\s*;\s*\Z/, '') | ||
self.important = !value.slice!(CssParser::IMPORTANT_IN_PROPERTY_RX).nil? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
neat!
replacement_values = declarations.values | ||
property_index = replacement_keys.index(property) | ||
|
||
replacements = replacement_declarations.each.with_object({}) do |(key, value), result| |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
some comment on what this section does / why it's needed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
lib/css_parser/rule_set.rb
Outdated
next | ||
end | ||
|
||
next if declarations[key].important && !value.important |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks like the the if
above, maybe assign to a variable ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
... or do some if + if/else flow
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's opposite of the one above, made order the same to make it more clear.
lib/css_parser/rule_set.rb
Outdated
def each(&block) | ||
declarations.sort_by { |_name, value| value.order }.each(&block) | ||
declarations.each(&block) | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can forward this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks great!
... lmk when you thing this is ready to merge
0fc2d37
to
c2d54df
Compare
CI failed |
lib/css_parser/rule_set.rb
Outdated
if !declarations.key?(key) || (value.important && !declarations[key].important) | ||
result[key] = value | ||
next unless declarations.key?(key) | ||
|
||
replaced_index = replacement_keys.index(key) | ||
replacement_keys.delete_at(replaced_index) | ||
replacement_values.delete_at(replaced_index) | ||
property_index -= 1 if replaced_index < property_index | ||
next | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if !declarations.key?(key) || (value.important && !declarations[key].important) | |
result[key] = value | |
next unless declarations.key?(key) | |
replaced_index = replacement_keys.index(key) | |
replacement_keys.delete_at(replaced_index) | |
replacement_values.delete_at(replaced_index) | |
property_index -= 1 if replaced_index < property_index | |
next | |
end | |
if !declarations.key?(key) | |
result[key] = value | |
elsif !declarations[key].important && value.important # explain what this means and does | |
result[key] = value | |
replaced_index = replacement_keys.index(key) | |
replacement_keys.delete_at(replaced_index) | |
replacement_values.delete_at(replaced_index) | |
property_index -= 1 if replaced_index < property_index | |
elsif declarations[key].important && value.important # explain what this means | |
# discard | |
elsif property_index < replacement_keys.index(key) # explain what this means | |
# discard | |
else | |
result[key] = value | |
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added more comments to the existing code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
great comments!
Yeah, trying to test forwardable, but looks like it's not trivial here minitest/minitest#545 |
c2d54df
to
e5421ee
Compare
Comments are more or less addressed, CI is passing, I think it's ready if everything is fine for you. |
Thanks 🎉! Can I haz release? |
1.8.0
…On Thu, Jan 21, 2021 at 11:36 AM Slava Kardakov ***@***.***> wrote:
Thanks 🎉! Can I haz release?
—
You are receiving this because you modified the open/close state.
Reply to this email directly, view it on GitHub
<#119 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAACYZ372C227ZMGZHBL4F3S3B62PANCNFSM4WJEACNA>
.
|
We can piggyback on
Hash
preserving it's order in all supported rubies & drop order. Looks quite ugly, but does the trick.I assume that it will be faster if we'll properly track and propagate
order
in all places, but I suspect it will be also harder to work with.And we're little faster at parsing because of
order
is removed:Before:
After:
And little slower at expanding shorthands (tested on github's 500KB css with
parser.each_rule_set { |rule_set, _media_type| rule_set.expand_shorthand! }
, not sure how relevant this file is though)Before:
After:
Fixes #111