-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
224 lines (197 loc) · 6.09 KB
/
index.js
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/**
* Module dependencies.
*/
var indexOf = require('index-of');
var debug = require('debug')('range-normalize');
// map to an Object for faster lookup times
var voidElements = require('void-elements').reduce(function (obj, name) {
obj[name.toUpperCase()] = true;
return obj;
}, {});
/**
* Module exports.
*/
module.exports = normalize;
/**
* "Normalizes" the DOM Range instance, such that slight variations in the start
* and end containers end up being normalized to the same "base" representation.
* The aim is to always have `startContainer` and `endContainer` pointing to
* TextNode instances.
*
* Pseudo-logic is as follows:
*
* For the "start":
*
* - Is start container already a text node?
* - Is start offset at the end of the text node?
* - Traverse "up" the start container until a `nextSibling` is found
* - Set new offset = 0
* - Go back to start, repeat
* - Else
* - Done! start container and offset are normalized
* - Else
* - Traverse "down" the start container until either a text node or void element is found
* - Set new start offset = 0
* - Is new start container a text node?
* - Go back to start, repeat
* - Else, is new start container a void element?
* - Set start container's `parentNode` as new start container
* - Set new start offset = indexOf(new start container's `childNodes`, old start container "void element")
* - Else (assume start container is any other HTML element which may or may not contain children)
* - ???
*
* For the "end":
*
* - Is end container already a text node?
* - Is end offset at the beginning of the text node?
* - Traverse "up" the end container until a `previousSibling` is found
*
* @param {Range} range - DOM Range instance to "normalize"
* @return {Range} returns `range`, after being "normalized"
*/
function normalize (range) {
var sc = range.startContainer;
var so = range.startOffset;
var ec = range.endContainer;
var eo = range.endOffset;
var collapsed = range.collapsed;
var end;
if (!collapsed && sc.nodeType === 3 && so === sc.nodeValue.length) {
debug('start is at end of TextNode, need to move to `nextSibling`');
while (sc && !sc.nextSibling) {
sc = sc.parentNode;
}
if (sc) {
sc = sc.nextSibling;
so = 0;
} else {
debug('could not find TextNode within %o, resetting `sc`', range.startContainer);
sc = range.startContainer;
}
}
if (sc.nodeType === 1) {
if (voidElements[sc.nodeName]) {
debug('start is a "void element", need to use parent node', sc);
var v = sc;
sc = v.parentNode;
so = indexOf(sc.childNodes, v);
} else {
debug('start is an Element, need to find deepest child node at offset %o', so);
if (so >= sc.childNodes.length) {
end = true;
sc = sc.childNodes[sc.childNodes.length - 1];
} else {
end = false;
sc = sc.childNodes[so];
}
var c;
while (sc) {
if (sc.nodeType === 3) break;
if (end) {
c = sc.lastChild;
} else {
c = sc.firstChild;
}
if (c && voidElements[c.nodeName]) break;
sc = c;
}
if (sc) {
if (sc.nodeType === 1) {
// a "void element"'s parent
so = end ? sc.childNodes.length : 0;
} else {
// text node
so = end ? sc.nodeValue.length : 0;
}
} else {
debug('could not find TextNode within %o, resetting `sc`', range.startContainer);
sc = range.startContainer;
so = range.startOffset;
}
}
}
if (collapsed) {
// if we're dealing with a collapsed Range, then update the end
// points to match our normalized start container and offset
ec = sc;
eo = so;
} else {
while (true) {
if (eo === 0) {
debug('end is at start of Node, need to move to `previousSibling`');
while (ec && !ec.previousSibling) {
ec = ec.parentNode;
}
if (ec) {
ec = ec.previousSibling;
eo = ec.nodeType === 3 ? ec.nodeValue.length : ec.childNodes.length;
} else {
debug('could not find Node within %o, resetting `ec`', range.endContainer);
if (collapsed) {
ec = sc;
eo = so;
} else {
ec = range.endContainer;
eo = range.endOffset;
}
break;
}
}
if (ec.nodeType === 1) {
if (voidElements[ec.nodeName]) {
debug('end is a "void element", need to use parent node', ec);
var v = ec;
ec = v.parentNode;
eo = indexOf(ec.childNodes, v) + 1;
break;
} else {
debug('end is an Element, need to find deepest node at offset %o', eo);
end = true;
if (eo >= ec.childNodes.length) {
ec = ec.childNodes[ec.childNodes.length - 1];
} else {
ec = ec.childNodes[eo - 1];
}
var c;
while (ec) {
if (ec.nodeType === 3) break;
c = ec.lastChild;
if (c && voidElements[c.nodeName]) break;
ec = c;
}
if (ec) {
if (ec.nodeType === 1) {
// a "void element"'s parent
eo = ec.childNodes.length;
} else {
// text node
eo = ec.nodeValue.length;
}
break;
} else {
debug('could not find TextNode within %o, resetting `ec`', range.endContainer);
if (collapsed) {
ec = sc;
eo = so;
} else {
ec = range.endContainer;
eo = range.endOffset;
}
break;
}
}
} else {
break;
}
}
}
if (sc !== range.startContainer || so !== range.startOffset) {
debug('normalizing Range `start` to %o %o:', sc, so);
range.setStart(sc, so);
}
if (ec !== range.endContainer || eo !== range.endOffset) {
debug('normalizing Range `end` to %o %o:', ec, eo);
range.setEnd(ec, eo);
}
return range;
}