Skip to content

An @supports, @keyframes, @layer or @container block swallows the rule after it #524

Closed

Description

@jakejackson1

Part of #520 (step 4).

CssParser::processCssString() splits the stylesheet with /(.*?)\{(.*?)\}/ (src/Css/CssParser.php:219). MediaQueryProcessor unwraps @media blocks first, but no other block at-rule. For any other, the regex stops at the first inner }, and what is left of the block becomes the start of the next selector.

Measured on gravitypdf at b6a07921, PHP 8.5.4. Each at-rule below is followed by p {color:green}, on <p>:

Before p {color:green} p
@supports (display:grid) { .a {color:red} } black: the p rule is lost
@keyframes spin { from {opacity:0} to {opacity:1} } black
@layer base { h1 {color:red} } black
@container (min-width:1px) { h1 {color:red} } black
@media print { … }, @font-face { … } green

The rules inside @supports and @layer are lost too: @supports (display:block) { p {color:green} } leaves the p black. Framework stylesheets use these; Bootstrap's spinners are @keyframes.

Fix. Before processCssString(), a brace-matching pass removes each @keyframes, @container, @font-feature-values and any other unknown block at-rule whole. It unwraps @supports and @layer, as @media is unwrapped. The alternative is to remove them too, if unwrapping the fallback rules in a @supports not (…) block is judged worse. #536 later replaces this pass with a tokenizer.

Tests. The rule after each at-rule applies, and an @media block inside @supports still works.

Also from #552: statement at-rules. @charset "UTF-8";, @namespace …; and @import …; lose the rule after them too, in a <style> block and in a stylesheet loaded with <link>. Measured on gravitypdf at d660a410: @charset "UTF-8"; p {color:#0f0} leaves the p black. They are not removed before the brace regex (src/Css/CssParser.php:219), so the selector becomes @CHARSET "UTF-8"; P and is dropped. CssLoader fetches an @import but leaves its text in place. Remove all three in the same pass, after CssLoader has fetched the imports.

Tests (also). A rule after each statement at-rule applies.

Unblocks #536.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions