fix(custom-element): prevent injecting child styles if shadowRoot is false (#12769)

close #12630
This commit is contained in:
edison 2025-06-05 10:02:26 +08:00 committed by GitHub
parent 4aa7a4aba3
commit 73055d8d95
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 30 additions and 1 deletions

View File

@ -86,6 +86,7 @@ import { isAsyncWrapper } from './apiAsyncComponent'
import { isCompatEnabled } from './compat/compatConfig'
import { DeprecationTypes } from './compat/compatConfig'
import type { TransitionHooks } from './components/BaseTransition'
import type { VueElement } from '@vue/runtime-dom'
export interface Renderer<HostElement = RendererElement> {
render: RootRenderFunction<HostElement>
@ -1348,7 +1349,11 @@ function baseCreateRenderer(
}
} else {
// custom element style injection
if (root.ce) {
if (
root.ce &&
// @ts-expect-error _def is private
(root.ce as VueElement)._def.shadowRoot !== false
) {
root.ce._injectChildStyle(type)
}

View File

@ -916,6 +916,30 @@ describe('defineCustomElement', () => {
assertStyles(el, [`div { color: blue; }`, `div { color: red; }`])
})
test("child components should not inject styles to root element's shadow root w/ shadowRoot false", async () => {
const Bar = defineComponent({
styles: [`div { color: green; }`],
render() {
return 'bar'
},
})
const Baz = () => h(Bar)
const Foo = defineCustomElement(
{
render() {
return [h(Baz)]
},
},
{ shadowRoot: false },
)
customElements.define('my-foo-with-shadowroot-false', Foo)
container.innerHTML = `<my-foo-with-shadowroot-false></my-foo-with-shadowroot-false>`
const el = container.childNodes[0] as VueElement
const style = el.shadowRoot?.querySelector('style')
expect(style).toBeUndefined()
})
test('with nonce', () => {
const Foo = defineCustomElement(
{