-
-
Notifications
You must be signed in to change notification settings - Fork 8.2k
/
text.js
273 lines (241 loc) · 8.01 KB
/
text.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
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
/**
* @fileoverview Text handling methods.
*/
goog.provide('core.text');
goog.require('bot');
goog.require('bot.events');
goog.require('bot.userAgent');
goog.require('core.patternMatcher');
goog.require('goog.dom');
goog.require('goog.dom.NodeType');
goog.require('goog.string');
goog.require('goog.userAgent');
var SHADOW_DOM_ENABLED = typeof ShadowRoot === 'function';
/**
* Attempt to normalize the text content of an element.
*
* @param {!Node} element The element to use.
* @param {boolean} preformatted Whether the text is preformatted or not.
* @return {string} The text content of the element.
* @private
*/
core.text.getTextContent_ = function(element, preformatted) {
if (element.style && (element.style.visibility == 'hidden' ||
element.style.display == 'none')) {
return '';
}
var text;
if (element.nodeType == goog.dom.NodeType.TEXT) {
text = element.data;
if (!preformatted) {
text = text.replace(/\n|\r|\t/g, ' ');
}
return text.replace(/ /, ' ');
}
if (SHADOW_DOM_ENABLED &&
element.nodeType == goog.dom.NodeType.ELEMENT &&
element.shadowRoot !== null) {
return core.text.getTextContent_(element.shadowRoot, preformatted);
}
if ((element.nodeType == goog.dom.NodeType.ELEMENT ||
element.nodeType == goog.dom.NodeType.DOCUMENT_FRAGMENT) &&
element.nodeName != 'SCRIPT' &&
element.nodeName != 'STYLE') {
var childrenPreformatted = preformatted || (element.tagName == 'PRE');
text = '';
for (var i = 0; i < element.childNodes.length; i++) {
var child = element.childNodes.item(i);
if (!child) {
continue;
}
if (SHADOW_DOM_ENABLED &&
(child.nodeName == 'CONTENT' || child.nodeName == 'SLOT')) {
var shadowChildren;
if (child.nodeName == 'CONTENT') {
shadowChildren = child.getDistributedNodes();
} else {
shadowChildren = child.assignedNodes();
}
for (var j = 0; j < shadowChildren.length; j++) {
var shadowChild = shadowChildren[j];
if (!shadowChild) {
continue;
}
text += core.text.getTextContent_(shadowChild, preformatted);
}
continue;
}
text += core.text.getTextContent_(child, childrenPreformatted);
}
// Handle block elements that introduce newlines
// -- From HTML spec:
//<!ENTITY % block
// "P | %heading; | %list; | %preformatted; | DL | DIV | NOSCRIPT |
// BLOCKQUOTE | F:wORM | HR | TABLE | FIELDSET | ADDRESS">
//
// TODO: should potentially introduce multiple newlines to separate blocks
if (element.tagName == 'P' || element.tagName == 'BR' ||
element.tagName == 'HR' || element.tagName == 'DIV') {
text += '\n';
}
text = text.replace(/ /, ' ');
if (bot.userAgent.IE && bot.userAgent.isProductVersion(9)) {
text = text.replace(/d/, ' ');
}
return text;
}
return '';
};
/**
* Convert all newlines to \n. A newline is defined as being one of the common
* line endings for Mac, Windows or UNIX.
*
* @param {string} text The text to normalize.
* @return {string} The converted text, with all line endings replaced
* with '\n'.
* @private
*/
core.text.normalizeNewlines_ = function(text) {
return text.replace(/\r\n|\r/g, '\n');
};
/**
* @param {string} text The text to perform the replacement on.
* @param {string} oldText The string to replace.
* @param {string} newText The replacement text.
* @return {string} 'text' with all occurances of 'oldText' replaced by
* 'newText'.
* @private
*/
core.text.replaceAll_ = function(text, oldText, newText) {
while (text.indexOf(oldText) != -1) {
text = text.replace(oldText, newText);
}
return text;
};
/**
* Replace multiple sequential spaces with a single space, and then convert
* to space.
*
* @param {string} text The text to normalize.
* @return {string} The normalized text.
* @private
*/
core.text.normalizeSpaces_ = function(text) {
// Replace multiple spaces with a single space
// TODO - this shouldn't occur inside PRE elements
text = text.replace(/\ +/g, ' ');
// Replace with a space
var nbspPattern = new RegExp(String.fromCharCode(160), 'g');
if (goog.userAgent.WEBKIT) {
return core.text.replaceAll_(text, String.fromCharCode(160), ' ');
} else {
return text.replace(nbspPattern, ' ');
}
};
/**
* Return an element's text content.
*
* @param {!Element} element The element whose text to retrieve.
* @return {string} The element's text content.
*/
core.text.getElementText = function(element) {
var text = '';
var isRecentFirefox =
(goog.userAgent.GECKO && goog.userAgent.VERSION >= '1.8');
if (isRecentFirefox || goog.userAgent.WEBKIT || goog.userAgent.IE) {
text = core.text.getTextContent_(element, false);
} else {
if (element.textContent) {
text = element.textContent;
} else {
if (element.innerText) {
text = element.innerText;
}
}
}
text = core.text.normalizeNewlines_(text);
text = core.text.normalizeSpaces_(text);
return goog.string.trim(text);
};
/**
* @return {string} The entire text content of the page.
*/
core.text.getBodyText = function() {
var doc = bot.getWindow().document;
var body = doc.body;
return !!body ? core.text.getElementText(body) : '';
};
/**
* Verifies that the specified text pattern appears somewhere on the rendered
* page shown to the user.
*
* @param {string} pattern A <a href="#patterns">pattern</a> to match with the
* text of the page.
* @return {boolean} Whether the pattern matches the text.
*/
core.text.isTextPresent = function(pattern) {
var allText = core.text.getBodyText();
var patternMatcher = core.patternMatcher.against(pattern);
if (patternMatcher.strategyName == 'glob') {
if (pattern.indexOf('glob:') == 0) {
pattern = pattern.substring('glob:'.length); // strip off "glob:"
}
patternMatcher = core.patternMatcher.against('globContains:' + pattern);
}
return patternMatcher(allText);
};
/**
* @param {string} locator The link text to search for.
* @param {Document=} opt_doc The document to search in.
* @return {Element} The located element or {@code null}.
*/
core.text.linkLocator = function(locator, opt_doc) {
var doc = opt_doc || goog.dom.getOwnerDocument(bot.getWindow());
var links = doc.getElementsByTagName('a');
for (var i = 0; i < links.length; i++) {
var element = links[i];
var text = core.text.getElementText(element);
if (core.patternMatcher.matches(locator, text)) {
return element;
}
}
return null;
};
/**
* Set a new caret position within an element.
*
* @param {!Element} element The element to use.
* @param {number} position The new caret position.
*/
core.text.setCursorPosition = function(element, position) {
if (position == -1) {
position = element.value.length;
}
if (element.setSelectionRange) {
element.focus();
element.setSelectionRange(/*start*/ position, /*end*/ position);
} else if (element.createTextRange) {
bot.events.fire(element, bot.events.EventType.FOCUS);
var range = element.createTextRange();
range.collapse(true);
range.moveEnd('character', position);
range.moveStart('character', position);
range.select();
}
};