Debian Codegraph @

closure-library/closure/goog/testing/editor/dom.js

raw
1 // Copyright 2009 The Closure Library Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 /**
16 * @fileoverview Testing utilities for editor specific DOM related tests.
17 *
18 */
19
20 goog.setTestOnly('goog.testing.editor.dom');
21 goog.provide('goog.testing.editor.dom');
22
23 goog.require('goog.dom.NodeType');
24 goog.require('goog.dom.TagIterator');
25 goog.require('goog.dom.TagWalkType');
26 goog.require('goog.iter');
27 goog.require('goog.string');
28 goog.require('goog.testing.asserts');
29
30
31 /**
32 * Returns the previous (in document order) node from the given node that is a
33 * non-empty text node, or null if none is found or opt_stopAt is not an
34 * ancestor of node. Note that if the given node has children, the search will
35 * start from the end tag of the node, meaning all its descendants will be
36 * included in the search, unless opt_skipDescendants is true.
37 * @param {Node} node Node to start searching from.
38 * @param {Node=} opt_stopAt Node to stop searching at (search will be
39 * restricted to this node's subtree), defaults to the body of the document
40 * containing node.
41 * @param {boolean=} opt_skipDescendants Whether to skip searching the given
42 * node's descentants.
43 * @return {Text} The previous (in document order) node from the given node
44 * that is a non-empty text node, or null if none is found.
45 */
46 goog.testing.editor.dom.getPreviousNonEmptyTextNode = function(
47 node, opt_stopAt, opt_skipDescendants) {
48 return goog.testing.editor.dom.getPreviousNextNonEmptyTextNodeHelper_(
49 node, opt_stopAt, opt_skipDescendants, true);
50 };
51
52
53 /**
54 * Returns the next (in document order) node from the given node that is a
55 * non-empty text node, or null if none is found or opt_stopAt is not an
56 * ancestor of node. Note that if the given node has children, the search will
57 * start from the start tag of the node, meaning all its descendants will be
58 * included in the search, unless opt_skipDescendants is true.
59 * @param {Node} node Node to start searching from.
60 * @param {Node=} opt_stopAt Node to stop searching at (search will be
61 * restricted to this node's subtree), defaults to the body of the document
62 * containing node.
63 * @param {boolean=} opt_skipDescendants Whether to skip searching the given
64 * node's descentants.
65 * @return {Text} The next (in document order) node from the given node that
66 * is a non-empty text node, or null if none is found or opt_stopAt is not
67 * an ancestor of node.
68 */
69 goog.testing.editor.dom.getNextNonEmptyTextNode = function(
70 node, opt_stopAt, opt_skipDescendants) {
71 return goog.testing.editor.dom.getPreviousNextNonEmptyTextNodeHelper_(
72 node, opt_stopAt, opt_skipDescendants, false);
73 };
74
75
76 /**
77 * Helper that returns the previous or next (in document order) node from the
78 * given node that is a non-empty text node, or null if none is found or
79 * opt_stopAt is not an ancestor of node. Note that if the given node has
80 * children, the search will start from the end or start tag of the node
81 * (depending on whether it's searching for the previous or next node), meaning
82 * all its descendants will be included in the search, unless
83 * opt_skipDescendants is true.
84 * @param {Node} node Node to start searching from.
85 * @param {Node=} opt_stopAt Node to stop searching at (search will be
86 * restricted to this node's subtree), defaults to the body of the document
87 * containing node.
88 * @param {boolean=} opt_skipDescendants Whether to skip searching the given
89 * node's descentants.
90 * @param {boolean=} opt_isPrevious Whether to search for the previous non-empty
91 * text node instead of the next one.
92 * @return {Text} The next (in document order) node from the given node that
93 * is a non-empty text node, or null if none is found or opt_stopAt is not
94 * an ancestor of node.
95 * @private
96 */
97 goog.testing.editor.dom.getPreviousNextNonEmptyTextNodeHelper_ = function(
98 node, opt_stopAt, opt_skipDescendants, opt_isPrevious) {
99 opt_stopAt = opt_stopAt || node.ownerDocument.body;
100 // Initializing the iterator to iterate over the children of opt_stopAt
101 // makes it stop only when it finishes iterating through all of that
102 // node's children, even though we will start at a different node and exit
103 // that starting node's subtree in the process.
104 var iter = new goog.dom.TagIterator(opt_stopAt, opt_isPrevious);
105
106 // TODO(user): Move this logic to a new method in TagIterator such as
107 // skipToNode().
108 // Then we set the iterator to start at the given start node, not opt_stopAt.
109 var walkType; // Let TagIterator set the initial walk type by default.
110 var depth = goog.testing.editor.dom.getRelativeDepth_(node, opt_stopAt);
111 if (depth == -1) {
112 return null; // Fail because opt_stopAt is not an ancestor of node.
113 }
114 if (node.nodeType == goog.dom.NodeType.ELEMENT) {
115 if (opt_skipDescendants) {
116 // Specifically set the initial walk type so that we skip the descendant
117 // subtree by starting at the start if going backwards or at the end if
118 // going forwards.
119 walkType = opt_isPrevious ? goog.dom.TagWalkType.START_TAG :
120 goog.dom.TagWalkType.END_TAG;
121 } else {
122 // We're starting "inside" an element node so the depth needs to be one
123 // deeper than the node's actual depth. That's how TagIterator works!
124 depth++;
125 }
126 }
127 iter.setPosition(node, walkType, depth);
128
129 // Advance the iterator so it skips the start node.
130 try {
131 iter.next();
132 } catch (e) {
133 return null; // It could have been a leaf node.
134 }
135 // Now just get the first non-empty text node the iterator finds.
136 var filter =
137 goog.iter.filter(iter, goog.testing.editor.dom.isNonEmptyTextNode_);
138 try {
139 return /** @type {Text} */ (filter.next());
140 } catch (e) { // No next item is available so return null.
141 return null;
142 }
143 };
144
145
146 /**
147 * Returns whether the given node is a non-empty text node.
148 * @param {Node} node Node to be checked.
149 * @return {boolean} Whether the given node is a non-empty text node.
150 * @private
151 */
152 goog.testing.editor.dom.isNonEmptyTextNode_ = function(node) {
153 return !!node && node.nodeType == goog.dom.NodeType.TEXT && node.length > 0;
154 };
155
156
157 /**
158 * Returns the depth of the given node relative to the given parent node, or -1
159 * if the given node is not a descendant of the given parent node. E.g. if
160 * node == parentNode returns 0, if node.parentNode == parentNode returns 1,
161 * etc.
162 * @param {Node} node Node whose depth to get.
163 * @param {Node} parentNode Node relative to which the depth should be
164 * calculated.
165 * @return {number} The depth of the given node relative to the given parent
166 * node, or -1 if the given node is not a descendant of the given parent
167 * node.
168 * @private
169 */
170 goog.testing.editor.dom.getRelativeDepth_ = function(node, parentNode) {
171 var depth = 0;
172 while (node) {
173 if (node == parentNode) {
174 return depth;
175 }
176 node = node.parentNode;
177 depth++;
178 }
179 return -1;
180 };
181
182
183 /**
184 * Assert that the range is surrounded by the given strings. This is useful
185 * because different browsers can place the range endpoints inside different
186 * nodes even when visually the range looks the same. Also, there may be empty
187 * text nodes in the way (again depending on the browser) making it difficult to
188 * use assertRangeEquals.
189 * @param {string} before String that should occur immediately before the start
190 * point of the range. If this is the empty string, assert will only succeed
191 * if there is no text before the start point of the range.
192 * @param {string} after String that should occur immediately after the end
193 * point of the range. If this is the empty string, assert will only succeed
194 * if there is no text after the end point of the range.
195 * @param {goog.dom.AbstractRange} range The range to be tested.
196 * @param {Node=} opt_stopAt Node to stop searching at (search will be
197 * restricted to this node's subtree).
198 */
199 goog.testing.editor.dom.assertRangeBetweenText = function(
200 before, after, range, opt_stopAt) {
201 var previousText =
202 goog.testing.editor.dom.getTextFollowingRange_(range, true, opt_stopAt);
203 if (before == '') {
204 assertNull(
205 'Expected nothing before range but found <' + previousText + '>',
206 previousText);
207 } else {
208 assertNotNull(
209 'Expected <' + before + '> before range but found nothing',
210 previousText);
211 assertTrue(
212 'Expected <' + before + '> before range but found <' + previousText +
213 '>',
214 goog.string.endsWith(
215 /** @type {string} */ (previousText), before));
216 }
217 var nextText =
218 goog.testing.editor.dom.getTextFollowingRange_(range, false, opt_stopAt);
219 if (after == '') {
220 assertNull(
221 'Expected nothing after range but found <' + nextText + '>', nextText);
222 } else {
223 assertNotNull(
224 'Expected <' + after + '> after range but found nothing', nextText);
225 assertTrue(
226 'Expected <' + after + '> after range but found <' + nextText + '>',
227 goog.string.startsWith(
228 /** @type {string} */ (nextText), after));
229 }
230 };
231
232
233 /**
234 * Returns the text that follows the given range, where the term "follows" means
235 * "comes immediately before the start of the range" if isBefore is true, and
236 * "comes immediately after the end of the range" if isBefore is false, or null
237 * if no non-empty text node is found.
238 * @param {goog.dom.AbstractRange} range The range to search from.
239 * @param {boolean} isBefore Whether to search before the range instead of
240 * after it.
241 * @param {Node=} opt_stopAt Node to stop searching at (search will be
242 * restricted to this node's subtree).
243 * @return {?string} The text that follows the given range, or null if no
244 * non-empty text node is found.
245 * @private
246 */
247 goog.testing.editor.dom.getTextFollowingRange_ = function(
248 range, isBefore, opt_stopAt) {
249 var followingTextNode;
250 var endpointNode = isBefore ? range.getStartNode() : range.getEndNode();
251 var endpointOffset = isBefore ? range.getStartOffset() : range.getEndOffset();
252 var getFollowingTextNode = isBefore ?
253 goog.testing.editor.dom.getPreviousNonEmptyTextNode :
254 goog.testing.editor.dom.getNextNonEmptyTextNode;
255
256 if (endpointNode.nodeType == goog.dom.NodeType.TEXT) {
257 // Range endpoint is in a text node.
258 var endText = endpointNode.nodeValue;
259 if (isBefore ? endpointOffset > 0 : endpointOffset < endText.length) {
260 // There is text in this node following the endpoint so return the portion
261 // that follows the endpoint.
262 return isBefore ? endText.substr(0, endpointOffset) :
263 endText.substr(endpointOffset);
264 } else {
265 // There is no text following the endpoint so look for the follwing text
266 // node.
267 followingTextNode = getFollowingTextNode(endpointNode, opt_stopAt);
268 return followingTextNode && followingTextNode.nodeValue;
269 }
270 } else {
271 // Range endpoint is in an element node.
272 var numChildren = endpointNode.childNodes.length;
273 if (isBefore ? endpointOffset > 0 : endpointOffset < numChildren) {
274 // There is at least one child following the endpoint.
275 var followingChild =
276 endpointNode
277 .childNodes[isBefore ? endpointOffset - 1 : endpointOffset];
278 if (goog.testing.editor.dom.isNonEmptyTextNode_(followingChild)) {
279 // The following child has text so return that.
280 return followingChild.nodeValue;
281 } else {
282 // The following child has no text so look for the following text node.
283 followingTextNode = getFollowingTextNode(followingChild, opt_stopAt);
284 return followingTextNode && followingTextNode.nodeValue;
285 }
286 } else {
287 // There is no child following the endpoint, so search from the endpoint
288 // node, but don't search its children because they are not following the
289 // endpoint!
290 followingTextNode = getFollowingTextNode(endpointNode, opt_stopAt, true);
291 return followingTextNode && followingTextNode.nodeValue;
292 }
293 }
294 };