-
Notifications
You must be signed in to change notification settings - Fork 100
Expand file tree
/
Copy pathrange.js
More file actions
129 lines (109 loc) · 3.05 KB
/
range.js
File metadata and controls
129 lines (109 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
'use strict';
// https://dom.spec.whatwg.org/#concept-live-range
const {END, NEXT, PREV, START} = require('../shared/symbols.js');
const {SVGElement} = require('../svg/element.js');
const {getEnd, htmlToFragment, setAdjacent} = require('../shared/utils.js');
const deleteContents = ({[START]: start, [END]: end}, fragment = null) => {
setAdjacent(start[PREV], end[NEXT]);
do {
const after = getEnd(start);
const next = after === end ? after : after[NEXT];
if (fragment)
fragment.insertBefore(start, fragment[END]);
else
start.remove();
start = next;
} while (start !== end);
};
/**
* @implements globalThis.Range
*/
class Range {
constructor() {
this[START] = null;
this[END] = null;
this.commonAncestorContainer = null;
}
/* TODO: this is more complicated than it looks
setStart(node, offset) {
this[START] = node.childNodes[offset];
}
setEnd(node, offset) {
this[END] = getEnd(node.childNodes[offset]);
}
//*/
insertNode(newNode) {
this[END].parentNode.insertBefore(newNode, this[START]);
}
selectNode(node) {
this[START] = node;
this[END] = getEnd(node);
}
// TODO: SVG elements should then create contextual fragments
// that return SVG nodes
selectNodeContents(node) {
this.selectNode(node);
this.commonAncestorContainer = node;
}
surroundContents(parentNode) {
parentNode.replaceChildren(this.extractContents());
}
setStartBefore(node) {
this[START] = node;
}
setStartAfter(node) {
this[START] = node.nextSibling;
}
setEndBefore(node) {
this[END] = getEnd(node.previousSibling);
}
setEndAfter(node) {
this[END] = getEnd(node);
}
cloneContents() {
let {[START]: start, [END]: end} = this;
const fragment = start.ownerDocument.createDocumentFragment();
while (start !== end) {
fragment.insertBefore(start.cloneNode(true), fragment[END]);
start = getEnd(start);
if (start !== end)
start = start[NEXT];
}
return fragment;
}
deleteContents() {
deleteContents(this);
}
extractContents() {
const fragment = this[START].ownerDocument.createDocumentFragment();
deleteContents(this, fragment);
return fragment;
}
createContextualFragment(html) {
const { commonAncestorContainer: doc } = this;
const isSVG = 'ownerSVGElement' in doc;
const document = isSVG ? doc.ownerDocument : doc;
let content = htmlToFragment(document, html);
if (isSVG) {
const childNodes = [...content.childNodes];
content = document.createDocumentFragment();
Object.setPrototypeOf(content, SVGElement.prototype);
content.ownerSVGElement = document;
for (const child of childNodes) {
Object.setPrototypeOf(child, SVGElement.prototype);
child.ownerSVGElement = document;
content.appendChild(child);
}
}
else
this.selectNode(content);
return content;
}
cloneRange() {
const range = new Range;
range[START] = this[START];
range[END] = this[END];
return range;
}
}
exports.Range = Range