-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtml-walker.js
161 lines (138 loc) · 4.62 KB
/
html-walker.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
var $ = require('jquery');
var HtmlWalker = function (window) {
this.window = window;
};
/**
* Clear all active selections in the DOM
*/
HtmlWalker.prototype.clearSelection = function () {
if (this.window.getSelection) {
this.window.getSelection().removeAllRanges();
}
else if (this.window.document.selection) {
this.window.document.selection.empty();
}
};
/**
* Given a list of text nodes that are descendants of a node and the
* character offsets in that node, return the text nodes at the start
* and end and the character offsets within those nodes.
*/
HtmlWalker.prototype.getRangeInText = function (textNodeList, startOffset, endOffset) {
var currentOffset = 0;
var nodeStartOffset, nodeEndOffset;
var startNode, endNode;
var foundStart = false, foundEnd = false;
textNodeList.some(function (node) {
var textLength = node.textContent.length;
if (startOffset >= currentOffset &&
startOffset < currentOffset + textLength) {
foundStart = true;
startNode = node;
nodeStartOffset = startOffset - currentOffset;
}
if (endOffset >= currentOffset &&
endOffset <= currentOffset + textLength) {
foundEnd = true;
endNode = node;
nodeEndOffset = endOffset - currentOffset;
}
currentOffset += textLength;
return foundStart && foundEnd;
});
return {
'start': {
'node': startNode,
'offset': nodeStartOffset
},
'end': {
'node': endNode,
'offset': nodeEndOffset
}
};
};
/**
* Get an ordered list of all text nodes within a node.
*/
HtmlWalker.prototype.getTextDescendants = function (node) {
var descendants = [];
var i, j;
var child;
var childDescendants;
if (node.nodeName === '#text') {
descendants.push(node);
} else {
for (i = 0; i < node.childNodes.length; i++) {
child = node.childNodes[i];
childDescendants = this.getTextDescendants(child);
for (j = 0; j < childDescendants.length; j++) {
descendants.push(childDescendants[j]);
}
}
}
return descendants;
};
/*
* Return the first single node that holds the full range.
*/
HtmlWalker.prototype.getEncapsulatingNode = function (range) {
var sCont = range.startContainer;
var eCont = range.endContainer;
var startParents = [sCont].concat($(sCont).parents().toArray());
var endParents = [eCont].concat($(eCont).parents().toArray());
var i, j, sNode, eNode;
for (i = 0; i < startParents.length; i++) {
for (j = 0; j < endParents.length; j++) {
sNode = startParents[i];
eNode = endParents[j];
if (sNode === eNode && sNode.nodeType === 1 && eNode.nodeType === 1) {
return sNode;
}
}
}
return null;
};
/**
* Get offsets of a range within an element
* REUSE: Based on code concept found at:
* http://stackoverflow.com/questions/4811822/get-a-ranges-start-and-end-offsets-relative-to-its-parent-container#4812022
*/
HtmlWalker.prototype.offsetsWithinElement = function (node, range) {
var beforeRange, afterRange, nodeLength, sOffset, eOffset;
beforeRange = range.cloneRange();
beforeRange.selectNodeContents(node);
beforeRange.setEnd(range.startContainer, range.startOffset);
sOffset = beforeRange.toString().length;
afterRange = range.cloneRange();
afterRange.selectNodeContents(node);
nodeLength = afterRange.toString().length;
afterRange.setStart(range.endContainer, range.endOffset);
eOffset = nodeLength - afterRange.toString().length;
return {
'start': sOffset,
'end': eOffset,
};
};
/**
* Given a selection, get the text of the selection plus 'edge_size' characters
* on either side of the original selection.
*/
HtmlWalker.prototype.getContext = function (selection, edge_size) {
var range = selection.getRangeAt(0);
var node = this.getEncapsulatingNode(range);
var parents = [node].concat($(node).parents().toArray());
var i, par, offsets, start_char, end_char;
for (i = 0; i < parents.length; i++) {
par = parents[i];
offsets = this.offsetsWithinElement(par, range);
start_char = offsets.start;
end_char = offsets.end - 1;
if (start_char >= edge_size && end_char < par.textContent.length - edge_size) {
return par.textContent.substring(start_char - edge_size, end_char + edge_size + 1);
}
}
return null;
};
module.exports = {
'HtmlWalker': HtmlWalker,
};