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

Fix parsing space-less media-features #141

Merged
merged 1 commit into from Sep 1, 2023

Conversation

jdelStrother
Copy link
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 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
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.

Copy link
Contributor

@grosser grosser left a comment

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
6 checks passed
@grosser
Copy link
Contributor

grosser commented Sep 1, 2023

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