Skip to content

Commit 73055d8

Browse files
authored
fix(custom-element): prevent injecting child styles if shadowRoot is false (#12769)
close #12630
1 parent 4aa7a4a commit 73055d8

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

packages/runtime-core/src/renderer.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ import { isAsyncWrapper } from './apiAsyncComponent'
8686
import { isCompatEnabled } from './compat/compatConfig'
8787
import { DeprecationTypes } from './compat/compatConfig'
8888
import type { TransitionHooks } from './components/BaseTransition'
89+
import type { VueElement } from '@vue/runtime-dom'
8990

9091
export interface Renderer<HostElement = RendererElement> {
9192
render: RootRenderFunction<HostElement>
@@ -1348,7 +1349,11 @@ function baseCreateRenderer(
13481349
}
13491350
} else {
13501351
// custom element style injection
1351-
if (root.ce) {
1352+
if (
1353+
root.ce &&
1354+
// @ts-expect-error _def is private
1355+
(root.ce as VueElement)._def.shadowRoot !== false
1356+
) {
13521357
root.ce._injectChildStyle(type)
13531358
}
13541359

packages/runtime-dom/__tests__/customElement.spec.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -916,6 +916,30 @@ describe('defineCustomElement', () => {
916916
assertStyles(el, [`div { color: blue; }`, `div { color: red; }`])
917917
})
918918

919+
test("child components should not inject styles to root element's shadow root w/ shadowRoot false", async () => {
920+
const Bar = defineComponent({
921+
styles: [`div { color: green; }`],
922+
render() {
923+
return 'bar'
924+
},
925+
})
926+
const Baz = () => h(Bar)
927+
const Foo = defineCustomElement(
928+
{
929+
render() {
930+
return [h(Baz)]
931+
},
932+
},
933+
{ shadowRoot: false },
934+
)
935+
936+
customElements.define('my-foo-with-shadowroot-false', Foo)
937+
container.innerHTML = `<my-foo-with-shadowroot-false></my-foo-with-shadowroot-false>`
938+
const el = container.childNodes[0] as VueElement
939+
const style = el.shadowRoot?.querySelector('style')
940+
expect(style).toBeUndefined()
941+
})
942+
919943
test('with nonce', () => {
920944
const Foo = defineCustomElement(
921945
{

0 commit comments

Comments
 (0)