Skip to content

Commit 8ffcde2

Browse files
authored
fix(runtime-dom): support mounting app to svg container (#2929)
fix #2926
1 parent 1a955e2 commit 8ffcde2

File tree

5 files changed

+44
-9
lines changed

5 files changed

+44
-9
lines changed

packages/runtime-core/__tests__/hydration.spec.ts

+14
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,20 @@ describe('SSR hydration', () => {
617617
expect(spy).toHaveBeenCalled()
618618
})
619619

620+
test('SVG as a mount container', () => {
621+
const svgContainer = document.createElement('svg')
622+
svgContainer.innerHTML = '<g></g>'
623+
const app = createSSRApp({
624+
render: () => h('g')
625+
})
626+
627+
expect(
628+
(app.mount(svgContainer).$.subTree as VNode<Node, Element> & {
629+
el: Element
630+
}).el instanceof SVGElement
631+
)
632+
})
633+
620634
describe('mismatch handling', () => {
621635
test('text node', () => {
622636
const { container } = mountWithHydration(`foo`, () => 'bar')

packages/runtime-core/src/apiCreateApp.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ export interface App<HostElement = any> {
2727
directive(name: string, directive: Directive): this
2828
mount(
2929
rootContainer: HostElement | string,
30-
isHydrate?: boolean
30+
isHydrate?: boolean,
31+
isSVG?: boolean
3132
): ComponentPublicInstance
3233
unmount(): void
3334
provide<T>(key: InjectionKey<T> | string, value: T): this
@@ -224,7 +225,11 @@ export function createAppAPI<HostElement>(
224225
return app
225226
},
226227

227-
mount(rootContainer: HostElement, isHydrate?: boolean): any {
228+
mount(
229+
rootContainer: HostElement,
230+
isHydrate?: boolean,
231+
isSVG?: boolean
232+
): any {
228233
if (!isMounted) {
229234
const vnode = createVNode(
230235
rootComponent as ConcreteComponent,
@@ -237,14 +242,14 @@ export function createAppAPI<HostElement>(
237242
// HMR root reload
238243
if (__DEV__) {
239244
context.reload = () => {
240-
render(cloneVNode(vnode), rootContainer)
245+
render(cloneVNode(vnode), rootContainer, isSVG)
241246
}
242247
}
243248

244249
if (isHydrate && hydrate) {
245250
hydrate(vnode as VNode<Node, Element>, rootContainer as any)
246251
} else {
247-
render(vnode, rootContainer)
252+
render(vnode, rootContainer, isSVG)
248253
}
249254
isMounted = true
250255
app._container = rootContainer

packages/runtime-core/src/renderer.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ export interface HydrationRenderer extends Renderer<Element> {
9393

9494
export type RootRenderFunction<HostElement = RendererElement> = (
9595
vnode: VNode | null,
96-
container: HostElement
96+
container: HostElement,
97+
isSVG?: boolean
9798
) => void
9899

99100
export interface RendererOptions<
@@ -2202,13 +2203,13 @@ function baseCreateRenderer(
22022203
return hostNextSibling((vnode.anchor || vnode.el)!)
22032204
}
22042205

2205-
const render: RootRenderFunction = (vnode, container) => {
2206+
const render: RootRenderFunction = (vnode, container, isSVG) => {
22062207
if (vnode == null) {
22072208
if (container._vnode) {
22082209
unmount(container._vnode, null, null, true)
22092210
}
22102211
} else {
2211-
patch(container._vnode || null, vnode, container)
2212+
patch(container._vnode || null, vnode, container, null, null, null, isSVG)
22122213
}
22132214
flushPostFlushCbs()
22142215
container._vnode = vnode
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { createApp, h } from '../src'
2+
3+
describe('createApp for dom', () => {
4+
// #2926
5+
test('mount to SVG container', () => {
6+
const root = document.createElementNS('http://www.w3.org/2000/svg', 'svg')
7+
createApp({
8+
render() {
9+
return h('g')
10+
}
11+
}).mount(root)
12+
expect(root.children.length).toBe(1)
13+
expect(root.children[0] instanceof SVGElement).toBe(true)
14+
})
15+
})

packages/runtime-dom/src/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export const createApp = ((...args) => {
6969
}
7070
// clear content before mounting
7171
container.innerHTML = ''
72-
const proxy = mount(container)
72+
const proxy = mount(container, false, container instanceof SVGElement)
7373
if (container instanceof Element) {
7474
container.removeAttribute('v-cloak')
7575
container.setAttribute('data-v-app', '')
@@ -92,7 +92,7 @@ export const createSSRApp = ((...args) => {
9292
app.mount = (containerOrSelector: Element | ShadowRoot | string): any => {
9393
const container = normalizeContainer(containerOrSelector)
9494
if (container) {
95-
return mount(container, true)
95+
return mount(container, true, container instanceof SVGElement)
9696
}
9797
}
9898

0 commit comments

Comments
 (0)