Debian Codegraph @

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

raw
1 // Copyright 2010 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.ui.editor.plugins.LinkDialogTest');
16 goog.setTestOnly('goog.ui.editor.plugins.LinkDialogTest');
17
18 goog.require('goog.dom');
19 goog.require('goog.dom.DomHelper');
20 goog.require('goog.dom.NodeType');
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.Link');
26 goog.require('goog.editor.plugins.LinkDialogPlugin');
27 goog.require('goog.html.SafeHtml');
28 goog.require('goog.string');
29 goog.require('goog.string.Unicode');
30 goog.require('goog.testing.MockControl');
31 goog.require('goog.testing.editor.FieldMock');
32 goog.require('goog.testing.editor.TestHelper');
33 goog.require('goog.testing.editor.dom');
34 goog.require('goog.testing.events');
35 goog.require('goog.testing.jsunit');
36 goog.require('goog.testing.mockmatchers');
37 goog.require('goog.ui.editor.AbstractDialog');
38 goog.require('goog.ui.editor.LinkDialog');
39 goog.require('goog.userAgent');
40
41 var plugin;
42 var anchorElem;
43 var extraAnchors;
44 var isNew;
45 var testDiv;
46
47 var mockCtrl;
48 var mockField;
49 var mockLink;
50 var mockAlert;
51
52 var OLD_LINK_TEXT = 'old text';
53 var OLD_LINK_URL = 'http://old.url/';
54 var NEW_LINK_TEXT = 'My Link Text';
55 var NEW_LINK_URL = 'http://my.link/url/';
56
57 var fieldElem;
58 var fieldObj;
59 var linkObj;
60
61 function setUp() {
62 testDiv = goog.dom.getDocument().getElementById('test');
63 goog.dom.setTextContent(testDiv, 'Some preceding text');
64
65 anchorElem = goog.dom.createElement(goog.dom.TagName.A);
66 anchorElem.href = 'http://www.google.com/';
67 goog.dom.setTextContent(anchorElem, 'anchor text');
68 goog.dom.appendChild(testDiv, anchorElem);
69 extraAnchors = [];
70
71 mockCtrl = new goog.testing.MockControl();
72 mockField = new goog.testing.editor.FieldMock();
73 mockCtrl.addMock(mockField);
74 mockLink = mockCtrl.createLooseMock(goog.editor.Link);
75 mockAlert = mockCtrl.createGlobalFunctionMock('alert');
76
77 isNew = false;
78 mockLink.isNew().$anyTimes().$does(function() { return isNew; });
79 mockLink
80 .setTextAndUrl(
81 goog.testing.mockmatchers.isString,
82 goog.testing.mockmatchers.isString)
83 .$anyTimes()
84 .$does(function(text, url) {
85 anchorElem.innerHTML = text;
86 anchorElem.href = url;
87 });
88 mockLink.getAnchor().$anyTimes().$returns(anchorElem);
89 mockLink.getExtraAnchors().$anyTimes().$returns(extraAnchors);
90 }
91
92 function tearDown() {
93 plugin.dispose();
94 tearDownRealEditableField();
95 goog.dom.removeChildren(testDiv);
96 mockCtrl.$tearDown();
97 }
98
99 function setUpAnchor(text, href, opt_isNew, opt_target, opt_rel) {
100 setUpGivenAnchor(anchorElem, text, href, opt_isNew, opt_target, opt_rel);
101 }
102
103 function setUpGivenAnchor(anchor, text, href, opt_isNew, opt_target, opt_rel) {
104 anchor.innerHTML = text;
105 anchor.href = href;
106 isNew = !!opt_isNew;
107 if (opt_target) {
108 anchor.target = opt_target;
109 }
110 if (opt_rel) {
111 anchor.rel = opt_rel;
112 }
113 }
114
115
116 /**
117 * Tests that the plugin's dialog is properly created.
118 */
119 function testCreateDialog() {
120 // Note: this tests simply creating the dialog because that's the only
121 // functionality added to this class. Opening or closing effects (editing
122 // the actual link) is tested in linkdialog_test.html, but should be moved
123 // here if that functionality gets refactored from the dialog to the plugin.
124 mockCtrl.$replayAll();
125
126 plugin = new goog.editor.plugins.LinkDialogPlugin();
127 plugin.registerFieldObject(mockField);
128
129 var dialog = plugin.createDialog(new goog.dom.DomHelper(), mockLink);
130 assertTrue(
131 'Dialog should be of type goog.ui.editor.LinkDialog',
132 dialog instanceof goog.ui.editor.LinkDialog);
133
134 mockCtrl.$verifyAll();
135 }
136
137
138 /**
139 * Tests that when the OK event fires the link is properly updated.
140 */
141 function testOk() {
142 mockLink.placeCursorRightOf();
143 mockField.dispatchSelectionChangeEvent();
144 mockField.dispatchChange();
145 mockField.focus();
146 mockCtrl.$replayAll();
147
148 setUpAnchor(OLD_LINK_TEXT, OLD_LINK_URL);
149 plugin = new goog.editor.plugins.LinkDialogPlugin();
150 plugin.registerFieldObject(mockField);
151 var dialog = plugin.createDialog(new goog.dom.DomHelper(), mockLink);
152
153 // Mock of execCommand + clicking OK without actually opening the dialog.
154 plugin.currentLink_ = mockLink;
155 dialog.dispatchEvent(
156 new goog.ui.editor.LinkDialog.OkEvent(NEW_LINK_TEXT, NEW_LINK_URL));
157
158 assertEquals('Display text incorrect', NEW_LINK_TEXT, anchorElem.innerHTML);
159 assertEquals('Anchor element href incorrect', NEW_LINK_URL, anchorElem.href);
160
161 mockCtrl.$verifyAll();
162 }
163
164
165 /**
166 * Tests that when the Cancel event fires the link is unchanged.
167 */
168 function testCancel() {
169 mockCtrl.$replayAll();
170
171 setUpAnchor(OLD_LINK_TEXT, OLD_LINK_URL);
172 plugin = new goog.editor.plugins.LinkDialogPlugin();
173 plugin.registerFieldObject(mockField);
174 var dialog = plugin.createDialog(new goog.dom.DomHelper(), mockLink);
175
176 // Mock of execCommand + cancel without actually opening the dialog.
177 plugin.currentLink_ = mockLink;
178 dialog.dispatchEvent(goog.ui.editor.AbstractDialog.EventType.CANCEL);
179
180 assertEquals(
181 'Display text should not be changed', OLD_LINK_TEXT,
182 anchorElem.innerHTML);
183 assertEquals(
184 'Anchor element href should not be changed', OLD_LINK_URL,
185 anchorElem.href);
186
187 mockCtrl.$verifyAll();
188 }
189
190
191 /**
192 * Tests that when the Cancel event fires for a new link it gets removed.
193 */
194 function testCancelNew() {
195 mockField.dispatchChange(); // Should be fired because link was removed.
196 mockCtrl.$replayAll();
197
198 setUpAnchor(OLD_LINK_TEXT, OLD_LINK_URL, true);
199 var prevSib = anchorElem.previousSibling;
200 plugin = new goog.editor.plugins.LinkDialogPlugin();
201 plugin.registerFieldObject(mockField);
202 var dialog = plugin.createDialog(new goog.dom.DomHelper(), mockLink);
203
204 // Mock of execCommand + cancel without actually opening the dialog.
205 plugin.currentLink_ = mockLink;
206 dialog.dispatchEvent(goog.ui.editor.AbstractDialog.EventType.CANCEL);
207
208 assertNotEquals(
209 'Anchor element should be removed from document body', testDiv,
210 anchorElem.parentNode);
211 var newElem = prevSib.nextSibling;
212 assertEquals(
213 'Link should be replaced by text node', goog.dom.NodeType.TEXT,
214 newElem.nodeType);
215 assertEquals(
216 'Original text should be left behind', OLD_LINK_TEXT, newElem.nodeValue);
217
218 mockCtrl.$verifyAll();
219 }
220
221
222 /**
223 * Tests that when the Cancel event fires for a new link it gets removed.
224 */
225 function testCancelNewMultiple() {
226 mockField.dispatchChange(); // Should be fired because link was removed.
227 mockCtrl.$replayAll();
228
229 var anchorElem1 = anchorElem;
230 var parent1 = goog.dom.createDom(goog.dom.TagName.DIV, null, anchorElem1);
231 goog.dom.appendChild(testDiv, parent1);
232 setUpGivenAnchor(anchorElem1, OLD_LINK_TEXT + '1', OLD_LINK_URL + '1', true);
233
234 anchorElem2 = goog.dom.createDom(goog.dom.TagName.A);
235 var parent2 = goog.dom.createDom(goog.dom.TagName.DIV, null, anchorElem2);
236 goog.dom.appendChild(testDiv, parent2);
237 setUpGivenAnchor(anchorElem2, OLD_LINK_TEXT + '2', OLD_LINK_URL + '2', true);
238 extraAnchors.push(anchorElem2);
239
240 anchorElem3 = goog.dom.createDom(goog.dom.TagName.A);
241 var parent3 = goog.dom.createDom(goog.dom.TagName.DIV, null, anchorElem3);
242 goog.dom.appendChild(testDiv, parent3);
243 setUpGivenAnchor(anchorElem3, OLD_LINK_TEXT + '3', OLD_LINK_URL + '3', true);
244 extraAnchors.push(anchorElem3);
245
246 plugin = new goog.editor.plugins.LinkDialogPlugin();
247 plugin.registerFieldObject(mockField);
248 var dialog = plugin.createDialog(new goog.dom.DomHelper(), mockLink);
249
250 // Mock of execCommand + cancel without actually opening the dialog.
251 plugin.currentLink_ = mockLink;
252 dialog.dispatchEvent(goog.ui.editor.AbstractDialog.EventType.CANCEL);
253
254 assertNotEquals(
255 'Anchor 1 element should be removed from document body', parent1,
256 anchorElem1.parentNode);
257 assertNotEquals(
258 'Anchor 2 element should be removed from document body', parent2,
259 anchorElem2.parentNode);
260 assertNotEquals(
261 'Anchor 3 element should be removed from document body', parent3,
262 anchorElem3.parentNode);
263
264 assertEquals(
265 'Link 1 should be replaced by text node', goog.dom.NodeType.TEXT,
266 parent1.firstChild.nodeType);
267 assertEquals(
268 'Link 2 should be replaced by text node', goog.dom.NodeType.TEXT,
269 parent2.firstChild.nodeType);
270 assertEquals(
271 'Link 3 should be replaced by text node', goog.dom.NodeType.TEXT,
272 parent3.firstChild.nodeType);
273
274 assertEquals(
275 'Original text 1 should be left behind', OLD_LINK_TEXT + '1',
276 parent1.firstChild.nodeValue);
277 assertEquals(
278 'Original text 2 should be left behind', OLD_LINK_TEXT + '2',
279 parent2.firstChild.nodeValue);
280 assertEquals(
281 'Original text 3 should be left behind', OLD_LINK_TEXT + '3',
282 parent3.firstChild.nodeValue);
283
284 mockCtrl.$verifyAll();
285 }
286
287
288 /**
289 * Tests that when the Cancel event fires for a new link it gets removed.
290 */
291 function testOkNewMultiple() {
292 mockLink.placeCursorRightOf();
293 mockField.dispatchSelectionChangeEvent();
294 mockField.dispatchChange();
295 mockField.focus();
296 mockCtrl.$replayAll();
297
298 var anchorElem1 = anchorElem;
299 setUpGivenAnchor(anchorElem1, OLD_LINK_TEXT + '1', OLD_LINK_URL + '1', true);
300
301 anchorElem2 = goog.dom.createElement(goog.dom.TagName.A);
302 goog.dom.appendChild(testDiv, anchorElem2);
303 setUpGivenAnchor(anchorElem2, OLD_LINK_TEXT + '2', OLD_LINK_URL + '2', true);
304 extraAnchors.push(anchorElem2);
305
306 anchorElem3 = goog.dom.createElement(goog.dom.TagName.A);
307 goog.dom.appendChild(testDiv, anchorElem3);
308 setUpGivenAnchor(anchorElem3, OLD_LINK_TEXT + '3', OLD_LINK_URL + '3', true);
309 extraAnchors.push(anchorElem3);
310
311 var prevSib = anchorElem1.previousSibling;
312 plugin = new goog.editor.plugins.LinkDialogPlugin();
313 plugin.registerFieldObject(mockField);
314 var dialog = plugin.createDialog(new goog.dom.DomHelper(), mockLink);
315
316 // Mock of execCommand + clicking OK without actually opening the dialog.
317 plugin.currentLink_ = mockLink;
318 dialog.dispatchEvent(
319 new goog.ui.editor.LinkDialog.OkEvent(NEW_LINK_TEXT, NEW_LINK_URL));
320
321 assertEquals(
322 'Display text 1 must update', NEW_LINK_TEXT, anchorElem1.innerHTML);
323 assertEquals(
324 'Display text 2 must not update', OLD_LINK_TEXT + '2',
325 anchorElem2.innerHTML);
326 assertEquals(
327 'Display text 3 must not update', OLD_LINK_TEXT + '3',
328 anchorElem3.innerHTML);
329
330 assertEquals(
331 'Anchor element 1 href must update', NEW_LINK_URL, anchorElem1.href);
332 assertEquals(
333 'Anchor element 2 href must update', NEW_LINK_URL, anchorElem2.href);
334 assertEquals(
335 'Anchor element 3 href must update', NEW_LINK_URL, anchorElem3.href);
336
337 mockCtrl.$verifyAll();
338 }
339
340
341 /**
342 * Tests the anchor's target is correctly modified with the "open in new
343 * window" feature on.
344 */
345 function testOkOpenInNewWindow() {
346 mockLink.placeCursorRightOf().$anyTimes();
347 mockField.dispatchSelectionChangeEvent().$anyTimes();
348 mockField.dispatchChange().$anyTimes();
349 mockField.focus().$anyTimes();
350 mockCtrl.$replayAll();
351
352 plugin = new goog.editor.plugins.LinkDialogPlugin();
353 plugin.registerFieldObject(mockField);
354 plugin.showOpenLinkInNewWindow(false);
355 plugin.currentLink_ = mockLink;
356
357 // Edit a link that doesn't open in a new window and leave it as such.
358 setUpAnchor(OLD_LINK_TEXT, OLD_LINK_URL);
359 var dialog = plugin.createDialog(new goog.dom.DomHelper(), mockLink);
360 dialog.dispatchEvent(
361 new goog.ui.editor.LinkDialog.OkEvent(
362 NEW_LINK_TEXT, NEW_LINK_URL, false, false));
363 assertEquals(
364 'Target should not be set for link that doesn\'t open in new window', '',
365 anchorElem.target);
366 assertFalse(
367 'Checked state should stay false',
368 plugin.getOpenLinkInNewWindowCheckedState());
369
370 // Edit a link that doesn't open in a new window and toggle it on.
371 setUpAnchor(OLD_LINK_TEXT, OLD_LINK_URL);
372 dialog = plugin.createDialog(new goog.dom.DomHelper(), mockLink);
373 dialog.dispatchEvent(
374 new goog.ui.editor.LinkDialog.OkEvent(NEW_LINK_TEXT, NEW_LINK_URL, true));
375 assertEquals(
376 'Target should be set to _blank for link that opens in new window',
377 '_blank', anchorElem.target);
378 assertTrue(
379 'Checked state should be true after toggling a link on',
380 plugin.getOpenLinkInNewWindowCheckedState());
381
382 // Edit a link that doesn't open in a named window and don't touch it.
383 setUpAnchor(OLD_LINK_TEXT, OLD_LINK_URL, false, 'named');
384 dialog = plugin.createDialog(new goog.dom.DomHelper(), mockLink);
385 dialog.dispatchEvent(
386 new goog.ui.editor.LinkDialog.OkEvent(
387 NEW_LINK_TEXT, NEW_LINK_URL, false));
388 assertEquals(
389 'Target should keep its original value', 'named', anchorElem.target);
390 assertFalse(
391 'Checked state should be false again',
392 plugin.getOpenLinkInNewWindowCheckedState());
393
394 // Edit a link that opens in a new window and toggle it off.
395 setUpAnchor(OLD_LINK_TEXT, OLD_LINK_URL, false, '_blank');
396 dialog = plugin.createDialog(new goog.dom.DomHelper(), mockLink);
397 dialog.dispatchEvent(
398 new goog.ui.editor.LinkDialog.OkEvent(
399 NEW_LINK_TEXT, NEW_LINK_URL, false));
400 assertEquals(
401 'Target should not be set for link that doesn\'t open in new window', '',
402 anchorElem.target);
403
404 mockCtrl.$verifyAll();
405 }
406
407 function testOkNoFollowEnabled() {
408 verifyRelNoFollow(true, null, 'nofollow');
409 }
410
411 function testOkNoFollowInUppercase() {
412 verifyRelNoFollow(true, 'NOFOLLOW', 'NOFOLLOW');
413 }
414
415 function testOkNoFollowEnabledHasMoreRelValues() {
416 verifyRelNoFollow(true, 'author', 'author nofollow');
417 }
418
419 function testOkNoFollowDisabled() {
420 verifyRelNoFollow(false, null, '');
421 }
422
423 function testOkNoFollowDisabledHasMoreRelValues() {
424 verifyRelNoFollow(false, 'author', 'author');
425 }
426
427 function testOkNoFollowDisabledHasMoreRelValues() {
428 verifyRelNoFollow(false, 'author nofollow', 'author ');
429 }
430
431 function testOkNoFollowInUppercaseWithMoreValues() {
432 verifyRelNoFollow(true, 'NOFOLLOW author', 'NOFOLLOW author');
433 }
434
435 function verifyRelNoFollow(noFollow, originalRel, expectedRel) {
436 mockLink.placeCursorRightOf();
437 mockField.dispatchSelectionChangeEvent();
438 mockField.dispatchChange();
439 mockField.focus();
440 mockCtrl.$replayAll();
441
442 plugin = new goog.editor.plugins.LinkDialogPlugin();
443 plugin.registerFieldObject(mockField);
444 plugin.showRelNoFollow();
445 plugin.currentLink_ = mockLink;
446
447 setUpAnchor(OLD_LINK_TEXT, OLD_LINK_URL, true, null, originalRel);
448 var dialog = plugin.createDialog(new goog.dom.DomHelper(), mockLink);
449 dialog.dispatchEvent(
450 new goog.ui.editor.LinkDialog.OkEvent(
451 NEW_LINK_TEXT, NEW_LINK_URL, false, noFollow));
452 assertEquals(expectedRel, anchorElem.rel);
453
454 mockCtrl.$verifyAll();
455 }
456
457
458 /**
459 * Tests that the selection is cleared when the dialog opens and is
460 * correctly restored after cancel is clicked.
461 */
462 function testRestoreSelectionOnOk() {
463 setUpAnchor('12345', '/');
464 setUpRealEditableField();
465
466 var elem = fieldObj.getElement();
467 var helper = new goog.testing.editor.TestHelper(elem);
468 helper.select('12345', 1, '12345', 4); // Selects '234'.
469
470 assertEquals(
471 'Incorrect text selected before dialog is opened', '234',
472 fieldObj.getRange().getText());
473 plugin.execCommand(goog.editor.Command.MODAL_LINK_EDITOR, linkObj);
474 if (!goog.userAgent.IE && !goog.userAgent.OPERA) {
475 // IE returns some bogus range when field doesn't have selection.
476 // You can't remove the selection from a whitebox field in Opera.
477 assertNull(
478 'There should be no selection while dialog is open',
479 fieldObj.getRange());
480 }
481 goog.testing.events.fireClickSequence(plugin.dialog_.getOkButtonElement());
482 assertEquals(
483 'No text should be selected after clicking ok', '',
484 fieldObj.getRange().getText());
485
486 // Test that the caret is placed at the end of the link text.
487 goog.testing.editor.dom.assertRangeBetweenText(
488 // If the browser gets stuck in links, an nbsp was added after the link
489 // to avoid that, otherwise we just look for the 5.
490 goog.editor.BrowserFeature.GETS_STUCK_IN_LINKS ?
491 goog.string.Unicode.NBSP :
492 '5',
493 '', fieldObj.getRange());
494
495 // NOTE(user): The functionality to avoid getting stuck in links is
496 // tested in editablelink_test.html::testPlaceCursorRightOf().
497 }
498
499
500 /**
501 * Tests that the selection is cleared when the dialog opens and is
502 * correctly restored after cancel is clicked.
503 * @param {boolean=} opt_isNew Whether to test behavior when creating a new
504 * link (cancelling will flatten it).
505 */
506 function testRestoreSelectionOnCancel(opt_isNew) {
507 setUpAnchor('12345', '/', opt_isNew);
508 setUpRealEditableField();
509
510 var elem = fieldObj.getElement();
511 var helper = new goog.testing.editor.TestHelper(elem);
512 helper.select('12345', 1, '12345', 4); // Selects '234'.
513
514 assertEquals(
515 'Incorrect text selected before dialog is opened', '234',
516 fieldObj.getRange().getText());
517 plugin.execCommand(goog.editor.Command.MODAL_LINK_EDITOR, linkObj);
518 if (!goog.userAgent.IE && !goog.userAgent.OPERA) {
519 // IE returns some bogus range when field doesn't have selection.
520 // You can't remove the selection from a whitebox field in Opera.
521 assertNull(
522 'There should be no selection while dialog is open',
523 fieldObj.getRange());
524 }
525 goog.testing.events.fireClickSequence(
526 plugin.dialog_.getCancelButtonElement());
527 assertEquals(
528 'Incorrect text selected after clicking cancel', '234',
529 fieldObj.getRange().getText());
530 }
531
532
533 /**
534 * Tests that the selection is cleared when the dialog opens and is
535 * correctly restored after cancel is clicked and the new link is removed.
536 */
537 function testRestoreSelectionOnCancelNew() {
538 testRestoreSelectionOnCancel(true);
539 }
540
541
542 /**
543 * Tests that the BeforeTestLink event is suppressed for invalid url schemes.
544 */
545 function testTestLinkDisabledForInvalidScheme() {
546 mockAlert(goog.testing.mockmatchers.isString);
547 mockCtrl.$replayAll();
548
549 var invalidUrl = 'javascript:document.write(\'hello\');';
550
551 plugin = new goog.editor.plugins.LinkDialogPlugin();
552 var dialog = plugin.createDialog(new goog.dom.DomHelper(), mockLink);
553
554 // Mock of execCommand + clicking test without actually opening the dialog.
555 var dispatched = dialog.dispatchEvent(
556 new goog.ui.editor.LinkDialog.BeforeTestLinkEvent(invalidUrl));
557
558 assertFalse(dispatched);
559 mockCtrl.$verifyAll();
560 }
561
562 function testIsSafeSchemeToOpen() {
563 plugin = new goog.editor.plugins.LinkDialogPlugin();
564 // Urls with no scheme at all are ok too since 'http://' will be prepended.
565 var good = [
566 'http://google.com', 'http://google.com/', 'https://google.com',
567 '[email protected]', 'http://www.google.com', 'http://site.com', 'google.com',
568 'google', 'http://google', 'HTTP://GOOGLE.COM', 'HtTp://www.google.com'
569 ];
570
571 var bad = [
572 'javascript:google.com', 'httpp://google.com', 'data:foo',
573 'javascript:alert(\'hi\');', 'abc:def'
574 ];
575
576 for (var i = 0; i < good.length; i++) {
577 assertTrue(
578 good[i] + ' should have a safe scheme',
579 plugin.isSafeSchemeToOpen_(good[i]));
580 }
581
582 for (i = 0; i < bad.length; i++) {
583 assertFalse(
584 bad[i] + ' should have an unsafe scheme',
585 plugin.isSafeSchemeToOpen_(bad[i]));
586 }
587 }
588
589 function testShouldOpenWithWhitelist() {
590 plugin.setSafeToOpenSchemes(['abc']);
591
592 assertTrue('Scheme should be safe', plugin.shouldOpenUrl('abc://google.com'));
593 assertFalse(
594 'Scheme should be unsafe', plugin.shouldOpenUrl('http://google.com'));
595
596 plugin.setBlockOpeningUnsafeSchemes(false);
597 assertTrue(
598 'Non-whitelisted should now be safe after disabling blocking',
599 plugin.shouldOpenUrl('http://google.com'));
600 }
601
602
603 /**
604 * Regression test for http://b/issue?id=1607766 . Without the fix, this
605 * should give an Invalid Argument error in IE, because the editable field
606 * caches a selection util that has a reference to the node of the link text
607 * before it is edited (which gets replaced by a new node for the new text
608 * after editing).
609 */
610 function testBug1607766() {
611 setUpAnchor('abc', 'def');
612 setUpRealEditableField();
613
614 var elem = fieldObj.getElement();
615 var helper = new goog.testing.editor.TestHelper(elem);
616 helper.select('abc', 1, 'abc', 2); // Selects 'b'.
617 // Dispatching a selection event causes the field to cache a selection
618 // util, which is the root of the bug.
619 plugin.fieldObject.dispatchSelectionChangeEvent();
620
621 plugin.execCommand(goog.editor.Command.MODAL_LINK_EDITOR, linkObj);
622 goog.dom.getElement(goog.ui.editor.LinkDialog.Id_.TEXT_TO_DISPLAY).value =
623 'Abc';
624 goog.testing.events.fireClickSequence(plugin.dialog_.getOkButtonElement());
625
626 // In IE the unit test somehow doesn't cause a browser focus event, so we
627 // need to manually invoke this, which is where the bug happens.
628 plugin.fieldObject.dispatchFocus_();
629 }
630
631
632 /**
633 * Regression test for http://b/issue?id=2215546 .
634 */
635 function testBug2215546() {
636 setUpRealEditableField();
637
638 var elem = fieldObj.getElement();
639 fieldObj.setSafeHtml(
640 false,
641 goog.html.SafeHtml.create(
642 'div', {}, goog.html.SafeHtml.create('a', {'href': '/'}, '')));
643 anchorElem = elem.firstChild.firstChild;
644 linkObj = new goog.editor.Link(anchorElem, true);
645
646 var helper = new goog.testing.editor.TestHelper(elem);
647 // Select "</a>" in a way, simulating what IE does if you hit enter twice,
648 // arrow up into the blank line and open the link dialog.
649 helper.select(anchorElem, 0, elem.firstChild, 1);
650
651 plugin.execCommand(goog.editor.Command.MODAL_LINK_EDITOR, linkObj);
652 goog.dom.getElement(goog.ui.editor.LinkDialog.Id_.TEXT_TO_DISPLAY).value =
653 'foo';
654 goog.dom.getElement(goog.ui.editor.LinkDialog.Id_.ON_WEB_INPUT).value = 'foo';
655 var okButton = plugin.dialog_.getOkButtonElement();
656 okButton.disabled = false;
657 goog.testing.events.fireClickSequence(okButton);
658
659 assertEquals(
660 'Link text should have been inserted', 'foo', anchorElem.innerHTML);
661 }
662
663
664 /**
665 * Test that link insertion doesn't scroll the field to the top
666 * after clicking Cancel or OK.
667 */
668 function testBug7279077ScrollOnFocus() {
669 if (goog.userAgent.IE) {
670 return; // TODO(user): take this out once b/7279077 fixed for IE too.
671 }
672 setUpAnchor('12345', '/');
673 setUpRealEditableField();
674
675 // Make the field scrollable and kinda small.
676 var elem = fieldObj.getElement();
677 elem.style.overflow = 'auto';
678 elem.style.height = '40px';
679 elem.style.width = '200px';
680 elem.style.contenteditable = 'true';
681
682 // Add a bunch of text before the anchor tag.
683 var longTextElem = goog.dom.createElement(goog.dom.TagName.SPAN);
684 longTextElem.innerHTML = goog.string.repeat('All work and no play.<p>', 20);
685 elem.insertBefore(longTextElem, elem.firstChild);
686
687 var helper = new goog.testing.editor.TestHelper(elem);
688 helper.select('12345', 1, '12345', 4); // Selects '234'.
689
690 // Scroll down.
691 elem.scrollTop = 60;
692
693 // Bring up the link insertion dialog, then cancel.
694 plugin.execCommand(goog.editor.Command.MODAL_LINK_EDITOR, linkObj);
695 goog.dom.getElement(goog.ui.editor.LinkDialog.Id_.TEXT_TO_DISPLAY).value =
696 'foo';
697 goog.dom.getElement(goog.ui.editor.LinkDialog.Id_.ON_WEB_INPUT).value = 'foo';
698 var cancelButton = plugin.dialog_.getCancelButtonElement();
699 goog.testing.events.fireClickSequence(cancelButton);
700
701 assertEquals(
702 'Field should not have scrolled after cancel', 60, elem.scrollTop);
703
704 // Now let's try it with clicking the OK button.
705 plugin.execCommand(goog.editor.Command.MODAL_LINK_EDITOR, linkObj);
706 goog.dom.getElement(goog.ui.editor.LinkDialog.Id_.TEXT_TO_DISPLAY).value =
707 'foo';
708 goog.dom.getElement(goog.ui.editor.LinkDialog.Id_.ON_WEB_INPUT).value = 'foo';
709 var okButton = plugin.dialog_.getOkButtonElement();
710 goog.testing.events.fireClickSequence(okButton);
711
712 assertEquals('Field should not have scrolled after OK', 60, elem.scrollTop);
713 }
714
715
716 /**
717 * Setup a real editable field (instead of a mock) and register the plugin to
718 * it.
719 */
720 function setUpRealEditableField() {
721 fieldElem = goog.dom.createElement(goog.dom.TagName.DIV);
722 fieldElem.id = 'myField';
723 document.body.appendChild(fieldElem);
724 fieldElem.appendChild(anchorElem);
725 fieldObj = new goog.editor.Field('myField', document);
726 fieldObj.makeEditable();
727 linkObj = new goog.editor.Link(fieldObj.getElement().firstChild, isNew);
728 // Register the plugin to that field.
729 plugin = new goog.editor.plugins.LinkDialogPlugin();
730 fieldObj.registerPlugin(plugin);
731 }
732
733
734 /**
735 * Tear down the real editable field.
736 */
737 function tearDownRealEditableField() {
738 if (fieldObj) {
739 fieldObj.makeUneditable();
740 fieldObj.dispose();
741 fieldObj = null;
742 }
743 goog.dom.removeNode(fieldElem);
744 }