Debian Codegraph @

closure-library/closure/goog/editor/plugins/basictextformatter_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.BasicTextFormatterTest');
16 goog.setTestOnly('goog.editor.plugins.BasicTextFormatterTest');
17
18 goog.require('goog.array');
19 goog.require('goog.dom');
20 goog.require('goog.dom.Range');
21 goog.require('goog.dom.TagName');
22 goog.require('goog.editor.BrowserFeature');
23 goog.require('goog.editor.Command');
24 goog.require('goog.editor.Field');
25 goog.require('goog.editor.Plugin');
26 goog.require('goog.editor.plugins.BasicTextFormatter');
27 goog.require('goog.html.SafeHtml');
28 goog.require('goog.object');
29 goog.require('goog.style');
30 goog.require('goog.testing.ExpectedFailures');
31 goog.require('goog.testing.LooseMock');
32 goog.require('goog.testing.PropertyReplacer');
33 goog.require('goog.testing.editor.FieldMock');
34 goog.require('goog.testing.editor.TestHelper');
35 goog.require('goog.testing.jsunit');
36 goog.require('goog.testing.mockmatchers');
37 goog.require('goog.userAgent');
38 goog.require('goog.userAgent.product');
39
40 var stubs;
41
42 var SAVED_HTML;
43 var FIELDMOCK;
44 var FORMATTER;
45 var ROOT;
46 var HELPER;
47 var OPEN_SUB;
48 var CLOSE_SUB;
49 var OPEN_SUPER;
50 var CLOSE_SUPER;
51 var MOCK_BLOCKQUOTE_STYLE = 'border-left: 1px solid gray;';
52 var MOCK_GET_BLOCKQUOTE_STYLES = function() {
53 return MOCK_BLOCKQUOTE_STYLE;
54 };
55
56 var REAL_FIELD;
57 var REAL_PLUGIN;
58
59 var expectedFailures;
60
61 function setUpPage() {
62 stubs = new goog.testing.PropertyReplacer();
63 SAVED_HTML = goog.dom.getElement('html').innerHTML;
64 FIELDMOCK;
65 FORMATTER;
66 ROOT = goog.dom.getElement('root');
67 HELPER;
68 OPEN_SUB = goog.userAgent.WEBKIT && !goog.userAgent.isVersionOrHigher('530') ?
69 '<span class="Apple-style-span" style="vertical-align: sub;">' :
70 '<sub>';
71 CLOSE_SUB =
72 goog.userAgent.WEBKIT && !goog.userAgent.isVersionOrHigher('530') ?
73 '</span>' :
74 '</sub>';
75 OPEN_SUPER =
76 goog.userAgent.WEBKIT && !goog.userAgent.isVersionOrHigher('530') ?
77 '<span class="Apple-style-span" style="vertical-align: super;">' :
78 '<sup>';
79 CLOSE_SUPER =
80 goog.userAgent.WEBKIT && !goog.userAgent.isVersionOrHigher('530') ?
81 '</span>' :
82 '</sup>';
83 expectedFailures = new goog.testing.ExpectedFailures();
84 }
85
86 function setUp() {
87 FIELDMOCK = new goog.testing.editor.FieldMock();
88
89 FORMATTER = new goog.editor.plugins.BasicTextFormatter();
90 FORMATTER.fieldObject = FIELDMOCK;
91 }
92
93 function setUpRealField() {
94 REAL_FIELD = new goog.editor.Field('real-field');
95 REAL_PLUGIN = new goog.editor.plugins.BasicTextFormatter();
96 REAL_FIELD.registerPlugin(REAL_PLUGIN);
97 REAL_FIELD.makeEditable();
98 }
99
100 function setUpRealFieldIframe() {
101 REAL_FIELD = new goog.editor.Field('iframe');
102 FORMATTER = new goog.editor.plugins.BasicTextFormatter();
103 REAL_FIELD.registerPlugin(FORMATTER);
104 REAL_FIELD.makeEditable();
105 }
106
107 function selectRealField() {
108 goog.dom.Range.createFromNodeContents(REAL_FIELD.getElement()).select();
109 REAL_FIELD.dispatchSelectionChangeEvent();
110 }
111
112 function tearDown() {
113 tearDownFontSizeTests();
114
115 if (REAL_FIELD) {
116 REAL_FIELD.makeUneditable();
117 REAL_FIELD.dispose();
118 REAL_FIELD = null;
119 }
120
121 expectedFailures.handleTearDown();
122 stubs.reset();
123
124 goog.dom.getElement('html').innerHTML = SAVED_HTML;
125 }
126
127 function setUpListAndBlockquoteTests() {
128 var htmlDiv = document.getElementById('html');
129 HELPER = new goog.testing.editor.TestHelper(htmlDiv);
130 HELPER.setUpEditableElement();
131
132 FIELDMOCK.getElement();
133 FIELDMOCK.$anyTimes();
134 FIELDMOCK.$returns(htmlDiv);
135 }
136
137 function tearDownHelper() {
138 HELPER.tearDownEditableElement();
139 HELPER.dispose();
140 HELPER = null;
141 }
142
143 function tearDownListAndBlockquoteTests() {
144 tearDownHelper();
145 }
146
147 function testIEList() {
148 if (goog.userAgent.IE) {
149 setUpListAndBlockquoteTests();
150
151 FIELDMOCK.queryCommandValue('rtl').$returns(null);
152
153 FIELDMOCK.$replay();
154 var ul = goog.dom.getElement('outerUL');
155 goog.dom.Range
156 .createFromNodeContents(goog.dom.getFirstElementChild(ul).firstChild)
157 .select();
158 FORMATTER.fixIELists_();
159 assertFalse('Unordered list must not have ordered type', ul.type == '1');
160 var ol = goog.dom.getElement('ol');
161 ol.type = 'disc';
162 goog.dom.Range
163 .createFromNodeContents(goog.dom.getFirstElementChild(ul).firstChild)
164 .select();
165 FORMATTER.fixIELists_();
166 assertFalse('Ordered list must not have unordered type', ol.type == 'disc');
167 ol.type = '1';
168 goog.dom.Range
169 .createFromNodeContents(goog.dom.getFirstElementChild(ul).firstChild)
170 .select();
171 FORMATTER.fixIELists_();
172 assertTrue('Ordered list must retain ordered list type', ol.type == '1');
173 tearDownListAndBlockquoteTests();
174 }
175 }
176
177 function testWebKitList() {
178 if (goog.userAgent.WEBKIT) {
179 setUpListAndBlockquoteTests();
180
181 FIELDMOCK.queryCommandValue('rtl').$returns(null);
182
183 FIELDMOCK.$replay();
184 var ul = document.getElementById('outerUL');
185 var html = ul.innerHTML;
186 goog.dom.Range.createFromNodeContents(ul).select();
187
188 FORMATTER.fixSafariLists_();
189 assertEquals('Contents of UL shouldn\'t change', html, ul.innerHTML);
190
191 ul = document.getElementById('outerUL2');
192 goog.dom.Range.createFromNodeContents(ul).select();
193
194 FORMATTER.fixSafariLists_();
195 var childULs = goog.dom.getElementsByTagName(goog.dom.TagName.UL, ul);
196 assertEquals('UL should have one child UL', 1, childULs.length);
197 tearDownListAndBlockquoteTests();
198 }
199 }
200
201 function testGeckoListFont() {
202 if (goog.userAgent.GECKO) {
203 setUpListAndBlockquoteTests();
204 FIELDMOCK.queryCommandValue(goog.editor.Command.DEFAULT_TAG)
205 .$returns('BR')
206 .$times(2);
207
208 FIELDMOCK.$replay();
209 var p = goog.dom.getElement('geckolist');
210 var font = p.firstChild;
211 goog.dom.Range.createFromNodeContents(font).select();
212 retVal = FORMATTER.beforeInsertListGecko_();
213 assertFalse('Workaround shouldn\'t be applied when not needed', retVal);
214
215 goog.dom.removeChildren(font);
216 goog.dom.Range.createFromNodeContents(font).select();
217 var retVal = FORMATTER.beforeInsertListGecko_();
218 assertTrue('Workaround should be applied when needed', retVal);
219 document.execCommand('insertorderedlist', false, true);
220 assertTrue(
221 'Font should be Courier',
222 /courier/i.test(document.queryCommandValue('fontname')));
223 tearDownListAndBlockquoteTests();
224 }
225 }
226
227 function testSwitchListType() {
228 if (!goog.userAgent.WEBKIT) {
229 return;
230 }
231 // Test that we're not seeing https://bugs.webkit.org/show_bug.cgi?id=19539,
232 // the type of multi-item lists.
233 setUpListAndBlockquoteTests();
234
235 FIELDMOCK.$replay();
236 var list = goog.dom.getElement('switchListType');
237 var parent = goog.dom.getParentElement(list);
238
239 goog.dom.Range.createFromNodeContents(list).select();
240 FORMATTER.execCommandInternal('insertunorderedlist');
241 list = goog.dom.getFirstElementChild(parent);
242 assertEquals(String(goog.dom.TagName.UL), list.tagName);
243 assertEquals(
244 3, goog.dom.getElementsByTagNameAndClass(goog.dom.TagName.LI, null, list)
245 .length);
246
247 goog.dom.Range.createFromNodeContents(list).select();
248 FORMATTER.execCommandInternal('insertorderedlist');
249 list = goog.dom.getFirstElementChild(parent);
250 assertEquals(String(goog.dom.TagName.OL), list.tagName);
251 assertEquals(
252 3, goog.dom.getElementsByTagNameAndClass(goog.dom.TagName.LI, null, list)
253 .length);
254
255 tearDownListAndBlockquoteTests();
256 }
257
258 function testIsSilentCommand() {
259 var commands =
260 goog.object.getValues(goog.editor.plugins.BasicTextFormatter.COMMAND);
261 var silentCommands =
262 [goog.editor.plugins.BasicTextFormatter.COMMAND.CREATE_LINK];
263
264 for (var i = 0; i < commands.length; i += 1) {
265 var command = commands[i];
266 var shouldBeSilent = goog.array.contains(silentCommands, command);
267 var isSilent =
268 goog.editor.plugins.BasicTextFormatter.prototype.isSilentCommand.call(
269 null, command);
270 assertEquals(shouldBeSilent, isSilent);
271 }
272 }
273
274 function setUpSubSuperTests() {
275 goog.dom.setTextContent(ROOT, '12345');
276 HELPER = new goog.testing.editor.TestHelper(ROOT);
277 HELPER.setUpEditableElement();
278 }
279
280 function tearDownSubSuperTests() {
281 tearDownHelper();
282 }
283
284 function testSubscriptRemovesSuperscript() {
285 setUpSubSuperTests();
286 FIELDMOCK.$replay();
287
288 HELPER.select('12345', 1, '12345', 4); // Selects '234'.
289 FORMATTER.execCommandInternal(
290 goog.editor.plugins.BasicTextFormatter.COMMAND.SUPERSCRIPT);
291 HELPER.assertHtmlMatches('1' + OPEN_SUPER + '234' + CLOSE_SUPER + '5');
292 FORMATTER.execCommandInternal(
293 goog.editor.plugins.BasicTextFormatter.COMMAND.SUBSCRIPT);
294 HELPER.assertHtmlMatches('1' + OPEN_SUB + '234' + CLOSE_SUB + '5');
295
296 FIELDMOCK.$verify();
297 tearDownSubSuperTests();
298 }
299
300 function testSuperscriptRemovesSubscript() {
301 setUpSubSuperTests();
302 FIELDMOCK.$replay();
303
304 HELPER.select('12345', 1, '12345', 4); // Selects '234'.
305 FORMATTER.execCommandInternal(
306 goog.editor.plugins.BasicTextFormatter.COMMAND.SUBSCRIPT);
307 HELPER.assertHtmlMatches('1' + OPEN_SUB + '234' + CLOSE_SUB + '5');
308 FORMATTER.execCommandInternal(
309 goog.editor.plugins.BasicTextFormatter.COMMAND.SUPERSCRIPT);
310 HELPER.assertHtmlMatches('1' + OPEN_SUPER + '234' + CLOSE_SUPER + '5');
311
312 FIELDMOCK.$verify();
313 tearDownSubSuperTests();
314 }
315
316 function testSubscriptRemovesSuperscriptIntersecting() {
317 // Tests: 12345 , sup(23) , sub(34) ==> 1+sup(2)+sub(34)+5
318 // This is more complex because the sub and sup calls are made on separate
319 // fields which intersect each other and queryCommandValue seems to return
320 // false if the command is only applied to part of the field.
321 setUpSubSuperTests();
322 FIELDMOCK.$replay();
323
324 HELPER.select('12345', 1, '12345', 3); // Selects '23'.
325 FORMATTER.execCommandInternal(
326 goog.editor.plugins.BasicTextFormatter.COMMAND.SUPERSCRIPT);
327 HELPER.assertHtmlMatches('1' + OPEN_SUPER + '23' + CLOSE_SUPER + '45');
328 HELPER.select('23', 1, '45', 1); // Selects '34'.
329 FORMATTER.execCommandInternal(
330 goog.editor.plugins.BasicTextFormatter.COMMAND.SUBSCRIPT);
331 HELPER.assertHtmlMatches(
332 '1' + OPEN_SUPER + '2' + CLOSE_SUPER + OPEN_SUB + '34' + CLOSE_SUB + '5');
333
334 FIELDMOCK.$verify();
335 tearDownSubSuperTests();
336 }
337
338 function testSuperscriptRemovesSubscriptIntersecting() {
339 // Tests: 12345 , sub(23) , sup(34) ==> 1+sub(2)+sup(34)+5
340 setUpSubSuperTests();
341 FIELDMOCK.$replay();
342
343 HELPER.select('12345', 1, '12345', 3); // Selects '23'.
344 FORMATTER.execCommandInternal(
345 goog.editor.plugins.BasicTextFormatter.COMMAND.SUBSCRIPT);
346 HELPER.assertHtmlMatches('1' + OPEN_SUB + '23' + CLOSE_SUB + '45');
347 HELPER.select('23', 1, '45', 1); // Selects '34'.
348 FORMATTER.execCommandInternal(
349 goog.editor.plugins.BasicTextFormatter.COMMAND.SUPERSCRIPT);
350 HELPER.assertHtmlMatches(
351 '1' + OPEN_SUB + '2' + CLOSE_SUB + OPEN_SUPER + '34' + CLOSE_SUPER + '5');
352
353 FIELDMOCK.$verify();
354 tearDownSubSuperTests();
355 }
356
357 function setUpLinkTests(text, url, isEditable) {
358 stubs.set(window, 'prompt', function() { return url; });
359
360 ROOT.innerHTML = text;
361 HELPER = new goog.testing.editor.TestHelper(ROOT);
362 if (isEditable) {
363 HELPER.setUpEditableElement();
364 FIELDMOCK
365 .execCommand(
366 goog.editor.Command.MODAL_LINK_EDITOR,
367 goog.testing.mockmatchers.isObject)
368 .$returns(undefined);
369 FORMATTER.focusField_ = function() {
370 throw 'Field should not be re-focused';
371 };
372 }
373
374 FIELDMOCK.getElement().$anyTimes().$returns(ROOT);
375
376 FIELDMOCK.setModalMode(true);
377
378 FIELDMOCK.isSelectionEditable().$anyTimes().$returns(isEditable);
379 }
380
381 function tearDownLinkTests() {
382 tearDownHelper();
383 }
384
385 function testLink() {
386 setUpLinkTests('12345', 'http://www.x.com/', true);
387 FIELDMOCK.$replay();
388
389 HELPER.select('12345', 3);
390 FORMATTER.execCommandInternal(goog.editor.Command.LINK);
391 HELPER.assertHtmlMatches(
392 goog.editor.BrowserFeature.GETS_STUCK_IN_LINKS ?
393 '123<a href="http://www.x.com/">http://www.x.com/</a>&nbsp;45' :
394 '123<a href="http://www.x.com/">http://www.x.com/</a>45');
395
396 FIELDMOCK.$verify();
397 tearDownLinkTests();
398 }
399
400 function testLinks() {
401 var url1 = 'http://google.com/1';
402 var url2 = 'http://google.com/2';
403 var dialogUrl = 'http://google.com/3';
404 var html = '<p>' + url1 + '</p><p>' + url2 + '</p>';
405 setUpLinkTests(html, dialogUrl, true);
406 FIELDMOCK.$replay();
407
408 HELPER.select(url1, 0, url2, url2.length);
409 FORMATTER.execCommandInternal(goog.editor.Command.LINK);
410 var expectDialogUrl = false;
411 if (goog.userAgent.IE ||
412 (goog.userAgent.EDGE && !goog.userAgent.product.isVersion(14))) {
413 expectDialogUrl = true;
414 }
415 HELPER.assertHtmlMatches(
416 '<p><a href="' + url1 + '">' + url1 + '</a></p><p>' +
417 '<a href="' + dialogUrl + '">' + (expectDialogUrl ? dialogUrl : url2) +
418 '</a></p>');
419 }
420
421 function testSelectedLink() {
422 setUpLinkTests('12345', 'http://www.x.com/', true);
423 FIELDMOCK.$replay();
424
425 HELPER.select('12345', 1, '12345', 4);
426 FORMATTER.execCommandInternal(goog.editor.Command.LINK);
427 HELPER.assertHtmlMatches(
428 goog.editor.BrowserFeature.GETS_STUCK_IN_LINKS ?
429 '1<a href="http://www.x.com/">234</a>&nbsp;5' :
430 '1<a href="http://www.x.com/">234</a>5');
431
432 FIELDMOCK.$verify();
433 tearDownLinkTests();
434 }
435
436 function testCanceledLink() {
437 setUpLinkTests('12345', undefined, true);
438 FIELDMOCK.$replay();
439
440 HELPER.select('12345', 1, '12345', 4);
441 FORMATTER.execCommandInternal(goog.editor.Command.LINK);
442 HELPER.assertHtmlMatches('12345');
443
444 assertEquals('234', FIELDMOCK.getRange().getText());
445
446 FIELDMOCK.$verify();
447 tearDownLinkTests();
448 }
449
450 function testUnfocusedLink() {
451 FIELDMOCK.$reset();
452 FIELDMOCK.getEditableDomHelper().$anyTimes().$returns(
453 goog.dom.getDomHelper(window.document));
454 setUpLinkTests('12345', undefined, false);
455 FIELDMOCK.getRange().$anyTimes().$returns(null);
456 FIELDMOCK.$replay();
457
458 FORMATTER.execCommandInternal(goog.editor.Command.LINK);
459 HELPER.assertHtmlMatches('12345');
460
461 FIELDMOCK.$verify();
462 tearDownLinkTests();
463 }
464
465 function testCreateLink() {
466 var text = 'some text here';
467 var url = 'http://google.com';
468
469 ROOT.innerHTML = text;
470 HELPER = new goog.testing.editor.TestHelper(ROOT);
471 HELPER.setUpEditableElement();
472 FIELDMOCK.isSelectionEditable().$anyTimes().$returns(true);
473 FIELDMOCK.getElement().$anyTimes().$returns(ROOT);
474 FIELDMOCK.$replay();
475
476 HELPER.select(text, 0, text, text.length);
477 FORMATTER.execCommandInternal(
478 goog.editor.plugins.BasicTextFormatter.COMMAND.CREATE_LINK,
479 FIELDMOCK.getRange(), url);
480 HELPER.assertHtmlMatches('<a href="' + url + '">' + text + '</a>');
481
482 FIELDMOCK.$verify();
483 tearDownLinkTests();
484 }
485
486 function setUpJustifyTests(html) {
487 ROOT.innerHTML = html;
488 HELPER = new goog.testing.editor.TestHelper(ROOT);
489 HELPER.setUpEditableElement(ROOT);
490
491 FIELDMOCK.getElement();
492 FIELDMOCK.$anyTimes();
493 FIELDMOCK.$returns(ROOT);
494
495 FIELDMOCK.getElement();
496 FIELDMOCK.$anyTimes();
497 FIELDMOCK.$returns(ROOT);
498 }
499
500 function tearDownJustifyTests() {
501 tearDownHelper();
502 }
503
504 function testJustify() {
505 setUpJustifyTests('<div>abc</div><p>def</p><div>ghi</div>');
506 FIELDMOCK.$replay();
507
508 HELPER.select('abc', 1, 'def', 1); // Selects 'bcd'.
509 FORMATTER.execCommandInternal(
510 goog.editor.plugins.BasicTextFormatter.COMMAND.JUSTIFY_CENTER);
511 HELPER.assertHtmlMatches(
512 '<div style="text-align: center">abc</div>' +
513 '<p style="text-align: center">def</p>' +
514 '<div>ghi</div>');
515
516 FIELDMOCK.$verify();
517 tearDownJustifyTests();
518 }
519
520 function testJustifyInInline() {
521 setUpJustifyTests('<div>a<i>b</i>c</div><div>d</div>');
522 FIELDMOCK.$replay();
523
524 HELPER.select('b', 0, 'b', 1); // Selects 'b'.
525 FORMATTER.execCommandInternal(
526 goog.editor.plugins.BasicTextFormatter.COMMAND.JUSTIFY_CENTER);
527 HELPER.assertHtmlMatches(
528 '<div style="text-align: center">a<i>b</i>c</div><div>d</div>');
529
530 FIELDMOCK.$verify();
531 tearDownJustifyTests();
532 }
533
534 function testJustifyInBlock() {
535 setUpJustifyTests('<div>a<div>b</div>c</div>');
536 FIELDMOCK.$replay();
537
538 HELPER.select('b', 0, 'b', 1); // Selects 'h'.
539 FORMATTER.execCommandInternal(
540 goog.editor.plugins.BasicTextFormatter.COMMAND.JUSTIFY_CENTER);
541 HELPER.assertHtmlMatches(
542 '<div>a<div style="text-align: center">b</div>c</div>');
543
544 FIELDMOCK.$verify();
545 tearDownJustifyTests();
546 }
547
548 var isFontSizeTest = false;
549 var defaultFontSizeMap;
550
551 function setUpFontSizeTests() {
552 isFontSizeTest = true;
553 ROOT.innerHTML = '1<span style="font-size:2px">23</span>4' +
554 '<span style="font-size:5px; white-space:pre">56</span>7';
555 HELPER = new goog.testing.editor.TestHelper(ROOT);
556 HELPER.setUpEditableElement();
557 FIELDMOCK.getElement().$returns(ROOT).$anyTimes();
558
559 // Map representing the sizes of the text in the HTML snippet used in these
560 // tests. The key is the exact text content of each text node, and the value
561 // is the initial size of the font in pixels. Since tests may cause a text
562 // node to be split in two, this also contains keys that initially don't
563 // match any text node, but may match one later if an existing node is
564 // split. The value for these keys is null, signifying no text node should
565 // exist with that content.
566 defaultFontSizeMap = {
567 '1': 16,
568 '2': null,
569 '23': 2,
570 '3': null,
571 '4': 16,
572 '5': null,
573 '56': 5,
574 '6': null,
575 '7': 16
576 };
577 assertFontSizes('Assertion failed on default font sizes!', {});
578 }
579
580 function tearDownFontSizeTests() {
581 if (isFontSizeTest) {
582 tearDownHelper();
583 isFontSizeTest = false;
584 }
585 }
586
587
588 /**
589 * Asserts that the text nodes set up by setUpFontSizeTests() have had their
590 * font sizes changed as described by sizeChangesMap.
591 * @param {string} msg Assertion error message.
592 * @param {Object<string,?number>} sizeChangesMap Maps the text content
593 * of a text node to be measured to its expected font size in pixels, or
594 * null if that text node should not be present in the document (i.e.
595 * because it was split into two). Only the text nodes that have changed
596 * from their default need to be specified.
597 */
598 function assertFontSizes(msg, sizeChangesMap) {
599 goog.object.extend(defaultFontSizeMap, sizeChangesMap);
600 for (var k in defaultFontSizeMap) {
601 var node = HELPER.findTextNode(k);
602 var expected = defaultFontSizeMap[k];
603 if (expected) {
604 assertNotNull(msg + ' [couldn\'t find text node "' + k + '"]', node);
605 assertEquals(
606 msg + ' [incorrect font size for "' + k + '"]', expected,
607 goog.style.getFontSize(node.parentNode));
608 } else {
609 assertNull(msg + ' [unexpected text node "' + k + '"]', node);
610 }
611 }
612 }
613
614
615 /**
616 * Regression test for {@bug 1286408}. Tests that when you change the font
617 * size of a selection, any font size styles that were nested inside are
618 * removed.
619 */
620 function testFontSizeOverridesStyleAttr() {
621 setUpFontSizeTests();
622 FIELDMOCK.$replay();
623
624 HELPER.select('1', 0, '4', 1); // Selects 1234.
625 FORMATTER.execCommandInternal(
626 goog.editor.plugins.BasicTextFormatter.COMMAND.FONT_SIZE, 6);
627
628 assertFontSizes(
629 'New font size should override existing font size',
630 {'1': 32, '23': 32, '4': 32});
631
632 if (goog.editor.BrowserFeature.DOESNT_OVERRIDE_FONT_SIZE_IN_STYLE_ATTR) {
633 var span = HELPER.findTextNode('23').parentNode;
634 assertFalse(
635 'Style attribute should be gone',
636 span.getAttributeNode('style') != null &&
637 span.getAttributeNode('style').specified);
638 }
639
640 FIELDMOCK.$verify();
641 }
642
643
644 /**
645 * Make sure the style stripping works when the selection starts and stops in
646 * different nodes that both contain font size styles.
647 */
648 function testFontSizeOverridesStyleAttrMultiNode() {
649 setUpFontSizeTests();
650 FIELDMOCK.$replay();
651
652 HELPER.select('23', 0, '56', 2); // Selects 23456.
653 FORMATTER.execCommandInternal(
654 goog.editor.plugins.BasicTextFormatter.COMMAND.FONT_SIZE, 6);
655 var span = HELPER.findTextNode('23').parentNode;
656 var span2 = HELPER.findTextNode('56').parentNode;
657
658 assertFontSizes(
659 'New font size should override existing font size in all spans',
660 {'23': 32, '4': 32, '56': 32});
661 var whiteSpace = goog.userAgent.IE ?
662 goog.style.getCascadedStyle(span2, 'whiteSpace') :
663 goog.style.getComputedStyle(span2, 'whiteSpace');
664 assertEquals(
665 'Whitespace style in last span should have been left', 'pre', whiteSpace);
666
667 if (goog.editor.BrowserFeature.DOESNT_OVERRIDE_FONT_SIZE_IN_STYLE_ATTR) {
668 assertFalse(
669 'Style attribute should be gone from first span',
670 span.getAttributeNode('style') != null &&
671 span.getAttributeNode('style').specified);
672 assertTrue(
673 'Style attribute should not be gone from last span',
674 span2.getAttributeNode('style').specified);
675 }
676
677 FIELDMOCK.$verify();
678 }
679
680
681 /**
682 * Makes sure the font size style is not removed when only a part of the
683 * element with font size style is selected during the font size command.
684 */
685 function testFontSizeDoesntOverrideStyleAttr() {
686 setUpFontSizeTests();
687 FIELDMOCK.$replay();
688
689 HELPER.select('23', 1, '4', 1); // Selects 34 (half of span with font size).
690 FORMATTER.execCommandInternal(
691 goog.editor.plugins.BasicTextFormatter.COMMAND.FONT_SIZE, 6);
692
693 assertFontSizes(
694 'New font size shouldn\'t override existing font size before selection',
695 {'2': 2, '23': null, '3': 32, '4': 32});
696
697 FIELDMOCK.$verify();
698 }
699
700
701 /**
702 * Makes sure the font size style is not removed when only a part of the
703 * element with font size style is selected during the font size command, but
704 * is removed for another element that is fully selected.
705 */
706 function testFontSizeDoesntOverrideStyleAttrMultiNode() {
707 setUpFontSizeTests();
708 FIELDMOCK.$replay();
709
710 HELPER.select('23', 1, '56', 2); // Selects 3456.
711 FORMATTER.execCommandInternal(
712 goog.editor.plugins.BasicTextFormatter.COMMAND.FONT_SIZE, 6);
713
714 assertFontSizes(
715 'New font size shouldn\'t override existing font size before ' +
716 'selection, but still override existing font size in last span',
717 {'2': 2, '23': null, '3': 32, '4': 32, '56': 32});
718
719 FIELDMOCK.$verify();
720 }
721
722
723 /**
724 * Helper to make sure the precondition that executing the font size command
725 * wraps the content in font tags instead of modifying the style attribute is
726 * maintained by the browser even if the selection is already text that is
727 * wrapped in a tag with a font size style. We test this with several
728 * permutations of how the selection looks: selecting the text in the text
729 * node, selecting the whole text node as a unit, or selecting the whole span
730 * node as a unit. Sometimes the browser wraps the text node with the font
731 * tag, sometimes it wraps the span with the font tag. Either one is ok as
732 * long as a font tag is actually being used instead of just modifying the
733 * span's style, because our fix for {@bug 1286408} would remove that style.
734 * @param {function()} doSelect Function to select the "23" text in the test
735 * content.
736 */
737 function doTestFontSizeStyledSpan(doSelect) {
738 // Make sure no new browsers start getting this bad behavior. If they do,
739 // this test will unexpectedly pass.
740 expectedFailures.expectFailureFor(
741 !goog.editor.BrowserFeature.DOESNT_OVERRIDE_FONT_SIZE_IN_STYLE_ATTR);
742
743 try {
744 setUpFontSizeTests();
745 FIELDMOCK.$replay();
746
747 doSelect();
748 FORMATTER.execCommandInternal(
749 goog.editor.plugins.BasicTextFormatter.COMMAND.FONT_SIZE, 7);
750 var parentNode = HELPER.findTextNode('23').parentNode;
751 var grandparentNode = parentNode.parentNode;
752 var fontNode = goog.dom.getElementsByTagNameAndClass(
753 goog.dom.TagName.FONT, undefined, ROOT)[0];
754 var spanNode = goog.dom.getElementsByTagNameAndClass(
755 goog.dom.TagName.SPAN, undefined, ROOT)[0];
756 assertTrue(
757 'A font tag should have been added either outside or inside' +
758 ' the existing span',
759 parentNode == spanNode && grandparentNode == fontNode ||
760 parentNode == fontNode && grandparentNode == spanNode);
761
762 FIELDMOCK.$verify();
763 } catch (e) {
764 expectedFailures.handleException(e);
765 }
766 }
767
768 function testFontSizeStyledSpanSelectingText() {
769 doTestFontSizeStyledSpan(function() { HELPER.select('23', 0, '23', 2); });
770 }
771
772 function testFontSizeStyledSpanSelectingTextNode() {
773 doTestFontSizeStyledSpan(function() {
774 var textNode = HELPER.findTextNode('23');
775 HELPER.select(textNode.parentNode, 0, textNode.parentNode, 1);
776 });
777 }
778
779 function testFontSizeStyledSpanSelectingSpanNode() {
780 doTestFontSizeStyledSpan(function() {
781 var spanNode = HELPER.findTextNode('23').parentNode;
782 HELPER.select(spanNode.parentNode, 1, spanNode.parentNode, 2);
783 });
784 }
785
786 function setUpIframeField(content) {
787 var ifr = document.getElementById('iframe');
788 var body = ifr.contentWindow.document.body;
789 body.innerHTML = content;
790
791 HELPER = new goog.testing.editor.TestHelper(body);
792 HELPER.setUpEditableElement();
793 FIELDMOCK = new goog.testing.editor.FieldMock(ifr.contentWindow);
794 FIELDMOCK.getElement();
795 FIELDMOCK.$anyTimes();
796 FIELDMOCK.$returns(body);
797 FIELDMOCK.queryCommandValue('rtl');
798 FIELDMOCK.$anyTimes();
799 FIELDMOCK.$returns(null);
800 FORMATTER.fieldObject = FIELDMOCK;
801 }
802
803 function tearDownIframeField() {
804 tearDownHelper();
805 }
806
807 function setUpConvertBreaksToDivTests() {
808 ROOT.innerHTML = '<p>paragraph</p>one<br id="br1">two<br><br><br>three';
809 HELPER = new goog.testing.editor.TestHelper(ROOT);
810 HELPER.setUpEditableElement();
811
812 FIELDMOCK.getElement();
813 FIELDMOCK.$anyTimes();
814 FIELDMOCK.$returns(ROOT);
815 }
816
817 function tearDownConvertBreaksToDivTests() {
818 tearDownHelper();
819 }
820
821
822 /** @bug 1414941 */
823 function testConvertBreaksToDivsKeepsP() {
824 if (goog.editor.BrowserFeature.CAN_LISTIFY_BR) {
825 return;
826 }
827 setUpConvertBreaksToDivTests();
828 FIELDMOCK.$replay();
829
830 HELPER.select('three', 0);
831 FORMATTER.convertBreaksToDivs_();
832 assertEquals(
833 'There should still be a <p> tag', 1,
834 goog.dom.getElementsByTagName(goog.dom.TagName.P, FIELDMOCK.getElement())
835 .length);
836 var html = FIELDMOCK.getElement().innerHTML.toLowerCase();
837 assertNotBadBrElements(html);
838 assertNotContains(
839 'There should not be empty <div> elements', '<div><\/div>', html);
840
841 FIELDMOCK.$verify();
842 tearDownConvertBreaksToDivTests();
843 }
844
845
846 /**
847 * @bug 1414937
848 * @bug 934535
849 */
850 function testConvertBreaksToDivsDoesntCollapseBR() {
851 if (goog.editor.BrowserFeature.CAN_LISTIFY_BR) {
852 return;
853 }
854 setUpConvertBreaksToDivTests();
855 FIELDMOCK.$replay();
856
857 HELPER.select('three', 0);
858 FORMATTER.convertBreaksToDivs_();
859 var html = FIELDMOCK.getElement().innerHTML.toLowerCase();
860 assertNotBadBrElements(html);
861 assertNotContains(
862 'There should not be empty <div> elements', '<div><\/div>', html);
863 if (goog.userAgent.IE) {
864 // <div><br></div> misbehaves in IE
865 assertNotContains(
866 '<br> should not be used to prevent <div> from collapsing',
867 '<div><br><\/div>', html);
868 }
869
870 FIELDMOCK.$verify();
871 tearDownConvertBreaksToDivTests();
872 }
873
874 function testConvertBreaksToDivsSelection() {
875 if (goog.editor.BrowserFeature.CAN_LISTIFY_BR) {
876 return;
877 }
878 setUpConvertBreaksToDivTests();
879 FIELDMOCK.$replay();
880
881 HELPER.select('two', 1, 'three', 3);
882 var before = FIELDMOCK.getRange().getText().replace(/\s/g, '');
883 FORMATTER.convertBreaksToDivs_();
884 assertEquals(
885 'Selection must not be changed', before,
886 FIELDMOCK.getRange().getText().replace(/\s/g, ''));
887
888 FIELDMOCK.$verify();
889 tearDownConvertBreaksToDivTests();
890 }
891
892
893 /** @bug 1414937 */
894 function testConvertBreaksToDivsInsertList() {
895 setUpConvertBreaksToDivTests();
896 FIELDMOCK.$replay();
897
898 HELPER.select('three', 0);
899 FORMATTER.execCommandInternal('insertorderedlist');
900 assertTrue(
901 'Ordered list must be inserted',
902 FIELDMOCK.getEditableDomHelper().getDocument().queryCommandState(
903 'insertorderedlist'));
904 tearDownConvertBreaksToDivTests();
905 }
906
907
908 /**
909 * Regression test for {@bug 1939883}, where if a br has an id, it causes
910 * the convert br code to throw a js error. This goes a step further and
911 * ensures that the id is preserved in the resulting div element.
912 */
913 function testConvertBreaksToDivsKeepsId() {
914 if (goog.editor.BrowserFeature.CAN_LISTIFY_BR) {
915 return;
916 }
917 setUpConvertBreaksToDivTests();
918 FIELDMOCK.$replay();
919
920 HELPER.select('one', 0, 'two', 0);
921 FORMATTER.convertBreaksToDivs_();
922 var html = FIELDMOCK.getElement().innerHTML.toLowerCase();
923 assertNotBadBrElements(html);
924 var idBr = document.getElementById('br1');
925 assertNotNull('There should still be a tag with id="br1"', idBr);
926 assertEquals('The tag with id="br1" should be a <div> now',
927 String(goog.dom.TagName.DIV), idBr.tagName);
928 assertNull(
929 'There should not be any tag with id="temp_br"',
930 document.getElementById('temp_br'));
931
932 FIELDMOCK.$verify();
933 tearDownConvertBreaksToDivTests();
934 }
935
936
937 /**
938 * @bug 2420054
939 */
940 var JUSTIFICATION_COMMANDS = [
941 goog.editor.plugins.BasicTextFormatter.COMMAND.JUSTIFY_LEFT,
942 goog.editor.plugins.BasicTextFormatter.COMMAND.JUSTIFY_RIGHT,
943 goog.editor.plugins.BasicTextFormatter.COMMAND.JUSTIFY_CENTER,
944 goog.editor.plugins.BasicTextFormatter.COMMAND.JUSTIFY_FULL
945 ];
946 function doTestIsJustification(command) {
947 setUpRealField();
948 REAL_FIELD.setSafeHtml(false, goog.html.SafeHtml.htmlEscape('foo'));
949 selectRealField();
950 REAL_FIELD.execCommand(command);
951
952 for (var i = 0; i < JUSTIFICATION_COMMANDS.length; i++) {
953 if (JUSTIFICATION_COMMANDS[i] == command) {
954 assertTrue(
955 'queryCommandValue(' + JUSTIFICATION_COMMANDS[i] +
956 ') should be true after execCommand(' + command + ')',
957 REAL_FIELD.queryCommandValue(JUSTIFICATION_COMMANDS[i]));
958 } else {
959 assertFalse(
960 'queryCommandValue(' + JUSTIFICATION_COMMANDS[i] +
961 ') should be false after execCommand(' + command + ')',
962 REAL_FIELD.queryCommandValue(JUSTIFICATION_COMMANDS[i]));
963 }
964 }
965 }
966 function testIsJustificationLeft() {
967 doTestIsJustification(
968 goog.editor.plugins.BasicTextFormatter.COMMAND.JUSTIFY_LEFT);
969 }
970 function testIsJustificationRight() {
971 doTestIsJustification(
972 goog.editor.plugins.BasicTextFormatter.COMMAND.JUSTIFY_RIGHT);
973 }
974 function testIsJustificationCenter() {
975 doTestIsJustification(
976 goog.editor.plugins.BasicTextFormatter.COMMAND.JUSTIFY_CENTER);
977 }
978 function testIsJustificationFull() {
979 doTestIsJustification(
980 goog.editor.plugins.BasicTextFormatter.COMMAND.JUSTIFY_FULL);
981 }
982
983
984 /**
985 * Regression test for {@bug 1414813}, where all 3 justification buttons are
986 * considered "on" when you first tab into the editable field. In this
987 * situation, when lorem ipsum is the only node in the editable field iframe
988 * body, mockField.getRange() returns an empty range.
989 */
990 function testIsJustificationEmptySelection() {
991 var mockField = new goog.testing.LooseMock(goog.editor.Field);
992
993 mockField.getRange();
994 mockField.$anyTimes();
995 mockField.$returns(null);
996 mockField.getPluginByClassId('Bidi');
997 mockField.$anyTimes();
998 mockField.$returns(null);
999 FORMATTER.fieldObject = mockField;
1000
1001 mockField.$replay();
1002
1003 assertFalse(
1004 'Empty selection should not be justified',
1005 FORMATTER.isJustification_(
1006 goog.editor.plugins.BasicTextFormatter.COMMAND.JUSTIFY_CENTER));
1007 assertFalse(
1008 'Empty selection should not be justified',
1009 FORMATTER.isJustification_(
1010 goog.editor.plugins.BasicTextFormatter.COMMAND.JUSTIFY_FULL));
1011 assertFalse(
1012 'Empty selection should not be justified',
1013 FORMATTER.isJustification_(
1014 goog.editor.plugins.BasicTextFormatter.COMMAND.JUSTIFY_RIGHT));
1015 assertFalse(
1016 'Empty selection should not be justified',
1017 FORMATTER.isJustification_(
1018 goog.editor.plugins.BasicTextFormatter.COMMAND.JUSTIFY_LEFT));
1019
1020 mockField.$verify();
1021 }
1022
1023 function testIsJustificationSimple1() {
1024 setUpRealField();
1025 REAL_FIELD.setSafeHtml(
1026 false, goog.html.SafeHtml.create('div', {'align': 'right'}, 'foo'));
1027 selectRealField();
1028
1029 assertFalse(
1030 REAL_FIELD.queryCommandValue(
1031 goog.editor.plugins.BasicTextFormatter.COMMAND.JUSTIFY_LEFT));
1032 assertTrue(
1033 REAL_FIELD.queryCommandValue(
1034 goog.editor.plugins.BasicTextFormatter.COMMAND.JUSTIFY_RIGHT));
1035 }
1036
1037 function testIsJustificationSimple2() {
1038 setUpRealField();
1039 REAL_FIELD.setSafeHtml(
1040 false,
1041 goog.html.SafeHtml.create(
1042 'div', {'style': {'text-align': 'right'}}, 'foo'));
1043 selectRealField();
1044
1045 assertFalse(
1046 REAL_FIELD.queryCommandValue(
1047 goog.editor.plugins.BasicTextFormatter.COMMAND.JUSTIFY_LEFT));
1048 assertTrue(
1049 REAL_FIELD.queryCommandValue(
1050 goog.editor.plugins.BasicTextFormatter.COMMAND.JUSTIFY_RIGHT));
1051 }
1052
1053 function testIsJustificationComplete1() {
1054 setUpRealField();
1055 REAL_FIELD.setSafeHtml(
1056 false,
1057 goog.html.SafeHtml.concat(
1058 goog.html.SafeHtml.create('div', {'align': 'left'}, 'a'),
1059 goog.html.SafeHtml.create('div', {'align': 'right'}, 'b')));
1060 selectRealField();
1061
1062 assertFalse(
1063 REAL_FIELD.queryCommandValue(
1064 goog.editor.plugins.BasicTextFormatter.COMMAND.JUSTIFY_LEFT));
1065 assertFalse(
1066 REAL_FIELD.queryCommandValue(
1067 goog.editor.plugins.BasicTextFormatter.COMMAND.JUSTIFY_RIGHT));
1068 }
1069
1070 function testIsJustificationComplete2() {
1071 setUpRealField();
1072 REAL_FIELD.setSafeHtml(
1073 false,
1074 goog.html.SafeHtml.concat(
1075 goog.html.SafeHtml.create('div', {'align': 'left'}, 'a'),
1076 goog.html.SafeHtml.create('div', {'align': 'left'}, 'b')));
1077 selectRealField();
1078
1079 assertTrue(
1080 REAL_FIELD.queryCommandValue(
1081 goog.editor.plugins.BasicTextFormatter.COMMAND.JUSTIFY_LEFT));
1082 assertFalse(
1083 REAL_FIELD.queryCommandValue(
1084 goog.editor.plugins.BasicTextFormatter.COMMAND.JUSTIFY_RIGHT));
1085 }
1086
1087 function testIsJustificationComplete3() {
1088 setUpRealField();
1089 REAL_FIELD.setSafeHtml(
1090 false,
1091 goog.html.SafeHtml.concat(
1092 goog.html.SafeHtml.create('div', {'align': 'right'}, 'a'),
1093 goog.html.SafeHtml.create('div', {'align': 'right'}, 'b')));
1094 selectRealField();
1095
1096 assertFalse(
1097 REAL_FIELD.queryCommandValue(
1098 goog.editor.plugins.BasicTextFormatter.COMMAND.JUSTIFY_LEFT));
1099 assertTrue(
1100 REAL_FIELD.queryCommandValue(
1101 goog.editor.plugins.BasicTextFormatter.COMMAND.JUSTIFY_RIGHT));
1102 }
1103
1104 function testIsJustificationComplete4() {
1105 setUpRealField();
1106 REAL_FIELD.setSafeHtml(
1107 false,
1108 goog.html.SafeHtml.concat(
1109 goog.html.SafeHtml.create(
1110 'div', {'align': 'right'},
1111 goog.html.SafeHtml.create('div', {'align': 'left'}, 'a')),
1112 goog.html.SafeHtml.create('div', {'align': 'right'}, 'b')));
1113 selectRealField();
1114
1115 assertFalse(
1116 REAL_FIELD.queryCommandValue(
1117 goog.editor.plugins.BasicTextFormatter.COMMAND.JUSTIFY_LEFT));
1118 assertTrue(
1119 REAL_FIELD.queryCommandValue(
1120 goog.editor.plugins.BasicTextFormatter.COMMAND.JUSTIFY_RIGHT));
1121 }
1122
1123 function testIsJustificationComplete5() {
1124 setUpRealField();
1125 REAL_FIELD.setSafeHtml(
1126 false,
1127 goog.html.SafeHtml.concat(
1128 goog.html.SafeHtml.create('div', {'align': 'right'}, 'a'), 'b',
1129 goog.html.SafeHtml.create('div', {'align': 'right'}, 'c')));
1130 selectRealField();
1131
1132 assertFalse(
1133 REAL_FIELD.queryCommandValue(
1134 goog.editor.plugins.BasicTextFormatter.COMMAND.JUSTIFY_LEFT));
1135 assertFalse(
1136 REAL_FIELD.queryCommandValue(
1137 goog.editor.plugins.BasicTextFormatter.COMMAND.JUSTIFY_RIGHT));
1138 }
1139
1140
1141 /** @bug 2472589 */
1142 function doTestIsJustificationPInDiv(useCss, align, command) {
1143 setUpRealField();
1144 var attrs = {};
1145 if (useCss) {
1146 attrs['style'] = {'text-align': align};
1147 } else {
1148 attrs['align'] = align;
1149 }
1150 var html = goog.html.SafeHtml.create(
1151 'div', attrs, goog.html.SafeHtml.create('p', {}, 'foo'));
1152
1153 REAL_FIELD.setSafeHtml(false, html);
1154 selectRealField();
1155 assertTrue(
1156 'P inside ' + align + ' aligned' + (useCss ? ' (using CSS)' : '') +
1157 ' DIV should be considered ' + align + ' aligned',
1158 REAL_FIELD.queryCommandValue(command));
1159 }
1160 function testIsJustificationPInDivLeft() {
1161 doTestIsJustificationPInDiv(
1162 false, 'left',
1163 goog.editor.plugins.BasicTextFormatter.COMMAND.JUSTIFY_LEFT);
1164 }
1165 function testIsJustificationPInDivRight() {
1166 doTestIsJustificationPInDiv(
1167 false, 'right',
1168 goog.editor.plugins.BasicTextFormatter.COMMAND.JUSTIFY_RIGHT);
1169 }
1170 function testIsJustificationPInDivCenter() {
1171 doTestIsJustificationPInDiv(
1172 false, 'center',
1173 goog.editor.plugins.BasicTextFormatter.COMMAND.JUSTIFY_CENTER);
1174 }
1175 function testIsJustificationPInDivJustify() {
1176 doTestIsJustificationPInDiv(
1177 false, 'justify',
1178 goog.editor.plugins.BasicTextFormatter.COMMAND.JUSTIFY_FULL);
1179 }
1180 function testIsJustificationPInDivLeftCss() {
1181 doTestIsJustificationPInDiv(
1182 true, 'left',
1183 goog.editor.plugins.BasicTextFormatter.COMMAND.JUSTIFY_LEFT);
1184 }
1185 function testIsJustificationPInDivRightCss() {
1186 doTestIsJustificationPInDiv(
1187 true, 'right',
1188 goog.editor.plugins.BasicTextFormatter.COMMAND.JUSTIFY_RIGHT);
1189 }
1190 function testIsJustificationPInDivCenterCss() {
1191 doTestIsJustificationPInDiv(
1192 true, 'center',
1193 goog.editor.plugins.BasicTextFormatter.COMMAND.JUSTIFY_CENTER);
1194 }
1195 function testIsJustificationPInDivJustifyCss() {
1196 doTestIsJustificationPInDiv(
1197 true, 'justify',
1198 goog.editor.plugins.BasicTextFormatter.COMMAND.JUSTIFY_FULL);
1199 }
1200
1201
1202 function testPrepareContent() {
1203 setUpRealField();
1204 assertPreparedContents('\n', '\n');
1205
1206 if (goog.editor.BrowserFeature.COLLAPSES_EMPTY_NODES) {
1207 assertPreparedContents(
1208 "&nbsp;<script>alert('hi')<" + '/script>',
1209 "<script>alert('hi')<" + '/script>');
1210 } else {
1211 assertNotPreparedContents("<script>alert('hi')<" + '/script>');
1212 }
1213
1214 if (goog.editor.BrowserFeature.CONVERT_TO_B_AND_I_TAGS) {
1215 assertPreparedContents(
1216 '<b id=\'foo\'>hi</b>', '<strong id=\'foo\'>hi</strong>');
1217 assertPreparedContents('<i>hi</i>', '<em>hi</em>');
1218 assertNotPreparedContents('<embed>');
1219 } else {
1220 assertNotPreparedContents('<em>hi</em>');
1221 assertNotPreparedContents('<strong id=\'foo\'>hi</strong>');
1222 }
1223 }
1224
1225 function testScrubImagesRemovesCustomAttributes() {
1226 var fieldElem = goog.dom.getElement('real-field');
1227 goog.dom.removeChildren(fieldElem);
1228 var attrs = {
1229 'src': 'http://www.google.com/foo.jpg',
1230 'tabIndex': '0',
1231 'tabIndexSet': '0'
1232 };
1233 attrs[goog.HASH_CODE_PROPERTY_] = '0';
1234 goog.dom.appendChild(
1235 fieldElem, goog.dom.createDom(goog.dom.TagName.IMG, attrs));
1236
1237 setUpRealField();
1238
1239 var html = REAL_FIELD.getCleanContents();
1240 assert(
1241 'Images must not have forbidden attributes: ' + html,
1242 -1 == html.indexOf('tabIndex') && -1 == html.indexOf('closure'));
1243 assert(
1244 'Image URLs must not be made relative by default: ' + html,
1245 -1 != html.indexOf('/foo.jpg'));
1246 }
1247
1248 function testGeckoSelectionChange() {
1249 if (!goog.userAgent.GECKO || !goog.userAgent.isVersionOrHigher('1.9')) {
1250 return;
1251 }
1252
1253 setUpRealFieldIframe();
1254 // Use native selection for this test because goog.dom.Range will
1255 // change selections of <br>
1256 var ifr = document.getElementById('iframe');
1257 ifr.contentWindow.document.body.innerHTML = 'hello<br id="br1"><br id="br2">';
1258 var body = ifr.contentWindow.document.body;
1259 var range = REAL_FIELD.getRange();
1260 var browserRange = range.getBrowserRangeObject();
1261 browserRange.setStart(body, 2);
1262 browserRange.setEnd(body, 2);
1263 FORMATTER.applyExecCommandGeckoFixes_('formatblock');
1264 var updatedRange = REAL_FIELD.getRange().getBrowserRangeObject();
1265 assertEquals(
1266 'Range should have been updated to deep range', 'br2',
1267 updatedRange.startContainer.id);
1268 assertEquals(
1269 'Range should have been updated to deep range', 0,
1270 updatedRange.startOffset);
1271 }
1272
1273 function testIEExecCommandFixes() {
1274 if (!goog.userAgent.IE) {
1275 return;
1276 }
1277
1278 setUpRealField();
1279 REAL_FIELD.setSafeHtml(
1280 false, goog.html.SafeHtml.create('blockquote', {}, 'hi'));
1281 goog.dom.Range.createFromNodeContents(REAL_FIELD.getElement()).select();
1282
1283 var nodes = REAL_PLUGIN.applyExecCommandIEFixes_('insertOrderedList');
1284 if (goog.userAgent.isVersionOrHigher('9')) {
1285 assertHTMLEquals(
1286 '<blockquote>hi<div style="height:0px"></div></blockquote>',
1287 REAL_FIELD.getCleanContents());
1288 } else {
1289 assertHTMLEquals(
1290 '<blockquote>hi <div style="height:0px"></div></blockquote>',
1291 REAL_FIELD.getCleanContents());
1292 }
1293 }
1294
1295
1296 /**
1297 * Assert that the prepared contents matches the expected.
1298 */
1299 function assertPreparedContents(expected, original) {
1300 assertEquals(
1301 expected, REAL_FIELD.reduceOp_(
1302 goog.editor.Plugin.Op.PREPARE_CONTENTS_HTML, original));
1303 }
1304
1305
1306 /**
1307 * Assert that sanitization doesn't affect the given text.
1308 */
1309 function assertNotPreparedContents(text) {
1310 assertPreparedContents(text, text);
1311 }
1312
1313
1314 /**
1315 * Assert that only BR elements expected to persist after convertBreaksToDivs_
1316 * are in the HTML.
1317 */
1318 function assertNotBadBrElements(html) {
1319 if (goog.userAgent.IE) {
1320 assertNotContains('There should not be <br> elements', '<br', html);
1321 } else {
1322 assertFalse(
1323 'There should not be <br> elements, except ones to prevent ' +
1324 '<div>s from collapsing' + html,
1325 /(?!<div>)<br>(?!<\/div>)/.test(html));
1326 }
1327 }