aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/editor/plugins/tagonenterhandler_test.html
blob: 48f20e4904f156b0bcdb4de15844480098cb994b (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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
<!DOCTYPE html>
<!--

  Tests for goog.editor.plugins.TagOnEnterHandler

  @author marcosalmeida@google.com
--><html>
<!--
Copyright 2008 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>goog.editor.plugins.TagOnEnterHandler jsunit tests</title>
<script src="../../base.js"></script>
<script>
  goog.require('goog.dom.TagName');
  goog.require('goog.editor.Field');
  goog.require('goog.editor.plugins.TagOnEnterHandler');
  goog.require('goog.testing.events');
  goog.require('goog.testing.jsunit');
  goog.require('goog.testing.editor.TestHelper');
</script>
</head>
<body>

<div id='root'>
  <div id='field1' class='tr-field'>
  </div>
</div>

<script>

var savedHtml;

var editor;
var field1;

function setUp() {
  field1 = makeField('field1');
  field1.makeEditable();
}


/**
 * Tests that deleting a BR that comes right before a block element works.
 * @bug 1471096
 */
function testDeleteBrBeforeBlock() {
  // This test only works on Gecko, because it's testing for manual deletion of
  // BR tags, which is done only for Gecko. For other browsers we fall through
  // and let the browser do the delete, which can only be tested with a robot
  // test (see javascript/apps/editor/tests/delete_br_robot.html).
  if (goog.userAgent.GECKO) {

    field1.setHtml(false, 'one<br><br><div>two</div>');
    var helper = new goog.testing.editor.TestHelper(field1.getElement());
    helper.select(field1.getElement(), 2); // Between the two BR's.
    goog.testing.events.fireKeySequence(field1.getElement(),
                                        goog.events.KeyCodes.DELETE);
    assertEquals('Should have deleted exactly one <br>',
                 'one<br><div>two</div>',
                 field1.getElement().innerHTML);

  } // End if GECKO
}


/**
 * Tests that deleting a BR is working normally (that the workaround for the
 * bug is not causing double deletes).
 * @bug 1471096
 */
function testDeleteBrNormal() {
  // This test only works on Gecko, because it's testing for manual deletion of
  // BR tags, which is done only for Gecko. For other browsers we fall through
  // and let the browser do the delete, which can only be tested with a robot
  // test (see javascript/apps/editor/tests/delete_br_robot.html).
  if (goog.userAgent.GECKO) {

    field1.setHtml(false, 'one<br><br><br>two');
    var helper = new goog.testing.editor.TestHelper(field1.getElement());
    helper.select(field1.getElement(), 2); // Between the first and second BR's.
    field1.getElement().focus();
    goog.testing.events.fireKeySequence(field1.getElement(),
                                        goog.events.KeyCodes.DELETE);
    assertEquals('Should have deleted exactly one <br>',
                 'one<br><br>two',
                 field1.getElement().innerHTML);

  } // End if GECKO
}


/**
 * Regression test for http://b/1991234 . Tests that when you hit enter and it
 * creates a blank line with whitespace and a BR, the cursor is placed in the
 * whitespace text node instead of the BR, otherwise continuing to type will
 * create adjacent text nodes, which causes browsers to mess up some
 * execcommands. Fix is in a Gecko-only codepath, thus test runs only for Gecko.
 * A full test for the entire sequence that reproed the bug is in
 * javascript/apps/editor/tests/ponenter_robot.html .
 */
function testEnterCreatesBlankLine() {
  if (goog.userAgent.GECKO) {
    field1.setHtml(false, '<p>one <br></p>');
    var helper = new goog.testing.editor.TestHelper(field1.getElement());
    // Place caret after 'one' but keeping a space and a BR as FF does.
    helper.select('one ', 3);
    field1.getElement().focus();
    goog.testing.events.fireKeySequence(field1.getElement(),
                                        goog.events.KeyCodes.ENTER);
    var range = field1.getRange();
    assertFalse('Selection should not be in BR tag',
                range.getStartNode().nodeType == goog.dom.NodeType.ELEMENT &&
                range.getStartNode().tagName == goog.dom.TagName.BR);
    assertEquals('Selection should be in text node to avoid creating adjacent' +
                 ' text nodes',
        goog.dom.NodeType.TEXT, range.getStartNode().nodeType);
    var rangeStartNode =
        goog.dom.Range.createFromNodeContents(range.getStartNode());
    assertHTMLEquals('The value of selected text node should be replaced with' +
                 '&nbsp;',
        '&nbsp;', rangeStartNode.getHtmlFragment());
  }
}


/**
 * Regression test for http://b/3051179 . Tests that when you hit enter and it
 * creates a blank line with a BR and the cursor is placed in P.
 * Splitting DOM causes to make an empty text node. Then if the cursor is placed
 * at the text node the cursor is shown at wrong location.
 * Therefore this test checks that the cursor is not placed at an empty node.
 * Fix is in a Gecko-only codepath, thus test runs only for Gecko.
 */
function testEnterNormalizeNodes() {
  if (goog.userAgent.GECKO) {
    field1.setHtml(false, '<p>one<br></p>');
    var helper = new goog.testing.editor.TestHelper(field1.getElement());
    // Place caret after 'one' but keeping a BR as FF does.
    helper.select('one', 3);
    field1.getElement().focus();
    goog.testing.events.fireKeySequence(field1.getElement(),
                                        goog.events.KeyCodes.ENTER);
    var range = field1.getRange();
    assertTrue('Selection should be in P tag',
                range.getStartNode().nodeType == goog.dom.NodeType.ELEMENT &&
                range.getStartNode().tagName == goog.dom.TagName.P);
    assertTrue('Selection should be at the head and collapsed',
                range.getStartOffset() == 0 && range.isCollapsed());
  }
}


/**
 * Verifies
 * goog.editor.plugins.TagOnEnterHandler.prototype.handleRegularEnterGecko_
 * when we explicitly split anchor elements. This test runs only for Gecko
 * since this is a Gecko-only codepath.
 */
function testEnterAtBeginningOfLink() {
  if (goog.userAgent.GECKO) {
    field1.setHtml(false, '<a href="/">b<br></a>');
    var helper = new goog.testing.editor.TestHelper(field1.getElement());
    field1.focusAndPlaceCursorAtStart();
    goog.testing.events.fireKeySequence(field1.getElement(),
                                        goog.events.KeyCodes.ENTER);
    helper.assertHtmlMatches(
        '<p>&nbsp;</p><p><a href="/">b<br></a></p>');
  }
}


/**
 * Verifies correct handling of pressing enter in an empty list item.
 */
function testEnterInEmptyListItemInEmptyList() {
  if (goog.userAgent.GECKO) {
    field1.setHtml(false, '<ul><li>&nbsp;</li></ul>');
    var helper = new goog.testing.editor.TestHelper(field1.getElement());
    var li = field1.getElement().getElementsByTagName(goog.dom.TagName.LI)[0];
    helper.select(li.firstChild, 0);
    goog.testing.events.fireKeySequence(field1.getElement(),
                                        goog.events.KeyCodes.ENTER);
    helper.assertHtmlMatches('<p>&nbsp;</p>');
  }
}


function testEnterInEmptyListItemAtBeginningOfList() {
  if (goog.userAgent.GECKO) {
    field1.setHtml(false,
        '<ul style="font-weight: bold">' +
            '<li>&nbsp;</li>' +
            '<li>1</li>' +
            '<li>2</li>' +
        '</ul>');
    var helper = new goog.testing.editor.TestHelper(field1.getElement());
    var li = field1.getElement().getElementsByTagName(goog.dom.TagName.LI)[0];
    helper.select(li.firstChild, 0);
    goog.testing.events.fireKeySequence(field1.getElement(),
                                        goog.events.KeyCodes.ENTER);
    helper.assertHtmlMatches(
        '<p>&nbsp;</p><ul style="font-weight: bold"><li>1</li><li>2</li></ul>');
  }
}


function testEnterInEmptyListItemAtEndOfList() {
  if (goog.userAgent.GECKO) {
    field1.setHtml(false,
        '<ul style="font-weight: bold">' +
            '<li>1</li>' +
            '<li>2</li>' +
            '<li>&nbsp;</li>' +
        '</ul>');
    var helper = new goog.testing.editor.TestHelper(field1.getElement());
    var li = field1.getElement().getElementsByTagName(goog.dom.TagName.LI)[2];
    helper.select(li.firstChild, 0);
    goog.testing.events.fireKeySequence(field1.getElement(),
                                        goog.events.KeyCodes.ENTER);
    helper.assertHtmlMatches(
        '<ul style="font-weight: bold"><li>1</li><li>2</li></ul><p>&nbsp;</p>');
  }
}


function testEnterInEmptyListItemInMiddleOfList() {
  if (goog.userAgent.GECKO) {
    field1.setHtml(false,
        '<ul style="font-weight: bold">' +
            '<li>1</li>' +
            '<li>&nbsp;</li>' +
            '<li>2</li>' +
        '</ul>');
    var helper = new goog.testing.editor.TestHelper(field1.getElement());
    var li = field1.getElement().getElementsByTagName(goog.dom.TagName.LI)[1];
    helper.select(li.firstChild, 0);
    goog.testing.events.fireKeySequence(field1.getElement(),
                                        goog.events.KeyCodes.ENTER);
    helper.assertHtmlMatches(
        '<ul style="font-weight: bold"><li>1</li></ul>' +
        '<p>&nbsp;</p>' +
        '<ul style="font-weight: bold"><li>2</li></ul>');
  }
}


function testEnterInEmptyListItemInSublist() {
  if (goog.userAgent.GECKO) {
    field1.setHtml(false,
        '<ul>' +
          '<li>A</li>' +
          '<ul style="font-weight: bold">' +
              '<li>1</li>' +
              '<li>&nbsp;</li>' +
              '<li>2</li>' +
          '</ul>' +
          '<li>B</li>' +
        '</ul>');
    var helper = new goog.testing.editor.TestHelper(field1.getElement());
    var li = field1.getElement().getElementsByTagName(goog.dom.TagName.LI)[2];
    helper.select(li.firstChild, 0);
    goog.testing.events.fireKeySequence(field1.getElement(),
                                        goog.events.KeyCodes.ENTER);
    helper.assertHtmlMatches(
        '<ul>' +
        '<li>A</li>' +
        '<ul style="font-weight: bold"><li>1</li></ul>' +
        '<li>&nbsp;</li>' +
        '<ul style="font-weight: bold"><li>2</li></ul>' +
        '<li>B</li>' +
        '</ul>');
  }
}


function testEnterInEmptyListItemAtBeginningOfSublist() {
  if (goog.userAgent.GECKO) {
    field1.setHtml(false,
        '<ul>' +
          '<li>A</li>' +
          '<ul style="font-weight: bold">' +
              '<li>&nbsp;</li>' +
              '<li>1</li>' +
              '<li>2</li>' +
          '</ul>' +
          '<li>B</li>' +
        '</ul>');
    var helper = new goog.testing.editor.TestHelper(field1.getElement());
    var li = field1.getElement().getElementsByTagName(goog.dom.TagName.LI)[1];
    helper.select(li.firstChild, 0);
    goog.testing.events.fireKeySequence(field1.getElement(),
                                        goog.events.KeyCodes.ENTER);
    helper.assertHtmlMatches(
        '<ul>' +
        '<li>A</li>' +
        '<li>&nbsp;</li>' +
        '<ul style="font-weight: bold"><li>1</li><li>2</li></ul>' +
        '<li>B</li>' +
        '</ul>');
  }
}


function testEnterInEmptyListItemAtEndOfSublist() {
  if (goog.userAgent.GECKO) {
    field1.setHtml(false,
        '<ul>' +
          '<li>A</li>' +
          '<ul style="font-weight: bold">' +
              '<li>1</li>' +
              '<li>2</li>' +
              '<li>&nbsp;</li>' +
          '</ul>' +
          '<li>B</li>' +
        '</ul>');
    var helper = new goog.testing.editor.TestHelper(field1.getElement());
    var li = field1.getElement().getElementsByTagName(goog.dom.TagName.LI)[3];
    helper.select(li.firstChild, 0);
    goog.testing.events.fireKeySequence(field1.getElement(),
                                        goog.events.KeyCodes.ENTER);
    helper.assertHtmlMatches(
        '<ul>' +
        '<li>A</li>' +
        '<ul style="font-weight: bold"><li>1</li><li>2</li></ul>' +
        '<li>&nbsp;</li>' +
        '<li>B</li>' +
        '</ul>');
  }
}


function testPrepareContentForPOnEnter() {
  assertPreparedContents('hi', 'hi');
  assertPreparedContents(
      goog.editor.BrowserFeature.COLLAPSES_EMPTY_NODES ? '<p>&nbsp;</p>' : '',
      '   ');
}


function testPrepareContentForDivOnEnter() {
  assertPreparedContents('hi', 'hi', goog.dom.TagName.DIV);
  assertPreparedContents(
      goog.editor.BrowserFeature.COLLAPSES_EMPTY_NODES ? '<div><br></div>' : '',
      '   ',
      goog.dom.TagName.DIV);
}


/**
 * Assert that the prepared contents matches the expected.
 */
function assertPreparedContents(expected, original, opt_tag) {
  var field = makeField('field1', opt_tag);
  field.makeEditable();
  assertEquals(expected,
      field.reduceOp_(
          goog.editor.Plugin.Op.PREPARE_CONTENTS_HTML, original));
}


/**
 * Selects the node at the given id, and simulates an ENTER keypress.
 * @param {googe.editor.Field} field The field with the node.
 * @param {string} id A DOM id.
 * @return {boolean} Whether preventDefault was called on the event.
 */
function selectNodeAndHitEnter(field, id) {
  var cursor = field.getEditableDomHelper().getElement(id);
  goog.dom.Range.createFromNodeContents(cursor).select();
  return goog.testing.events.fireKeySequence(
      cursor, goog.events.KeyCodes.ENTER);
}


/**
 * Creates a field with only the enter handler plugged in, for testing.
 * @param {string} id A DOM id.
 * @param {boolean=} opt_tag The block tag to use.  Defaults to P.
 * @return {goog.editor.Field} A field.
 */
function makeField(id, opt_tag) {
  var field = new goog.editor.Field(id);
  field.registerPlugin(
      new goog.editor.plugins.TagOnEnterHandler(opt_tag || goog.dom.TagName.P));
  return field;
}


/**
 * Runs a test for splitting the dom.
 * @param offset Index into the text node to split.
 * @param firstHalfString What the html of the first half of the DOM should be.
 * @param secondHalfString What the html of the 2nd half of the DOM should be.
 * @param isAppend True if the second half should be appended to the DOM.
 * @param opt_goToRoot True if the root argument for splitDom should be
 *                     excluded.
 */
function helpTestSplit_(offset, firstHalfString, secondHalfString, isAppend,
    opt_goToBody) {
  var node = document.createElement('div');
  node.innerHTML = '<b>begin bold<i>italic</i>end bold</b>';
  document.body.appendChild(node);

  var italic = node.getElementsByTagName('i')[0].firstChild;

  var splitFn = isAppend ?
      goog.editor.plugins.TagOnEnterHandler.splitDomAndAppend_ :
      goog.editor.plugins.TagOnEnterHandler.splitDom_;
  var secondHalf = splitFn(italic, offset, opt_goToBody ? undefined : node);

  if (opt_goToBody) {
    secondHalfString = '<div>' + secondHalfString + '</div>';
  }

  assertEquals('original node should have first half of the html',
               firstHalfString,
               node.innerHTML.toLowerCase().
                  replace(goog.string.Unicode.NBSP, '&nbsp;'));
  assertEquals('new node should have second half of the html',
               secondHalfString,
               secondHalf.innerHTML.toLowerCase().
                   replace(goog.string.Unicode.NBSP, '&nbsp;'));

  if (isAppend) {
    assertTrue('second half of dom should be the original node\'s next' +
               'sibling', node.nextSibling == secondHalf);
    goog.dom.removeNode(secondHalf);
  }

  goog.dom.removeNode(node);
}


/**
 * Runs different cases of splitting the DOM.
 * @param testFn Function that takes an offset, firstHalfString and
 *               secondHalfString as parameters.
 */
function splitDomCases_(testFn) {
  testFn(3, '<b>begin bold<i>ita</i></b>', '<b><i>lic</i>end bold</b>');
  testFn(0, '<b>begin bold<i>&nbsp;</i></b>', '<b><i>italic</i>end bold</b>');
  testFn(6, '<b>begin bold<i>italic</i></b>', '<b><i>&nbsp;</i>end bold</b>');
}


function testSplitDom() {
  splitDomCases_(function(offset, firstHalfString, secondHalfString) {
    helpTestSplit_(offset, firstHalfString, secondHalfString, false, true);
    helpTestSplit_(offset, firstHalfString, secondHalfString, false, false);
  });
}


function testSplitDomAndAppend() {
  splitDomCases_(function(offset, firstHalfString, secondHalfString) {
    helpTestSplit_(offset, firstHalfString, secondHalfString, true, false);
  });
}


function testSplitDomAtElement() {
  var node = document.createElement('div');
  node.innerHTML = '<div>abc<br>def</div>';
  document.body.appendChild(node);

  goog.editor.plugins.TagOnEnterHandler.splitDomAndAppend_(node.firstChild, 1,
      node.firstChild);

  goog.testing.dom.assertHtmlContentsMatch('<div>abc</div><div><br>def</div>',
      node);

  goog.dom.removeNode(node);
}


function testSplitDomAtElementStart() {
  var node = document.createElement('div');
  node.innerHTML = '<div>abc<br>def</div>';
  document.body.appendChild(node);

  goog.editor.plugins.TagOnEnterHandler.splitDomAndAppend_(node.firstChild, 0,
      node.firstChild);

  goog.testing.dom.assertHtmlContentsMatch('<div></div><div>abc<br>def</div>',
      node);

  goog.dom.removeNode(node);
}


function testSplitDomAtChildlessElement() {
  var node = document.createElement('div');
  node.innerHTML = '<div>abc<br>def</div>';
  document.body.appendChild(node);

  var br = node.getElementsByTagName(goog.dom.TagName.BR)[0];
  goog.editor.plugins.TagOnEnterHandler.splitDomAndAppend_(
      br, 0, node.firstChild);

  goog.testing.dom.assertHtmlContentsMatch('<div>abc</div><div><br>def</div>',
      node);

  goog.dom.removeNode(node);
}

function testReplaceWhiteSpaceWithNbsp() {
  var node = document.createElement('div');
  var textNode = document.createTextNode('');
  node.appendChild(textNode);

  textNode.nodeValue = ' test ';
  goog.editor.plugins.TagOnEnterHandler.replaceWhiteSpaceWithNbsp_(
      node.firstChild, true, false);
  assertHTMLEquals('&nbsp;test ', node.innerHTML);

  textNode.nodeValue = '  test ';
  goog.editor.plugins.TagOnEnterHandler.replaceWhiteSpaceWithNbsp_(
      node.firstChild, true, false);
  assertHTMLEquals('&nbsp;test ', node.innerHTML);

  textNode.nodeValue = ' test ';
  goog.editor.plugins.TagOnEnterHandler.replaceWhiteSpaceWithNbsp_(
      node.firstChild, false, false);
  assertHTMLEquals(' test&nbsp;', node.innerHTML);

  textNode.nodeValue = ' test  ';
  goog.editor.plugins.TagOnEnterHandler.replaceWhiteSpaceWithNbsp_(
      node.firstChild, false, false);
  assertHTMLEquals(' test&nbsp;', node.innerHTML);

  textNode.nodeValue = '';
  goog.editor.plugins.TagOnEnterHandler.replaceWhiteSpaceWithNbsp_(
      node.firstChild, false, false);
  assertHTMLEquals('&nbsp;', node.innerHTML);

  textNode.nodeValue = '';
  goog.editor.plugins.TagOnEnterHandler.replaceWhiteSpaceWithNbsp_(
      node.firstChild, false, true);
  assertHTMLEquals('', node.innerHTML);
}

</script>
</body>
</html>