| 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.EnterHandlerTest'); |
| 16 |
goog.setTestOnly('goog.editor.plugins.EnterHandlerTest'); |
| 17 |
|
| 18 |
goog.require('goog.dom'); |
| 19 |
goog.require('goog.dom.NodeType'); |
| 20 |
goog.require('goog.dom.Range'); |
| 21 |
goog.require('goog.dom.TagName'); |
| 22 |
goog.require('goog.editor.BrowserFeature'); |
| 23 |
goog.require('goog.editor.Field'); |
| 24 |
goog.require('goog.editor.Plugin'); |
| 25 |
goog.require('goog.editor.plugins.Blockquote'); |
| 26 |
goog.require('goog.editor.plugins.EnterHandler'); |
| 27 |
goog.require('goog.editor.range'); |
| 28 |
goog.require('goog.events'); |
| 29 |
goog.require('goog.events.KeyCodes'); |
| 30 |
goog.require('goog.html.testing'); |
| 31 |
goog.require('goog.testing.ExpectedFailures'); |
| 32 |
goog.require('goog.testing.MockClock'); |
| 33 |
goog.require('goog.testing.dom'); |
| 34 |
goog.require('goog.testing.editor.TestHelper'); |
| 35 |
goog.require('goog.testing.events'); |
| 36 |
goog.require('goog.testing.jsunit'); |
| 37 |
goog.require('goog.userAgent'); |
| 38 |
|
| 39 |
var savedHtml; |
| 40 |
|
| 41 |
var field1; |
| 42 |
var field2; |
| 43 |
var firedDelayedChange; |
| 44 |
var firedBeforeChange; |
| 45 |
var clock; |
| 46 |
var container; |
| 47 |
var EXPECTEDFAILURES; |
| 48 |
|
| 49 |
function setUpPage() { |
| 50 |
container = goog.dom.getElement('container'); |
| 51 |
} |
| 52 |
|
| 53 |
function setUp() { |
| 54 |
EXPECTEDFAILURES = new goog.testing.ExpectedFailures(); |
| 55 |
savedHtml = goog.dom.getElement('root').innerHTML; |
| 56 |
clock = new goog.testing.MockClock(true); |
| 57 |
} |
| 58 |
|
| 59 |
function setUpFields(classnameRequiredToSplitBlockquote) { |
| 60 |
field1 = makeField('field1', classnameRequiredToSplitBlockquote); |
| 61 |
field2 = makeField('field2', classnameRequiredToSplitBlockquote); |
| 62 |
|
| 63 |
field1.makeEditable(); |
| 64 |
field2.makeEditable(); |
| 65 |
} |
| 66 |
|
| 67 |
function tearDown() { |
| 68 |
clock.dispose(); |
| 69 |
|
| 70 |
EXPECTEDFAILURES.handleTearDown(); |
| 71 |
|
| 72 |
goog.dom.getElement('root').innerHTML = savedHtml; |
| 73 |
} |
| 74 |
|
| 75 |
function testEnterInNonSetupBlockquote() { |
| 76 |
setUpFields(true); |
| 77 |
resetChangeFlags(); |
| 78 |
var prevented = !selectNodeAndHitEnter(field1, 'field1cursor'); |
| 79 |
waitForChangeEvents(); |
| 80 |
assertChangeFlags(); |
| 81 |
|
| 82 |
// make sure there's just one blockquote, and that the text has been deleted. |
| 83 |
var elem = field1.getElement(); |
| 84 |
var dom = field1.getEditableDomHelper(); |
| 85 |
EXPECTEDFAILURES.expectFailureFor( |
| 86 |
goog.userAgent.OPERA, |
| 87 |
'The blockquote is overwritten with DIV due to CORE-22104 -- Opera ' + |
| 88 |
'overwrites the BLOCKQUOTE ancestor with DIV when doing FormatBlock ' + |
| 89 |
'for DIV'); |
| 90 |
try { |
| 91 |
assertEquals( |
| 92 |
'Blockquote should not be split', 1, |
| 93 |
dom.getElementsByTagNameAndClass( |
| 94 |
goog.dom.TagName.BLOCKQUOTE, null, elem) |
| 95 |
.length); |
| 96 |
} catch (e) { |
| 97 |
EXPECTEDFAILURES.handleException(e); |
| 98 |
} |
| 99 |
assert( |
| 100 |
'Selection should be deleted', -1 == elem.innerHTML.indexOf('selection')); |
| 101 |
|
| 102 |
assertEquals( |
| 103 |
'The event should have been prevented only on webkit', prevented, |
| 104 |
goog.userAgent.WEBKIT); |
| 105 |
} |
| 106 |
|
| 107 |
function testEnterInSetupBlockquote() { |
| 108 |
setUpFields(true); |
| 109 |
resetChangeFlags(); |
| 110 |
var prevented = !selectNodeAndHitEnter(field2, 'field2cursor'); |
| 111 |
waitForChangeEvents(); |
| 112 |
assertChangeFlags(); |
| 113 |
|
| 114 |
// make sure there are two blockquotes, and a DIV with nbsp in the middle. |
| 115 |
var elem = field2.getElement(); |
| 116 |
var dom = field2.getEditableDomHelper(); |
| 117 |
assertEquals( |
| 118 |
'Blockquote should be split', 2, |
| 119 |
dom.getElementsByTagNameAndClass(goog.dom.TagName.BLOCKQUOTE, null, elem) |
| 120 |
.length); |
| 121 |
assert( |
| 122 |
'Selection should be deleted', -1 == elem.innerHTML.indexOf('selection')); |
| 123 |
|
| 124 |
assert( |
| 125 |
'should have div with ', |
| 126 |
-1 != elem.innerHTML.indexOf('>' + getNbsp() + '<')); |
| 127 |
assert('event should have been prevented', prevented); |
| 128 |
} |
| 129 |
|
| 130 |
function testEnterInNonSetupBlockquoteWhenClassnameIsNotRequired() { |
| 131 |
setUpFields(false); |
| 132 |
|
| 133 |
resetChangeFlags(); |
| 134 |
var prevented = !selectNodeAndHitEnter(field1, 'field1cursor'); |
| 135 |
waitForChangeEvents(); |
| 136 |
assertChangeFlags(); |
| 137 |
|
| 138 |
// make sure there are two blockquotes, and a DIV with nbsp in the middle. |
| 139 |
var elem = field1.getElement(); |
| 140 |
var dom = field1.getEditableDomHelper(); |
| 141 |
assertEquals( |
| 142 |
'Blockquote should be split', 2, |
| 143 |
dom.getElementsByTagNameAndClass(goog.dom.TagName.BLOCKQUOTE, null, elem) |
| 144 |
.length); |
| 145 |
assert( |
| 146 |
'Selection should be deleted', -1 == elem.innerHTML.indexOf('selection')); |
| 147 |
|
| 148 |
assert( |
| 149 |
'should have div with ', |
| 150 |
-1 != elem.innerHTML.indexOf('>' + getNbsp() + '<')); |
| 151 |
assert('event should have been prevented', prevented); |
| 152 |
} |
| 153 |
|
| 154 |
function testEnterInBlockquoteCreatesDivInBrMode() { |
| 155 |
setUpFields(true); |
| 156 |
selectNodeAndHitEnter(field2, 'field2cursor'); |
| 157 |
var elem = field2.getElement(); |
| 158 |
var dom = field2.getEditableDomHelper(); |
| 159 |
|
| 160 |
var firstBlockquote = dom.getElementsByTagNameAndClass( |
| 161 |
goog.dom.TagName.BLOCKQUOTE, null, elem)[0]; |
| 162 |
var div = dom.getNextElementSibling(firstBlockquote); |
| 163 |
assertEquals('Element after blockquote should be a div', 'DIV', div.tagName); |
| 164 |
assertEquals( |
| 165 |
'Element after div should be second blockquote', 'BLOCKQUOTE', |
| 166 |
dom.getNextElementSibling(div).tagName); |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
/** |
| 171 |
* Tests that breaking after a BR doesn't result in unnecessary newlines. |
| 172 |
* @bug 1471047 |
| 173 |
*/ |
| 174 |
function testEnterInBlockquoteRemovesUnnecessaryBrWithCursorAfterBr() { |
| 175 |
setUpFields(true); |
| 176 |
|
| 177 |
// Assume the following HTML snippet:- |
| 178 |
// <blockquote>one<br>|two<br></blockquote> |
| 179 |
// |
| 180 |
// After enter on the cursor position without the fix, the resulting HTML |
| 181 |
// after the blockquote split was:- |
| 182 |
// <blockquote>one</blockquote> |
| 183 |
// <div> </div> |
| 184 |
// <blockquote><br>two<br></blockquote> |
| 185 |
// |
| 186 |
// This creates the impression on an unnecessary newline. The resulting HTML |
| 187 |
// after the fix is:- |
| 188 |
// |
| 189 |
// <blockquote>one<br></blockquote> |
| 190 |
// <div> </div> |
| 191 |
// <blockquote>two<br></blockquote> |
| 192 |
field1.setSafeHtml( |
| 193 |
false, |
| 194 |
goog.html.testing.newSafeHtmlForTest( |
| 195 |
'<blockquote id="quote" class="tr_bq">one<br>' + |
| 196 |
'two<br></blockquote>')); |
| 197 |
var dom = field1.getEditableDomHelper(); |
| 198 |
goog.dom.Range.createCaret(dom.getElement('quote'), 2).select(); |
| 199 |
goog.testing.events.fireKeySequence( |
| 200 |
field1.getElement(), goog.events.KeyCodes.ENTER); |
| 201 |
var elem = field1.getElement(); |
| 202 |
var secondBlockquote = dom.getElementsByTagNameAndClass( |
| 203 |
goog.dom.TagName.BLOCKQUOTE, null, elem)[1]; |
| 204 |
assertHTMLEquals('two<br>', secondBlockquote.innerHTML); |
| 205 |
|
| 206 |
// Verifies that a blockquote split doesn't happen if it doesn't need to. |
| 207 |
field1.setSafeHtml( |
| 208 |
false, |
| 209 |
goog.html.testing.newSafeHtmlForTest( |
| 210 |
'<blockquote class="tr_bq">one<br id="brcursor"></blockquote>')); |
| 211 |
selectNodeAndHitEnter(field1, 'brcursor'); |
| 212 |
assertEquals( |
| 213 |
1, |
| 214 |
dom.getElementsByTagNameAndClass(goog.dom.TagName.BLOCKQUOTE, null, elem) |
| 215 |
.length); |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
/** |
| 220 |
* Tests that breaking in a text node before a BR doesn't result in unnecessary |
| 221 |
* newlines. |
| 222 |
* @bug 1471047 |
| 223 |
*/ |
| 224 |
function testEnterInBlockquoteRemovesUnnecessaryBrWithCursorBeforeBr() { |
| 225 |
setUpFields(true); |
| 226 |
|
| 227 |
// Assume the following HTML snippet:- |
| 228 |
// <blockquote>one|<br>two<br></blockquote> |
| 229 |
// |
| 230 |
// After enter on the cursor position, the resulting HTML should be. |
| 231 |
// <blockquote>one<br></blockquote> |
| 232 |
// <div> </div> |
| 233 |
// <blockquote>two<br></blockquote> |
| 234 |
field1.setSafeHtml( |
| 235 |
false, |
| 236 |
goog.html.testing.newSafeHtmlForTest( |
| 237 |
'<blockquote id="quote" class="tr_bq">one<br>' + |
| 238 |
'two<br></blockquote>')); |
| 239 |
var dom = field1.getEditableDomHelper(); |
| 240 |
var cursor = dom.getElement('quote').firstChild; |
| 241 |
goog.dom.Range.createCaret(cursor, 3).select(); |
| 242 |
goog.testing.events.fireKeySequence( |
| 243 |
field1.getElement(), goog.events.KeyCodes.ENTER); |
| 244 |
var elem = field1.getElement(); |
| 245 |
var secondBlockquote = dom.getElementsByTagNameAndClass( |
| 246 |
goog.dom.TagName.BLOCKQUOTE, null, elem)[1]; |
| 247 |
assertHTMLEquals('two<br>', secondBlockquote.innerHTML); |
| 248 |
|
| 249 |
// Ensures that standard text node split works as expected with the new |
| 250 |
// change. |
| 251 |
field1.setSafeHtml( |
| 252 |
false, |
| 253 |
goog.html.testing.newSafeHtmlForTest( |
| 254 |
'<blockquote id="quote" class="tr_bq">one<b>two</b><br>')); |
| 255 |
cursor = dom.getElement('quote').firstChild; |
| 256 |
goog.dom.Range.createCaret(cursor, 3).select(); |
| 257 |
goog.testing.events.fireKeySequence( |
| 258 |
field1.getElement(), goog.events.KeyCodes.ENTER); |
| 259 |
secondBlockquote = dom.getElementsByTagNameAndClass( |
| 260 |
goog.dom.TagName.BLOCKQUOTE, null, elem)[1]; |
| 261 |
assertHTMLEquals('<b>two</b><br>', secondBlockquote.innerHTML); |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
/** |
| 266 |
* Tests that pressing enter in a blockquote doesn't create unnecessary |
| 267 |
* DOM subtrees. |
| 268 |
* |
| 269 |
* @bug 1991539 |
| 270 |
* @bug 1991392 |
| 271 |
*/ |
| 272 |
function testEnterInBlockquoteRemovesExtraNodes() { |
| 273 |
setUpFields(true); |
| 274 |
|
| 275 |
// Let's assume we have the following DOM structure and the |
| 276 |
// cursor is placed after the first numbered list item "one". |
| 277 |
// |
| 278 |
// <blockquote class="tr_bq"> |
| 279 |
// <div><div>a</div><ol><li>one|</li></div> |
| 280 |
// <div>two</div> |
| 281 |
// </blockquote> |
| 282 |
// |
| 283 |
// After pressing enter, we have the following structure. |
| 284 |
// |
| 285 |
// <blockquote class="tr_bq"> |
| 286 |
// <div><div>a</div><ol><li>one|</li></div> |
| 287 |
// </blockquote> |
| 288 |
// <div> </div> |
| 289 |
// <blockquote class="tr_bq"> |
| 290 |
// <div><ol><li><span id=""></span></li></ol></div> |
| 291 |
// <div>two</div> |
| 292 |
// </blockquote> |
| 293 |
// |
| 294 |
// This appears to the user as an empty list. After the fix, the HTML |
| 295 |
// will be |
| 296 |
// |
| 297 |
// <blockquote class="tr_bq"> |
| 298 |
// <div><div>a</div><ol><li>one|</li></div> |
| 299 |
// </blockquote> |
| 300 |
// <div> </div> |
| 301 |
// <blockquote class="tr_bq"> |
| 302 |
// <div>two</div> |
| 303 |
// </blockquote> |
| 304 |
// |
| 305 |
field1.setSafeHtml( |
| 306 |
false, |
| 307 |
goog.html.testing.newSafeHtmlForTest( |
| 308 |
'<blockquote class="tr_bq">' + |
| 309 |
'<div><div>a</div><ol><li id="cursor">one</li></div>' + |
| 310 |
'<div>b</div>' + |
| 311 |
'</blockquote>')); |
| 312 |
var dom = field1.getEditableDomHelper(); |
| 313 |
goog.dom.Range.createCaret(dom.getElement('cursor').firstChild, 3).select(); |
| 314 |
goog.testing.events.fireKeySequence( |
| 315 |
field1.getElement(), goog.events.KeyCodes.ENTER); |
| 316 |
var elem = field1.getElement(); |
| 317 |
var secondBlockquote = dom.getElementsByTagNameAndClass( |
| 318 |
goog.dom.TagName.BLOCKQUOTE, null, elem)[1]; |
| 319 |
assertHTMLEquals('<div>b</div>', secondBlockquote.innerHTML); |
| 320 |
|
| 321 |
// Ensure that we remove only unnecessary subtrees. |
| 322 |
field1.setSafeHtml( |
| 323 |
false, |
| 324 |
goog.html.testing.newSafeHtmlForTest( |
| 325 |
'<blockquote class="tr_bq">' + |
| 326 |
'<div><span>a</span><div id="cursor">one</div><div>two</div></div>' + |
| 327 |
'<div><span>c</span></div>' + |
| 328 |
'</blockquote>')); |
| 329 |
goog.dom.Range.createCaret(dom.getElement('cursor').firstChild, 3).select(); |
| 330 |
goog.testing.events.fireKeySequence( |
| 331 |
field1.getElement(), goog.events.KeyCodes.ENTER); |
| 332 |
secondBlockquote = dom.getElementsByTagNameAndClass( |
| 333 |
goog.dom.TagName.BLOCKQUOTE, null, elem)[1]; |
| 334 |
var expectedHTML = '<div><div>two</div></div>' + |
| 335 |
'<div><span>c</span></div>'; |
| 336 |
assertHTMLEquals(expectedHTML, secondBlockquote.innerHTML); |
| 337 |
|
| 338 |
// Place the cursor in the middle of a line. |
| 339 |
field1.setSafeHtml( |
| 340 |
false, |
| 341 |
goog.html.testing.newSafeHtmlForTest( |
| 342 |
'<blockquote id="quote" class="tr_bq">' + |
| 343 |
'<div>one</div><div>two</div>' + |
| 344 |
'</blockquote>')); |
| 345 |
goog.dom.Range.createCaret(dom.getElement('quote').firstChild.firstChild, 1) |
| 346 |
.select(); |
| 347 |
goog.testing.events.fireKeySequence( |
| 348 |
field1.getElement(), goog.events.KeyCodes.ENTER); |
| 349 |
var blockquotes = |
| 350 |
dom.getElementsByTagNameAndClass(goog.dom.TagName.BLOCKQUOTE, null, elem); |
| 351 |
assertEquals(2, blockquotes.length); |
| 352 |
assertHTMLEquals('<div>o</div>', blockquotes[0].innerHTML); |
| 353 |
assertHTMLEquals('<div>ne</div><div>two</div>', blockquotes[1].innerHTML); |
| 354 |
} |
| 355 |
|
| 356 |
function testEnterInList() { |
| 357 |
setUpFields(true); |
| 358 |
|
| 359 |
// <enter> in a list should *never* be handled by custom code. Lists are |
| 360 |
// just way too complicated to get right. |
| 361 |
field1.setSafeHtml( |
| 362 |
false, |
| 363 |
goog.html.testing.newSafeHtmlForTest( |
| 364 |
'<ol><li>hi!<span id="field1cursor"></span></li></ol>')); |
| 365 |
if (goog.userAgent.OPERA) { |
| 366 |
// Opera doesn't actually place the selection in the empty span |
| 367 |
// unless we add a text node first. |
| 368 |
var dom = field1.getEditableDomHelper(); |
| 369 |
dom.getElement('field1cursor').appendChild(dom.createTextNode('')); |
| 370 |
} |
| 371 |
var prevented = !selectNodeAndHitEnter(field1, 'field1cursor'); |
| 372 |
assertFalse('<enter> in a list should not be prevented', prevented); |
| 373 |
} |
| 374 |
|
| 375 |
function testEnterAtEndOfBlockInWebkit() { |
| 376 |
setUpFields(true); |
| 377 |
|
| 378 |
if (goog.userAgent.WEBKIT) { |
| 379 |
field1.setSafeHtml( |
| 380 |
false, |
| 381 |
goog.html.testing.newSafeHtmlForTest( |
| 382 |
'<blockquote>hi!<span id="field1cursor"></span></blockquote>')); |
| 383 |
|
| 384 |
var cursor = field1.getEditableDomHelper().getElement('field1cursor'); |
| 385 |
goog.editor.range.placeCursorNextTo(cursor, false); |
| 386 |
goog.dom.removeNode(cursor); |
| 387 |
|
| 388 |
var prevented = !goog.testing.events.fireKeySequence( |
| 389 |
field1.getElement(), goog.events.KeyCodes.ENTER); |
| 390 |
waitForChangeEvents(); |
| 391 |
assertChangeFlags(); |
| 392 |
assert('event should have been prevented', prevented); |
| 393 |
|
| 394 |
// Make sure that the block now has two brs. |
| 395 |
var elem = field1.getElement(); |
| 396 |
assertEquals( |
| 397 |
'should have inserted two br tags: ' + elem.innerHTML, 2, |
| 398 |
goog.dom.getElementsByTagNameAndClass(goog.dom.TagName.BR, null, elem) |
| 399 |
.length); |
| 400 |
} |
| 401 |
} |
| 402 |
|
| 403 |
|
| 404 |
/** |
| 405 |
* Tests that deleting a BR that comes right before a block element works. |
| 406 |
* @bug 1471096 |
| 407 |
* @bug 2056376 |
| 408 |
*/ |
| 409 |
function testDeleteBrBeforeBlock() { |
| 410 |
setUpFields(true); |
| 411 |
|
| 412 |
// This test only works on Gecko, because it's testing for manual deletion of |
| 413 |
// BR tags, which is done only for Gecko. For other browsers we fall through |
| 414 |
// and let the browser do the delete, which can only be tested with a robot |
| 415 |
// test (see javascript/apps/editor/tests/delete_br_robot.html). |
| 416 |
if (goog.userAgent.GECKO) { |
| 417 |
field1.setSafeHtml( |
| 418 |
false, |
| 419 |
goog.html.testing.newSafeHtmlForTest('one<br><br><div>two</div>')); |
| 420 |
var helper = new goog.testing.editor.TestHelper(field1.getElement()); |
| 421 |
helper.select(field1.getElement(), 2); // Between the two BR's. |
| 422 |
goog.testing.events.fireKeySequence( |
| 423 |
field1.getElement(), goog.events.KeyCodes.DELETE); |
| 424 |
assertEquals( |
| 425 |
'Should have deleted exactly one <br>', 'one<br><div>two</div>', |
| 426 |
field1.getElement().innerHTML); |
| 427 |
|
| 428 |
// We test the case where the BR has a previous sibling which is not |
| 429 |
// a block level element. |
| 430 |
field1.setSafeHtml( |
| 431 |
false, |
| 432 |
goog.html.testing.newSafeHtmlForTest('one<br><ul><li>two</li></ul>')); |
| 433 |
helper.select(field1.getElement(), 1); // Between one and BR. |
| 434 |
goog.testing.events.fireKeySequence( |
| 435 |
field1.getElement(), goog.events.KeyCodes.DELETE); |
| 436 |
assertEquals( |
| 437 |
'Should have deleted the <br>', 'one<ul><li>two</li></ul>', |
| 438 |
field1.getElement().innerHTML); |
| 439 |
// Verify that the cursor is placed at the end of the text node "one". |
| 440 |
var range = field1.getRange(); |
| 441 |
var focusNode = range.getFocusNode(); |
| 442 |
assertTrue('The selected range should be collapsed', range.isCollapsed()); |
| 443 |
assertTrue( |
| 444 |
'The focus node should be the text node "one"', |
| 445 |
focusNode.nodeType == goog.dom.NodeType.TEXT && |
| 446 |
focusNode.data == 'one'); |
| 447 |
assertEquals( |
| 448 |
'The focus offset should be at the end of the text node "one"', |
| 449 |
focusNode.length, range.getFocusOffset()); |
| 450 |
assertTrue( |
| 451 |
'The next sibling of the focus node should be the UL', |
| 452 |
focusNode.nextSibling && |
| 453 |
focusNode.nextSibling.tagName == goog.dom.TagName.UL); |
| 454 |
|
| 455 |
// We test the case where the previous sibling of the BR is a block |
| 456 |
// level element. |
| 457 |
field1.setSafeHtml( |
| 458 |
false, |
| 459 |
goog.html.testing.newSafeHtmlForTest( |
| 460 |
'<div>foo</div><br><div><span>bar</span></div>')); |
| 461 |
helper.select(field1.getElement(), 1); // Before the BR. |
| 462 |
goog.testing.events.fireKeySequence( |
| 463 |
field1.getElement(), goog.events.KeyCodes.DELETE); |
| 464 |
assertEquals( |
| 465 |
'Should have deleted the <br>', |
| 466 |
'<div>foo</div><div><span>bar</span></div>', |
| 467 |
field1.getElement().innerHTML); |
| 468 |
range = field1.getRange(); |
| 469 |
assertEquals( |
| 470 |
'The selected range should be contained within the <span>', |
| 471 |
String(goog.dom.TagName.SPAN), range.getContainerElement().tagName); |
| 472 |
assertTrue('The selected range should be collapsed', range.isCollapsed()); |
| 473 |
// Verify that the cursor is placed inside the span at the beginning of bar. |
| 474 |
focusNode = range.getFocusNode(); |
| 475 |
assertTrue( |
| 476 |
'The focus node should be the text node "bar"', |
| 477 |
focusNode.nodeType == goog.dom.NodeType.TEXT && |
| 478 |
focusNode.data == 'bar'); |
| 479 |
assertEquals( |
| 480 |
'The focus offset should be at the beginning ' + |
| 481 |
'of the text node "bar"', |
| 482 |
0, range.getFocusOffset()); |
| 483 |
|
| 484 |
// We test the case where the BR does not have a previous sibling. |
| 485 |
field1.setSafeHtml( |
| 486 |
false, |
| 487 |
goog.html.testing.newSafeHtmlForTest('<br><ul><li>one</li></ul>')); |
| 488 |
helper.select(field1.getElement(), 0); // Before the BR. |
| 489 |
goog.testing.events.fireKeySequence( |
| 490 |
field1.getElement(), goog.events.KeyCodes.DELETE); |
| 491 |
assertEquals( |
| 492 |
'Should have deleted the <br>', '<ul><li>one</li></ul>', |
| 493 |
field1.getElement().innerHTML); |
| 494 |
range = field1.getRange(); |
| 495 |
// Verify that the cursor is placed inside the LI at the text node "one". |
| 496 |
assertEquals( |
| 497 |
'The selected range should be contained within the <li>', |
| 498 |
String(goog.dom.TagName.LI), range.getContainerElement().tagName); |
| 499 |
assertTrue('The selected range should be collapsed', range.isCollapsed()); |
| 500 |
focusNode = range.getFocusNode(); |
| 501 |
assertTrue( |
| 502 |
'The focus node should be the text node "one"', |
| 503 |
(focusNode.nodeType == goog.dom.NodeType.TEXT && |
| 504 |
focusNode.data == 'one')); |
| 505 |
assertEquals( |
| 506 |
'The focus offset should be at the beginning of ' + |
| 507 |
'the text node "one"', |
| 508 |
0, range.getFocusOffset()); |
| 509 |
|
| 510 |
// Testing deleting a BR followed by a block level element and preceded |
| 511 |
// by a BR. |
| 512 |
field1.setSafeHtml( |
| 513 |
false, |
| 514 |
goog.html.testing.newSafeHtmlForTest('<br><br><ul><li>one</li></ul>')); |
| 515 |
helper.select(field1.getElement(), 1); // Between the BR's. |
| 516 |
goog.testing.events.fireKeySequence( |
| 517 |
field1.getElement(), goog.events.KeyCodes.DELETE); |
| 518 |
assertEquals( |
| 519 |
'Should have deleted the <br>', '<br><ul><li>one</li></ul>', |
| 520 |
field1.getElement().innerHTML); |
| 521 |
// Verify that the cursor is placed inside the LI at the text node "one". |
| 522 |
range = field1.getRange(); |
| 523 |
assertEquals( |
| 524 |
'The selected range should be contained within the <li>', |
| 525 |
String(goog.dom.TagName.LI), range.getContainerElement().tagName); |
| 526 |
assertTrue('The selected range should be collapsed', range.isCollapsed()); |
| 527 |
focusNode = range.getFocusNode(); |
| 528 |
assertTrue( |
| 529 |
'The focus node should be the text node "one"', |
| 530 |
(focusNode.nodeType == goog.dom.NodeType.TEXT && |
| 531 |
focusNode.data == 'one')); |
| 532 |
assertEquals( |
| 533 |
'The focus offset should be at the beginning of ' + |
| 534 |
'the text node "one"', |
| 535 |
0, range.getFocusOffset()); |
| 536 |
} // End if GECKO |
| 537 |
} |
| 538 |
|
| 539 |
|
| 540 |
/** |
| 541 |
* Tests that deleting a BR before a blockquote doesn't remove quoted text. |
| 542 |
* @bug 1471075 |
| 543 |
*/ |
| 544 |
function testDeleteBeforeBlockquote() { |
| 545 |
setUpFields(true); |
| 546 |
|
| 547 |
if (goog.userAgent.GECKO) { |
| 548 |
field1.setSafeHtml( |
| 549 |
false, |
| 550 |
goog.html.testing.newSafeHtmlForTest( |
| 551 |
'<br><br><div><br><blockquote>foo</blockquote></div>')); |
| 552 |
var helper = new goog.testing.editor.TestHelper(field1.getElement()); |
| 553 |
helper.select(field1.getElement(), 0); // Before the first BR. |
| 554 |
// Fire three deletes in quick succession. |
| 555 |
goog.testing.events.fireKeySequence( |
| 556 |
field1.getElement(), goog.events.KeyCodes.DELETE); |
| 557 |
goog.testing.events.fireKeySequence( |
| 558 |
field1.getElement(), goog.events.KeyCodes.DELETE); |
| 559 |
goog.testing.events.fireKeySequence( |
| 560 |
field1.getElement(), goog.events.KeyCodes.DELETE); |
| 561 |
assertEquals( |
| 562 |
'Should have deleted all the <br>\'s and the blockquote ' + |
| 563 |
'isn\'t affected', |
| 564 |
'<div><blockquote>foo</blockquote></div>', |
| 565 |
field1.getElement().innerHTML); |
| 566 |
var range = field1.getRange(); |
| 567 |
assertEquals( |
| 568 |
'The selected range should be contained within the ' + |
| 569 |
'<blockquote>', |
| 570 |
String(goog.dom.TagName.BLOCKQUOTE), range.getContainerElement().tagName); |
| 571 |
assertTrue('The selected range should be collapsed', range.isCollapsed()); |
| 572 |
var focusNode = range.getFocusNode(); |
| 573 |
assertTrue( |
| 574 |
'The focus node should be the text node "foo"', |
| 575 |
(focusNode.nodeType == goog.dom.NodeType.TEXT && |
| 576 |
focusNode.data == 'foo')); |
| 577 |
assertEquals( |
| 578 |
'The focus offset should be at the ' + |
| 579 |
'beginning of the text node "foo"', |
| 580 |
0, range.getFocusOffset()); |
| 581 |
} |
| 582 |
} |
| 583 |
|
| 584 |
|
| 585 |
/** |
| 586 |
* Tests that deleting a BR is working normally (that the workaround for the |
| 587 |
* bug is not causing double deletes). |
| 588 |
* @bug 1471096 |
| 589 |
*/ |
| 590 |
function testDeleteBrNormal() { |
| 591 |
setUpFields(true); |
| 592 |
|
| 593 |
// This test only works on Gecko, because it's testing for manual deletion of |
| 594 |
// BR tags, which is done only for Gecko. For other browsers we fall through |
| 595 |
// and let the browser do the delete, which can only be tested with a robot |
| 596 |
// test (see javascript/apps/editor/tests/delete_br_robot.html). |
| 597 |
if (goog.userAgent.GECKO) { |
| 598 |
field1.setSafeHtml( |
| 599 |
false, goog.html.testing.newSafeHtmlForTest('one<br><br><br>two')); |
| 600 |
var helper = new goog.testing.editor.TestHelper(field1.getElement()); |
| 601 |
helper.select( |
| 602 |
field1.getElement(), 2); // Between the first and second BR's. |
| 603 |
field1.getElement().focus(); |
| 604 |
goog.testing.events.fireKeySequence( |
| 605 |
field1.getElement(), goog.events.KeyCodes.DELETE); |
| 606 |
assertEquals( |
| 607 |
'Should have deleted exactly one <br>', 'one<br><br>two', |
| 608 |
field1.getElement().innerHTML); |
| 609 |
|
| 610 |
} // End if GECKO |
| 611 |
} |
| 612 |
|
| 613 |
|
| 614 |
/** |
| 615 |
* Tests that deleteCursorSelectionW3C_ correctly recognizes visually |
| 616 |
* collapsed selections in Opera even if they contain a <br>. |
| 617 |
* See the deleteCursorSelectionW3C_ comment in enterhandler.js. |
| 618 |
*/ |
| 619 |
function testCollapsedSelectionKeepsBrOpera() { |
| 620 |
setUpFields(true); |
| 621 |
|
| 622 |
if (goog.userAgent.OPERA) { |
| 623 |
field1.setSafeHtml( |
| 624 |
false, |
| 625 |
goog.html.testing.newSafeHtmlForTest( |
| 626 |
'<div><br id="pleasedontdeleteme"></div>')); |
| 627 |
field1.focus(); |
| 628 |
goog.testing.events.fireKeySequence( |
| 629 |
field1.getElement(), goog.events.KeyCodes.ENTER); |
| 630 |
assertNotNull( |
| 631 |
'The <br> must not have been deleted', |
| 632 |
goog.dom.getElement('pleasedontdeleteme')); |
| 633 |
} |
| 634 |
} |
| 635 |
|
| 636 |
|
| 637 |
/** |
| 638 |
* Selects the node at the given id, and simulates an ENTER keypress. |
| 639 |
* @param {goog.editor.Field} field The field with the node. |
| 640 |
* @param {string} id A DOM id. |
| 641 |
* @return {boolean} Whether preventDefault was called on the event. |
| 642 |
*/ |
| 643 |
function selectNodeAndHitEnter(field, id) { |
| 644 |
var dom = field.getEditableDomHelper(); |
| 645 |
var cursor = dom.getElement(id); |
| 646 |
goog.dom.Range.createFromNodeContents(cursor).select(); |
| 647 |
return goog.testing.events.fireKeySequence( |
| 648 |
cursor, goog.events.KeyCodes.ENTER); |
| 649 |
} |
| 650 |
|
| 651 |
|
| 652 |
/** |
| 653 |
* Creates a field with only the enter handler plugged in, for testing. |
| 654 |
* @param {string} id A DOM id. |
| 655 |
* @return {goog.editor.Field} A field. |
| 656 |
*/ |
| 657 |
function makeField(id, classnameRequiredToSplitBlockquote) { |
| 658 |
var field = new goog.editor.Field(id); |
| 659 |
field.registerPlugin(new goog.editor.plugins.EnterHandler()); |
| 660 |
field.registerPlugin( |
| 661 |
new goog.editor.plugins.Blockquote(classnameRequiredToSplitBlockquote)); |
| 662 |
|
| 663 |
goog.events.listen( |
| 664 |
field, goog.editor.Field.EventType.BEFORECHANGE, function() { |
| 665 |
// set the global flag that beforechange was fired. |
| 666 |
firedBeforeChange = true; |
| 667 |
}); |
| 668 |
goog.events.listen( |
| 669 |
field, goog.editor.Field.EventType.DELAYEDCHANGE, function() { |
| 670 |
// set the global flag that delayed change was fired. |
| 671 |
firedDelayedChange = true; |
| 672 |
}); |
| 673 |
|
| 674 |
return field; |
| 675 |
} |
| 676 |
|
| 677 |
|
| 678 |
/** |
| 679 |
* Reset all the global flags related to change events. |
| 680 |
*/ |
| 681 |
function resetChangeFlags() { |
| 682 |
waitForChangeEvents(); |
| 683 |
firedBeforeChange = firedDelayedChange = false; |
| 684 |
} |
| 685 |
|
| 686 |
|
| 687 |
/** |
| 688 |
* Asserts that both change flags were fired since the last reset. |
| 689 |
*/ |
| 690 |
function assertChangeFlags() { |
| 691 |
assert('Beforechange should have fired', firedBeforeChange); |
| 692 |
assert('Delayedchange should have fired', firedDelayedChange); |
| 693 |
} |
| 694 |
|
| 695 |
|
| 696 |
/** |
| 697 |
* Wait for delayedchange to propagate. |
| 698 |
*/ |
| 699 |
function waitForChangeEvents() { |
| 700 |
clock.tick( |
| 701 |
goog.editor.Field.DELAYED_CHANGE_FREQUENCY + |
| 702 |
goog.editor.Field.CHANGE_FREQUENCY); |
| 703 |
} |
| 704 |
|
| 705 |
function getNbsp() { |
| 706 |
// On WebKit (pre-528) and Opera, shows up as its unicode character in |
| 707 |
// innerHTML under some circumstances. |
| 708 |
return (goog.userAgent.WEBKIT && !goog.userAgent.isVersionOrHigher('528')) || |
| 709 |
goog.userAgent.OPERA ? |
| 710 |
'\u00a0' : |
| 711 |
' '; |
| 712 |
} |
| 713 |
|
| 714 |
|
| 715 |
function testPrepareContent() { |
| 716 |
setUpFields(true); |
| 717 |
assertPreparedContents('hi', 'hi'); |
| 718 |
assertPreparedContents( |
| 719 |
goog.editor.BrowserFeature.COLLAPSES_EMPTY_NODES ? '<br>' : '', ' '); |
| 720 |
} |
| 721 |
|
| 722 |
|
| 723 |
/** |
| 724 |
* Assert that the prepared contents matches the expected. |
| 725 |
*/ |
| 726 |
function assertPreparedContents(expected, original) { |
| 727 |
assertEquals( |
| 728 |
expected, |
| 729 |
field1.reduceOp_(goog.editor.Plugin.Op.PREPARE_CONTENTS_HTML, original)); |
| 730 |
} |
| 731 |
|
| 732 |
// UTILITY FUNCTION TESTS. |
| 733 |
|
| 734 |
function testDeleteW3CSimple() { |
| 735 |
if (goog.editor.BrowserFeature.HAS_W3C_RANGES) { |
| 736 |
container.innerHTML = '<div>abcd</div>'; |
| 737 |
var range = goog.dom.Range.createFromNodes( |
| 738 |
container.firstChild.firstChild, 1, container.firstChild.firstChild, 3); |
| 739 |
range.select(); |
| 740 |
goog.editor.plugins.EnterHandler.deleteW3cRange_(range); |
| 741 |
|
| 742 |
goog.testing.dom.assertHtmlContentsMatch('<div>ad</div>', container); |
| 743 |
} |
| 744 |
} |
| 745 |
|
| 746 |
function testDeleteW3CAll() { |
| 747 |
if (goog.editor.BrowserFeature.HAS_W3C_RANGES) { |
| 748 |
container.innerHTML = '<div>abcd</div>'; |
| 749 |
var range = goog.dom.Range.createFromNodes( |
| 750 |
container.firstChild.firstChild, 0, container.firstChild.firstChild, 4); |
| 751 |
range.select(); |
| 752 |
goog.editor.plugins.EnterHandler.deleteW3cRange_(range); |
| 753 |
|
| 754 |
goog.testing.dom.assertHtmlContentsMatch('<div> </div>', container); |
| 755 |
} |
| 756 |
} |
| 757 |
|
| 758 |
function testDeleteW3CPartialEnd() { |
| 759 |
if (goog.editor.BrowserFeature.HAS_W3C_RANGES) { |
| 760 |
container.innerHTML = '<div>ab</div><div>cd</div>'; |
| 761 |
var range = goog.dom.Range.createFromNodes( |
| 762 |
container.firstChild.firstChild, 1, container.lastChild.firstChild, 1); |
| 763 |
range.select(); |
| 764 |
goog.editor.plugins.EnterHandler.deleteW3cRange_(range); |
| 765 |
|
| 766 |
goog.testing.dom.assertHtmlContentsMatch('<div>ad</div>', container); |
| 767 |
} |
| 768 |
} |
| 769 |
|
| 770 |
function testDeleteW3CNonPartialEnd() { |
| 771 |
if (goog.editor.BrowserFeature.HAS_W3C_RANGES) { |
| 772 |
container.innerHTML = '<div>ab</div><div>cd</div>'; |
| 773 |
var range = goog.dom.Range.createFromNodes( |
| 774 |
container.firstChild.firstChild, 1, container.lastChild.firstChild, 2); |
| 775 |
range.select(); |
| 776 |
goog.editor.plugins.EnterHandler.deleteW3cRange_(range); |
| 777 |
|
| 778 |
goog.testing.dom.assertHtmlContentsMatch('<div>a</div>', container); |
| 779 |
} |
| 780 |
} |
| 781 |
|
| 782 |
function testIsInOneContainer() { |
| 783 |
if (goog.editor.BrowserFeature.HAS_W3C_RANGES) { |
| 784 |
container.innerHTML = '<div><br></div>'; |
| 785 |
var div = container.firstChild; |
| 786 |
var range = goog.dom.Range.createFromNodes(div, 0, div, 1); |
| 787 |
range.select(); |
| 788 |
assertTrue( |
| 789 |
'Selection must be recognized as being in one container', |
| 790 |
goog.editor.plugins.EnterHandler.isInOneContainerW3c_(range)); |
| 791 |
} |
| 792 |
} |
| 793 |
|
| 794 |
function testDeletingEndNodesWithNoNewLine() { |
| 795 |
if (goog.editor.BrowserFeature.HAS_W3C_RANGES) { |
| 796 |
container.innerHTML = |
| 797 |
'a<div>b</div><div><br></div><div>c</div><div>d</div>'; |
| 798 |
var range = goog.dom.Range.createFromNodes( |
| 799 |
container.childNodes[2], 0, container.childNodes[4].childNodes[0], 1); |
| 800 |
range.select(); |
| 801 |
var newRange = goog.editor.plugins.EnterHandler.deleteW3cRange_(range); |
| 802 |
goog.testing.dom.assertHtmlContentsMatch('a<div>b</div>', container); |
| 803 |
assertTrue(newRange.isCollapsed()); |
| 804 |
assertEquals(container, newRange.getStartNode()); |
| 805 |
assertEquals(2, newRange.getStartOffset()); |
| 806 |
} |
| 807 |
} |