-
Notifications
You must be signed in to change notification settings - Fork 54
/
parser.js
377 lines (323 loc) · 9.56 KB
/
parser.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
'use strict';
/**
* Create the Content Parser namespace. This component is injected into the
* page and constructs a representation of the DOM, which will be used for
* occurrence matching by the background script and highlighting by the
* highlighter.
*
* As the document representation object is constructed, text nodes in the page
* are wrapped in a span, and assigned a UUID which is used to reference it.
*
* Once the extension closes, the page is restored using the UUIDs from the
* document representation object.
* */
Find.register('Content.Parser', function(self) {
/**
* Walk the pages DOM tree and construct the document representation object, while
* wrapping text nodes with wrapper elements.
*
* @return {object} the document representation object
* */
self.buildDOMReferenceObject = function() {
let DOMTreeWalker = document.createTreeWalker(document.body, NodeFilter.SHOW_ALL, {acceptNode: nodeFilter}, false);
let DOMModelObject = {};
let reachedEndOfTree = false;
let groupIndex = 0;
let blockLevels = [];
let elementBoundary = false;
let preformatted = {flag: false, index: null};
let hidden = {flag: false, index: null};
let node = DOMTreeWalker.root;
while (!reachedEndOfTree) {
node = DOMTreeWalker.nextNode();
if (!node) {
reachedEndOfTree = true;
}
let textGroup = {group: [], preformatted: false};
while (node) {
let nodeDepth = getNodeTreeDepth(node);
if (!preformatted.flag && isPreformattedElement(node)) {
preformatted.flag = true;
preformatted.index = nodeDepth;
} else if (preformatted.flag && nodeDepth <= preformatted.index) {
preformatted.flag = false;
preformatted.index = null;
}
if (!hidden.flag && isHiddenElement(node)) {
hidden.flag = true;
hidden.index = nodeDepth;
} else if (hidden.flag && nodeDepth <= hidden.index) {
if (!isHiddenElement(node)) {
hidden.flag = false;
hidden.index = null;
} else {
hidden.index = nodeDepth;
}
}
if (hidden.flag) {
node = DOMTreeWalker.nextNode();
continue;
}
if (isElementNode(node)) {
if (nodeDepth <= blockLevels[blockLevels.length - 1]) {
while (nodeDepth <= blockLevels[blockLevels.length - 1]) {
blockLevels.pop();
}
if (!isInlineLevelElement(node)) {
blockLevels.push(nodeDepth);
}
elementBoundary = true;
break;
} else {
if (!isInlineLevelElement(node)) {
blockLevels.push(nodeDepth);
elementBoundary = true;
break;
}
}
} else if (isTextNode(node)) {
if (nodeDepth <= blockLevels[blockLevels.length - 1]) {
while (nodeDepth <= blockLevels[blockLevels.length - 1]) {
blockLevels.pop();
}
DOMTreeWalker.previousNode();
elementBoundary = true;
break;
}
if (!preformatted.flag && isNodeTextValueWhitespaceOnly(node) && node.nodeValue.length !== 1) {
node = DOMTreeWalker.nextNode();
continue;
} else if (node.nodeValue.length === 1 && node.nodeValue.charCodeAt(0) === 10) {
node = DOMTreeWalker.nextNode();
continue;
}
let identifierUUID = generateElementUUID();
let nodeText = formatTextNodeValue(node, preformatted.flag, elementBoundary);
if (nodeText.length === 0) {
node = DOMTreeWalker.nextNode();
continue;
}
let wrapperElement = document.createElement('span');
wrapperElement.style.cssText = 'all: unset;';
wrapperElement.setAttribute('id', identifierUUID);
node.parentNode.insertBefore(wrapperElement, node);
wrapperElement.appendChild(node);
let textNodeInformation = {groupIndex: groupIndex, text: nodeText, elementUUID: identifierUUID};
textGroup.group.push(textNodeInformation);
textGroup.preformatted = preformatted.flag;
}
node = DOMTreeWalker.nextNode();
elementBoundary = false;
if (!node) {
reachedEndOfTree = true;
}
}
if (textGroup.group.length === 0) {
continue;
}
DOMModelObject[groupIndex++] = textGroup;
}
return DOMModelObject;
};
/**
* Restore the web page by removing any wrapper elements.
*
* @param {array} uuids - A list of UUIDs
* */
self.restoreWebPage = function(uuids) {
for (let index = 0; index < uuids.length; index++) {
let el = document.getElementById(uuids[index]);
let parent = el.parentElement;
while (el.firstChild) {
parent.insertBefore(el.firstChild, el);
}
parent.removeChild(el);
parent.normalize();
}
};
/**
* Filter used by the DOM tree walker. Used to skip certain elements.
* @private
* @param {Element} node - The DOM node.
* @return {number} NodeFilter.FILTER_ACCEPT if the node is accepted, or NodeFilter.FILTER_REJECT
* if the node is rejected.
* */
function nodeFilter(node) {
if (isElementNode(node)) {
switch(node.tagName.toLowerCase()) {
case 'script':
case 'noscript':
case 'style':
case 'textarea':
case 'math':
return NodeFilter.FILTER_REJECT;
default:
return NodeFilter.FILTER_ACCEPT;
}
}
if (isTextNode(node)) {
return NodeFilter.FILTER_ACCEPT;
}
return NodeFilter.FILTER_REJECT;
}
/**
* Decode any HTML character entities, strip consecutive whitespaces,
* and return the node text value.
*
* @private
* @param {Node} node - The DOM node.
* @param {boolean} preformatted - Whether or not the node is a preformatted text element.
* @param {boolean} elementBoundary - Whether the element is a boundary element.
* @return {string} the formatted text.
* */
function formatTextNodeValue(node, preformatted, elementBoundary) {
if (isElementNode(node)) {
return;
}
let nodeText = decode(node.nodeValue);
if (preformatted) {
return nodeText;
}
let text = nodeText.replace(/[\t\n\r ]+/g, ' ');
if (elementBoundary) {
text = text.replace(/^[\t\n\r ]+/g, '');
}
return text;
}
/**
* Determine whether a given node is preformatted.
*
* A node is preformatted if it has:
* - tag name 'pre'
* - style 'whitespace: pre'
*
* @private
* @param {Element} node - The DOM node.
* @return {boolean} true of the element is a preformatted element, false if the
* element is not preformatted, and undefined if the node is not an element.
* */
function isPreformattedElement(node) {
if (!isElementNode(node)) {
return undefined;
}
if (node.tagName.toLowerCase() === 'pre' || node.style.whiteSpace.toLowerCase() === 'pre') {
return true;
}
let computedStyle = window.getComputedStyle(node);
if (computedStyle.getPropertyValue('whitespace').toLowerCase() === 'pre') {
return true;
}
return false;
}
/**
* Determine whether a given node is visible in the page.
*
* @private
* @param {Node} node - The DOM node.
* @return {boolean} true if the element is hidden, false if the element is visible,
* and undefined if the not an element.
* */
function isHiddenElement(node) {
if (!isElementNode(node)) {
return undefined;
}
if (node.style.display === 'none' || node.style.display === 'hidden') {
return true;
}
let computedStyle = window.getComputedStyle(node);
if (computedStyle.getPropertyValue('display').toLowerCase() === 'none') {
return true;
}
if (computedStyle.getPropertyValue('display').toLowerCase() === 'hidden') {
return true;
}
return false;
}
/**
* Determine whether or not a given DOM node is an Element.
*
* @private
* @param {Node} node - The DOM node.
* @return {boolean} true if the node is an element, false otherwise.
* */
function isElementNode(node) {
return node.nodeType === Node.ELEMENT_NODE;
}
/**
* Determine whether or not a given DOM node is a text node.
*
* @private
* @param {Node} node - The DOM node.
* @return {boolean} true if the node is a text node, false otherwise.
* */
function isTextNode(node) {
return node.nodeType === Node.TEXT_NODE;
}
/**
* Determine whether or not an element is inline-level or block-level.
*
* @private
* @param {Element} element - The DOM element.
* @return {boolean} true if the element is inline, false otherwise.
* */
function isInlineLevelElement(element) {
if (!isElementNode(element)) {
return false;
}
//Special case: will treat <br> as block element
let elementTagName = element.tagName.toLowerCase();
if (elementTagName === 'br') {
return false;
}
if (window.getComputedStyle(element).display === 'inline') {
return true;
}
return false;
}
/**
* Determine whether a text node value is whitespace only.
*
* @private
* @param {Node} node - The DOM node.
* @return {boolean} true if the node value is whitespace only, false otherwise.
* */
function isNodeTextValueWhitespaceOnly(node) {
return !(/[^\t\n\r ]/.test(node.nodeValue));
}
/**
* Determine the depth of a given node in the DOM tree.
*
* @private
* @param {Node} node - The DOM node.
* @return {number} the depth of the DOM node in the tree.
* */
function getNodeTreeDepth(node) {
let depth = -1;
while (node != null) {
depth++;
node = node.parentNode;
}
return depth;
}
/**
* Generate a UUIDv4.
*
* @private
* @return {string} a new UUIDv4.
* */
function generateElementUUID() {
let generateBlock = (size) => {
let block = '';
for (let index = 0; index < size; index++) {
block += Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
}
return block;
};
const blockSizes = [2, 1, 1, 1, 3];
let uuid = '';
for (let index = 0; index < blockSizes.length; index++) {
uuid += generateBlock(blockSizes[index]) + (index === blockSizes.length - 1 ? '' : '-');
}
return uuid;
}
});