Skip to content

Commit

Permalink
fix(compiler-sfc): fix universal selector scope
Browse files Browse the repository at this point in the history
  • Loading branch information
Doctor-wu committed Mar 20, 2024
1 parent ab59bed commit 5f81b26
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
11 changes: 11 additions & 0 deletions packages/compiler-sfc/__tests__/compileStyle.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Up @@ -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
}
})
Expand Down

0 comments on commit 5f81b26

Please sign in to comment.