Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 10 additions & 0 deletions packages/compiler-sfc/__tests__/compileStyle.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,16 @@ color: red
".foo .bar { color: red;
}"
`)
expect(compileScoped(`:global(body) h1 { color: red; }`))
.toMatchInlineSnapshot(`
"body h1[data-v-test] { color: red;
}"
`)
expect(compileScoped(`:global(html.dark) .foo { color: red; }`))
.toMatchInlineSnapshot(`
"html.dark .foo[data-v-test] { color: red;
}"
`)
})

test(':is() and :where() with multiple selectors', () => {
Expand Down
31 changes: 31 additions & 0 deletions packages/compiler-sfc/src/style/pluginScoped.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ function rewriteSelector(
let shouldInject = !deep
let hasNestedDeep = false
let splitForNestedDeep = false
if (rewriteGlobalSelector(selector)) {
return
}
// find the last child node to insert attribute selector
selector.each(n => {
// DEPRECATED ">>>" and "/deep/" combinator
Expand Down Expand Up @@ -340,6 +343,34 @@ function rewriteSelector(
}
}

function rewriteGlobalSelector(selector: selectorParser.Selector): boolean {
const globalIndex = selector.nodes.findIndex(
node =>
node.type === 'pseudo' &&
(node.value === ':global' || node.value === '::v-global'),
)
if (globalIndex === -1) {
return false
}

const global = selector.at(globalIndex) as selectorParser.Pseudo
const hasTrailingNodes = globalIndex < selector.length - 1
for (const node of selector.nodes.slice(0, globalIndex)) {
selector.removeChild(node)
}
let last: selectorParser.Selector['nodes'][0] = global
global.nodes[0].each(node => {
selector.insertAfter(last, node)
last = node
})
selector.removeChild(global)
if (selector.first) {
selector.first.spaces.before = ''
}

return !hasTrailingNodes
}

function isSpaceCombinator(node: selectorParser.Node) {
return node.type === 'combinator' && /^\s+$/.test(node.value)
}
Expand Down