Debian Codegraph @

closure-library/closure/goog/dom/selection_test.js

raw
1 // Copyright 2007 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 goog.provide('goog.dom.selectionTest');
16 goog.setTestOnly('goog.dom.selectionTest');
17
18 goog.require('goog.dom');
19 goog.require('goog.dom.InputType');
20 goog.require('goog.dom.TagName');
21 goog.require('goog.dom.selection');
22 goog.require('goog.testing.jsunit');
23 goog.require('goog.userAgent');
24
25 var input;
26 var hiddenInput;
27 var textarea;
28 var hiddenTextarea;
29
30 function setUp() {
31 input = goog.dom.createDom(
32 goog.dom.TagName.INPUT, {type: goog.dom.InputType.TEXT});
33 textarea = goog.dom.createDom(goog.dom.TagName.TEXTAREA);
34 hiddenInput = goog.dom.createDom(
35 goog.dom.TagName.INPUT,
36 {type: goog.dom.InputType.TEXT, style: 'display: none'});
37 hiddenTextarea =
38 goog.dom.createDom(goog.dom.TagName.TEXTAREA, {style: 'display: none'});
39
40 document.body.appendChild(input);
41 document.body.appendChild(textarea);
42 document.body.appendChild(hiddenInput);
43 document.body.appendChild(hiddenTextarea);
44 }
45
46 function tearDown() {
47 goog.dom.removeChildren(document.body);
48 }
49
50
51 /**
52 * Tests getStart routine in both input and textarea.
53 */
54 function testGetStartInput() {
55 getStartHelper(input, hiddenInput);
56 }
57
58 function testGetStartTextarea() {
59 getStartHelper(textarea, hiddenTextarea);
60 }
61
62 /**
63 * Tests that getStart routine does not error for elements that are neither
64 * text inputs nor text areas.
65 */
66 function testGetStartOther() {
67 var button = goog.dom.createDom(goog.dom.TagName.BUTTON);
68 var hiddenButton =
69 goog.dom.createDom(goog.dom.TagName.BUTTON, {style: 'display: none'});
70 document.body.appendChild(button);
71 document.body.appendChild(hiddenButton);
72 getStartHelper(button, hiddenButton);
73 }
74
75 function getStartHelper(field, hiddenField) {
76 assertEquals(0, goog.dom.selection.getStart(field));
77 assertEquals(0, goog.dom.selection.getStart(hiddenField));
78
79 field.focus();
80 assertEquals(0, goog.dom.selection.getStart(field));
81 }
82
83
84 /**
85 * Tests the setText routine for both input and textarea
86 * with a single line of text.
87 */
88 function testSetTextInput() {
89 setTextHelper(input);
90 }
91
92 function testSetTextTextarea() {
93 setTextHelper(textarea);
94 }
95
96 function setTextHelper(field) {
97 // Test one line string only
98 select(field);
99 assertEquals('', goog.dom.selection.getText(field));
100
101 goog.dom.selection.setText(field, 'Get Behind Me Satan');
102 assertEquals('Get Behind Me Satan', goog.dom.selection.getText(field));
103 }
104
105
106 /**
107 * Tests the setText routine for textarea with multiple lines of text.
108 */
109 function testSetTextMultipleLines() {
110 select(textarea);
111 assertEquals('', goog.dom.selection.getText(textarea));
112 var isLegacyIE = goog.userAgent.IE && !goog.userAgent.isVersionOrHigher('9');
113 var message = isLegacyIE ? 'Get Behind Me\r\nSatan' : 'Get Behind Me\nSatan';
114 goog.dom.selection.setText(textarea, message);
115 assertEquals(message, goog.dom.selection.getText(textarea));
116
117 // Select the text up to the point just after the \r\n combination
118 // or \n in GECKO.
119 var endOfNewline = isLegacyIE ? 15 : 14;
120 var selectedMessage = message.substring(0, endOfNewline);
121 goog.dom.selection.setStart(textarea, 0);
122 goog.dom.selection.setEnd(textarea, endOfNewline);
123 assertEquals(selectedMessage, goog.dom.selection.getText(textarea));
124
125 selectedMessage = isLegacyIE ? '\r\n' : '\n';
126 goog.dom.selection.setStart(textarea, 13);
127 goog.dom.selection.setEnd(textarea, endOfNewline);
128 assertEquals(selectedMessage, goog.dom.selection.getText(textarea));
129 }
130
131
132 /**
133 * Tests the setCursor routine for both input and textarea.
134 */
135 function testSetCursorInput() {
136 setCursorHelper(input);
137 }
138
139 function testSetCursorTextarea() {
140 setCursorHelper(textarea);
141 }
142
143 function setCursorHelper(field) {
144 select(field);
145 // try to set the cursor beyond the length of the content
146 goog.dom.selection.setStart(field, 5);
147 goog.dom.selection.setEnd(field, 15);
148 assertEquals(0, goog.dom.selection.getStart(field));
149 assertEquals(0, goog.dom.selection.getEnd(field));
150
151 select(field);
152 var message = 'Get Behind Me Satan';
153 goog.dom.selection.setText(field, message);
154 goog.dom.selection.setStart(field, 5);
155 goog.dom.selection.setEnd(field, message.length);
156 assertEquals(5, goog.dom.selection.getStart(field));
157 assertEquals(message.length, goog.dom.selection.getEnd(field));
158
159 // Set the end before the start, and see if getEnd returns the start
160 // position itself.
161 goog.dom.selection.setStart(field, 5);
162 goog.dom.selection.setEnd(field, 3);
163 assertEquals(3, goog.dom.selection.getEnd(field));
164 }
165
166
167 /**
168 * Tests the getText and setText routines acting on selected text in
169 * both input and textarea.
170 */
171 function testGetAndSetSelectedTextInput() {
172 getAndSetSelectedTextHelper(input);
173 }
174
175 function testGetAndSetSelectedTextTextarea() {
176 getAndSetSelectedTextHelper(textarea);
177 }
178
179 function getAndSetSelectedTextHelper(field) {
180 select(field);
181 goog.dom.selection.setText(field, 'Get Behind Me Satan');
182
183 // select 'Behind'
184 goog.dom.selection.setStart(field, 4);
185 goog.dom.selection.setEnd(field, 10);
186 assertEquals('Behind', goog.dom.selection.getText(field));
187
188 goog.dom.selection.setText(field, 'In Front Of');
189 goog.dom.selection.setStart(field, 0);
190 goog.dom.selection.setEnd(field, 100);
191 assertEquals('Get In Front Of Me Satan', goog.dom.selection.getText(field));
192 }
193
194
195 /**
196 * Test setStart on hidden input and hidden textarea.
197 */
198 function testSetCursorOnHiddenInput() {
199 setCursorOnHiddenInputHelper(hiddenInput);
200 }
201
202 function testSetCursorOnHiddenTextarea() {
203 setCursorOnHiddenInputHelper(hiddenTextarea);
204 }
205
206 function setCursorOnHiddenInputHelper(hiddenField) {
207 goog.dom.selection.setStart(hiddenField, 0);
208 assertEquals(0, goog.dom.selection.getStart(hiddenField));
209 }
210
211
212 /**
213 * Test setStart, setEnd, getStart and getEnd in textarea with text
214 * containing line breaks.
215 */
216 function testSetAndGetCursorWithLineBreaks() {
217 select(textarea);
218 var isLegacyIE = goog.userAgent.IE && !goog.userAgent.isVersionOrHigher('9');
219 var newline = isLegacyIE ? '\r\n' : '\n';
220 var message = 'Hello' + newline + 'World';
221 goog.dom.selection.setText(textarea, message);
222
223 // Test setEnd and getEnd, by setting the cursor somewhere after the
224 // \r\n combination.
225 goog.dom.selection.setEnd(textarea, 9);
226 assertEquals(9, goog.dom.selection.getEnd(textarea));
227
228 // Test basic setStart and getStart
229 goog.dom.selection.setStart(textarea, 10);
230 assertEquals(10, goog.dom.selection.getStart(textarea));
231
232 // Test setEnd and getEnd, by setting the cursor exactly after the
233 // \r\n combination in IE or after \n in GECKO.
234 var endOfNewline = isLegacyIE ? 7 : 6;
235 checkSetAndGetTextarea(endOfNewline, endOfNewline);
236
237 // Select a \r\n combination in IE or \n in GECKO and see if
238 // getStart and getEnd work correctly.
239 clearField(textarea);
240 message = 'Hello' + newline + newline + 'World';
241 goog.dom.selection.setText(textarea, message);
242 var startOfNewline = isLegacyIE ? 7 : 6;
243 endOfNewline = isLegacyIE ? 9 : 7;
244 checkSetAndGetTextarea(startOfNewline, endOfNewline);
245
246 // Select 2 \r\n combinations in IE or 2 \ns in GECKO and see if getStart
247 // and getEnd work correctly.
248 checkSetAndGetTextarea(5, endOfNewline);
249
250 // Position cursor b/w 2 \r\n combinations in IE or 2 \ns in GECKO and see
251 // if getStart and getEnd work correctly.
252 clearField(textarea);
253 message = 'Hello' + newline + newline + newline + newline + 'World';
254 goog.dom.selection.setText(textarea, message);
255 var middleOfNewlines = isLegacyIE ? 9 : 7;
256 checkSetAndGetTextarea(middleOfNewlines, middleOfNewlines);
257
258 // Position cursor at end of a textarea which ends with \r\n in IE or \n in
259 // GECKO.
260 if (!goog.userAgent.IE || !goog.userAgent.isVersionOrHigher('11')) {
261 // TODO(johnlenz): investigate why this fails in IE 11.
262 clearField(textarea);
263 message = 'Hello' + newline + newline;
264 goog.dom.selection.setText(textarea, message);
265 var endOfTextarea = message.length;
266 checkSetAndGetTextarea(endOfTextarea, endOfTextarea);
267 }
268
269 // Position cursor at the end of the 2 starting \r\ns in IE or \ns in GECKO
270 // within a textarea.
271 clearField(textarea);
272 message = newline + newline + 'World';
273 goog.dom.selection.setText(textarea, message);
274 var endOfTwoNewlines = isLegacyIE ? 4 : 2;
275 checkSetAndGetTextarea(endOfTwoNewlines, endOfTwoNewlines);
276
277 // Position cursor at the end of the first \r\n in IE or \n in
278 // GECKO within a textarea.
279 endOfOneNewline = isLegacyIE ? 2 : 1;
280 checkSetAndGetTextarea(endOfOneNewline, endOfOneNewline);
281 }
282
283
284 /**
285 * Test to make sure there's no error when getting the range of an unselected
286 * textarea. See bug 1274027.
287 */
288 function testGetStartOnUnfocusedTextarea() {
289 input.value = 'White Blood Cells';
290 input.focus();
291 goog.dom.selection.setCursorPosition(input, 5);
292
293 assertEquals(
294 'getStart on input should return where we put the cursor', 5,
295 goog.dom.selection.getStart(input));
296
297 assertEquals(
298 'getStart on unfocused textarea should succeed without error', 0,
299 goog.dom.selection.getStart(textarea));
300 }
301
302
303 /**
304 * Test to make sure there's no error setting cursor position within a
305 * textarea after a newline. This is problematic on IE because of the
306 * '\r\n' vs '\n' issue.
307 */
308 function testSetCursorPositionTextareaWithNewlines() {
309 textarea.value = 'Hello\nWorld';
310 textarea.focus();
311
312 // Set the selection point between 'W' and 'o'. Position is computed this
313 // way instead of being hard-coded because it's different in IE due to \r\n
314 // vs \n.
315 goog.dom.selection.setCursorPosition(textarea, textarea.value.length - 4);
316
317 var isLegacyIE = goog.userAgent.IE && !goog.userAgent.isVersionOrHigher('9');
318 var linebreak = isLegacyIE ? '\r\n' : '\n';
319 var expectedLeftString = 'Hello' + linebreak + 'W';
320
321 assertEquals(
322 'getStart on input should return after the newline',
323 expectedLeftString.length, goog.dom.selection.getStart(textarea));
324 assertEquals(
325 'getEnd on input should return after the newline',
326 expectedLeftString.length, goog.dom.selection.getEnd(textarea));
327
328 goog.dom.selection.setEnd(textarea, textarea.value.length);
329 assertEquals('orld', goog.dom.selection.getText(textarea));
330 }
331
332
333 /**
334 * Helper function to clear the textfield contents.
335 */
336 function clearField(field) {
337 field.value = '';
338 }
339
340
341 /**
342 * Helper function to set the start and end and assert the getter values.
343 */
344 function checkSetAndGetTextarea(start, end) {
345 goog.dom.selection.setStart(textarea, start);
346 goog.dom.selection.setEnd(textarea, end);
347 assertEquals(start, goog.dom.selection.getStart(textarea));
348 assertEquals(end, goog.dom.selection.getEnd(textarea));
349 }
350
351
352 /**
353 * Helper function to focus and select a field. In IE8, selected
354 * fields need focus.
355 */
356 function select(field) {
357 field.focus();
358 field.select();
359 }