1 |
|
2 |
|
3 |
|
4 | const DocumentFragment = require('./DocumentFragment');
|
5 |
|
6 | function append(node) {
|
7 | this.appendChild(node);
|
8 | }
|
9 |
|
10 | function clone(node) {
|
11 | return node.cloneNode(true);
|
12 | }
|
13 |
|
14 | function remove(node) {
|
15 | this.removeChild(node);
|
16 | }
|
17 |
|
18 | function getContents(start, end) {
|
19 | const nodes = [start];
|
20 | while (start !== end) {
|
21 | nodes.push(start = start.nextSibling);
|
22 | }
|
23 | return nodes;
|
24 | }
|
25 |
|
26 |
|
27 | module.exports = class Range {
|
28 |
|
29 | cloneContents() {
|
30 | const fragment = new DocumentFragment(this._start.ownerDocument);
|
31 | getContents(this._start, this._end).map(clone).forEach(append, fragment);
|
32 | return fragment;
|
33 | }
|
34 |
|
35 | deleteContents() {
|
36 | getContents(this._start, this._end)
|
37 | .forEach(remove, this._start.parentNode);
|
38 | }
|
39 |
|
40 | extractContents() {
|
41 | const fragment = new DocumentFragment(this._start.ownerDocument);
|
42 | const nodes = getContents(this._start, this._end);
|
43 | nodes.forEach(remove, this._start.parentNode);
|
44 | nodes.forEach(append, fragment);
|
45 | return fragment;
|
46 | }
|
47 |
|
48 | cloneRange() {
|
49 | const range = new Range;
|
50 | range._start = this._start;
|
51 | range._end = this._end;
|
52 | return range;
|
53 | }
|
54 |
|
55 | setStartAfter(node) {
|
56 | this._start = node.nextSibling;
|
57 | }
|
58 |
|
59 | setStartBefore(node) {
|
60 | this._start = node;
|
61 | }
|
62 |
|
63 | setEndAfter(node) {
|
64 | this._end = node;
|
65 | }
|
66 |
|
67 | setEndBefore(node) {
|
68 | this._end = node.previousSibling;
|
69 | }
|
70 |
|
71 | };
|