| 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.nodeTest'); |
| 16 |
goog.setTestOnly('goog.editor.nodeTest'); |
| 17 |
|
| 18 |
goog.require('goog.array'); |
| 19 |
goog.require('goog.dom'); |
| 20 |
goog.require('goog.dom.NodeType'); |
| 21 |
goog.require('goog.dom.TagName'); |
| 22 |
goog.require('goog.editor.node'); |
| 23 |
goog.require('goog.style'); |
| 24 |
goog.require('goog.testing.ExpectedFailures'); |
| 25 |
goog.require('goog.testing.dom'); |
| 26 |
goog.require('goog.testing.jsunit'); |
| 27 |
goog.require('goog.userAgent'); |
| 28 |
|
| 29 |
var expectedFailures; |
| 30 |
var parentNode; |
| 31 |
var childNode1; |
| 32 |
var childNode2; |
| 33 |
var childNode3; |
| 34 |
|
| 35 |
var gChildWsNode1 = null; |
| 36 |
var gChildTextNode1 = null; |
| 37 |
var gChildNbspNode1 = null; |
| 38 |
var gChildMixedNode1 = null; |
| 39 |
var gChildWsNode2a = null; |
| 40 |
var gChildWsNode2b = null; |
| 41 |
var gChildTextNode3a = null; |
| 42 |
var gChildWsNode3 = null; |
| 43 |
var gChildTextNode3b = null; |
| 44 |
|
| 45 |
function setUpPage() { |
| 46 |
expectedFailures = new goog.testing.ExpectedFailures(); |
| 47 |
parentNode = document.getElementById('parentNode'); |
| 48 |
childNode1 = parentNode.childNodes[0]; |
| 49 |
childNode2 = parentNode.childNodes[1]; |
| 50 |
childNode3 = parentNode.childNodes[2]; |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
function tearDown() { |
| 55 |
expectedFailures.handleTearDown(); |
| 56 |
} |
| 57 |
|
| 58 |
function setUpDomTree() { |
| 59 |
gChildWsNode1 = document.createTextNode(' \t\r\n'); |
| 60 |
gChildTextNode1 = document.createTextNode('Child node'); |
| 61 |
gChildNbspNode1 = document.createTextNode('\u00a0'); |
| 62 |
gChildMixedNode1 = document.createTextNode('Text\n plus\u00a0'); |
| 63 |
gChildWsNode2a = document.createTextNode(''); |
| 64 |
gChildWsNode2b = document.createTextNode(' '); |
| 65 |
gChildTextNode3a = document.createTextNode('I am a grand child'); |
| 66 |
gChildWsNode3 = document.createTextNode(' \t \r \n'); |
| 67 |
gChildTextNode3b = document.createTextNode('I am also a grand child'); |
| 68 |
|
| 69 |
childNode3.appendChild(gChildTextNode3a); |
| 70 |
childNode3.appendChild(gChildWsNode3); |
| 71 |
childNode3.appendChild(gChildTextNode3b); |
| 72 |
|
| 73 |
childNode1.appendChild(gChildMixedNode1); |
| 74 |
childNode1.appendChild(gChildWsNode1); |
| 75 |
childNode1.appendChild(gChildNbspNode1); |
| 76 |
childNode1.appendChild(gChildTextNode1); |
| 77 |
|
| 78 |
childNode2.appendChild(gChildWsNode2a); |
| 79 |
childNode2.appendChild(gChildWsNode2b); |
| 80 |
document.body.appendChild(parentNode); |
| 81 |
} |
| 82 |
|
| 83 |
function tearDownDomTree() { |
| 84 |
goog.dom.removeChildren(childNode1); |
| 85 |
goog.dom.removeChildren(childNode2); |
| 86 |
goog.dom.removeChildren(childNode3); |
| 87 |
gChildWsNode1 = null; |
| 88 |
gChildTextNode1 = null; |
| 89 |
gChildNbspNode1 = null; |
| 90 |
gChildMixedNode1 = null; |
| 91 |
gChildWsNode2a = null; |
| 92 |
gChildWsNode2b = null; |
| 93 |
gChildTextNode3a = null; |
| 94 |
gChildWsNode3 = null; |
| 95 |
gChildTextNode3b = null; |
| 96 |
} |
| 97 |
|
| 98 |
function testGetCompatModeQuirks() { |
| 99 |
var quirksIfr = goog.dom.createElement(goog.dom.TagName.IFRAME); |
| 100 |
document.body.appendChild(quirksIfr); |
| 101 |
// Webkit used to default to standards mode, but fixed this in |
| 102 |
// Safari 4/Chrome 2, aka, WebKit 530. |
| 103 |
// Also IE10 fails here. |
| 104 |
// TODO(johnlenz): IE10+ inherit quirks mode from the owner document |
| 105 |
// according to: |
| 106 |
// http://msdn.microsoft.com/en-us/library/ff955402(v=vs.85).aspx |
| 107 |
// but this test shows different behavior for IE10 and 11. If we discover |
| 108 |
// that we care about quirks mode documents we should investigate |
| 109 |
// this failure. |
| 110 |
expectedFailures.expectFailureFor( |
| 111 |
(goog.userAgent.WEBKIT && !goog.userAgent.isVersionOrHigher('530')) || |
| 112 |
(goog.userAgent.IE && goog.userAgent.isVersionOrHigher('10') && |
| 113 |
!goog.userAgent.isVersionOrHigher('11'))); |
| 114 |
expectedFailures.run(function() { |
| 115 |
assertFalse( |
| 116 |
'Empty sourceless iframe is quirks mode, not standards mode', |
| 117 |
goog.editor.node.isStandardsMode( |
| 118 |
goog.dom.getFrameContentDocument(quirksIfr))); |
| 119 |
}); |
| 120 |
document.body.removeChild(quirksIfr); |
| 121 |
} |
| 122 |
|
| 123 |
function testGetCompatModeStandards() { |
| 124 |
var standardsIfr = goog.dom.createElement(goog.dom.TagName.IFRAME); |
| 125 |
document.body.appendChild(standardsIfr); |
| 126 |
var doc = goog.dom.getFrameContentDocument(standardsIfr); |
| 127 |
doc.open(); |
| 128 |
doc.write('<!DOCTYPE HTML><html><head></head><body> </body></html>'); |
| 129 |
doc.close(); |
| 130 |
assertTrue( |
| 131 |
'Iframe with DOCTYPE written in is standards mode', |
| 132 |
goog.editor.node.isStandardsMode(doc)); |
| 133 |
document.body.removeChild(standardsIfr); |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
/** |
| 138 |
* Creates a DOM tree and tests that getLeftMostLeaf returns proper node |
| 139 |
*/ |
| 140 |
function testGetLeftMostLeaf() { |
| 141 |
setUpDomTree(); |
| 142 |
|
| 143 |
assertEquals( |
| 144 |
'Should skip ws node', gChildMixedNode1, |
| 145 |
goog.editor.node.getLeftMostLeaf(parentNode)); |
| 146 |
assertEquals( |
| 147 |
'Should skip ws node', gChildMixedNode1, |
| 148 |
goog.editor.node.getLeftMostLeaf(childNode1)); |
| 149 |
assertEquals( |
| 150 |
'Has no non ws leaves', childNode2, |
| 151 |
goog.editor.node.getLeftMostLeaf(childNode2)); |
| 152 |
assertEquals( |
| 153 |
'Should return first child', gChildTextNode3a, |
| 154 |
goog.editor.node.getLeftMostLeaf(childNode3)); |
| 155 |
assertEquals( |
| 156 |
'Has no children', gChildTextNode1, |
| 157 |
goog.editor.node.getLeftMostLeaf(gChildTextNode1)); |
| 158 |
|
| 159 |
tearDownDomTree(); |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
/** |
| 164 |
* Creates a DOM tree and tests that getRightMostLeaf returns proper node |
| 165 |
*/ |
| 166 |
function testGetRightMostLeaf() { |
| 167 |
setUpDomTree(); |
| 168 |
|
| 169 |
assertEquals( |
| 170 |
"Should return child3's rightmost child", gChildTextNode3b, |
| 171 |
goog.editor.node.getRightMostLeaf(parentNode)); |
| 172 |
assertEquals( |
| 173 |
'Should skip ws node', gChildTextNode1, |
| 174 |
goog.editor.node.getRightMostLeaf(childNode1)); |
| 175 |
assertEquals( |
| 176 |
'Has no non ws leaves', childNode2, |
| 177 |
goog.editor.node.getRightMostLeaf(childNode2)); |
| 178 |
assertEquals( |
| 179 |
'Should return last child', gChildTextNode3b, |
| 180 |
goog.editor.node.getRightMostLeaf(childNode3)); |
| 181 |
assertEquals( |
| 182 |
'Has no children', gChildTextNode1, |
| 183 |
goog.editor.node.getRightMostLeaf(gChildTextNode1)); |
| 184 |
|
| 185 |
tearDownDomTree(); |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
/** |
| 190 |
* Creates a DOM tree and tests that getFirstChild properly ignores |
| 191 |
* ignorable nodes |
| 192 |
*/ |
| 193 |
function testGetFirstChild() { |
| 194 |
setUpDomTree(); |
| 195 |
|
| 196 |
assertNull( |
| 197 |
'Has no none ws children', goog.editor.node.getFirstChild(childNode2)); |
| 198 |
assertEquals( |
| 199 |
'Should skip first child, as it is ws', gChildMixedNode1, |
| 200 |
goog.editor.node.getFirstChild(childNode1)); |
| 201 |
assertEquals( |
| 202 |
'Should just return first child', gChildTextNode3a, |
| 203 |
goog.editor.node.getFirstChild(childNode3)); |
| 204 |
assertEquals( |
| 205 |
'Should return first child', childNode1, |
| 206 |
goog.editor.node.getFirstChild(parentNode)); |
| 207 |
|
| 208 |
assertNull( |
| 209 |
'First child of a text node should return null', |
| 210 |
goog.editor.node.getFirstChild(gChildTextNode1)); |
| 211 |
assertNull( |
| 212 |
'First child of null should return null', |
| 213 |
goog.editor.node.getFirstChild(null)); |
| 214 |
|
| 215 |
tearDownDomTree(); |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
/** |
| 220 |
* Create a DOM tree and test that getLastChild properly ignores |
| 221 |
* ignorable nodes |
| 222 |
*/ |
| 223 |
function testGetLastChild() { |
| 224 |
setUpDomTree(); |
| 225 |
|
| 226 |
assertNull( |
| 227 |
'Has no none ws children', goog.editor.node.getLastChild(childNode2)); |
| 228 |
assertEquals( |
| 229 |
'Should skip last child, as it is ws', gChildTextNode1, |
| 230 |
goog.editor.node.getLastChild(childNode1)); |
| 231 |
assertEquals( |
| 232 |
'Should just return last child', gChildTextNode3b, |
| 233 |
goog.editor.node.getLastChild(childNode3)); |
| 234 |
assertEquals( |
| 235 |
'Should return last child', childNode3, |
| 236 |
goog.editor.node.getLastChild(parentNode)); |
| 237 |
|
| 238 |
assertNull( |
| 239 |
'Last child of a text node should return null', |
| 240 |
goog.editor.node.getLastChild(gChildTextNode1)); |
| 241 |
assertNull( |
| 242 |
'Last child of null should return null', |
| 243 |
goog.editor.node.getLastChild(gChildTextNode1)); |
| 244 |
|
| 245 |
tearDownDomTree(); |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
/** |
| 250 |
* Test if nodes that should be ignorable return false and nodes that should |
| 251 |
* not be ignored return true. |
| 252 |
*/ |
| 253 |
function testIsImportant() { |
| 254 |
var wsNode = document.createTextNode(' \t\r\n'); |
| 255 |
assertFalse( |
| 256 |
'White space node is ignorable', goog.editor.node.isImportant(wsNode)); |
| 257 |
var textNode = document.createTextNode('Hello'); |
| 258 |
assertTrue('Text node is important', goog.editor.node.isImportant(textNode)); |
| 259 |
var nbspNode = document.createTextNode('\u00a0'); |
| 260 |
assertTrue( |
| 261 |
'Node with nbsp is important', goog.editor.node.isImportant(nbspNode)); |
| 262 |
var imageNode = goog.dom.createElement(goog.dom.TagName.IMG); |
| 263 |
assertTrue( |
| 264 |
'Image node is important', goog.editor.node.isImportant(imageNode)); |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
/** |
| 269 |
* Test that isAllNonNbspWhiteSpace returns true if node contains only |
| 270 |
* whitespace that is not nbsp and false otherwise |
| 271 |
*/ |
| 272 |
function testIsAllNonNbspWhiteSpace() { |
| 273 |
var wsNode = document.createTextNode(' \t\r\n'); |
| 274 |
assertTrue( |
| 275 |
'String is all non nbsp', |
| 276 |
goog.editor.node.isAllNonNbspWhiteSpace(wsNode)); |
| 277 |
var textNode = document.createTextNode('Hello'); |
| 278 |
assertFalse( |
| 279 |
'String should not be whitespace', |
| 280 |
goog.editor.node.isAllNonNbspWhiteSpace(textNode)); |
| 281 |
var nbspNode = document.createTextNode('\u00a0'); |
| 282 |
assertFalse( |
| 283 |
'String has nbsp', goog.editor.node.isAllNonNbspWhiteSpace(nbspNode)); |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
/** |
| 288 |
* Creates a DOM tree and Test that getPreviousSibling properly ignores |
| 289 |
* ignorable nodes |
| 290 |
*/ |
| 291 |
function testGetPreviousSibling() { |
| 292 |
setUpDomTree(); |
| 293 |
|
| 294 |
assertNull( |
| 295 |
'No previous sibling', |
| 296 |
goog.editor.node.getPreviousSibling(gChildTextNode3a)); |
| 297 |
assertEquals( |
| 298 |
'Should have text sibling', gChildTextNode3a, |
| 299 |
goog.editor.node.getPreviousSibling(gChildWsNode3)); |
| 300 |
assertEquals( |
| 301 |
'Should skip over white space sibling', gChildTextNode3a, |
| 302 |
goog.editor.node.getPreviousSibling(gChildTextNode3b)); |
| 303 |
assertNull( |
| 304 |
'No previous sibling', |
| 305 |
goog.editor.node.getPreviousSibling(gChildMixedNode1)); |
| 306 |
assertEquals( |
| 307 |
'Should have mixed text sibling', gChildMixedNode1, |
| 308 |
goog.editor.node.getPreviousSibling(gChildWsNode1)); |
| 309 |
assertEquals( |
| 310 |
'Should skip over white space sibling', gChildMixedNode1, |
| 311 |
goog.editor.node.getPreviousSibling(gChildNbspNode1)); |
| 312 |
assertNotEquals( |
| 313 |
'Should not move past ws and nbsp', gChildMixedNode1, |
| 314 |
goog.editor.node.getPreviousSibling(gChildTextNode1)); |
| 315 |
assertEquals( |
| 316 |
'Should go to child 2', childNode2, |
| 317 |
goog.editor.node.getPreviousSibling(childNode3)); |
| 318 |
assertEquals( |
| 319 |
'Should go to child 1', childNode1, |
| 320 |
goog.editor.node.getPreviousSibling(childNode2)); |
| 321 |
assertNull( |
| 322 |
'Only has white space siblings', |
| 323 |
goog.editor.node.getPreviousSibling(gChildWsNode2b)); |
| 324 |
|
| 325 |
tearDownDomTree(); |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
/** |
| 330 |
* Creates a DOM tree and tests that getNextSibling properly ignores igrnorable |
| 331 |
* nodes when determining the next sibling |
| 332 |
*/ |
| 333 |
function testGetNextSibling() { |
| 334 |
setUpDomTree(); |
| 335 |
|
| 336 |
assertEquals( |
| 337 |
'Child 1 should have Child 2', childNode2, |
| 338 |
goog.editor.node.getNextSibling(childNode1)); |
| 339 |
assertEquals( |
| 340 |
'Child 2 should have child 3', childNode3, |
| 341 |
goog.editor.node.getNextSibling(childNode2)); |
| 342 |
assertNull( |
| 343 |
'Child 3 has no next sibling', |
| 344 |
goog.editor.node.getNextSibling(childNode3)); |
| 345 |
assertNotEquals( |
| 346 |
'Should not skip ws and nbsp nodes', gChildTextNode1, |
| 347 |
goog.editor.node.getNextSibling(gChildMixedNode1)); |
| 348 |
assertNotEquals( |
| 349 |
'Should not skip nbsp node', gChildTextNode1, |
| 350 |
goog.editor.node.getNextSibling(gChildWsNode1)); |
| 351 |
assertEquals( |
| 352 |
'Should have sibling', gChildTextNode1, |
| 353 |
goog.editor.node.getNextSibling(gChildNbspNode1)); |
| 354 |
assertNull( |
| 355 |
'Should have no next sibling', |
| 356 |
goog.editor.node.getNextSibling(gChildTextNode1)); |
| 357 |
assertNull( |
| 358 |
'Only has ws sibling', goog.editor.node.getNextSibling(gChildWsNode2a)); |
| 359 |
assertNull( |
| 360 |
'Has no next sibling', goog.editor.node.getNextSibling(gChildWsNode2b)); |
| 361 |
assertEquals( |
| 362 |
'Should skip ws node', gChildTextNode3b, |
| 363 |
goog.editor.node.getNextSibling(gChildTextNode3a)); |
| 364 |
|
| 365 |
tearDownDomTree(); |
| 366 |
} |
| 367 |
|
| 368 |
|
| 369 |
function testIsEmpty() { |
| 370 |
var textNode = document.createTextNode(''); |
| 371 |
assertTrue( |
| 372 |
'Text node with no content should be empty', |
| 373 |
goog.editor.node.isEmpty(textNode)); |
| 374 |
textNode.data = '\xa0'; |
| 375 |
assertTrue( |
| 376 |
'Text node with nbsp should be empty', |
| 377 |
goog.editor.node.isEmpty(textNode)); |
| 378 |
assertFalse( |
| 379 |
'Text node with nbsp should not be empty when prohibited', |
| 380 |
goog.editor.node.isEmpty(textNode, true)); |
| 381 |
|
| 382 |
textNode.data = ' '; |
| 383 |
assertTrue( |
| 384 |
'Text node with whitespace should be empty', |
| 385 |
goog.editor.node.isEmpty(textNode)); |
| 386 |
textNode.data = 'notEmpty'; |
| 387 |
assertFalse( |
| 388 |
'Text node with text should not be empty', |
| 389 |
goog.editor.node.isEmpty(textNode)); |
| 390 |
|
| 391 |
var div = goog.dom.createElement(goog.dom.TagName.DIV); |
| 392 |
assertTrue('Empty div should be empty', goog.editor.node.isEmpty(div)); |
| 393 |
div.innerHTML = '<iframe></iframe>'; |
| 394 |
assertFalse( |
| 395 |
'Div containing an iframe is not empty', goog.editor.node.isEmpty(div)); |
| 396 |
div.innerHTML = '<img></img>'; |
| 397 |
assertFalse( |
| 398 |
'Div containing an image is not empty', goog.editor.node.isEmpty(div)); |
| 399 |
div.innerHTML = '<embed></embed>'; |
| 400 |
assertFalse( |
| 401 |
'Div containing an embed is not empty', goog.editor.node.isEmpty(div)); |
| 402 |
div.innerHTML = '<div><span></span></div>'; |
| 403 |
assertTrue( |
| 404 |
'Div containing other empty tags is empty', |
| 405 |
goog.editor.node.isEmpty(div)); |
| 406 |
div.innerHTML = '<div><span> </span></div>'; |
| 407 |
assertTrue( |
| 408 |
'Div containing other empty tags and whitespace is empty', |
| 409 |
goog.editor.node.isEmpty(div)); |
| 410 |
div.innerHTML = '<div><span>Not empty</span></div>'; |
| 411 |
assertFalse( |
| 412 |
'Div containing tags and text is not empty', |
| 413 |
goog.editor.node.isEmpty(div)); |
| 414 |
|
| 415 |
var img = goog.dom.createElement(goog.dom.TagName.IMG); |
| 416 |
assertFalse('Empty img should not be empty', goog.editor.node.isEmpty(img)); |
| 417 |
|
| 418 |
var iframe = goog.dom.createElement(goog.dom.TagName.IFRAME); |
| 419 |
assertFalse( |
| 420 |
'Empty iframe should not be empty', goog.editor.node.isEmpty(iframe)); |
| 421 |
|
| 422 |
var embed = goog.dom.createElement(goog.dom.TagName.EMBED); |
| 423 |
assertFalse( |
| 424 |
'Empty embed should not be empty', goog.editor.node.isEmpty(embed)); |
| 425 |
} |
| 426 |
|
| 427 |
|
| 428 |
/** |
| 429 |
* Test that getLength returns 0 if the node has no length and no children, |
| 430 |
* the # of children if the node has no length but does have children, |
| 431 |
* and the length of the node if the node does have length |
| 432 |
*/ |
| 433 |
function testGetLength() { |
| 434 |
var parentNode = goog.dom.createElement(goog.dom.TagName.P); |
| 435 |
|
| 436 |
assertEquals( |
| 437 |
'Length 0 and no children', 0, goog.editor.node.getLength(parentNode)); |
| 438 |
|
| 439 |
var childNode1 = document.createTextNode('node 1'); |
| 440 |
var childNode2 = document.createTextNode('node number 2'); |
| 441 |
var childNode3 = document.createTextNode(''); |
| 442 |
parentNode.appendChild(childNode1); |
| 443 |
parentNode.appendChild(childNode2); |
| 444 |
parentNode.appendChild(childNode3); |
| 445 |
assertEquals( |
| 446 |
'Length 0 and 3 children', 3, goog.editor.node.getLength(parentNode)); |
| 447 |
assertEquals( |
| 448 |
'Text node, length 6', 6, goog.editor.node.getLength(childNode1)); |
| 449 |
assertEquals( |
| 450 |
'Text node, length 0', 0, goog.editor.node.getLength(childNode3)); |
| 451 |
} |
| 452 |
|
| 453 |
function testFindInChildrenSuccess() { |
| 454 |
var parentNode = goog.dom.createElement(goog.dom.TagName.DIV); |
| 455 |
parentNode.innerHTML = '<div>foo</div><b>foo2</b>'; |
| 456 |
|
| 457 |
var index = goog.editor.node.findInChildren(parentNode, function(node) { |
| 458 |
return node.tagName == goog.dom.TagName.B; |
| 459 |
}); |
| 460 |
assertEquals('Should find second child', index, 1); |
| 461 |
} |
| 462 |
|
| 463 |
function testFindInChildrenFailure() { |
| 464 |
var parentNode = goog.dom.createElement(goog.dom.TagName.DIV); |
| 465 |
parentNode.innerHTML = '<div>foo</div><b>foo2</b>'; |
| 466 |
|
| 467 |
var index = goog.editor.node.findInChildren( |
| 468 |
parentNode, function(node) { return false; }); |
| 469 |
assertNull("Shouldn't find a child", index); |
| 470 |
} |
| 471 |
|
| 472 |
function testFindHighestMatchingAncestor() { |
| 473 |
setUpDomTree(); |
| 474 |
var predicateFunc = function(node) { |
| 475 |
return node.tagName == goog.dom.TagName.DIV; |
| 476 |
}; |
| 477 |
var node = goog.editor.node.findHighestMatchingAncestor( |
| 478 |
gChildTextNode3a, predicateFunc); |
| 479 |
assertNotNull('Should return an ancestor', node); |
| 480 |
assertEquals( |
| 481 |
'Should have found "parentNode" as the last ' + |
| 482 |
'ancestor matching the predicate', |
| 483 |
parentNode, node); |
| 484 |
|
| 485 |
predicateFunc = function(node) { return node.childNodes.length == 1; }; |
| 486 |
node = goog.editor.node.findHighestMatchingAncestor( |
| 487 |
gChildTextNode3a, predicateFunc); |
| 488 |
assertNull("Shouldn't return an ancestor", node); |
| 489 |
|
| 490 |
tearDownDomTree(); |
| 491 |
} |
| 492 |
|
| 493 |
function testIsBlock() { |
| 494 |
var blockDisplays = [ |
| 495 |
'block', 'list-item', 'table', 'table-caption', 'table-cell', |
| 496 |
'table-column', 'table-column-group', 'table-footer', 'table-footer-group', |
| 497 |
'table-header-group', 'table-row', 'table-row-group' |
| 498 |
]; |
| 499 |
|
| 500 |
var structuralTags = [ |
| 501 |
goog.dom.TagName.BODY, goog.dom.TagName.FRAME, goog.dom.TagName.FRAMESET, |
| 502 |
goog.dom.TagName.HEAD, goog.dom.TagName.HTML |
| 503 |
]; |
| 504 |
|
| 505 |
// The following tags are considered inline in IE, except LEGEND which is |
| 506 |
// only a block element in WEBKIT. |
| 507 |
var ambiguousTags = [ |
| 508 |
goog.dom.TagName.DETAILS, goog.dom.TagName.HR, goog.dom.TagName.ISINDEX, |
| 509 |
goog.dom.TagName.LEGEND, goog.dom.TagName.MAIN, goog.dom.TagName.MAP, |
| 510 |
goog.dom.TagName.NOFRAMES, goog.dom.TagName.OPTGROUP, |
| 511 |
goog.dom.TagName.OPTION, goog.dom.TagName.SUMMARY |
| 512 |
]; |
| 513 |
|
| 514 |
// Older versions of IE and Gecko consider the following elements to be |
| 515 |
// inline, but IE9+ and Gecko 2.0+ recognize the new elements. |
| 516 |
var legacyAmbiguousTags = [ |
| 517 |
goog.dom.TagName.ARTICLE, goog.dom.TagName.ASIDE, |
| 518 |
goog.dom.TagName.FIGCAPTION, goog.dom.TagName.FIGURE, |
| 519 |
goog.dom.TagName.FOOTER, goog.dom.TagName.HEADER, goog.dom.TagName.HGROUP, |
| 520 |
goog.dom.TagName.NAV, goog.dom.TagName.SECTION |
| 521 |
]; |
| 522 |
|
| 523 |
var tagsToIgnore = goog.array.flatten(structuralTags, ambiguousTags); |
| 524 |
|
| 525 |
if ((goog.userAgent.IE && !goog.userAgent.isDocumentModeOrHigher(9)) || |
| 526 |
(goog.userAgent.GECKO && !goog.userAgent.isVersionOrHigher('2'))) { |
| 527 |
goog.array.extend(tagsToIgnore, legacyAmbiguousTags); |
| 528 |
} |
| 529 |
|
| 530 |
// Appending an applet tag can cause the test to hang if Java is blocked on |
| 531 |
// the system. |
| 532 |
tagsToIgnore.push(goog.dom.TagName.APPLET); |
| 533 |
|
| 534 |
// Appending an embed tag to the page in IE brings up a warning dialog about |
| 535 |
// loading Java content. |
| 536 |
if (goog.userAgent.IE) { |
| 537 |
tagsToIgnore.push(goog.dom.TagName.EMBED); |
| 538 |
} |
| 539 |
|
| 540 |
var failures = []; |
| 541 |
for (var tag in goog.dom.TagName) { |
| 542 |
if (goog.array.contains(tagsToIgnore, goog.dom.TagName[tag])) { |
| 543 |
continue; |
| 544 |
} |
| 545 |
|
| 546 |
var el = goog.dom.createElement(tag); |
| 547 |
document.body.appendChild(el); |
| 548 |
var display = goog.style.getCascadedStyle(el, 'display') || |
| 549 |
goog.style.getComputedStyle(el, 'display'); |
| 550 |
goog.dom.removeNode(el); |
| 551 |
|
| 552 |
if (goog.editor.node.isBlockTag(el)) { |
| 553 |
if (!goog.array.contains(blockDisplays, display)) { |
| 554 |
failures.push('Display for ' + tag + ' should be block-like'); |
| 555 |
} |
| 556 |
} else { |
| 557 |
if (goog.array.contains(blockDisplays, display)) { |
| 558 |
failures.push('Display for ' + tag + ' should not be block-like'); |
| 559 |
} |
| 560 |
} |
| 561 |
} |
| 562 |
if (failures.length) { |
| 563 |
fail(failures.join('\n')); |
| 564 |
} |
| 565 |
} |
| 566 |
|
| 567 |
function createDivWithTextNodes(var_args) { |
| 568 |
var dom = goog.dom.createDom(goog.dom.TagName.DIV); |
| 569 |
for (var i = 0; i < arguments.length; i++) { |
| 570 |
goog.dom.appendChild(dom, goog.dom.createTextNode(arguments[i])); |
| 571 |
} |
| 572 |
return dom; |
| 573 |
} |
| 574 |
|
| 575 |
function testSkipEmptyTextNodes() { |
| 576 |
assertNull( |
| 577 |
'skipEmptyTextNodes should gracefully handle null', |
| 578 |
goog.editor.node.skipEmptyTextNodes(null)); |
| 579 |
|
| 580 |
var dom1 = createDivWithTextNodes('abc', '', 'xyz', '', ''); |
| 581 |
assertEquals( |
| 582 |
'expected not to skip first child', dom1.firstChild, |
| 583 |
goog.editor.node.skipEmptyTextNodes(dom1.firstChild)); |
| 584 |
assertEquals( |
| 585 |
'expected to skip second child', dom1.childNodes[2], |
| 586 |
goog.editor.node.skipEmptyTextNodes(dom1.childNodes[1])); |
| 587 |
assertNull( |
| 588 |
'expected to skip all the rest of the children', |
| 589 |
goog.editor.node.skipEmptyTextNodes(dom1.childNodes[3])); |
| 590 |
} |
| 591 |
|
| 592 |
function testIsEditableContainer() { |
| 593 |
var editableContainerElement = document.getElementById('editableTest'); |
| 594 |
assertTrue( |
| 595 |
'Container element should be considered editable container', |
| 596 |
goog.editor.node.isEditableContainer(editableContainerElement)); |
| 597 |
|
| 598 |
var nonEditableContainerElement = document.getElementById('parentNode'); |
| 599 |
assertFalse( |
| 600 |
'Other element should not be considered editable container', |
| 601 |
goog.editor.node.isEditableContainer(nonEditableContainerElement)); |
| 602 |
} |
| 603 |
|
| 604 |
function testIsEditable() { |
| 605 |
var editableContainerElement = document.getElementById('editableTest'); |
| 606 |
var childNode = editableContainerElement.firstChild; |
| 607 |
var childElement = goog.dom.getElementsByTagName( |
| 608 |
goog.dom.TagName.SPAN, editableContainerElement)[0]; |
| 609 |
|
| 610 |
assertFalse( |
| 611 |
'Container element should not be considered editable', |
| 612 |
goog.editor.node.isEditable(editableContainerElement)); |
| 613 |
assertTrue( |
| 614 |
'Child text node should be considered editable', |
| 615 |
goog.editor.node.isEditable(childNode)); |
| 616 |
assertTrue( |
| 617 |
'Child element should be considered editable', |
| 618 |
goog.editor.node.isEditable(childElement)); |
| 619 |
assertTrue( |
| 620 |
'Grandchild node should be considered editable', |
| 621 |
goog.editor.node.isEditable(childElement.firstChild)); |
| 622 |
assertFalse( |
| 623 |
'Other element should not be considered editable', |
| 624 |
goog.editor.node.isEditable(document.getElementById('parentNode'))); |
| 625 |
} |
| 626 |
|
| 627 |
function testFindTopMostEditableAncestor() { |
| 628 |
var root = document.getElementById('editableTest'); |
| 629 |
var span = goog.dom.getElementsByTagName(goog.dom.TagName.SPAN, root)[0]; |
| 630 |
var textNode = span.firstChild; |
| 631 |
|
| 632 |
assertEquals( |
| 633 |
'Should return self if self is matched.', textNode, |
| 634 |
goog.editor.node.findTopMostEditableAncestor(textNode, function(node) { |
| 635 |
return node.nodeType == goog.dom.NodeType.TEXT; |
| 636 |
})); |
| 637 |
assertEquals( |
| 638 |
'Should not walk out of editable node.', null, |
| 639 |
goog.editor.node.findTopMostEditableAncestor(textNode, function(node) { |
| 640 |
return node.tagName == goog.dom.TagName.BODY; |
| 641 |
})); |
| 642 |
assertEquals( |
| 643 |
'Should not match editable container.', null, |
| 644 |
goog.editor.node.findTopMostEditableAncestor(textNode, function(node) { |
| 645 |
return node.tagName == goog.dom.TagName.DIV; |
| 646 |
})); |
| 647 |
assertEquals( |
| 648 |
'Should find node in editable container.', span, |
| 649 |
goog.editor.node.findTopMostEditableAncestor(textNode, function(node) { |
| 650 |
return node.tagName == goog.dom.TagName.SPAN; |
| 651 |
})); |
| 652 |
} |
| 653 |
|
| 654 |
function testSplitDomTreeAt() { |
| 655 |
var innerHTML = '<p>1<b>2</b>3</p>'; |
| 656 |
var root = goog.dom.createElement(goog.dom.TagName.DIV); |
| 657 |
|
| 658 |
root.innerHTML = innerHTML; |
| 659 |
var result = goog.editor.node.splitDomTreeAt( |
| 660 |
goog.dom.getElementsByTagName(goog.dom.TagName.B, root)[0], null, root); |
| 661 |
goog.testing.dom.assertHtmlContentsMatch('<p>1<b>2</b></p>', root); |
| 662 |
goog.testing.dom.assertHtmlContentsMatch('<p>3</p>', result); |
| 663 |
|
| 664 |
root.innerHTML = innerHTML; |
| 665 |
result = goog.editor.node.splitDomTreeAt( |
| 666 |
goog.dom.getElementsByTagName(goog.dom.TagName.B, root)[0], |
| 667 |
goog.dom.createTextNode('and'), root); |
| 668 |
goog.testing.dom.assertHtmlContentsMatch('<p>1<b>2</b></p>', root); |
| 669 |
goog.testing.dom.assertHtmlContentsMatch('<p>and3</p>', result); |
| 670 |
} |
| 671 |
|
| 672 |
function testTransferChildren() { |
| 673 |
var prefix = '<b>Bold 1</b>'; |
| 674 |
var innerHTML = '<b>Bold</b><ul><li>Item 1</li><li>Item 2</li></ul>'; |
| 675 |
|
| 676 |
var root1 = goog.dom.createElement(goog.dom.TagName.DIV); |
| 677 |
root1.innerHTML = innerHTML; |
| 678 |
|
| 679 |
var root2 = goog.dom.createElement(goog.dom.TagName.P); |
| 680 |
root2.innerHTML = prefix; |
| 681 |
|
| 682 |
var b = goog.dom.getElementsByTagName(goog.dom.TagName.B, root1)[0]; |
| 683 |
|
| 684 |
// Transfer the children. |
| 685 |
goog.editor.node.transferChildren(root2, root1); |
| 686 |
assertEquals(0, root1.childNodes.length); |
| 687 |
goog.testing.dom.assertHtmlContentsMatch(prefix + innerHTML, root2); |
| 688 |
assertEquals(b, goog.dom.getElementsByTagName(goog.dom.TagName.B, root2)[1]); |
| 689 |
|
| 690 |
// Transfer them back. |
| 691 |
goog.editor.node.transferChildren(root1, root2); |
| 692 |
assertEquals(0, root2.childNodes.length); |
| 693 |
goog.testing.dom.assertHtmlContentsMatch(prefix + innerHTML, root1); |
| 694 |
assertEquals(b, goog.dom.getElementsByTagName(goog.dom.TagName.B, root1)[1]); |
| 695 |
} |