From 9b0bf7713e94b2c5d303437cd676522f74f5c1c6 Mon Sep 17 00:00:00 2001 From: Kate Corcoran Date: Wed, 19 Feb 2025 11:57:36 -0800 Subject: [PATCH 1/3] fix(compiler-sfc): add scoping tag to trailing universal selector --- .../__tests__/compileStyle.spec.ts | 26 ++++++++++++++++++- .../compiler-sfc/src/style/pluginScoped.ts | 25 +++++++++++++++--- 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/packages/compiler-sfc/__tests__/compileStyle.spec.ts b/packages/compiler-sfc/__tests__/compileStyle.spec.ts index b76414364..216f64e57 100644 --- a/packages/compiler-sfc/__tests__/compileStyle.spec.ts +++ b/packages/compiler-sfc/__tests__/compileStyle.spec.ts @@ -489,7 +489,31 @@ describe('SFC style preprocessors', () => { }" `) expect(compileScoped(`.foo * { color: red; }`)).toMatchInlineSnapshot(` - ".foo[data-v-test] * { color: red; + ".foo[data-v-test] [data-v-test] { color: red; + }" + `) + expect(compileScoped(`.foo :active { color: red; }`)) + .toMatchInlineSnapshot(` + ".foo[data-v-test] :active { color: red; + }" + `) + expect(compileScoped(`.foo *:active { color: red; }`)) + .toMatchInlineSnapshot(` + ".foo[data-v-test] [data-v-test]:active { color: red; + }" + `) + expect(compileScoped(`.foo * .bar { color: red; }`)).toMatchInlineSnapshot(` + ".foo * .bar[data-v-test] { color: red; + }" + `) + expect(compileScoped(`:last-child * { color: red; }`)) + .toMatchInlineSnapshot(` + "[data-v-test]:last-child [data-v-test] { color: red; + }" + `) + expect(compileScoped(`:last-child *:active { color: red; }`)) + .toMatchInlineSnapshot(` + "[data-v-test]:last-child [data-v-test]:active { color: red; }" `) }) diff --git a/packages/compiler-sfc/src/style/pluginScoped.ts b/packages/compiler-sfc/src/style/pluginScoped.ts index d0aaddd76..357ec28cc 100644 --- a/packages/compiler-sfc/src/style/pluginScoped.ts +++ b/packages/compiler-sfc/src/style/pluginScoped.ts @@ -102,6 +102,7 @@ function rewriteSelector( slotted = false, ) { let node: selectorParser.Node | null = null + let starNode: selectorParser.Node | null = null let shouldInject = !deep // find the last child node to insert attribute selector selector.each(n => { @@ -216,17 +217,21 @@ function rewriteSelector( return false } } - // .foo * -> .foo[xxxxxxx] * - if (node) return + // v- store this + // .foo * -> .foo[xxxxxxx] *[xxxxxxx] + starNode = n } if ( - (n.type !== 'pseudo' && n.type !== 'combinator') || + (n.type !== 'pseudo' && + n.type !== 'combinator' && + n.type !== 'universal') || (n.type === 'pseudo' && (n.value === ':is' || n.value === ':where') && !node) ) { node = n + starNode = null } }) @@ -274,6 +279,20 @@ function rewriteSelector( quoteMark: `"`, }), ) + // Used for trailing universal selectors (#12906) + // `.foo * {}` -> `.foo[xxxxxxx] [xxxxxxx] {}` + if (starNode) { + selector.insertBefore( + starNode, + selectorParser.attribute({ + attribute: idToAdd, + value: idToAdd, + raws: {}, + quoteMark: `"`, + }), + ) + selector.removeChild(starNode) + } } } From 0a036265832357ddd24bc9c19bc10f35d5d8e628 Mon Sep 17 00:00:00 2001 From: edison Date: Thu, 20 Feb 2025 08:55:07 +0800 Subject: [PATCH 2/3] Apply suggestions from code review --- packages/compiler-sfc/src/style/pluginScoped.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/compiler-sfc/src/style/pluginScoped.ts b/packages/compiler-sfc/src/style/pluginScoped.ts index 357ec28cc..54e074de5 100644 --- a/packages/compiler-sfc/src/style/pluginScoped.ts +++ b/packages/compiler-sfc/src/style/pluginScoped.ts @@ -218,7 +218,7 @@ function rewriteSelector( } } // v- store this - // .foo * -> .foo[xxxxxxx] *[xxxxxxx] + // .foo * -> .foo[xxxxxxx] [xxxxxxx] starNode = n } From 92621b857db9e954efde5563cfeb52cf5968855c Mon Sep 17 00:00:00 2001 From: edison Date: Thu, 20 Feb 2025 08:56:16 +0800 Subject: [PATCH 3/3] Apply suggestions from code review --- packages/compiler-sfc/src/style/pluginScoped.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/compiler-sfc/src/style/pluginScoped.ts b/packages/compiler-sfc/src/style/pluginScoped.ts index 54e074de5..4845d8eee 100644 --- a/packages/compiler-sfc/src/style/pluginScoped.ts +++ b/packages/compiler-sfc/src/style/pluginScoped.ts @@ -217,7 +217,7 @@ function rewriteSelector( return false } } - // v- store this + // store the universal selector so it can be rewritten later // .foo * -> .foo[xxxxxxx] [xxxxxxx] starNode = n }