Bugfix: scoped styles (#178)

This commit is contained in:
Drew Powers 2021-05-06 15:39:34 -06:00 committed by GitHub
parent 7b55d3d43e
commit 87ab4c6927
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 5 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Bugfix: Scoped CSS with pseudoclasses and direct children selectors

View file

@ -75,18 +75,18 @@ export function scopeRule(selector: string, className: string) {
continue;
}
// dont scope body, title, etc.
if (NEVER_SCOPED_TAGS.has(value)) {
// dont scope body, title, etc.
if (CSS_SEPARATORS.has(value) || NEVER_SCOPED_TAGS.has(value)) {
ss = head + value + tail;
continue;
}
// scope everything else
let newSelector = ss.substring(start, end);
let newSelector = value;
const pseudoIndex = newSelector.indexOf(':');
if (pseudoIndex > 0) {
// if theres a pseudoclass (:focus)
ss = head + newSelector.substring(start, pseudoIndex) + c + newSelector.substr(pseudoIndex) + tail;
// if theres a pseudoclass (:focus or ::before)
ss = head + newSelector.substring(0, pseudoIndex) + c + newSelector.substr(pseudoIndex) + tail;
} else {
ss = head + newSelector + c + tail;
}

View file

@ -16,6 +16,9 @@ ScopedStyles('Scopes rules correctly', () => {
'.class~:global(a)': `.class.${className}~a`,
'.class *': `.class.${className} .${className}`,
'.class>*': `.class.${className}>.${className}`,
'.class button:focus': `.class.${className} button.${className}:focus`,
'.class h3::before': `.class.${className} h3.${className}::before`,
'button:focus::before': `button.${className}:focus::before`,
'.class :global(*)': `.class.${className} *`,
'.class :global(.nav:not(.is-active))': `.class.${className} .nav:not(.is-active)`, // preserve nested parens
'.class :global(ul li)': `.class.${className} ul li`, // allow doubly-scoped selectors