Debian Codegraph @

closure-library/closure/goog/editor/plugins/removeformatting_test.js

raw
1 // Copyright 2008 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.editor.plugins.RemoveFormattingTest');
16 goog.setTestOnly('goog.editor.plugins.RemoveFormattingTest');
17
18 goog.require('goog.dom');
19 goog.require('goog.dom.Range');
20 goog.require('goog.dom.TagName');
21 goog.require('goog.editor.BrowserFeature');
22 goog.require('goog.editor.plugins.RemoveFormatting');
23 goog.require('goog.string');
24 goog.require('goog.testing.ExpectedFailures');
25 goog.require('goog.testing.dom');
26 goog.require('goog.testing.editor.FieldMock');
27 goog.require('goog.testing.editor.TestHelper');
28 goog.require('goog.testing.jsunit');
29 goog.require('goog.userAgent');
30 goog.require('goog.userAgent.product');
31
32 function shouldRunTests() {
33 // This test has not yet been updated to run on IE8 and up. See b/2997691.
34 return !goog.userAgent.IE || !goog.userAgent.isVersionOrHigher(8);
35 }
36
37 var SAVED_HTML;
38 var FIELDMOCK;
39 var FORMATTER;
40 var testHelper;
41 var WEBKIT_BEFORE_CHROME_8;
42 var WEBKIT_AFTER_CHROME_16;
43 var WEBKIT_AFTER_CHROME_21;
44 var insertImageBoldGarbage = '';
45 var insertImageFontGarbage = '';
46 var controlHtml;
47 var controlCleanHtml;
48 var expectedFailures;
49
50 function setUpPage() {
51 WEBKIT_BEFORE_CHROME_8 =
52 goog.userAgent.WEBKIT && !goog.userAgent.isVersionOrHigher('534.10');
53
54 WEBKIT_AFTER_CHROME_16 =
55 goog.userAgent.WEBKIT && goog.userAgent.isVersionOrHigher('535.7');
56
57 WEBKIT_AFTER_CHROME_21 =
58 goog.userAgent.WEBKIT && goog.userAgent.isVersionOrHigher('537.1');
59 // On Chrome 16, execCommand('insertImage') inserts a garbage BR
60 // after the image that we insert. We use this command to paste HTML
61 // in-place, because it has better paragraph-preserving semantics.
62 //
63 // TODO(nicksantos): Figure out if there are better chrome APIs that we
64 // should be using, or if insertImage should just be fixed.
65 if (WEBKIT_AFTER_CHROME_21) {
66 insertImageBoldGarbage = '<br>';
67 insertImageFontGarbage = '<br>';
68 } else if (WEBKIT_AFTER_CHROME_16) {
69 insertImageBoldGarbage = '<b><br/></b>';
70 insertImageFontGarbage = '<font size="1"><br/></font>';
71 } else if (goog.userAgent.EDGE) {
72 if (goog.userAgent.product.isVersion(14)) {
73 insertImageFontGarbage = '<fontsize="-1"></fontsize="-1">';
74 } else {
75 insertImageFontGarbage =
76 '<fontsize="-1"><font class="p" size="-1"></font></fontsize="-1">';
77 }
78 }
79 // Extra html to add to test html to make sure removeformatting is actually
80 // getting called when you're testing if it leaves certain styles alone
81 // (instead of not even running at all due to some other bug). However, adding
82 // this extra text into the node to be selected screws up IE.
83 // (e.g. <a><img></a><b>t</b> --> <a></a><a><img></a>t )
84 // TODO(user): Remove this special casing once http://b/3131117 is
85 // fixed.
86 controlHtml = goog.userAgent.IE ? '' : '<u>control</u>';
87 controlCleanHtml = goog.userAgent.IE ? '' : 'control';
88 if (goog.userAgent.EDGE) {
89 controlCleanHtml = 'control<u></u>';
90 }
91 expectedFailures = new goog.testing.ExpectedFailures();
92 }
93
94 function setUp() {
95 testHelper =
96 new goog.testing.editor.TestHelper(document.getElementById('html'));
97 testHelper.setUpEditableElement();
98
99 FIELDMOCK = new goog.testing.editor.FieldMock();
100 FIELDMOCK.getElement();
101 FIELDMOCK.$anyTimes();
102 FIELDMOCK.$returns(document.getElementById('html'));
103
104 FORMATTER = new goog.editor.plugins.RemoveFormatting();
105 FORMATTER.fieldObject = FIELDMOCK;
106
107 FIELDMOCK.$replay();
108 }
109
110 function tearDown() {
111 expectedFailures.handleTearDown();
112 testHelper.tearDownEditableElement();
113 }
114
115 function setUpTableTests() {
116 var div = document.getElementById('html');
117 div.innerHTML = '<table><tr> <th> head1</th><th id= "outerTh">' +
118 '<span id="emptyTh">head2</span></th> </tr><tr> <td> one </td> <td>' +
119 'two </td> </tr><tr><td> three</td><td id="outerTd"> ' +
120 '<span id="emptyTd"><strong>four</strong></span></td></tr>' +
121 '<tr id="outerTr"><td><span id="emptyTr"> five </span></td></tr>' +
122 '<tr id="outerTr2"><td id="cell1"><b>seven</b></td><td id="cell2">' +
123 '<u>eight</u><span id="cellspan2"> foo</span></td></tr></table>';
124 }
125
126 function testTableTagsAreNotRemoved() {
127 setUpTableTests();
128 var span;
129
130 // TD
131 span = document.getElementById('emptyTd');
132 goog.dom.Range.createFromNodeContents(span).select();
133 FORMATTER.removeFormatting_();
134
135 var elem = document.getElementById('outerTd');
136 assertTrue('TD should not be removed', !!elem);
137 if (!goog.userAgent.WEBKIT && !goog.userAgent.EDGE) {
138 // webkit seems to have an Apple-style-span
139 assertEquals(
140 'TD should be clean', 'four', goog.string.trim(elem.innerHTML));
141 }
142
143 // TR
144 span = document.getElementById('outerTr');
145 goog.dom.Range.createFromNodeContents(span).select();
146 FORMATTER.removeFormatting_();
147
148 elem = document.getElementById('outerTr');
149 assertTrue('TR should not be removed', !!elem);
150
151 // TH
152 span = document.getElementById('emptyTh');
153 goog.dom.Range.createFromNodeContents(span).select();
154 FORMATTER.removeFormatting_();
155
156 elem = document.getElementById('outerTh');
157 assertTrue('TH should not be removed', !!elem);
158 if (!goog.userAgent.WEBKIT && !goog.userAgent.EDGE) {
159 // webkit seems to have an Apple-style-span
160 assertEquals('TH should be clean', 'head2', elem.innerHTML);
161 }
162 }
163
164
165 /**
166 * We select two cells from the table and then make sure that there is no
167 * data loss and basic formatting is removed from each cell.
168 */
169 function testTableDataIsNotRemoved() {
170 setUpTableTests();
171 if (goog.userAgent.IE) {
172 // IE returns an "unspecified error" which seems to be beyond
173 // ExpectedFailures' ability to catch.
174 return;
175 }
176
177 expectedFailures.expectFailureFor(
178 goog.userAgent.WEBKIT || goog.userAgent.EDGE,
179 'The content moves out of the table in WebKit and Edge.');
180
181 if (goog.userAgent.IE) {
182 // Not used since we bail out early for IE, but this is there so that
183 // developers can easily reproduce IE error.
184 goog.dom.Range.createFromNodeContents(document.getElementById('outerTr2'))
185 .select();
186 } else {
187 var selection = window.getSelection();
188 if (selection.rangeCount > 0) selection.removeAllRanges();
189 var range = document.createRange();
190 range.selectNode(document.getElementById('cell1'));
191 selection.addRange(range);
192 range = document.createRange();
193 range.selectNode(document.getElementById('cell2'));
194 selection.addRange(range);
195 }
196
197 expectedFailures.run(function() {
198 FORMATTER.removeFormatting_();
199
200 span = document.getElementById('outerTr2');
201 assertEquals(
202 'Table data should not be removed',
203 '<td id="cell1">seven</td><td id="cell2">eight foo</td>',
204 span.innerHTML);
205 });
206 }
207
208 function testLinksAreNotRemoved() {
209 expectedFailures.expectFailureFor(
210 WEBKIT_BEFORE_CHROME_8,
211 'WebKit\'s removeFormatting command removes links.');
212
213 var anchor;
214 var div = document.getElementById('html');
215 div.innerHTML = 'Foo<span id="link">Pre<a href="http://www.google.com">' +
216 'Outside Span<span style="font-size:15pt">Inside Span' +
217 '</span></a></span>';
218
219 anchor = document.getElementById('link');
220 goog.dom.Range.createFromNodeContents(anchor).select();
221
222 expectedFailures.run(function() {
223 FORMATTER.removeFormatting_();
224 assertHTMLEquals(
225 'link should not be removed',
226 'FooPre<a href="http://www.google.com/">Outside SpanInside Span</a>',
227 div.innerHTML);
228 });
229 }
230
231
232 /**
233 * A short formatting removal function for use with the RemoveFormatting
234 * plugin. Does enough that we can tell this function was run over the
235 * document.
236 * @param {string} text The HTML in from the document.
237 * @return {string} The "cleaned" HTML out.
238 */
239 function replacementFormattingFunc(text) {
240 // Really basic so that we can just see this is executing.
241 return text.replace(/Foo/gi, 'Bar').replace(/<[\/]*span[^>]*>/gi, '');
242 }
243
244 function testAlternateRemoveFormattingFunction() {
245 var div = document.getElementById('html');
246 div.innerHTML = 'Start<span id="remFormat">Foo<pre>Bar</pre>Baz</span>';
247
248 FORMATTER.setRemoveFormattingFunc(replacementFormattingFunc);
249 var area = document.getElementById('remFormat');
250 goog.dom.Range.createFromNodeContents(area).select();
251 FORMATTER.removeFormatting_();
252 // Webkit will change all tags to non-formatted ones anyway.
253 // Make sure 'Foo' was changed to 'Bar'
254 if (WEBKIT_BEFORE_CHROME_8) {
255 assertHTMLEquals(
256 'regular cleaner should not have run', 'StartBar<br>Bar<br>Baz',
257 div.innerHTML);
258 } else {
259 assertHTMLEquals(
260 'regular cleaner should not have run', 'StartBar<pre>Bar</pre>Baz',
261 div.innerHTML);
262 }
263 }
264
265 function testGetValueForNode() {
266 // Override getValueForNode to keep bold tags.
267 var oldGetValue =
268 goog.editor.plugins.RemoveFormatting.prototype.getValueForNode;
269 goog.editor.plugins.RemoveFormatting.prototype.getValueForNode = function(
270 node) {
271 if (node.nodeName == goog.dom.TagName.B) {
272 return '<b>' + this.removeFormattingWorker_(node.innerHTML) + '</b>';
273 }
274 return null;
275 };
276
277 var html = FORMATTER.removeFormattingWorker_('<div>foo<b>bar</b></div>');
278 assertHTMLEquals('B tags should remain', 'foo<b>bar</b>', html);
279
280 // Override getValueForNode to throw out bold tags, and their contents.
281 goog.editor.plugins.RemoveFormatting.prototype.getValueForNode = function(
282 node) {
283 if (node.nodeName == goog.dom.TagName.B) {
284 return '';
285 }
286 return null;
287 };
288
289 html = FORMATTER.removeFormattingWorker_('<div>foo<b>bar</b></div>');
290 assertHTMLEquals('B tag and its contents should be removed', 'foo', html);
291
292 FIELDMOCK.$verify();
293 goog.editor.plugins.RemoveFormatting.prototype.getValueForNode = oldGetValue;
294 }
295
296 function testRemoveFormattingAddsNoNbsps() {
297 var div = document.getElementById('html');
298 div.innerHTML = '"<span id="toStrip">Twin <b>Cinema</b></span>"';
299
300 var span = document.getElementById('toStrip');
301 goog.dom.Range.createFromNodeContents(span).select();
302
303 FORMATTER.removeFormatting_();
304
305 assertEquals(
306 'Text should be the same, with no non-breaking spaces', '"Twin Cinema"',
307 div.innerHTML);
308
309 FIELDMOCK.$verify();
310 }
311
312
313 /**
314 * @bug 992795
315 */
316 function testRemoveFormattingNestedDivs() {
317 var html =
318 FORMATTER.removeFormattingWorker_('<div>1</div><div><div>2</div></div>');
319
320 goog.testing.dom.assertHtmlMatches('1<br>2', html);
321 }
322
323
324 function testTheJavascriptReplaceMetacharacters() {
325 var div = document.getElementById('html');
326 div.innerHTML = '123 $< $> $" $& $$ $` $\' 456';
327 var expected = '123 $&lt; $&gt; $" $&amp; $$ $` $\' 456' +
328 (goog.userAgent.product.SAFARI ? '<br>' : '');
329 // No idea why these <br> appear, but they're fairly insignificant anyways.
330
331 goog.dom.Range.createFromNodeContents(div).select();
332
333 FORMATTER.removeFormatting_();
334 assertHTMLEquals(
335 'String.prototype.replace metacharacters should not trigger', expected,
336 div.innerHTML);
337 }
338
339 /**
340 * Test that when we perform remove formatting on an entire table,
341 * that the visual look is similar to as if there was a table there.
342 */
343 function testRemoveFormattingForTableFormatting() {
344 // We preserve the table formatting as much as possible.
345 // Spaces separate TD's, <br>'s separate TR's.
346 // <br>'s separate the start and end of a table.
347 var html = '<table><tr><td>cell00</td><td>cell01</td></tr>' +
348 '<tr><td>cell10</td><td>cell11</td></tr></table>';
349 html = FORMATTER.removeFormattingWorker_(html);
350 assertHTMLEquals('<br>cell00 cell01<br>cell10 cell11<br>', html);
351 }
352
353
354 /**
355 * @bug 1319715
356 */
357 function testRemoveFormattingDoesNotShrinkSelection() {
358 var div = document.getElementById('html');
359 div.innerHTML = '<div>l </div><div><br><b>a</b>foo bar</div>';
360 var div2 = div.lastChild;
361
362 goog.dom.Range.createFromNodes(div2.firstChild, 0, div2.lastChild, 7)
363 .select();
364
365 FORMATTER.removeFormatting_();
366
367 var range = goog.dom.Range.createFromWindow();
368 assertEquals('Correct text should be selected', 'afoo bar', range.getText());
369
370 // We have to trim out the leading BR in IE due to execCommand issues,
371 // so it isn't sent off to the removeFormattingWorker.
372 // Workaround for broken removeFormat in old webkit added an extra
373 // <br> to the end of the html.
374 var html = '<div>l </div><br class="GECKO WEBKIT">afoo bar' +
375 (goog.editor.BrowserFeature.ADDS_NBSPS_IN_REMOVE_FORMAT ? '<br>' : '');
376 if (goog.userAgent.EDGE) { // TODO(user): I have no idea where this comes from
377 html = html.replace(' class="GECKO WEBKIT"', '');
378 }
379
380 goog.testing.dom.assertHtmlContentsMatch(html, div);
381 FIELDMOCK.$verify();
382 }
383
384
385 /**
386 * @bug 1447374
387 */
388 function testInsideListRemoveFormat() {
389 var div = document.getElementById('html');
390 div.innerHTML = '<ul><li>one</li><li><b>two</b></li><li>three</li></ul>';
391
392 var twoLi = div.firstChild.childNodes[1];
393 goog.dom.Range.createFromNodeContents(twoLi).select();
394
395 expectedFailures.expectFailureFor(
396 goog.userAgent.IE,
397 'IE adds the "two" to the "three" li, and leaves empty B tags.');
398 expectedFailures.expectFailureFor(
399 goog.userAgent.WEBKIT || goog.userAgent.EDGE,
400 'WebKit and Edge leave the "two" orphaned outside of an li but ' +
401 'inside the ul (invalid HTML).');
402
403 expectedFailures.run(function() {
404 FORMATTER.removeFormatting_();
405 // Test that we split the list.
406 assertHTMLEquals(
407 '<ul><li>one</li></ul><br>two<ul><li>three</li></ul>', div.innerHTML);
408 FIELDMOCK.$verify();
409 });
410 }
411
412 function testFullListRemoveFormat() {
413 var div = document.getElementById('html');
414 div.innerHTML = '<ul><li>one</li><li><b>two</b></li><li>three</li></ul>after';
415
416 goog.dom.Range.createFromNodeContents(div.firstChild).select();
417
418 // Note: This may just be a createFromNodeContents issue, as
419 // I can't ever make this happen with real user selection.
420 expectedFailures.expectFailureFor(
421 goog.userAgent.IE,
422 'IE combines everything into a single LI and leaves the UL.');
423
424 expectedFailures.run(function() {
425 FORMATTER.removeFormatting_();
426 // Test that we completely remove the list.
427 assertHTMLEquals('<br>one<br>two<br>threeafter', div.innerHTML);
428 FIELDMOCK.$verify();
429 });
430 }
431
432
433 /**
434 * @bug 1440935
435 */
436 function testPartialListRemoveFormat() {
437 var div = document.getElementById('html');
438 div.innerHTML = '<ul><li>one</li><li>two</li><li>three</li></ul>after';
439
440 // Select "two three after".
441 goog.dom.Range
442 .createFromNodes(div.firstChild.childNodes[1], 0, div.lastChild, 5)
443 .select();
444
445 expectedFailures.expectFailureFor(
446 goog.userAgent.IE, 'IE leaves behind an empty LI.');
447 expectedFailures.expectFailureFor(
448 goog.userAgent.WEBKIT, 'WebKit completely loses the "one".');
449 if (goog.userAgent.EDGE) {
450 // Edge leaves "two" and "threeafter" orphaned outside of an li but inside
451 // the ul (invalid HTML).
452 // Skip this test instead of using expectedFailures because this failure
453 // mode wrecks the DOM and causes later tests to fail as well.
454 return;
455 }
456
457 expectedFailures.run(function() {
458 FORMATTER.removeFormatting_();
459 // Test that we leave the list start alone.
460 assertHTMLEquals(
461 '<ul><li>one</li></ul><br>two<br>threeafter', div.innerHTML);
462 FIELDMOCK.$verify();
463 });
464 }
465
466 function testBasicRemoveFormatting() {
467 // IE will clobber the editable div.
468 // Note: I can't repro this using normal user selections.
469 if (goog.userAgent.IE) {
470 return;
471 }
472 var div = document.getElementById('html');
473 div.innerHTML = '<b>bold<i>italic</i></b>';
474
475 goog.dom.Range.createFromNodeContents(div).select();
476
477 expectedFailures.expectFailureFor(
478 goog.editor.BrowserFeature.ADDS_NBSPS_IN_REMOVE_FORMAT,
479 'The workaround for the nbsp bug adds an extra br at the end.');
480
481 expectedFailures.run(function() {
482 FORMATTER.removeFormatting_();
483 assertHTMLEquals('bolditalic' + insertImageBoldGarbage, div.innerHTML);
484 FIELDMOCK.$verify();
485 });
486 }
487
488
489 /**
490 * @bug 1480260
491 */
492 function testPartialBasicRemoveFormatting() {
493 var div = document.getElementById('html');
494 div.innerHTML = '<b>bold<i>italic</i></b>';
495
496 goog.dom.Range
497 .createFromNodes(
498 div.firstChild.firstChild, 2, div.firstChild.lastChild.firstChild, 3)
499 .select();
500
501 expectedFailures.expectFailureFor(
502 WEBKIT_BEFORE_CHROME_8,
503 'WebKit just gets this all wrong. Everything stays bold and ' +
504 '"lditalic" gets italicised.');
505
506 expectedFailures.run(function() {
507 FORMATTER.removeFormatting_();
508 assertHTMLEquals('<b>bo</b>ldita<b><i>lic</i></b>', div.innerHTML);
509 FIELDMOCK.$verify();
510 });
511 }
512
513
514 /**
515 * @bug 3075557
516 */
517 function testRemoveFormattingLinkedImageBorderZero() {
518 var testHtml = '<a href="http://www.google.com/">' +
519 '<img src="http://www.google.com/images/logo.gif" border="0"></a>';
520 var div = document.getElementById('html');
521 div.innerHTML = testHtml + controlHtml;
522 goog.dom.Range.createFromNodeContents(div).select();
523 FORMATTER.removeFormatting_();
524
525 expectedFailures.expectFailureFor(
526 goog.userAgent.WEBKIT, 'WebKit removes the image entirely, see ' +
527 'https://bugs.webkit.org/show_bug.cgi?id=13125 .');
528
529 expectedFailures.run(function() {
530 assertHTMLEquals(
531 'Image\'s border=0 should not be removed during remove formatting',
532 testHtml + controlCleanHtml, div.innerHTML);
533 FIELDMOCK.$verify();
534 });
535 }
536
537
538 /**
539 * @bug 3075557
540 */
541 function testRemoveFormattingLinkedImageBorderNonzero() {
542 var testHtml = '<a href="http://www.google.com/">' +
543 '<img src="http://www.google.com/images/logo.gif" border="1"></a>';
544 var div = document.getElementById('html');
545 div.innerHTML = testHtml + controlHtml;
546 goog.dom.Range.createFromNodeContents(div).select();
547 FORMATTER.removeFormatting_();
548
549 expectedFailures.expectFailureFor(
550 goog.userAgent.WEBKIT, 'WebKit removes the image entirely, see ' +
551 'https://bugs.webkit.org/show_bug.cgi?id=13125 .');
552
553 expectedFailures.run(function() {
554 assertHTMLEquals(
555 'Image\'s border should be removed during remove formatting' +
556 ' if non-zero',
557 testHtml.replace(' border="1"', '') + controlCleanHtml, div.innerHTML);
558 FIELDMOCK.$verify();
559 });
560 }
561
562
563 /**
564 * @bug 3075557
565 */
566 function testRemoveFormattingUnlinkedImage() {
567 var testHtml = '<img src="http://www.google.com/images/logo.gif" border="0">';
568 var div = document.getElementById('html');
569 div.innerHTML = testHtml + controlHtml;
570 goog.dom.Range.createFromNodeContents(div).select();
571 FORMATTER.removeFormatting_();
572
573 expectedFailures.expectFailureFor(
574 goog.userAgent.WEBKIT, 'WebKit removes the image entirely, see ' +
575 'https://bugs.webkit.org/show_bug.cgi?id=13125 .');
576
577 expectedFailures.run(function() {
578 assertHTMLEquals(
579 'Image\'s border=0 should not be removed during remove formatting' +
580 ' even if not wrapped by a link',
581 testHtml + controlCleanHtml, div.innerHTML);
582 FIELDMOCK.$verify();
583 });
584 }
585
586
587 /**
588 * @bug 3075557
589 */
590 function testRemoveFormattingLinkedImageDeep() {
591 var testHtml = '<a href="http://www.google.com/"><b>hello' +
592 '<img src="http://www.google.com/images/logo.gif" border="0">' +
593 'world</b></a>';
594 var div = document.getElementById('html');
595 div.innerHTML = testHtml + controlHtml;
596 goog.dom.Range.createFromNodeContents(div).select();
597 FORMATTER.removeFormatting_();
598
599 expectedFailures.expectFailureFor(
600 WEBKIT_BEFORE_CHROME_8, 'WebKit removes the image entirely, see ' +
601 'https://bugs.webkit.org/show_bug.cgi?id=13125 .');
602
603 expectedFailures.run(function() {
604 assertHTMLEquals(
605 'Image\'s border=0 should not be removed during remove formatting' +
606 ' even if deep inside anchor tag',
607 testHtml.replace(/<\/?b>/g, '') + controlCleanHtml +
608 insertImageBoldGarbage,
609 div.innerHTML);
610 FIELDMOCK.$verify();
611 });
612 }
613
614 function testFullTableRemoveFormatting() {
615 // Something goes horrible wrong in case 1 below. It was crashing all
616 // WebKit browsers, and now seems to be giving errors as it is trying
617 // to perform remove formatting on the little expected failures window
618 // instead of the dom we select. WTF. Since I'm gutting this code,
619 // I'm not going to look into this anymore right now. For what its worth,
620 // I can't repro any issues in standalone TrogEdit.
621 if (goog.userAgent.WEBKIT) {
622 return;
623 }
624
625 var div = document.getElementById('html');
626
627 // WebKit has an extra BR in case 2.
628 expectedFailures.expectFailureFor(
629 goog.userAgent.IE,
630 'IE clobbers the editable node in case 2 (can\'t repro with real ' +
631 'user selections). IE doesn\'t remove the table in case 1.');
632
633 expectedFailures.run(function() {
634
635 // When a full table is selected, we remove it completely.
636 div.innerHTML = 'foo<table><tr><td>bar</td></tr></table>baz1';
637 goog.dom.Range.createFromNodeContents(div.childNodes[1]).select();
638 FORMATTER.removeFormatting_();
639 assertHTMLEquals('foo<br>bar<br>baz1', div.innerHTML);
640 FIELDMOCK.$verify();
641
642 // Remove the full table when it is selected with additional
643 // contents too.
644 div.innerHTML = 'foo<table><tr><td>bar</td></tr></table>baz2';
645 goog.dom.Range.createFromNodes(div.firstChild, 0, div.lastChild, 1)
646 .select();
647 FORMATTER.removeFormatting_();
648 assertHTMLEquals('foo<br>bar<br>baz2', div.innerHTML);
649 FIELDMOCK.$verify();
650
651 // We should still remove the table, even if the selection is inside the
652 // table and it is fully selected.
653 div.innerHTML = 'foo<table><tr><td id=\'td\'>bar</td></tr></table>baz3';
654 goog.dom.Range.createFromNodeContents(goog.dom.getElement('td').firstChild)
655 .select();
656 FORMATTER.removeFormatting_();
657 assertHTMLEquals('foo<br>bar<br>baz3', div.innerHTML);
658 FIELDMOCK.$verify();
659 });
660 }
661
662 function testInsideTableRemoveFormatting() {
663 var div = document.getElementById('html');
664 div.innerHTML =
665 '<table><tr><td><b id="b">foo</b></td></tr><tr><td>ba</td></tr></table>';
666
667 goog.dom.Range.createFromNodeContents(goog.dom.getElement('b')).select();
668
669 // Webkit adds some apple style span crap during execCommand("removeFormat")
670 // Our workaround for the nbsp bug removes these, but causes worse problems.
671 // See bugs.webkit.org/show_bug.cgi?id=29164 for more details.
672 expectedFailures.expectFailureFor(
673 WEBKIT_BEFORE_CHROME_8 &&
674 !goog.editor.BrowserFeature.ADDS_NBSPS_IN_REMOVE_FORMAT,
675 'Extra apple-style-spans');
676
677 expectedFailures.run(function() {
678 FORMATTER.removeFormatting_();
679
680 // Only remove styling from inside tables.
681 assertHTMLEquals(
682 '<table><tr><td>foo' + insertImageBoldGarbage +
683 '</td></tr><tr><td>ba</td></tr></table>',
684 div.innerHTML);
685 FIELDMOCK.$verify();
686 });
687 }
688
689 function testPartialTableRemoveFormatting() {
690 if (goog.userAgent.IE) {
691 // IE returns an "unspecified error" which seems to be beyond
692 // ExpectedFailures' ability to catch.
693 return;
694 }
695
696 var div = document.getElementById('html');
697 div.innerHTML = 'bar<table><tr><td><b id="b">foo</b></td></tr>' +
698 '<tr><td><i>banana</i></td></tr></table><div id="baz">' +
699 'baz</div>';
700
701 // Select from the "oo" inside the b tag to the end of "baz".
702 goog.dom.Range
703 .createFromNodes(
704 goog.dom.getElement('b').firstChild, 1,
705 goog.dom.getElement('baz').firstChild, 3)
706 .select();
707
708 // All browsers currently clobber the table cells that are selected.
709 expectedFailures.expectFailureFor(goog.userAgent.WEBKIT);
710
711 expectedFailures.run(function() {
712 FORMATTER.removeFormatting_();
713 // Only remove styling from inside tables.
714 assertHTMLEquals(
715 'bar<table><tr><td><b id="b">f</b>oo</td></tr>' +
716 '<tr><td>banana</td></tr></table>baz',
717 div.innerHTML);
718 FIELDMOCK.$verify();
719 });
720 }
721
722 // Runs tests knowing some browsers will fail, because the new
723 // table functionality hasn't been implemented in them yet.
724 function runExpectingFailuresForUnimplementedBrowsers(func) {
725 if (goog.userAgent.IE) {
726 // IE returns an "unspecified error" which seems to be beyond
727 // ExpectedFailures' ability to catch.
728 return;
729 }
730
731 expectedFailures.expectFailureFor(
732 goog.userAgent.IE, 'Proper behavior not yet implemented for IE.');
733 expectedFailures.expectFailureFor(
734 goog.userAgent.WEBKIT, 'Proper behavior not yet implemented for WebKit.');
735
736 expectedFailures.run(func);
737 }
738
739
740 function testTwoTablesSelectedFullyRemoveFormatting() {
741 runExpectingFailuresForUnimplementedBrowsers(function() {
742 var div = document.getElementById('html');
743 // When two tables are fully selected, we remove them completely.
744 div.innerHTML = '<table><tr><td>foo</td></tr></table>' +
745 '<table><tr><td>bar</td></tr></table>';
746 goog.dom.Range.createFromNodes(div.firstChild, 0, div.lastChild, 1)
747 .select();
748 FORMATTER.removeFormatting_();
749 assertHTMLEquals('<br>foo<br><br>bar<br>', div.innerHTML);
750 FIELDMOCK.$verify();
751 });
752 }
753
754 function testTwoTablesSelectedFullyInsideRemoveFormatting() {
755 if (goog.userAgent.WEBKIT) {
756 // Something goes very wrong here, but it did before
757 // Julie started writing v2. Will address when converting
758 // safari to v2.
759 return;
760 }
761
762 runExpectingFailuresForUnimplementedBrowsers(function() {
763 var div = document.getElementById('html');
764 // When two tables are selected from inside but fully,
765 // also remove them completely.
766 div.innerHTML = '<table><tr><td id="td1">foo</td></tr></table>' +
767 '<table><tr><td id="td2">bar</td></tr></table>';
768 goog.dom.Range
769 .createFromNodes(
770 goog.dom.getElement('td1').firstChild, 0,
771 goog.dom.getElement('td2').firstChild, 3)
772 .select();
773 FORMATTER.removeFormatting_();
774 assertHTMLEquals('<br>foo<br><br>bar<br>', div.innerHTML);
775 FIELDMOCK.$verify();
776 });
777 }
778
779 function testTwoTablesSelectedFullyAndPartiallyRemoveFormatting() {
780 runExpectingFailuresForUnimplementedBrowsers(function() {
781 var div = document.getElementById('html');
782 // Two tables selected, one fully, one partially. Remove
783 // only the fully selected one and remove styles only from
784 // partially selected one.
785 div.innerHTML = '<table><tr><td id="td1">foo</td></tr></table>' +
786 '<table><tr><td id="td2"><b>bar<b></td></tr></table>';
787 goog.dom.Range
788 .createFromNodes(
789 goog.dom.getElement('td1').firstChild, 0,
790 goog.dom.getElement('td2').firstChild.firstChild, 2)
791 .select();
792 FORMATTER.removeFormatting_();
793 var expectedHtml = '<br>foo<br>' +
794 '<table><tr><td id="td2">ba<b>r</b></td></tr></table>';
795 if (goog.userAgent.EDGE) {
796 // TODO(user): Edge inserts an extra empty <b> tag but is otherwise correct
797 expectedHtml = expectedHtml.replace('</b>', '<b></b></b>');
798 }
799 assertHTMLEquals(expectedHtml, div.innerHTML);
800 FIELDMOCK.$verify();
801 });
802 }
803
804 function testTwoTablesSelectedPartiallyRemoveFormatting() {
805 runExpectingFailuresForUnimplementedBrowsers(function() {
806 var div = document.getElementById('html');
807 // Two tables selected, both partially. Don't remove tables,
808 // but remove styles.
809 div.innerHTML = '<table><tr><td id="td1">f<i>o</i>o</td></tr></table>' +
810 '<table><tr><td id="td2">b<b>a</b>r</td></tr></table>';
811 goog.dom.Range
812 .createFromNodes(
813 goog.dom.getElement('td1').firstChild, 1,
814 goog.dom.getElement('td2').childNodes[1], 1)
815 .select();
816 FORMATTER.removeFormatting_();
817 assertHTMLEquals(
818 '<table><tr><td id="td1">foo</td></tr></table>' +
819 '<table><tr><td id="td2">bar</td></tr></table>',
820 div.innerHTML);
821 FIELDMOCK.$verify();
822 });
823 }
824
825
826 /**
827 * Test a random snippet from Google News (Google News has complicated
828 * dom structure, including tables, links, images, etc).
829 */
830 function testRandomGoogleNewsSnippetRemoveFormatting() {
831 if (goog.userAgent.IE) {
832 // IE returns an "unspecified error" which seems to be beyond
833 // ExpectedFailures' ability to catch.
834 return;
835 }
836
837 var div = document.getElementById('html');
838 div.innerHTML =
839 '<font size="-3"><br></font><table align="right" border="0" ' +
840 'cellpadding="0" cellspacing="0"><tbody><tr><td style="padding-left:' +
841 '6px;" valign="top" width="80" align="center"><a href="http://www.wash' +
842 'ingtonpost.com/wp-dyn/content/article/2008/11/11/AR2008111101090.htm' +
843 'l" + id="s-skHRvWH7ryqkcA4caGv0QQ:u-AFQjCNG3vx1HJOxKxMQPzCvYOVRE0JUDe' +
844 'Q:r-1-0i_1268233361_6_H0_MH20_PL60"><img src="http://news.google.com/' +
845 'news?imgefp=4LFiNNP62TgJ&amp;imgurl=media3.washingtonpost.com/wp-dyn/' +
846 'content/photo/2008/11/11/PH2008111101091.jpg" alt="" width="60" ' +
847 'border="1" height="80"><br><font size="-2">Washington Post</font></a>' +
848 '</td></tr></tbody></table><a href="http://www.nme.com/news/britney-' +
849 'spears/40995" id="s-xZUO-t0c1IpsVjyJj0rgxw:u-AFQjCNEZAMQCseEW6uTgXI' +
850 'iPvAMHe_0B4A:r-1-0_1268233361_6_H0_MH20_PL60"><b>Britney\'s son ' +
851 'released from hospital</b></a><br><font size="-1"><b><font color=' +
852 '"#6f6f6f">NME.com&nbsp;-</font> <nobr>53 minutes ago</nobr></b>' +
853 '</font><br><font size="-1">Britney Spears� youngest son Jayden James ' +
854 'has been released from hospital, having been admitted on Sunday after' +
855 ' suffering a severe reaction to something he ingested.</font><br><fon' +
856 'tsize="-1"><a href="http://www.celebrity-gossip.net/celebrities/holly' +
857 'wood/britney-and-jamie-lynn-spears-alligator-alley-208944/" id="s-nM' +
858 'PzHclcMG0J2WZkw9gnVQ:u-AFQjCNHal08usOQ5e5CAQsck2yGsTYeGVQ">Britney ' +
859 'and Jamie Lynn Spears: Alligator Alley!</a> <font size="-1" color=' +
860 '"#6f6f6f"><nobr>The Gossip Girls</nobr></font></font><br><font size=' +
861 '"-1"><a href="http://foodconsumer.org/7777/8888/Other_N_ews_51/111101' +
862 '362008_Allergy_incident_could_spell_custody_trouble_for_Britney_Spear' +
863 's.shtml" id="s-2lMNDY4joOprVvkkY_b-6A:u-AFQjCNGAeFNutMEbSg5zAvrh5reBF' +
864 'lqUmA">Allergy incident could spell trouble for Britney Spears</a> ' +
865 '<font size="-1" color="#6f6f6f"><nobr>Food Consumer</nobr></font>' +
866 '</font><br><font class="p" size="-1"><a href="http://www.people.com/' +
867 'people/article/0,,20239458,00.html" id="s-x9thwVUYVET0ZJOnkkcsjw:u-A' +
868 'FQjCNE99eijVIrezr9AFRjLkmo5j_Jr7A"><nobr>People Magazine</nobr></a>&nb' +
869 'sp;- <a href="http://www.eonline.com/uberblog/b68226_hospital_run_cou' +
870 'ld_cost_britney_custody.html" id="s-kYt5LHDhlDnhUL9kRLuuwA:u-AFQjCNF8' +
871 '8eOy2utriYuF0icNrZQPzwK8gg"><nobr>E! Online</nobr></a>&nbsp;- <a href' +
872 '="http://justjared.buzznet.com/2008/11/11/britney-spears-alligator-fa' +
873 'rm/" id="s--VDy1fyacNvaRo_aXb02Dw:u-AFQjCNEn0Rz3wg0PMwDdzKTDug-9k5W6y' +
874 'g"><nobr>Just Jared</nobr></a>&nbsp;- <a href="http://www.efluxmedia.' +
875 'com/news_Britney_Spears_Son_Released_from_Hospital_28696.html" id="s-' +
876 '8oX6hVDe4Qbcl1x5Rua_EA:u-AFQjCNEpn3nOHA8EB0pxJAPf6diOicMRDg"><nobr>eF' +
877 'luxMedia</nobr></a></font><br><font class="p" size="-1"><a class="p" ' +
878 'href="http://news.google.com/news?ncl=1268233361&amp;hl=en"><nobr><b>' +
879 'all 950 news articles&nbsp;�</b></nobr></a></font>';
880 // Select it all.
881 goog.dom.Range.createFromNodeContents(div).select();
882
883 expectedFailures.expectFailureFor(
884 WEBKIT_BEFORE_CHROME_8,
885 'WebKit barfs apple-style-spans all over the place, and removes links.');
886
887 expectedFailures.run(function() {
888 FORMATTER.removeFormatting_();
889 // Leave links and images alone, remove all other formatting.
890 assertHTMLEquals(
891 '<br><br><a href="http://www.washingtonpost.com/wp-dyn/' +
892 'content/article/2008/11/11/AR2008111101090.html"><img src="http://n' +
893 'ews.google.com/news?imgefp=4LFiNNP62TgJ&amp;imgurl=media3.washingto' +
894 'npost.com/wp-dyn/content/photo/2008/11/11/PH2008111101091.jpg"><br>' +
895 'Washington Post</a><br><a href="http://www.nme.com/news/britney-spe' +
896 'ars/40995">Britney\'s son released from hospital</a><br>NME.com - 5' +
897 '3 minutes ago<br>Britney Spears� youngest son Jayden James has been' +
898 ' released from hospital, having been admitted on Sunday after suffe' +
899 'ring a severe reaction to something he ingested.<br><a href="http:/' +
900 '/www.celebrity-gossip.net/celebrities/hollywood/britney-and-jamie-l' +
901 'ynn-spears-alligator-alley-208944/">Britney and Jamie Lynn Spears: ' +
902 'Alligator Alley!</a> The Gossip Girls<br><a href="http://foodconsum' +
903 'er.org/7777/8888/Other_N_ews_51/111101362008_Allergy_incident_could' +
904 '_spell_custody_trouble_for_Britney_Spears.shtml">Allergy incident c' +
905 'ould spell trouble for Britney Spears</a> Food Consumer<br><a href=' +
906 '"http://www.people.com/people/article/0,,20239458,00.html">People M' +
907 'agazine</a> - <a href="http://www.eonline.com/uberblog/b68226_hospi' +
908 'tal_run_could_cost_britney_custody.html">E! Online</a> - <a href="h' +
909 'ttp://justjared.buzznet.com/2008/11/11/britney-spears-alligator-far' +
910 'm/">Just Jared</a> - <a href="http://www.efluxmedia.com/news_Britne' +
911 'y_Spears_Son_Released_from_Hospital_28696.html">eFluxMedia</a><br><' +
912 'a href="http://news.google.com/news?ncl=1268233361&amp;hl=en">all 9' +
913 '50 news articles �</a>' + insertImageFontGarbage,
914 div.innerHTML);
915 FIELDMOCK.$verify();
916 });
917 }
918
919 function testRangeDelimitedByRanges() {
920 var abcde = goog.dom.getElement('abcde').firstChild;
921 var start = goog.dom.Range.createFromNodes(abcde, 1, abcde, 2);
922 var end = goog.dom.Range.createFromNodes(abcde, 3, abcde, 4);
923
924 goog.testing.dom.assertRangeEquals(
925 abcde, 1, abcde, 4,
926 goog.editor.plugins.RemoveFormatting.createRangeDelimitedByRanges_(
927 start, end));
928 }
929
930 function testGetTableAncestor() {
931 var div = document.getElementById('html');
932
933 div.innerHTML = 'foo<table><tr><td>foo</td></tr></table>bar';
934 assertTrue(
935 'Full table is in table',
936 !!FORMATTER.getTableAncestor_(div.childNodes[1]));
937
938 assertFalse(
939 'Outside of table', !!FORMATTER.getTableAncestor_(div.firstChild));
940
941 assertTrue(
942 'Table cell is in table',
943 !!FORMATTER.getTableAncestor_(
944 div.childNodes[1].firstChild.firstChild.firstChild));
945
946 goog.dom.setTextContent(div, 'foo');
947 assertNull(
948 'No table inside field.', FORMATTER.getTableAncestor_(div.childNodes[0]));
949 }
950
951
952 /**
953 * @bug 1272905
954 */
955 function testHardReturnsInHeadersPreserved() {
956 var div = document.getElementById('html');
957 div.innerHTML = '<h1>abcd</h1><h2>efgh</h2><h3>ijkl</h3>';
958
959 // Select efgh.
960 goog.dom.Range.createFromNodeContents(div.childNodes[1]).select();
961 FORMATTER.removeFormatting_();
962
963 expectedFailures.expectFailureFor(
964 goog.userAgent.IE, 'Proper behavior not yet implemented for IE.');
965 expectedFailures.expectFailureFor(
966 goog.userAgent.WEBKIT, 'Proper behavior not yet implemented for WebKit.');
967 expectedFailures.run(function() {
968 assertHTMLEquals('<h1>abcd</h1><br>efgh<h3>ijkl</h3>', div.innerHTML);
969 });
970
971 // Select ijkl.
972 goog.dom.Range.createFromNodeContents(div.lastChild).select();
973 FORMATTER.removeFormatting_();
974
975 expectedFailures.expectFailureFor(
976 goog.userAgent.IE, 'Proper behavior not yet implemented for IE.');
977 expectedFailures.expectFailureFor(
978 goog.userAgent.WEBKIT, 'Proper behavior not yet implemented for WebKit.');
979 expectedFailures.run(function() {
980 assertHTMLEquals('<h1>abcd</h1><br>efgh<br>ijkl', div.innerHTML);
981 });
982
983 // Select abcd.
984 goog.dom.Range.createFromNodeContents(div.firstChild).select();
985 FORMATTER.removeFormatting_();
986
987 expectedFailures.expectFailureFor(
988 goog.userAgent.IE, 'Proper behavior not yet implemented for IE.');
989 expectedFailures.expectFailureFor(
990 goog.userAgent.WEBKIT, 'Proper behavior not yet implemented for WebKit.');
991 expectedFailures.run(function() {
992 assertHTMLEquals('<br>abcd<br>efgh<br>ijkl', div.innerHTML);
993 });
994 }
995
996 function testKeyboardShortcut_space() {
997 FIELDMOCK.$reset();
998
999 FIELDMOCK.execCommand(
1000 goog.editor.plugins.RemoveFormatting.REMOVE_FORMATTING_COMMAND);
1001
1002 FIELDMOCK.$replay();
1003
1004 var e = {};
1005 var key = ' ';
1006 var result = FORMATTER.handleKeyboardShortcut(e, key, true);
1007 assertTrue(result);
1008
1009 FIELDMOCK.$verify();
1010 }
1011
1012 function testKeyboardShortcut_other() {
1013 FIELDMOCK.$reset();
1014 FIELDMOCK.$replay();
1015
1016 var e = {};
1017 var key = 'x';
1018 var result = FORMATTER.handleKeyboardShortcut(e, key, true);
1019 assertFalse(result);
1020
1021 FIELDMOCK.$verify();
1022 }
1023
1024 function testCustomKeyboardShortcut_custom() {
1025 FIELDMOCK.$reset();
1026
1027 FIELDMOCK.execCommand(
1028 goog.editor.plugins.RemoveFormatting.REMOVE_FORMATTING_COMMAND);
1029
1030 FIELDMOCK.$replay();
1031
1032 var e = {};
1033 var key = '\\';
1034 FORMATTER.setKeyboardShortcutKey(key);
1035 var result = FORMATTER.handleKeyboardShortcut(e, key, true);
1036 assertTrue(result);
1037
1038 FIELDMOCK.$verify();
1039 }
1040
1041 function testCustomKeyboardShortcut_default() {
1042 FIELDMOCK.$reset();
1043 FIELDMOCK.$replay();
1044
1045 var e = {};
1046 var key = ' ';
1047 FORMATTER.setKeyboardShortcutKey('\\');
1048 var result = FORMATTER.handleKeyboardShortcut(e, key, true);
1049 assertFalse(result);
1050
1051 FIELDMOCK.$verify();
1052 }
1053
1054 function testKeyboardShortcut_withBothModifierKeys() {
1055 FIELDMOCK.$reset();
1056 FIELDMOCK.$replay();
1057
1058 var e = {};
1059 e.metaKey = true;
1060 e.ctrlKey = true;
1061 var key = ' ';
1062 var result = FORMATTER.handleKeyboardShortcut(e, key, true);
1063 assertFalse(result);
1064
1065 FIELDMOCK.$verify();
1066 }
1067
1068
1069 function testKeyboardShortcut_withMetaKeyAndShiftKey() {
1070 FIELDMOCK.$reset();
1071 FIELDMOCK.$replay();
1072
1073 var e = {};
1074 e.metaKey = true;
1075 e.shiftKey = true;
1076 var key = ' ';
1077 var result = FORMATTER.handleKeyboardShortcut(e, key, true);
1078 assertFalse(result);
1079
1080 FIELDMOCK.$verify();
1081 }
1082
1083
1084 function testKeyboardShortcut_withCtrlKeyAndShiftKey() {
1085 FIELDMOCK.$reset();
1086 FIELDMOCK.$replay();
1087
1088 var e = {};
1089 e.ctrlKey = true;
1090 e.shiftKey = true;
1091 var key = ' ';
1092 var result = FORMATTER.handleKeyboardShortcut(e, key, true);
1093 assertFalse(result);
1094
1095 FIELDMOCK.$verify();
1096 }