UNPKG

uhtml

Version:
50 lines (41 loc) • 1.13 kB
'use strict'; const { childNodes, ownerDocument, ownerElement, parentNode } = require('./symbols.js'); const { setParentNode } = require('./utils.js'); const { parseString } = require('./string-parser.js'); const start = Symbol('start'); const end = Symbol('end'); module.exports = class Range { constructor() { this[ownerElement] = null; this[start] = null; this[end] = null; } setStartAfter(node) { this[start] = node.nextSibling; } setStartBefore(node) { this[start] = node; } setEndAfter(node) { this[end] = node; } deleteContents() { const { [start]: s, [end]: e } = this; const { [childNodes]: nodes } = s[parentNode]; const si = nodes.indexOf(s); this[start] = null; this[end] = null; nodes.splice(si, nodes.indexOf(e) + 1 - si).forEach(setParentNode, null); } selectNodeContents(node) { this[ownerElement] = node; } createContextualFragment(text) { const { [ownerElement]: context } = this; return parseString( context[ownerDocument].createDocumentFragment(), 'ownerSVGElement' in context, text ); } }