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(compiler-sfc): fix universal selector scope #10551

Merged
merged 8 commits into from
Apr 15, 2024
Next Next commit
fix(compiler-sfc): fix universal selector scope
close #10548
Doctor-wu committed Mar 20, 2024

Unverified

No user is associated with the committer email.
commit 5f81b26c56986c430df7888f921119b2f37e3d32
11 changes: 11 additions & 0 deletions packages/compiler-sfc/__tests__/compileStyle.spec.ts
Original file line number Diff line number Diff line change
@@ -390,4 +390,15 @@ describe('SFC style preprocessors', () => {

expect(res.errors.length).toBe(0)
})

test('should mount scope on correct selector when have universal selector', () => {
expect(compileScoped(`* { color: red; }`)).toMatchInlineSnapshot(`
"[data-v-test] { color: red;
}"
`)
expect(compileScoped(`.foo * { color: red; }`)).toMatchInlineSnapshot(`
".foo[data-v-test] * { color: red;
}"
`)
})
})
13 changes: 13 additions & 0 deletions packages/compiler-sfc/src/style/pluginScoped.ts
Original file line number Diff line number Diff line change
@@ -170,10 +170,23 @@ function rewriteSelector(
}
}

if (n.type === 'universal') {
const prev = selector.at(selector.index(n) - 1)
const next = selector.at(selector.index(n) + 1)
if (!prev && !next) {
node = selectorParser.combinator({
value: '',
})
selector.insertBefore(n, node)
selector.removeChild(n)
}
}

if (
(n.type !== 'pseudo' && n.type !== 'combinator') ||
(n.type === 'pseudo' && (n.value === ':is' || n.value === ':where'))
) {
if (n.type === 'universal' && node) return
node = n
}
})