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

47 lines
1.1 KiB
TypeScript
Raw Normal View History

2019-05-29 08:10:25 +00:00
const doc = document
const svgNS = 'http://www.w3.org/2000/svg'
2019-06-20 13:28:37 +00:00
export const nodeOps = {
2019-05-26 07:38:55 +00:00
insert: (child: Node, parent: Node, anchor?: Node) => {
if (anchor != null) {
2019-10-10 19:37:17 +00:00
parent.insertBefore(child, anchor)
} else {
parent.appendChild(child)
}
},
remove: (child: Node) => {
const parent = child.parentNode
if (parent != null) {
parent.removeChild(child)
}
},
createElement: (tag: string, isSVG?: boolean): Element =>
2019-05-29 08:10:25 +00:00
isSVG ? doc.createElementNS(svgNS, tag) : doc.createElement(tag),
2019-05-29 08:10:25 +00:00
createText: (text: string): Text => doc.createTextNode(text),
2019-05-29 08:10:25 +00:00
createComment: (text: string): Comment => doc.createComment(text),
setText: (node: Text, text: string) => {
node.nodeValue = text
},
setElementText: (el: HTMLElement, text: string) => {
el.textContent = text
},
parentNode: (node: Node): HTMLElement | null =>
node.parentNode as HTMLElement,
2019-05-28 05:27:31 +00:00
2019-05-29 08:10:25 +00:00
nextSibling: (node: Node): Node | null => node.nextSibling,
2019-09-06 20:58:32 +00:00
querySelector: (selector: string): Element | null =>
2019-12-16 18:33:10 +00:00
doc.querySelector(selector),
setScopeId(el: Element, id: string) {
el.setAttribute(id, '')
}
}