Skip to content

Fix parsing space-less media-features#141

Merged
grosser merged 1 commit into
premailer:masterfrom
jdelStrother:media-spacers
Sep 1, 2023
Merged

Fix parsing space-less media-features#141
grosser merged 1 commit into
premailer:masterfrom
jdelStrother:media-spacers

Conversation

@jdelStrother

Copy link
Copy Markdown
Contributor

Previously, @media (width:500px) would be successfully parsed, but not @media(width:500px) - it would result in @media(width:500px) as a single token, effectively ignoring the media:500px media-feature and assigning all the rules in that media query to the :all namespace.

This tokenizes ( and ) separately ... which naively seems ok to me and passes all the tests, but I'd confess to being a little nervous how it behaves with real-world CSS.

Fixes #139

Pre-Merge Checklist

  • CHANGELOG.md updated with short summary

Previously, `@media (width: 500px)` would be successfully parsed, but
not `@media(width: 500px)` - it would tokenize to `@media(width:` and
then get discarded.

Fixes premailer#139
Comment thread lib/css_parser/parser.rb
Comment on lines +394 to 404
# special-case the ( and ) tokens to remove inner-whitespace
# (eg we'd prefer '(width: 500px)' to '( width: 500px )' )
case token
when '('
current_media_query << token
when ')'
current_media_query.sub!(/ ?$/, token)
else
current_media_query << token << ' '
end
end

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.

This isn't necessary to fix the bug, but does give nicer media query names. It could be skipped if we're happy with

@media(min-width: 500px) {...}

getting re-written to

@media ( min-width: 500px ) {...}

I'm happy with either, but it's maybe not worth the extra complexity. Without it, I thought the test suite needing the extra spaces in, eg, @cp.find_by_selector('body', :'( min-width: 500px )') was kind of ugly... but I don't know how much real-world usage the media_types argument to find_selector is getting.

@grosser grosser left a comment

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.

looks a bit ugly, but the test-case is nice 👍

@grosser
grosser merged commit d6368ef into premailer:master Sep 1, 2023
@grosser

grosser commented Sep 1, 2023

Copy link
Copy Markdown
Contributor

1.16.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Parsing failure with minified @media(xxx){...} rule

2 participants