Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Teleport SVG elements #2648

Merged
merged 6 commits into from
Nov 30, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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
36 changes: 34 additions & 2 deletions packages/runtime-core/__tests__/components/Teleport.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import {
Text,
ref,
nextTick,
markRaw
markRaw,
defineComponent
} from '@vue/runtime-test'
import { createVNode, Fragment } from '../../src/vnode'
import { compile } from 'vue'
import { compile, render as domRender } from 'vue'

describe('renderer: teleport', () => {
test('should work', () => {
Expand All @@ -33,6 +34,37 @@ describe('renderer: teleport', () => {
)
})

test('should work with SVG', async () => {
const root = document.createElement('div')
const svg = ref()
const circle = ref()

const Comp = defineComponent({
setup() {
return {
svg,
circle
}
},
template: `
<svg ref="svg"></svg>
<teleport :to="svg" v-if="svg">
<circle ref="circle"></circle>
</teleport>`
})

domRender(h(Comp), root)

await nextTick()

expect(root.innerHTML).toMatchInlineSnapshot(
`"<svg><circle></circle></svg><!--teleport start--><!--teleport end-->"`
)

expect(svg.value.namespaceURI).toBe('http://www.w3.org/2000/svg')
expect(circle.value.namespaceURI).toBe('http://www.w3.org/2000/svg')
})

test('should update target', async () => {
const targetA = nodeOps.createElement('div')
const targetB = nodeOps.createElement('div')
Expand Down
7 changes: 5 additions & 2 deletions packages/runtime-core/src/components/Teleport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
traverseStaticChildren
} from '../renderer'
import { VNode, VNodeArrayChildren, VNodeProps } from '../vnode'
import { isString, ShapeFlags } from '@vue/shared'
import { isString, isSVGTag, ShapeFlags } from '@vue/shared'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI isSVGTag is for compiler only and should be avoided in runtime code since it bloats the bundle size.

import { warn } from '../warning'

export type TeleportVNode = VNode<RendererNode, RendererElement, TeleportProps>
Expand Down Expand Up @@ -80,6 +80,7 @@ export const TeleportImpl = {

const disabled = isTeleportDisabled(n2.props)
const { shapeFlag, children } = n2

if (n1 == null) {
// insert anchors in the main view
const placeholder = (n2.el = __DEV__
Expand All @@ -90,11 +91,12 @@ export const TeleportImpl = {
: createText(''))
insert(placeholder, container, anchor)
insert(mainAnchor, container, anchor)

const target = (n2.target = resolveTarget(n2.props, querySelector))
const targetAnchor = (n2.targetAnchor = createText(''))
if (target) {
insert(targetAnchor, target)
isSVG =
isSVG || Boolean(target && isSVGTag(target.tagName || target.tag))
} else if (__DEV__ && !disabled) {
warn('Invalid Teleport target on mount:', target, `(${typeof target})`)
}
Expand Down Expand Up @@ -129,6 +131,7 @@ export const TeleportImpl = {
const wasDisabled = isTeleportDisabled(n1.props)
const currentContainer = wasDisabled ? container : target
const currentAnchor = wasDisabled ? mainAnchor : targetAnchor
isSVG = isSVG || Boolean(target && isSVGTag(target.tagName || target.tag))

if (n2.dynamicChildren) {
// fast path when the teleport happens to be a block root
Expand Down