aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/dom/selection_test.html
blob: 8cfc6240e076cdc24de5958f22155320062c0c85 (plain)
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
<!DOCTYPE html>
<html>
<!--
Copyright 2007 The Closure Library Authors. All Rights Reserved.

Use of this source code is governed by the Apache License, Version 2.0.
See the COPYING file for details.
-->
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Closure Unit Tests - goog.dom.selection</title>
<script src="../base.js"></script>
<script>
  goog.require('goog.dom');
  goog.require('goog.dom.selection');
  goog.require('goog.testing.jsunit');
  goog.require('goog.userAgent');
</script>
</head>
<body>

<script>

  var input;
  var hiddenInput;
  var textarea;
  var hiddenTextarea;

  function setUp() {
    input = goog.dom.createDom('input', {type: 'text'});
    textarea = goog.dom.createDom('textarea');
    hiddenInput = goog.dom.createDom(
        'input', {type: 'text', style: 'display: none'});
    hiddenTextarea = goog.dom.createDom(
        'textarea', {style: 'display: none'});

    document.body.appendChild(input);
    document.body.appendChild(textarea);
    document.body.appendChild(hiddenInput);
    document.body.appendChild(hiddenTextarea);
  }

  function tearDown() {
    goog.dom.removeNode(input);
    goog.dom.removeNode(textarea);
    goog.dom.removeNode(hiddenInput);
    goog.dom.removeNode(hiddenTextarea);
  }

  /**
   * Tests getStart routine in both input and textarea.
   */
  function testGetStartInput() {
    getStartHelper(input, hiddenInput);
  }

  function testGetStartTextarea() {
    getStartHelper(textarea, hiddenTextarea);
  }

  function getStartHelper(field, hiddenField) {
    assertEquals(0, goog.dom.selection.getStart(field));
    assertEquals(0, goog.dom.selection.getStart(hiddenField));

    field.focus();
    assertEquals(0, goog.dom.selection.getStart(field));
  }

  /**
   * Tests the setText routine for both input and textarea
   * with a single line of text.
   */
  function testSetTextInput() {
    setTextHelper(input);
  }

  function testSetTextTextarea() {
    setTextHelper(textarea);
  }

  function setTextHelper(field) {
    // Test one line string only
    select(field);
    assertEquals('', goog.dom.selection.getText(field));

    goog.dom.selection.setText(field, 'Get Behind Me Satan');
    assertEquals('Get Behind Me Satan', goog.dom.selection.getText(field));
  }

  /**
   * Tests the setText routine for textarea with multiple lines of text.
   */
  function testSetTextMultipleLines() {
    select(textarea);
    assertEquals('', goog.dom.selection.getText(textarea));
    var message = goog.userAgent.IE ?
                  'Get Behind Me\r\nSatan' :
                  'Get Behind Me\nSatan';
    goog.dom.selection.setText(textarea, message);
    assertEquals(message, goog.dom.selection.getText(textarea));

    // Select the text upto the point just after the \r\n combination
    // or \n in GECKO.
    var endOfNewline = goog.userAgent.IE ? 15 : 14;
    var selectedMessage = message.substring(0, endOfNewline);
    goog.dom.selection.setStart(textarea, 0);
    goog.dom.selection.setEnd(textarea, endOfNewline);
    assertEquals(selectedMessage, goog.dom.selection.getText(textarea));

    selectedMessage = goog.userAgent.IE ? '\r\n' : '\n';
    goog.dom.selection.setStart(textarea, 13);
    goog.dom.selection.setEnd(textarea, endOfNewline);
    assertEquals(selectedMessage, goog.dom.selection.getText(textarea));
  }

  /**
   * Tests the setCursor routine for both input and textarea.
   */
  function testSetCursorInput() {
    setCursorHelper(input);
  }

  function testSetCursorTextarea() {
    setCursorHelper(textarea);
  }

  function setCursorHelper(field) {
    select(field);
    // try to set the cursor beyond the length of the content
    goog.dom.selection.setStart(field, 5);
    goog.dom.selection.setEnd(field, 15);
    assertEquals(0, goog.dom.selection.getStart(field));
    assertEquals(0, goog.dom.selection.getEnd(field));

    select(field);
    var message = 'Get Behind Me Satan';
    goog.dom.selection.setText(field, message);
    goog.dom.selection.setStart(field, 5);
    goog.dom.selection.setEnd(field, message.length);
    assertEquals(5, goog.dom.selection.getStart(field));
    assertEquals(message.length, goog.dom.selection.getEnd(field));

    // Set the end before the start, and see if getEnd returns the start
    // position itself.
    goog.dom.selection.setStart(field, 5);
    goog.dom.selection.setEnd(field, 3);
    assertEquals(3, goog.dom.selection.getEnd(field));
  }

  /**
   * Tests the getText and setText routines acting on selected text in
   * both input and textarea.
   */
  function testGetAndSetSelectedTextInput() {
    getAndSetSelectedTextHelper(input);
  }

  function testGetAndSetSelectedTextTextarea() {
    getAndSetSelectedTextHelper(textarea);
  }

  function getAndSetSelectedTextHelper(field) {
    select(field);
    goog.dom.selection.setText(field, 'Get Behind Me Satan');

    // select 'Behind'
    goog.dom.selection.setStart(field, 4);
    goog.dom.selection.setEnd(field, 10);
    assertEquals('Behind', goog.dom.selection.getText(field));

    goog.dom.selection.setText(field, 'In Front Of');
    goog.dom.selection.setStart(field, 0);
    goog.dom.selection.setEnd(field, 100);
    assertEquals('Get In Front Of Me Satan', goog.dom.selection.getText(field));
  }

  /**
   * Test setStart on hidden input and hidden textarea.
   */
  function testSetCursorOnHiddenInput() {
    setCursorOnHiddenInputHelper(hiddenInput);
  }

  function testSetCursorOnHiddenTextarea() {
    setCursorOnHiddenInputHelper(hiddenTextarea);
  }

  function setCursorOnHiddenInputHelper(hiddenField) {
    goog.dom.selection.setStart(hiddenField, 0);
    assertEquals(0, goog.dom.selection.getStart(hiddenField));
  }

  /**
   * Test setStart, setEnd, getStart and getEnd in textarea with text
   * containing line breaks.
   */
  function testSetAndGetCursorWithLineBreaks() {
    select(textarea);
    var newline = goog.userAgent.IE ? '\r\n' : '\n';
    var message = 'Hello' + newline + 'World';
    goog.dom.selection.setText(textarea, message);

    // Test setEnd and getEnd, by setting the cursor somewhere after the
    // \r\n combination.
    goog.dom.selection.setEnd(textarea, 9);
    assertEquals(9, goog.dom.selection.getEnd(textarea));

    // Test basic setStart and getStart
    goog.dom.selection.setStart(textarea, 10);
    assertEquals(10, goog.dom.selection.getStart(textarea));

    // Test setEnd and getEnd, by setting the cursor exactly after the
    // \r\n combination in IE or after \n in GECKO.
    var endOfNewline = goog.userAgent.IE ? 7 : 6;
    checkSetAndGetTextarea(endOfNewline, endOfNewline);

    // Select a \r\n combination in IE or \n in GECKO and see if
    // getStart and getEnd work correctly.
    clearField(textarea);
    message = 'Hello' + newline + newline + 'World';
    goog.dom.selection.setText(textarea, message);
    var startOfNewline = goog.userAgent.IE ? 7 : 6;
    endOfNewline = goog.userAgent.IE ? 9 : 7;
    checkSetAndGetTextarea(startOfNewline, endOfNewline);

    // Select 2 \r\n combinations in IE or 2 \ns in GECKO and see if getStart
    // and getEnd work correctly.
    checkSetAndGetTextarea(5, endOfNewline);

    // Position cursor b/w 2 \r\n combinations in IE or 2 \ns in GECKO and see
    // if getStart and getEnd work correctly.
    clearField(textarea);
    message = 'Hello' + newline + newline + newline + newline + 'World';
    goog.dom.selection.setText(textarea, message);
    var middleOfNewlines = goog.userAgent.IE ? 9 : 7;
    checkSetAndGetTextarea(middleOfNewlines, middleOfNewlines);

    // Position cursor at end of a textarea which ends with \r\n in IE or \n in
    // GECKO.
    clearField(textarea);
    message = 'Hello' + newline + newline;
    goog.dom.selection.setText(textarea, message);
    var endOfTextarea = message.length;
    checkSetAndGetTextarea(endOfTextarea, endOfTextarea);

    // Position cursor at the end of the 2 starting \r\ns in IE or \ns in GECKO
    // within a textarea.
    clearField(textarea);
    message = newline + newline + 'World';
    goog.dom.selection.setText(textarea, message);
    var endOfTwoNewlines = goog.userAgent.IE ? 4 : 2;
    checkSetAndGetTextarea(endOfTwoNewlines, endOfTwoNewlines);

    // Position cursor at the end of the first \r\n in IE or \n in
    // GECKO within a textarea.
    endOfOneNewline = goog.userAgent.IE ? 2 : 1;
    checkSetAndGetTextarea(endOfOneNewline, endOfOneNewline);
  }

  /**
   * Test to make sure there's no error when getting the range of an unselected
   * textarea. See bug 1274027.
   */
  function testGetStartOnUnfocusedTextarea() {
    input.value = 'White Blood Cells';
    input.focus();
    goog.dom.selection.setCursorPosition(input, 5);

    assertEquals('getStart on input should return where we put the cursor',
        5, goog.dom.selection.getStart(input));

    assertEquals('getStart on unfocused textarea should succeed without error',
        0, goog.dom.selection.getStart(textarea));
  }

  /**
   * Test to make sure there's no error setting cursor position within a
   * textarea after a newline. This is problematic on IE because of the
   * '\r\n' vs '\n' issue.
   */
  function testSetCursorPositionTextareaWithNewlines() {
    textarea.value = 'Hello\nWorld';
    textarea.focus();

    // Set the selection point between 'W' and 'o'. Position is computed this
    // way instead of being hard-coded because it's different in IE due to \r\n
    // vs \n.
    goog.dom.selection.setCursorPosition(textarea, textarea.value.length - 4);

    var linebreak = goog.userAgent.IE ? '\r\n' : '\n';
    var expectedLeftString = 'Hello' + linebreak + 'W';

    assertEquals('getStart on input should return after the newline',
        expectedLeftString.length, goog.dom.selection.getStart(textarea));
    assertEquals('getEnd on input should return after the newline',
        expectedLeftString.length, goog.dom.selection.getEnd(textarea));

    goog.dom.selection.setEnd(textarea, textarea.value.length);
    assertEquals('orld', goog.dom.selection.getText(textarea));
  }

  /**
   * Helper function to clear the textfield contents.
   */
  function clearField(field) {
    field.value = '';
  }

  /**
   * Helper function to set the start and end and assert the getter values.
   */
  function checkSetAndGetTextarea(start, end) {
    goog.dom.selection.setStart(textarea, start);
    goog.dom.selection.setEnd(textarea, end);
    assertEquals(start, goog.dom.selection.getStart(textarea));
    assertEquals(end, goog.dom.selection.getEnd(textarea));
  }

  /**
   * Helper function to focus and select a field. In IE8, selected
   * fields need focus.
   */
  function select(field) {
    field.focus();
    field.select();
  }
</script>
</body>
</html>