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 body scoping #1130

Merged
merged 1 commit into from
Aug 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/calm-walls-unite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fix CSS scoping issue
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Declaration, Plugin } from 'postcss';
import { Plugin } from 'postcss';

interface AstroScopedOptions {
className: string;
Expand All @@ -13,6 +13,11 @@ interface Selector {
const CSS_SEPARATORS = new Set([' ', ',', '+', '>', '~']);
const KEYFRAME_PERCENT = /\d+\.?\d*%/;

/** minify selector CSS */
function minifySelector(selector: string): string {
return selector.replace(/(\r?\n|\s)+/g, ' ').replace(/\s*(,|\+|>|~|\(|\))\s*/g, '$1');
Copy link
Member Author

@drwpow drwpow Aug 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change here is CSS sanitization. This removes any extraneous whitespace characters (including linebreaks) that aren’t essential to the CSS selector (keeping in mind that some whitespace is essential as child selector, but other whitespace is non-essential such as .foo + \n .bar and can be removed).

The issue was whitespace not being handled properly, resulting in things like  body (rather than body) being a selector and incorrectly marked as needing scoping.

}

/** HTML tags that should never get scoped classes */
export const NEVER_SCOPED_TAGS = new Set<string>(['base', 'body', 'font', 'frame', 'frameset', 'head', 'html', 'link', 'meta', 'noframes', 'noscript', 'script', 'style', 'title']);
/**
Expand Down Expand Up @@ -101,7 +106,7 @@ export default function astroScopedStyles(options: AstroScopedOptions): Plugin {
postcssPlugin: '@astrojs/postcss-scoped-styles',
Rule(rule) {
if (!rulesScopedCache.has(rule)) {
rule.selector = scopeRule(rule.selector, options.className);
rule.selector = scopeRule(minifySelector(rule.selector), options.className);
rulesScopedCache.add(rule);
}
},
Expand Down
1 change: 1 addition & 0 deletions packages/astro/test/astro-scoped-styles.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ ScopedStyles('Scopes rules correctly', () => {
'.class :global(ul li)': `.class.${className} ul li`, // allow doubly-scoped selectors
'.class:not(.is-active)': `.class.${className}:not(.is-active)`, // Note: the :not() selector can NOT contain multiple classes, so this is correct; if this causes issues for some people then it‘s worth a discussion
'body h1': `body h1.${className}`, // body shouldn‘t be scoped; it‘s not a component
'html,body': `html,body`,
from: 'from', // ignore keyframe keywords (below)
to: 'to',
'55%': '55%',
Expand Down