svgdom
Version:
Straightforward DOM implementation for SVG, HTML and XML
28 lines (26 loc) • 893 B
JavaScript
import { nodesToNode } from '../../utils/nodesToNode.js'
// https://dom.spec.whatwg.org/#interface-childnode
// Todo: check if this is contained in nodes or siblings are contained (viablePreviousSibling, viableNextSibling)
export const ChildNode = {
before (...nodes) {
if (!this.parentNode) return
const node = nodesToNode(nodes, this.ownerDocument)
this.parentNode.insertBefore(node, this)
},
after (...nodes) {
if (!this.parentNode) return
const node = nodesToNode(nodes, this.ownerDocument)
this.parentNode.insertBefore(node, this.nextSibling)
},
replaceWith (...nodes) {
if (!this.parentNode) return
const next = this.nextSibling
const node = nodesToNode(nodes, this.ownerDocument)
this.parentNode.insertBefore(node, next)
this.remove()
},
remove () {
if (!this.parentNode) return
this.parentNode.removeChild(this)
}
}