Skip to content

fix: use hybrid scoping strategy for consistent specificity increase #10443

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

Merged
merged 4 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
tweaks
  • Loading branch information
Rich-Harris committed Feb 9, 2024
commit e8b9b1117e93c9519553da3096fa9a4e8abd9e9d
15 changes: 10 additions & 5 deletions packages/svelte/src/compiler/phases/2-analyze/css/Selector.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ export default class Selector {

/**
* @param {import('magic-string').default} code
* @param {string} attr
* @param {string} modifier
*/
transform(code, attr) {
transform(code, modifier) {
/** @param {import('#compiler').Css.SimpleSelector} selector */
function remove_global_pseudo_class(selector) {
code
Expand All @@ -90,20 +90,24 @@ export default class Selector {
remove_global_pseudo_class(selector);
}
}

let i = block.selectors.length;
while (i--) {
const selector = block.selectors[i];

if (selector.type === 'PseudoElementSelector' || selector.type === 'PseudoClassSelector') {
if (selector.name !== 'root' && selector.name !== 'host') {
if (i === 0) code.prependRight(selector.start, modifier);
}
continue;
}

if (selector.type === 'TypeSelector' && selector.name === '*') {
code.update(selector.start, selector.end, modifier);
} else {
code.appendLeft(selector.end, modifier);
}

break;
}
}
Expand All @@ -115,9 +119,10 @@ export default class Selector {
}

if (block.should_encapsulate) {
const modifier = first ? attr : `:where(${attr})`;
encapsulate_block(block, modifier);

// for the first occurrence, we use a classname selector, so that every
// encapsulated selector gets a +0-1-0 specificity bump. thereafter,
// we use a `:where` selector, which does not affect specificity
encapsulate_block(block, first ? modifier : `:where(${modifier})`);
first = false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ class Rule {
return;
}

const attr = `.${id}`;
this.selectors.forEach((selector) => selector.transform(code, attr));
const modifier = `.${id}`;
this.selectors.forEach((selector) => selector.transform(code, modifier));
this.declarations.forEach((declaration) => declaration.transform(code, keyframes));
}

Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy