From 522077b1bcd5266d5d7f7a6dd353441ea8869168 Mon Sep 17 00:00:00 2001 From: daiwei Date: Tue, 27 May 2025 17:06:46 +0800 Subject: [PATCH] fix(compiler-sfc): don't inject scope id before child combinator > --- .../__tests__/compileStyle.spec.ts | 7 ++++ .../compiler-sfc/src/style/pluginScoped.ts | 38 +++++++++++-------- 2 files changed, 30 insertions(+), 15 deletions(-) diff --git a/packages/compiler-sfc/__tests__/compileStyle.spec.ts b/packages/compiler-sfc/__tests__/compileStyle.spec.ts index 70c6af557a..c29598f150 100644 --- a/packages/compiler-sfc/__tests__/compileStyle.spec.ts +++ b/packages/compiler-sfc/__tests__/compileStyle.spec.ts @@ -520,5 +520,12 @@ describe('SFC style preprocessors', () => { "[data-v-test]:last-child [data-v-test]:active { color: red; }" `) + expect(compileScoped(`main { > * { background-color: yellow; } }`)) + .toMatchInlineSnapshot(` + "main { + > [data-v-test] { background-color: yellow; + } + }" + `) }) }) diff --git a/packages/compiler-sfc/src/style/pluginScoped.ts b/packages/compiler-sfc/src/style/pluginScoped.ts index 4845d8eee3..51e30fc088 100644 --- a/packages/compiler-sfc/src/style/pluginScoped.ts +++ b/packages/compiler-sfc/src/style/pluginScoped.ts @@ -255,6 +255,13 @@ function rewriteSelector( ) shouldInject = false } + } else { + // #13387 don't inject [id] at the selector start if node is null + // and the selector starts with `>` + const { type, value } = selector.first + if (type === 'combinator' && value === '>') { + shouldInject = false + } } if (node) { @@ -266,8 +273,8 @@ function rewriteSelector( selector.first.spaces.before = '' } + const idToAdd = slotted ? id + '-s' : id if (shouldInject) { - const idToAdd = slotted ? id + '-s' : id selector.insertAfter( // If node is null it means we need to inject [id] at the start // insertAfter can handle `null` here @@ -279,20 +286,21 @@ 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) - } + } + + // 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) } } -- 2.47.2