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

Allow replacing stylesheet to preserve the cascade #345

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

henrahmagix
Copy link

Usually this library will append media queries in separate <style> elements.
However, doing this changes the cascade such that a media rule will be used over
a non-media rule. This is because the media rule is removed from its position
and appended after everything else. It is a common case that a non-media rule
will override a media rule, so this inversion of the cascade makes it very
difficult to style for all browsers using this polyfill.

Turn on this option by setting window.RESPOND_REPLACE_STYLES = true before
sourcing this script. It will remove every stylesheet it finds and move all CSS
into the media="all" stylesheet that is appended, minus the inactive media
query rules and those of other media types, like print. The other types are
appended in the usual manner.

@scottjehl What do you think? I'm currently using it on a project and it ensures we can style for mobile first (min-width all the way) and not have to worry about IE8 looking like a really narrow mobile screen.

This fixes #325

henrahmagix and others added 5 commits November 27, 2015 16:28
Usually this library will append media queries in separate <style> elements.
However, doing this changes the cascade such that a media rule will be used over
a non-media rule. This is because the media rule is removed from its position
and appended after everything else. It is a common case that a non-media rule
will override a media rule, so this inversion of the cascade makes it very
difficult to style for all browsers using this polyfill.

Turn on this option by setting `window.RESPOND_REPLACE_STYLES = true` before
sourcing this script. It will remove every stylesheet it finds and move all CSS
into the `media="all"` stylesheet that is appended, minus the inactive media
query rules and those of other media types, like print. The other types are
appended in the usual manner.
Bug was introduced when DRYing code to the `insertCss` function.
As a result of rebasing the `RESPOND_REPLACE_STYLES` option code onto
1c8ce14 (after previously being on 1.4.2), the
replace index was being calculated as -1, meaning the query could not be found
in the total styles. This was because comments are now being stripped.

To fix that, the stripping of comments and keyframes from styles are done
immediately in the `translate` function, and then stored with their replaced
urls if `RESPOND_REPLACE_STYLES === true`.
Allow replacing stylesheet to preserve the cascade
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.

Final cascaded rule does not work, always override by last Media Query
1 participant