nodeOps.ts 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. const doc = document
  2. const svgNS = 'http://www.w3.org/2000/svg'
  3. export const nodeOps = {
  4. insert: (child: Node, parent: Node, anchor?: Node) => {
  5. if (anchor != null) {
  6. parent.insertBefore(child, anchor)
  7. } else {
  8. parent.appendChild(child)
  9. }
  10. },
  11. remove: (child: Node) => {
  12. const parent = child.parentNode
  13. if (parent != null) {
  14. parent.removeChild(child)
  15. }
  16. },
  17. createElement: (tag: string, isSVG?: boolean): Element =>
  18. isSVG ? doc.createElementNS(svgNS, tag) : doc.createElement(tag),
  19. createText: (text: string): Text => doc.createTextNode(text),
  20. createComment: (text: string): Comment => doc.createComment(text),
  21. setText: (node: Text, text: string) => {
  22. node.nodeValue = text
  23. },
  24. setElementText: (el: HTMLElement, text: string) => {
  25. el.textContent = text
  26. },
  27. parentNode: (node: Node): HTMLElement | null =>
  28. node.parentNode as HTMLElement,
  29. nextSibling: (node: Node): Node | null => node.nextSibling,
  30. querySelector: (selector: string): Element | null =>
  31. doc.querySelector(selector)
  32. }