fix(runtime-dom): support mounting app to svg container (#2929)
fix #2926
This commit is contained in:
parent
1a955e2278
commit
8ffcde2836
@ -617,6 +617,20 @@ describe('SSR hydration', () => {
|
|||||||
expect(spy).toHaveBeenCalled()
|
expect(spy).toHaveBeenCalled()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('SVG as a mount container', () => {
|
||||||
|
const svgContainer = document.createElement('svg')
|
||||||
|
svgContainer.innerHTML = '<g></g>'
|
||||||
|
const app = createSSRApp({
|
||||||
|
render: () => h('g')
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(
|
||||||
|
(app.mount(svgContainer).$.subTree as VNode<Node, Element> & {
|
||||||
|
el: Element
|
||||||
|
}).el instanceof SVGElement
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
describe('mismatch handling', () => {
|
describe('mismatch handling', () => {
|
||||||
test('text node', () => {
|
test('text node', () => {
|
||||||
const { container } = mountWithHydration(`foo`, () => 'bar')
|
const { container } = mountWithHydration(`foo`, () => 'bar')
|
||||||
|
@ -27,7 +27,8 @@ export interface App<HostElement = any> {
|
|||||||
directive(name: string, directive: Directive): this
|
directive(name: string, directive: Directive): this
|
||||||
mount(
|
mount(
|
||||||
rootContainer: HostElement | string,
|
rootContainer: HostElement | string,
|
||||||
isHydrate?: boolean
|
isHydrate?: boolean,
|
||||||
|
isSVG?: boolean
|
||||||
): ComponentPublicInstance
|
): ComponentPublicInstance
|
||||||
unmount(): void
|
unmount(): void
|
||||||
provide<T>(key: InjectionKey<T> | string, value: T): this
|
provide<T>(key: InjectionKey<T> | string, value: T): this
|
||||||
@ -224,7 +225,11 @@ export function createAppAPI<HostElement>(
|
|||||||
return app
|
return app
|
||||||
},
|
},
|
||||||
|
|
||||||
mount(rootContainer: HostElement, isHydrate?: boolean): any {
|
mount(
|
||||||
|
rootContainer: HostElement,
|
||||||
|
isHydrate?: boolean,
|
||||||
|
isSVG?: boolean
|
||||||
|
): any {
|
||||||
if (!isMounted) {
|
if (!isMounted) {
|
||||||
const vnode = createVNode(
|
const vnode = createVNode(
|
||||||
rootComponent as ConcreteComponent,
|
rootComponent as ConcreteComponent,
|
||||||
@ -237,14 +242,14 @@ export function createAppAPI<HostElement>(
|
|||||||
// HMR root reload
|
// HMR root reload
|
||||||
if (__DEV__) {
|
if (__DEV__) {
|
||||||
context.reload = () => {
|
context.reload = () => {
|
||||||
render(cloneVNode(vnode), rootContainer)
|
render(cloneVNode(vnode), rootContainer, isSVG)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isHydrate && hydrate) {
|
if (isHydrate && hydrate) {
|
||||||
hydrate(vnode as VNode<Node, Element>, rootContainer as any)
|
hydrate(vnode as VNode<Node, Element>, rootContainer as any)
|
||||||
} else {
|
} else {
|
||||||
render(vnode, rootContainer)
|
render(vnode, rootContainer, isSVG)
|
||||||
}
|
}
|
||||||
isMounted = true
|
isMounted = true
|
||||||
app._container = rootContainer
|
app._container = rootContainer
|
||||||
|
@ -93,7 +93,8 @@ export interface HydrationRenderer extends Renderer<Element> {
|
|||||||
|
|
||||||
export type RootRenderFunction<HostElement = RendererElement> = (
|
export type RootRenderFunction<HostElement = RendererElement> = (
|
||||||
vnode: VNode | null,
|
vnode: VNode | null,
|
||||||
container: HostElement
|
container: HostElement,
|
||||||
|
isSVG?: boolean
|
||||||
) => void
|
) => void
|
||||||
|
|
||||||
export interface RendererOptions<
|
export interface RendererOptions<
|
||||||
@ -2202,13 +2203,13 @@ function baseCreateRenderer(
|
|||||||
return hostNextSibling((vnode.anchor || vnode.el)!)
|
return hostNextSibling((vnode.anchor || vnode.el)!)
|
||||||
}
|
}
|
||||||
|
|
||||||
const render: RootRenderFunction = (vnode, container) => {
|
const render: RootRenderFunction = (vnode, container, isSVG) => {
|
||||||
if (vnode == null) {
|
if (vnode == null) {
|
||||||
if (container._vnode) {
|
if (container._vnode) {
|
||||||
unmount(container._vnode, null, null, true)
|
unmount(container._vnode, null, null, true)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
patch(container._vnode || null, vnode, container)
|
patch(container._vnode || null, vnode, container, null, null, null, isSVG)
|
||||||
}
|
}
|
||||||
flushPostFlushCbs()
|
flushPostFlushCbs()
|
||||||
container._vnode = vnode
|
container._vnode = vnode
|
||||||
|
15
packages/runtime-dom/__tests__/createApp.spec.ts
Normal file
15
packages/runtime-dom/__tests__/createApp.spec.ts
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
import { createApp, h } from '../src'
|
||||||
|
|
||||||
|
describe('createApp for dom', () => {
|
||||||
|
// #2926
|
||||||
|
test('mount to SVG container', () => {
|
||||||
|
const root = document.createElementNS('http://www.w3.org/2000/svg', 'svg')
|
||||||
|
createApp({
|
||||||
|
render() {
|
||||||
|
return h('g')
|
||||||
|
}
|
||||||
|
}).mount(root)
|
||||||
|
expect(root.children.length).toBe(1)
|
||||||
|
expect(root.children[0] instanceof SVGElement).toBe(true)
|
||||||
|
})
|
||||||
|
})
|
@ -69,7 +69,7 @@ export const createApp = ((...args) => {
|
|||||||
}
|
}
|
||||||
// clear content before mounting
|
// clear content before mounting
|
||||||
container.innerHTML = ''
|
container.innerHTML = ''
|
||||||
const proxy = mount(container)
|
const proxy = mount(container, false, container instanceof SVGElement)
|
||||||
if (container instanceof Element) {
|
if (container instanceof Element) {
|
||||||
container.removeAttribute('v-cloak')
|
container.removeAttribute('v-cloak')
|
||||||
container.setAttribute('data-v-app', '')
|
container.setAttribute('data-v-app', '')
|
||||||
@ -92,7 +92,7 @@ export const createSSRApp = ((...args) => {
|
|||||||
app.mount = (containerOrSelector: Element | ShadowRoot | string): any => {
|
app.mount = (containerOrSelector: Element | ShadowRoot | string): any => {
|
||||||
const container = normalizeContainer(containerOrSelector)
|
const container = normalizeContainer(containerOrSelector)
|
||||||
if (container) {
|
if (container) {
|
||||||
return mount(container, true)
|
return mount(container, true, container instanceof SVGElement)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user