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: Ensure @import rules are always inserted at the top of the stylesheet #1165

Open
wants to merge 1 commit into
base: canary
Choose a base branch
from

Conversation

parvez
Copy link

@parvez parvez commented Dec 23, 2024

This PR addresses an issue in the createGlobalCssFunction where @import rules might not always be inserted at the top of the stylesheet. According to the CSS specification, all @import rules must appear before any other rules in a stylesheet. Failing to comply with this requirement results in the browser ignoring the @import rules.

Problem:

Previously, the @import rules were being inserted just before the themed.group marker using the following logic:

let importIndex = [].indexOf.call(sheet.sheet.cssRules, sheet.rules.themed.group) - 1;

This logic did not guarantee compliance with the CSS requirement for @import to always be at the top, as it depended on the position of the themed.group.

Reference:

Screenshot 2024-12-23 at 6 03 29 AM

https://developer.mozilla.org/en-US/docs/Web/CSS/@import

Solution:

The importIndex is now explicitly set to 0, ensuring that all @import rules are added to the top of the stylesheet:

let importIndex = 0; // Always insert @import rules at the top

This guarantees that all @import rules comply with the CSS specification, regardless of other rules or markers in the stylesheet.


Reason for Change:

  • CSS Specification:

    • The CSS standard requires @import rules to be placed at the top of the stylesheet, before any other types of rules (e.g., style declarations, media queries, keyframes).
    • If @import rules appear later in the stylesheet, they are ignored by the browser.
  • Practical Implication:

    • Placing @import rules at the top ensures that external stylesheets are loaded and applied correctly, maintaining the intended style order and 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.

1 participant