vue3-yuanma/packages/runtime-dom/src/nodeOps.ts

69 lines
1.7 KiB
TypeScript
Raw Normal View History

2020-02-12 17:12:53 +00:00
import { RendererOptions } from '@vue/runtime-core'
const doc = (typeof document !== 'undefined' ? document : null) as Document
const svgNS = 'http://www.w3.org/2000/svg'
let tempContainer: HTMLElement
let tempSVGContainer: SVGElement
export const nodeOps: Omit<RendererOptions<Node, Element>, 'patchProp'> = {
insert: (child, parent, anchor) => {
if (anchor != null) {
2019-10-10 19:37:17 +00:00
parent.insertBefore(child, anchor)
} else {
parent.appendChild(child)
}
},
remove: child => {
const parent = child.parentNode
if (parent != null) {
parent.removeChild(child)
}
},
createElement: (tag, isSVG): Element =>
2019-05-29 08:10:25 +00:00
isSVG ? doc.createElementNS(svgNS, tag) : doc.createElement(tag),
createText: text => doc.createTextNode(text),
createComment: text => doc.createComment(text),
setText: (node, text) => {
node.nodeValue = text
},
setElementText: (el, text) => {
el.textContent = text
},
parentNode: node => node.parentNode as Element | null,
2019-05-28 05:27:31 +00:00
nextSibling: node => node.nextSibling,
2019-05-29 08:10:25 +00:00
querySelector: selector => doc.querySelector(selector),
2019-12-16 18:33:10 +00:00
setScopeId(el, id) {
2019-12-16 18:33:10 +00:00
el.setAttribute(id, '')
},
cloneNode(el) {
return el.cloneNode(true)
},
// __UNSAFE__
// Reason: innerHTML.
// Static content here can only come from compiled templates.
// As long as the user only uses trusted templates, this is safe.
insertStaticContent(content, parent, anchor, isSVG) {
const temp = isSVG
? tempSVGContainer ||
(tempSVGContainer = doc.createElementNS(svgNS, 'svg'))
: tempContainer || (tempContainer = doc.createElement('div'))
temp.innerHTML = content
const node = temp.children[0]
nodeOps.insert(node, parent, anchor)
return node
2019-12-16 18:33:10 +00:00
}
}