| 1 |
// Copyright 2009 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 |
/** |
| 16 |
* @fileoverview Shared code for dom_test.html and dom_quirks_test.html. |
| 17 |
*/ |
| 18 |
|
| 19 |
/** @suppress {extraProvide} */ |
| 20 |
goog.provide('goog.dom.dom_test'); |
| 21 |
|
| 22 |
goog.require('goog.array'); |
| 23 |
goog.require('goog.dom'); |
| 24 |
goog.require('goog.dom.BrowserFeature'); |
| 25 |
goog.require('goog.dom.DomHelper'); |
| 26 |
goog.require('goog.dom.InputType'); |
| 27 |
goog.require('goog.dom.NodeType'); |
| 28 |
goog.require('goog.dom.TagName'); |
| 29 |
goog.require('goog.functions'); |
| 30 |
goog.require('goog.html.SafeUrl'); |
| 31 |
goog.require('goog.html.testing'); |
| 32 |
goog.require('goog.object'); |
| 33 |
goog.require('goog.string.Const'); |
| 34 |
goog.require('goog.string.Unicode'); |
| 35 |
goog.require('goog.testing.PropertyReplacer'); |
| 36 |
goog.require('goog.testing.asserts'); |
| 37 |
goog.require('goog.testing.jsunit'); |
| 38 |
goog.require('goog.userAgent'); |
| 39 |
goog.require('goog.userAgent.product'); |
| 40 |
goog.require('goog.userAgent.product.isVersion'); |
| 41 |
|
| 42 |
goog.setTestOnly('dom_test'); |
| 43 |
|
| 44 |
var $ = goog.dom.getElement; |
| 45 |
|
| 46 |
var divForTestingScrolling; |
| 47 |
var myIframe; |
| 48 |
var myIframeDoc; |
| 49 |
var stubs; |
| 50 |
|
| 51 |
function setUpPage() { |
| 52 |
stubs = new goog.testing.PropertyReplacer(); |
| 53 |
divForTestingScrolling = goog.dom.createElement(goog.dom.TagName.DIV); |
| 54 |
divForTestingScrolling.style.width = '5000px'; |
| 55 |
divForTestingScrolling.style.height = '5000px'; |
| 56 |
document.body.appendChild(divForTestingScrolling); |
| 57 |
|
| 58 |
// Setup for the iframe |
| 59 |
myIframe = $('myIframe'); |
| 60 |
myIframeDoc = goog.dom.getFrameContentDocument( |
| 61 |
/** @type {HTMLIFrameElement} */ (myIframe)); |
| 62 |
|
| 63 |
// Set up document for iframe: total height of elements in document is 65 |
| 64 |
// If the elements are not create like below, IE will get a wrong height for |
| 65 |
// the document. |
| 66 |
myIframeDoc.open(); |
| 67 |
// Make sure we progate the compat mode |
| 68 |
myIframeDoc.write( |
| 69 |
(goog.dom.isCss1CompatMode() ? '<!DOCTYPE html>' : '') + |
| 70 |
'<style>body{margin:0;padding:0}</style>' + |
| 71 |
'<div style="height:42px;font-size:1px;line-height:0;">' + |
| 72 |
'hello world</div>' + |
| 73 |
'<div style="height:23px;font-size:1px;line-height:0;">' + |
| 74 |
'hello world</div>'); |
| 75 |
myIframeDoc.close(); |
| 76 |
} |
| 77 |
|
| 78 |
function tearDownPage() { |
| 79 |
document.body.removeChild(divForTestingScrolling); |
| 80 |
} |
| 81 |
|
| 82 |
function tearDown() { |
| 83 |
window.scrollTo(0, 0); |
| 84 |
stubs.reset(); |
| 85 |
} |
| 86 |
|
| 87 |
function testDom() { |
| 88 |
assert('Dom library exists', typeof goog.dom != 'undefined'); |
| 89 |
} |
| 90 |
|
| 91 |
function testGetElement() { |
| 92 |
var el = $('testEl'); |
| 93 |
assertEquals('Should be able to get id', el.id, 'testEl'); |
| 94 |
|
| 95 |
assertEquals($, goog.dom.getElement); |
| 96 |
assertEquals(goog.dom.$, goog.dom.getElement); |
| 97 |
} |
| 98 |
|
| 99 |
function testGetElementDomHelper() { |
| 100 |
var domHelper = new goog.dom.DomHelper(); |
| 101 |
var el = domHelper.getElement('testEl'); |
| 102 |
assertEquals('Should be able to get id', el.id, 'testEl'); |
| 103 |
} |
| 104 |
|
| 105 |
function testGetRequiredElement() { |
| 106 |
var el = goog.dom.getRequiredElement('testEl'); |
| 107 |
assertTrue(goog.isDefAndNotNull(el)); |
| 108 |
assertEquals('testEl', el.id); |
| 109 |
assertThrows(function() { goog.dom.getRequiredElement('does_not_exist'); }); |
| 110 |
} |
| 111 |
|
| 112 |
function testGetRequiredElementDomHelper() { |
| 113 |
var domHelper = new goog.dom.DomHelper(); |
| 114 |
var el = domHelper.getRequiredElement('testEl'); |
| 115 |
assertTrue(goog.isDefAndNotNull(el)); |
| 116 |
assertEquals('testEl', el.id); |
| 117 |
assertThrows(function() { |
| 118 |
goog.dom.getRequiredElementByClass('does_not_exist', container); |
| 119 |
}); |
| 120 |
} |
| 121 |
|
| 122 |
function testGetRequiredElementByClassDomHelper() { |
| 123 |
var domHelper = new goog.dom.DomHelper(); |
| 124 |
assertNotNull(domHelper.getRequiredElementByClass('test1')); |
| 125 |
assertNotNull(domHelper.getRequiredElementByClass('test2')); |
| 126 |
|
| 127 |
var container = domHelper.getElement('span-container'); |
| 128 |
assertNotNull(domHelper.getElementByClass('test1', container)); |
| 129 |
assertThrows(function() { |
| 130 |
domHelper.getRequiredElementByClass('does_not_exist', container); |
| 131 |
}); |
| 132 |
} |
| 133 |
|
| 134 |
function testGetElementsByTagName() { |
| 135 |
var divs = goog.dom.getElementsByTagName(goog.dom.TagName.DIV); |
| 136 |
assertTrue(divs.length > 0); |
| 137 |
var el = goog.dom.getRequiredElement('testEl'); |
| 138 |
var spans = goog.dom.getElementsByTagName(goog.dom.TagName.SPAN, el); |
| 139 |
assertTrue(spans.length > 0); |
| 140 |
} |
| 141 |
|
| 142 |
function testGetElementsByTagNameDomHelper() { |
| 143 |
var domHelper = new goog.dom.DomHelper(); |
| 144 |
var divs = domHelper.getElementsByTagName(goog.dom.TagName.DIV); |
| 145 |
assertTrue(divs.length > 0); |
| 146 |
var el = domHelper.getRequiredElement('testEl'); |
| 147 |
var spans = domHelper.getElementsByTagName(goog.dom.TagName.SPAN, el); |
| 148 |
assertTrue(spans.length > 0); |
| 149 |
} |
| 150 |
|
| 151 |
function testGetElementsByTagNameAndClass() { |
| 152 |
assertEquals( |
| 153 |
'Should get 6 spans', |
| 154 |
goog.dom.getElementsByTagNameAndClass(goog.dom.TagName.SPAN).length, 6); |
| 155 |
assertEquals( |
| 156 |
'Should get 6 spans', |
| 157 |
goog.dom.getElementsByTagNameAndClass(goog.dom.TagName.SPAN).length, 6); |
| 158 |
assertEquals( |
| 159 |
'Should get 3 spans', |
| 160 |
goog.dom.getElementsByTagNameAndClass(goog.dom.TagName.SPAN, 'test1') |
| 161 |
.length, |
| 162 |
3); |
| 163 |
assertEquals( |
| 164 |
'Should get 1 span', |
| 165 |
goog.dom.getElementsByTagNameAndClass(goog.dom.TagName.SPAN, 'test2') |
| 166 |
.length, |
| 167 |
1); |
| 168 |
assertEquals( |
| 169 |
'Should get 1 span', |
| 170 |
goog.dom.getElementsByTagNameAndClass(goog.dom.TagName.SPAN, 'test2') |
| 171 |
.length, |
| 172 |
1); |
| 173 |
assertEquals( |
| 174 |
'Should get lots of elements', |
| 175 |
goog.dom.getElementsByTagNameAndClass().length, |
| 176 |
document.getElementsByTagName('*').length); |
| 177 |
|
| 178 |
assertEquals( |
| 179 |
'Should get 1 span', goog.dom |
| 180 |
.getElementsByTagNameAndClass( |
| 181 |
goog.dom.TagName.SPAN, null, $('testEl')) |
| 182 |
.length, |
| 183 |
1); |
| 184 |
|
| 185 |
// '*' as the tag name should be equivalent to all tags |
| 186 |
var container = goog.dom.getElement('span-container'); |
| 187 |
assertEquals( |
| 188 |
5, |
| 189 |
goog.dom.getElementsByTagNameAndClass('*', undefined, container).length); |
| 190 |
assertEquals( |
| 191 |
3, goog.dom.getElementsByTagNameAndClass('*', 'test1', container).length); |
| 192 |
assertEquals( |
| 193 |
1, goog.dom.getElementsByTagNameAndClass('*', 'test2', container).length); |
| 194 |
|
| 195 |
// Some version of WebKit have problems with mixed-case class names |
| 196 |
assertEquals( |
| 197 |
1, goog.dom.getElementsByTagNameAndClass(undefined, 'mixedCaseClass') |
| 198 |
.length); |
| 199 |
|
| 200 |
// Make sure that out of bounds indices are OK |
| 201 |
assertUndefined( |
| 202 |
goog.dom.getElementsByTagNameAndClass(undefined, 'noSuchClass')[0]); |
| 203 |
|
| 204 |
assertEquals( |
| 205 |
goog.dom.getElementsByTagNameAndClass, |
| 206 |
goog.dom.getElementsByTagNameAndClass); |
| 207 |
} |
| 208 |
|
| 209 |
function testGetElementsByClass() { |
| 210 |
assertEquals(3, goog.dom.getElementsByClass('test1').length); |
| 211 |
assertEquals(1, goog.dom.getElementsByClass('test2').length); |
| 212 |
assertEquals(0, goog.dom.getElementsByClass('nonexistant').length); |
| 213 |
|
| 214 |
var container = goog.dom.getElement('span-container'); |
| 215 |
assertEquals(3, goog.dom.getElementsByClass('test1', container).length); |
| 216 |
} |
| 217 |
|
| 218 |
function testGetElementByClass() { |
| 219 |
assertNotNull(goog.dom.getElementByClass('test1')); |
| 220 |
assertNotNull(goog.dom.getElementByClass('test2')); |
| 221 |
// assertNull(goog.dom.getElementByClass('nonexistant')); |
| 222 |
|
| 223 |
var container = goog.dom.getElement('span-container'); |
| 224 |
assertNotNull(goog.dom.getElementByClass('test1', container)); |
| 225 |
} |
| 226 |
|
| 227 |
function testGetElementByTagNameAndClass() { |
| 228 |
assertNotNull(goog.dom.getElementByTagNameAndClass('', 'test1')); |
| 229 |
assertNotNull(goog.dom.getElementByTagNameAndClass('*', 'test1')); |
| 230 |
assertNotNull(goog.dom.getElementByTagNameAndClass('span', 'test1')); |
| 231 |
assertNull(goog.dom.getElementByTagNameAndClass('div', 'test1')); |
| 232 |
assertNull(goog.dom.getElementByTagNameAndClass('*', 'nonexistant')); |
| 233 |
|
| 234 |
var container = goog.dom.getElement('span-container'); |
| 235 |
assertNotNull(goog.dom.getElementByTagNameAndClass('*', 'test1', container)); |
| 236 |
} |
| 237 |
|
| 238 |
function testSetProperties() { |
| 239 |
var attrs = { |
| 240 |
'name': 'test3', |
| 241 |
'title': 'A title', |
| 242 |
'random': 'woop', |
| 243 |
'other-random': null, |
| 244 |
'href': goog.html.SafeUrl.sanitize('https://google.com') |
| 245 |
}; |
| 246 |
var el = $('testEl'); |
| 247 |
|
| 248 |
goog.dom.setProperties(el, attrs); |
| 249 |
assertEquals(el.name, 'test3'); |
| 250 |
assertEquals(el.title, 'A title'); |
| 251 |
assertEquals(el.random, 'woop'); |
| 252 |
assertEquals(el.href, 'https://google.com'); |
| 253 |
} |
| 254 |
|
| 255 |
function testSetPropertiesDirectAttributeMap() { |
| 256 |
var attrs = {'usemap': '#myMap'}; |
| 257 |
var el = goog.dom.createDom(goog.dom.TagName.IMG); |
| 258 |
|
| 259 |
var res = goog.dom.setProperties(el, attrs); |
| 260 |
assertEquals('Should be equal', '#myMap', el.getAttribute('usemap')); |
| 261 |
} |
| 262 |
|
| 263 |
function testSetPropertiesDirectAttributeMapChecksForOwnProperties() { |
| 264 |
stubs.set(Object.prototype, 'customProp', 'sdflasdf.,m.,<>fsdflas213!@#'); |
| 265 |
var attrs = {'usemap': '#myMap'}; |
| 266 |
var el = goog.dom.createDom(goog.dom.TagName.IMG); |
| 267 |
|
| 268 |
var res = goog.dom.setProperties(el, attrs); |
| 269 |
assertEquals('Should be equal', '#myMap', el.getAttribute('usemap')); |
| 270 |
} |
| 271 |
|
| 272 |
function testSetPropertiesAria() { |
| 273 |
var attrs = { |
| 274 |
'aria-hidden': 'true', |
| 275 |
'aria-label': 'This is a label', |
| 276 |
'role': 'presentation' |
| 277 |
}; |
| 278 |
var el = goog.dom.createDom(goog.dom.TagName.DIV); |
| 279 |
|
| 280 |
goog.dom.setProperties(el, attrs); |
| 281 |
assertEquals('Should be equal', 'true', el.getAttribute('aria-hidden')); |
| 282 |
assertEquals( |
| 283 |
'Should be equal', 'This is a label', el.getAttribute('aria-label')); |
| 284 |
assertEquals('Should be equal', 'presentation', el.getAttribute('role')); |
| 285 |
} |
| 286 |
|
| 287 |
function testSetPropertiesData() { |
| 288 |
var attrs = { |
| 289 |
'data-tooltip': 'This is a tooltip', |
| 290 |
'data-tooltip-delay': '100' |
| 291 |
}; |
| 292 |
var el = goog.dom.createDom(goog.dom.TagName.DIV); |
| 293 |
|
| 294 |
goog.dom.setProperties(el, attrs); |
| 295 |
assertEquals( |
| 296 |
'Should be equal', 'This is a tooltip', el.getAttribute('data-tooltip')); |
| 297 |
assertEquals('Should be equal', '100', el.getAttribute('data-tooltip-delay')); |
| 298 |
} |
| 299 |
|
| 300 |
function testSetTableProperties() { |
| 301 |
var attrs = { |
| 302 |
'style': 'padding-left: 10px;', |
| 303 |
'class': 'mytestclass', |
| 304 |
'height': '101', |
| 305 |
'cellpadding': '15' |
| 306 |
}; |
| 307 |
var el = $('testTable1'); |
| 308 |
|
| 309 |
var res = goog.dom.setProperties(el, attrs); |
| 310 |
assertEquals('Should be equal', el.style.paddingLeft, '10px'); |
| 311 |
assertEquals('Should be equal', el.className, 'mytestclass'); |
| 312 |
assertEquals('Should be equal', el.getAttribute('height'), '101'); |
| 313 |
assertEquals('Should be equal', el.cellPadding, '15'); |
| 314 |
} |
| 315 |
|
| 316 |
function testGetViewportSize() { |
| 317 |
// TODO: This is failing in the test runner now, fix later. |
| 318 |
// var dims = getViewportSize(); |
| 319 |
// assertNotUndefined('Should be defined at least', dims.width); |
| 320 |
// assertNotUndefined('Should be defined at least', dims.height); |
| 321 |
} |
| 322 |
|
| 323 |
function testGetViewportSizeInIframe() { |
| 324 |
var iframe = /** @type {HTMLIFrameElement} */ (goog.dom.getElement('iframe')); |
| 325 |
var contentDoc = goog.dom.getFrameContentDocument(iframe); |
| 326 |
var outerSize = goog.dom.getViewportSize(); |
| 327 |
var innerSize = (new goog.dom.DomHelper(contentDoc)).getViewportSize(); |
| 328 |
assert('Viewport sizes must not match', innerSize.width != outerSize.width); |
| 329 |
} |
| 330 |
|
| 331 |
function testGetDocumentHeightInIframe() { |
| 332 |
var doc = goog.dom.getDomHelper(myIframeDoc).getDocument(); |
| 333 |
var height = goog.dom.getDomHelper(myIframeDoc).getDocumentHeight(); |
| 334 |
|
| 335 |
// Broken in webkit/edge quirks mode and in IE8+ |
| 336 |
if ((goog.dom.isCss1CompatMode_(doc) || |
| 337 |
!goog.userAgent.WEBKIT && !goog.userAgent.EDGE) && |
| 338 |
!isIE8OrHigher()) { |
| 339 |
assertEquals('height should be 65', 42 + 23, height); |
| 340 |
} |
| 341 |
} |
| 342 |
|
| 343 |
function testCreateDom() { |
| 344 |
var el = goog.dom.createDom( |
| 345 |
goog.dom.TagName.DIV, { |
| 346 |
style: 'border: 1px solid black; width: 50%; background-color: #EEE;', |
| 347 |
onclick: 'alert(\'woo\')' |
| 348 |
}, |
| 349 |
goog.dom.createDom( |
| 350 |
goog.dom.TagName.P, {style: 'font: normal 12px arial; color: red; '}, |
| 351 |
'Para 1'), |
| 352 |
goog.dom.createDom( |
| 353 |
goog.dom.TagName.P, |
| 354 |
{style: 'font: bold 18px garamond; color: blue; '}, 'Para 2'), |
| 355 |
goog.dom.createDom( |
| 356 |
goog.dom.TagName.P, |
| 357 |
{style: 'font: normal 24px monospace; color: green'}, 'Para 3 ', |
| 358 |
goog.dom.createDom( |
| 359 |
goog.dom.TagName.A, { |
| 360 |
name: 'link', |
| 361 |
href: goog.html.SafeUrl.sanitize('http://bbc.co.uk/') |
| 362 |
}, |
| 363 |
'has a link'), |
| 364 |
', how cool is this?')); |
| 365 |
|
| 366 |
assertEquals( |
| 367 |
'Tagname should be a DIV', String(goog.dom.TagName.DIV), el.tagName); |
| 368 |
assertEquals('Style width should be 50%', '50%', el.style.width); |
| 369 |
assertEquals( |
| 370 |
'first child is a P tag', String(goog.dom.TagName.P), |
| 371 |
el.childNodes[0].tagName); |
| 372 |
assertEquals('second child .innerHTML', 'Para 2', el.childNodes[1].innerHTML); |
| 373 |
assertEquals( |
| 374 |
'Link href as SafeUrl', 'http://bbc.co.uk/', |
| 375 |
el.childNodes[2].childNodes[1].href); |
| 376 |
} |
| 377 |
|
| 378 |
function testCreateDomNoChildren() { |
| 379 |
var el; |
| 380 |
|
| 381 |
// Test unspecified children. |
| 382 |
el = goog.dom.createDom(goog.dom.TagName.DIV); |
| 383 |
assertNull('firstChild should be null', el.firstChild); |
| 384 |
|
| 385 |
// Test null children. |
| 386 |
el = goog.dom.createDom(goog.dom.TagName.DIV, null, null); |
| 387 |
assertNull('firstChild should be null', el.firstChild); |
| 388 |
|
| 389 |
// Test empty array of children. |
| 390 |
el = goog.dom.createDom(goog.dom.TagName.DIV, null, []); |
| 391 |
assertNull('firstChild should be null', el.firstChild); |
| 392 |
} |
| 393 |
|
| 394 |
function testCreateDomAcceptsArray() { |
| 395 |
var items = [ |
| 396 |
goog.dom.createDom(goog.dom.TagName.LI, {}, 'Item 1'), |
| 397 |
goog.dom.createDom(goog.dom.TagName.LI, {}, 'Item 2') |
| 398 |
]; |
| 399 |
var ul = goog.dom.createDom(goog.dom.TagName.UL, {}, items); |
| 400 |
assertEquals('List should have two children', 2, ul.childNodes.length); |
| 401 |
assertEquals( |
| 402 |
'First child should be an LI tag', String(goog.dom.TagName.LI), |
| 403 |
ul.firstChild.tagName); |
| 404 |
assertEquals('Item 1', ul.childNodes[0].innerHTML); |
| 405 |
assertEquals('Item 2', ul.childNodes[1].innerHTML); |
| 406 |
} |
| 407 |
|
| 408 |
function testCreateDomStringArg() { |
| 409 |
var el; |
| 410 |
|
| 411 |
// Test string arg. |
| 412 |
el = goog.dom.createDom(goog.dom.TagName.DIV, null, 'Hello'); |
| 413 |
assertEquals( |
| 414 |
'firstChild should be a text node', goog.dom.NodeType.TEXT, |
| 415 |
el.firstChild.nodeType); |
| 416 |
assertEquals( |
| 417 |
'firstChild should have node value "Hello"', 'Hello', |
| 418 |
el.firstChild.nodeValue); |
| 419 |
|
| 420 |
// Test text node arg. |
| 421 |
el = goog.dom.createDom( |
| 422 |
goog.dom.TagName.DIV, null, goog.dom.createTextNode('World')); |
| 423 |
assertEquals( |
| 424 |
'firstChild should be a text node', goog.dom.NodeType.TEXT, |
| 425 |
el.firstChild.nodeType); |
| 426 |
assertEquals( |
| 427 |
'firstChild should have node value "World"', 'World', |
| 428 |
el.firstChild.nodeValue); |
| 429 |
} |
| 430 |
|
| 431 |
function testCreateDomNodeListArg() { |
| 432 |
var el; |
| 433 |
var emptyElem = goog.dom.createDom(goog.dom.TagName.DIV); |
| 434 |
var simpleElem = |
| 435 |
goog.dom.createDom(goog.dom.TagName.DIV, null, 'Hello, world!'); |
| 436 |
var complexElem = goog.dom.createDom( |
| 437 |
goog.dom.TagName.DIV, null, 'Hello, ', |
| 438 |
goog.dom.createDom(goog.dom.TagName.B, null, 'world'), |
| 439 |
goog.dom.createTextNode('!')); |
| 440 |
|
| 441 |
// Test empty node list. |
| 442 |
el = goog.dom.createDom(goog.dom.TagName.DIV, null, emptyElem.childNodes); |
| 443 |
assertNull('emptyElem.firstChild should be null', emptyElem.firstChild); |
| 444 |
assertNull('firstChild should be null', el.firstChild); |
| 445 |
|
| 446 |
// Test simple node list. |
| 447 |
el = goog.dom.createDom(goog.dom.TagName.DIV, null, simpleElem.childNodes); |
| 448 |
assertNull('simpleElem.firstChild should be null', simpleElem.firstChild); |
| 449 |
assertEquals( |
| 450 |
'firstChild should be a text node with value "Hello, world!"', |
| 451 |
'Hello, world!', el.firstChild.nodeValue); |
| 452 |
|
| 453 |
// Test complex node list. |
| 454 |
el = goog.dom.createDom(goog.dom.TagName.DIV, null, complexElem.childNodes); |
| 455 |
assertNull('complexElem.firstChild should be null', complexElem.firstChild); |
| 456 |
assertEquals('Element should have 3 child nodes', 3, el.childNodes.length); |
| 457 |
assertEquals( |
| 458 |
'childNodes[0] should be a text node with value "Hello, "', 'Hello, ', |
| 459 |
el.childNodes[0].nodeValue); |
| 460 |
assertEquals( |
| 461 |
'childNodes[1] should be an element node with tagName "B"', |
| 462 |
String(goog.dom.TagName.B), el.childNodes[1].tagName); |
| 463 |
assertEquals( |
| 464 |
'childNodes[2] should be a text node with value "!"', '!', |
| 465 |
el.childNodes[2].nodeValue); |
| 466 |
} |
| 467 |
|
| 468 |
function testCreateDomWithTypeAttribute() { |
| 469 |
var el = goog.dom.createDom( |
| 470 |
goog.dom.TagName.BUTTON, |
| 471 |
{'type': goog.dom.InputType.RESET, 'id': 'cool-button'}, 'Cool button'); |
| 472 |
assertNotNull('Button with type attribute was created successfully', el); |
| 473 |
assertEquals( |
| 474 |
'Button has correct type attribute', goog.dom.InputType.RESET, el.type); |
| 475 |
assertEquals('Button has correct id', 'cool-button', el.id); |
| 476 |
} |
| 477 |
|
| 478 |
function testCreateDomWithClassList() { |
| 479 |
var el = goog.dom.createDom(goog.dom.TagName.DIV, ['foo', 'bar']); |
| 480 |
assertEquals('foo bar', el.className); |
| 481 |
} |
| 482 |
|
| 483 |
function testContains() { |
| 484 |
assertTrue( |
| 485 |
'HTML should contain BODY', |
| 486 |
goog.dom.contains(document.documentElement, document.body)); |
| 487 |
assertTrue( |
| 488 |
'Document should contain BODY', |
| 489 |
goog.dom.contains(document, document.body)); |
| 490 |
|
| 491 |
var d = goog.dom.createDom(goog.dom.TagName.P, null, 'A paragraph'); |
| 492 |
var t = d.firstChild; |
| 493 |
assertTrue('Same element', goog.dom.contains(d, d)); |
| 494 |
assertTrue('Same text', goog.dom.contains(t, t)); |
| 495 |
assertTrue('Nested text', goog.dom.contains(d, t)); |
| 496 |
assertFalse('Nested text, reversed', goog.dom.contains(t, d)); |
| 497 |
assertFalse('Disconnected element', goog.dom.contains(document, d)); |
| 498 |
goog.dom.appendChild(document.body, d); |
| 499 |
assertTrue('Connected element', goog.dom.contains(document, d)); |
| 500 |
goog.dom.removeNode(d); |
| 501 |
} |
| 502 |
|
| 503 |
function testCreateDomWithClassName() { |
| 504 |
var el = goog.dom.createDom(goog.dom.TagName.DIV, 'cls'); |
| 505 |
assertNull('firstChild should be null', el.firstChild); |
| 506 |
assertEquals( |
| 507 |
'Tagname should be a DIV', String(goog.dom.TagName.DIV), el.tagName); |
| 508 |
assertEquals('ClassName should be cls', 'cls', el.className); |
| 509 |
|
| 510 |
el = goog.dom.createDom(goog.dom.TagName.DIV, ''); |
| 511 |
assertEquals('ClassName should be empty', '', el.className); |
| 512 |
} |
| 513 |
|
| 514 |
function testCompareNodeOrder() { |
| 515 |
var b1 = $('b1'); |
| 516 |
var b2 = $('b2'); |
| 517 |
var p2 = $('p2'); |
| 518 |
|
| 519 |
assertEquals( |
| 520 |
'equal nodes should compare to 0', 0, goog.dom.compareNodeOrder(b1, b1)); |
| 521 |
|
| 522 |
assertTrue( |
| 523 |
'parent should come before child', goog.dom.compareNodeOrder(p2, b1) < 0); |
| 524 |
assertTrue( |
| 525 |
'child should come after parent', goog.dom.compareNodeOrder(b1, p2) > 0); |
| 526 |
|
| 527 |
assertTrue( |
| 528 |
'parent should come before text child', |
| 529 |
goog.dom.compareNodeOrder(b1, b1.firstChild) < 0); |
| 530 |
assertTrue( |
| 531 |
'text child should come after parent', |
| 532 |
goog.dom.compareNodeOrder(b1.firstChild, b1) > 0); |
| 533 |
|
| 534 |
assertTrue( |
| 535 |
'first sibling should come before second', |
| 536 |
goog.dom.compareNodeOrder(b1, b2) < 0); |
| 537 |
assertTrue( |
| 538 |
'second sibling should come after first', |
| 539 |
goog.dom.compareNodeOrder(b2, b1) > 0); |
| 540 |
|
| 541 |
assertTrue( |
| 542 |
'text node after cousin element returns correct value', |
| 543 |
goog.dom.compareNodeOrder(b1.nextSibling, b1) > 0); |
| 544 |
assertTrue( |
| 545 |
'text node before cousin element returns correct value', |
| 546 |
goog.dom.compareNodeOrder(b1, b1.nextSibling) < 0); |
| 547 |
|
| 548 |
assertTrue( |
| 549 |
'text node is before once removed cousin element', |
| 550 |
goog.dom.compareNodeOrder(b1.firstChild, b2) < 0); |
| 551 |
assertTrue( |
| 552 |
'once removed cousin element is before text node', |
| 553 |
goog.dom.compareNodeOrder(b2, b1.firstChild) > 0); |
| 554 |
|
| 555 |
assertTrue( |
| 556 |
'text node is after once removed cousin text node', |
| 557 |
goog.dom.compareNodeOrder(b1.nextSibling, b1.firstChild) > 0); |
| 558 |
assertTrue( |
| 559 |
'once removed cousin text node is before text node', |
| 560 |
goog.dom.compareNodeOrder(b1.firstChild, b1.nextSibling) < 0); |
| 561 |
|
| 562 |
assertTrue( |
| 563 |
'first text node is before second text node', |
| 564 |
goog.dom.compareNodeOrder(b1.previousSibling, b1.nextSibling) < 0); |
| 565 |
assertTrue( |
| 566 |
'second text node is after first text node', |
| 567 |
goog.dom.compareNodeOrder(b1.nextSibling, b1.previousSibling) > 0); |
| 568 |
|
| 569 |
assertTrue( |
| 570 |
'grandchild is after grandparent', |
| 571 |
goog.dom.compareNodeOrder(b1.firstChild, b1.parentNode) > 0); |
| 572 |
assertTrue( |
| 573 |
'grandparent is after grandchild', |
| 574 |
goog.dom.compareNodeOrder(b1.parentNode, b1.firstChild) < 0); |
| 575 |
|
| 576 |
assertTrue( |
| 577 |
'grandchild is after grandparent', |
| 578 |
goog.dom.compareNodeOrder(b1.firstChild, b1.parentNode) > 0); |
| 579 |
assertTrue( |
| 580 |
'grandparent is after grandchild', |
| 581 |
goog.dom.compareNodeOrder(b1.parentNode, b1.firstChild) < 0); |
| 582 |
|
| 583 |
assertTrue( |
| 584 |
'second cousins compare correctly', |
| 585 |
goog.dom.compareNodeOrder(b1.firstChild, b2.firstChild) < 0); |
| 586 |
assertTrue( |
| 587 |
'second cousins compare correctly in reverse', |
| 588 |
goog.dom.compareNodeOrder(b2.firstChild, b1.firstChild) > 0); |
| 589 |
|
| 590 |
assertTrue( |
| 591 |
'testEl2 is after testEl', |
| 592 |
goog.dom.compareNodeOrder($('testEl2'), $('testEl')) > 0); |
| 593 |
assertTrue( |
| 594 |
'testEl is before testEl2', |
| 595 |
goog.dom.compareNodeOrder($('testEl'), $('testEl2')) < 0); |
| 596 |
|
| 597 |
var p = $('order-test'); |
| 598 |
var text1 = document.createTextNode('1'); |
| 599 |
p.appendChild(text1); |
| 600 |
var text2 = document.createTextNode('1'); |
| 601 |
p.appendChild(text2); |
| 602 |
|
| 603 |
assertEquals( |
| 604 |
'Equal text nodes should compare to 0', 0, |
| 605 |
goog.dom.compareNodeOrder(text1, text1)); |
| 606 |
assertTrue( |
| 607 |
'First text node is before second', |
| 608 |
goog.dom.compareNodeOrder(text1, text2) < 0); |
| 609 |
assertTrue( |
| 610 |
'Second text node is after first', |
| 611 |
goog.dom.compareNodeOrder(text2, text1) > 0); |
| 612 |
assertTrue( |
| 613 |
'Late text node is after b1', |
| 614 |
goog.dom.compareNodeOrder(text1, $('b1')) > 0); |
| 615 |
|
| 616 |
assertTrue( |
| 617 |
'Document node is before non-document node', |
| 618 |
goog.dom.compareNodeOrder(document, b1) < 0); |
| 619 |
assertTrue( |
| 620 |
'Non-document node is after document node', |
| 621 |
goog.dom.compareNodeOrder(b1, document) > 0); |
| 622 |
} |
| 623 |
|
| 624 |
function testFindCommonAncestor() { |
| 625 |
var b1 = $('b1'); |
| 626 |
var b2 = $('b2'); |
| 627 |
var p1 = $('p1'); |
| 628 |
var p2 = $('p2'); |
| 629 |
var testEl2 = $('testEl2'); |
| 630 |
|
| 631 |
assertNull('findCommonAncestor() = null', goog.dom.findCommonAncestor()); |
| 632 |
assertEquals( |
| 633 |
'findCommonAncestor(b1) = b1', b1, goog.dom.findCommonAncestor(b1)); |
| 634 |
assertEquals( |
| 635 |
'findCommonAncestor(b1, b1) = b1', b1, |
| 636 |
goog.dom.findCommonAncestor(b1, b1)); |
| 637 |
assertEquals( |
| 638 |
'findCommonAncestor(b1, b2) = p2', p2, |
| 639 |
goog.dom.findCommonAncestor(b1, b2)); |
| 640 |
assertEquals( |
| 641 |
'findCommonAncestor(p1, b2) = body', document.body, |
| 642 |
goog.dom.findCommonAncestor(p1, b2)); |
| 643 |
assertEquals( |
| 644 |
'findCommonAncestor(testEl2, b1, b2, p1, p2) = body', document.body, |
| 645 |
goog.dom.findCommonAncestor(testEl2, b1, b2, p1, p2)); |
| 646 |
|
| 647 |
var outOfDoc = goog.dom.createElement(goog.dom.TagName.DIV); |
| 648 |
assertNull( |
| 649 |
'findCommonAncestor(outOfDoc, b1) = null', |
| 650 |
goog.dom.findCommonAncestor(outOfDoc, b1)); |
| 651 |
} |
| 652 |
|
| 653 |
function testRemoveNode() { |
| 654 |
var b = goog.dom.createElement(goog.dom.TagName.B); |
| 655 |
var el = $('p1'); |
| 656 |
el.appendChild(b); |
| 657 |
goog.dom.removeNode(b); |
| 658 |
assertTrue('b should have been removed', el.lastChild != b); |
| 659 |
} |
| 660 |
|
| 661 |
function testReplaceNode() { |
| 662 |
var n = $('toReplace'); |
| 663 |
var previousSibling = n.previousSibling; |
| 664 |
var goodNode = |
| 665 |
goog.dom.createDom(goog.dom.TagName.DIV, {'id': 'goodReplaceNode'}); |
| 666 |
goog.dom.replaceNode(goodNode, n); |
| 667 |
|
| 668 |
assertEquals( |
| 669 |
'n should have been replaced', previousSibling.nextSibling, goodNode); |
| 670 |
assertNull('n should no longer be in the DOM tree', $('toReplace')); |
| 671 |
|
| 672 |
var badNode = |
| 673 |
goog.dom.createDom(goog.dom.TagName.DIV, {'id': 'badReplaceNode'}); |
| 674 |
goog.dom.replaceNode(badNode, n); |
| 675 |
assertNull('badNode should not be in the DOM tree', $('badReplaceNode')); |
| 676 |
} |
| 677 |
|
| 678 |
function testAppendChildAt() { |
| 679 |
var parent = $('p2'); |
| 680 |
var origNumChildren = parent.childNodes.length; |
| 681 |
|
| 682 |
var child1 = goog.dom.createElement(goog.dom.TagName.DIV); |
| 683 |
goog.dom.insertChildAt(parent, child1, origNumChildren); |
| 684 |
assertEquals(origNumChildren + 1, parent.childNodes.length); |
| 685 |
|
| 686 |
var child2 = goog.dom.createElement(goog.dom.TagName.DIV); |
| 687 |
goog.dom.insertChildAt(parent, child2, origNumChildren + 42); |
| 688 |
assertEquals(origNumChildren + 2, parent.childNodes.length); |
| 689 |
|
| 690 |
var child3 = goog.dom.createElement(goog.dom.TagName.DIV); |
| 691 |
goog.dom.insertChildAt(parent, child3, 0); |
| 692 |
assertEquals(origNumChildren + 3, parent.childNodes.length); |
| 693 |
|
| 694 |
var child4 = goog.dom.createElement(goog.dom.TagName.DIV); |
| 695 |
goog.dom.insertChildAt(parent, child3, 2); |
| 696 |
assertEquals(origNumChildren + 3, parent.childNodes.length); |
| 697 |
|
| 698 |
parent.removeChild(child1); |
| 699 |
parent.removeChild(child2); |
| 700 |
parent.removeChild(child3); |
| 701 |
|
| 702 |
var emptyParentNotInDocument = goog.dom.createElement(goog.dom.TagName.DIV); |
| 703 |
goog.dom.insertChildAt(emptyParentNotInDocument, child1, 0); |
| 704 |
assertEquals(1, emptyParentNotInDocument.childNodes.length); |
| 705 |
} |
| 706 |
|
| 707 |
function testFlattenElement() { |
| 708 |
var text = document.createTextNode('Text'); |
| 709 |
var br = goog.dom.createElement(goog.dom.TagName.BR); |
| 710 |
var span = goog.dom.createDom(goog.dom.TagName.SPAN, null, text, br); |
| 711 |
assertEquals('span should have 2 children', 2, span.childNodes.length); |
| 712 |
|
| 713 |
var el = $('p1'); |
| 714 |
el.appendChild(span); |
| 715 |
|
| 716 |
var ret = goog.dom.flattenElement(span); |
| 717 |
|
| 718 |
assertTrue('span should have been removed', el.lastChild != span); |
| 719 |
assertFalse( |
| 720 |
'span should have no parent', !!span.parentNode && |
| 721 |
span.parentNode.nodeType != goog.dom.NodeType.DOCUMENT_FRAGMENT); |
| 722 |
assertEquals('span should have no children', 0, span.childNodes.length); |
| 723 |
assertEquals('Last child of p should be br', br, el.lastChild); |
| 724 |
assertEquals( |
| 725 |
'Previous sibling of br should be text', text, br.previousSibling); |
| 726 |
|
| 727 |
var outOfDoc = goog.dom.createDom(goog.dom.TagName.SPAN, null, '1 child'); |
| 728 |
// Should do nothing. |
| 729 |
goog.dom.flattenElement(outOfDoc); |
| 730 |
assertEquals( |
| 731 |
'outOfDoc should still have 1 child', 1, outOfDoc.childNodes.length); |
| 732 |
} |
| 733 |
|
| 734 |
function testIsNodeLike() { |
| 735 |
assertTrue('document should be node like', goog.dom.isNodeLike(document)); |
| 736 |
assertTrue( |
| 737 |
'document.body should be node like', goog.dom.isNodeLike(document.body)); |
| 738 |
assertTrue( |
| 739 |
'a text node should be node like', |
| 740 |
goog.dom.isNodeLike(document.createTextNode(''))); |
| 741 |
|
| 742 |
assertFalse('null should not be node like', goog.dom.isNodeLike(null)); |
| 743 |
assertFalse('a string should not be node like', goog.dom.isNodeLike('abcd')); |
| 744 |
|
| 745 |
assertTrue( |
| 746 |
'custom object should be node like', goog.dom.isNodeLike({nodeType: 1})); |
| 747 |
} |
| 748 |
|
| 749 |
function testIsElement() { |
| 750 |
assertFalse('document is not an element', goog.dom.isElement(document)); |
| 751 |
assertTrue('document.body is an element', goog.dom.isElement(document.body)); |
| 752 |
assertFalse( |
| 753 |
'a text node is not an element', |
| 754 |
goog.dom.isElement(document.createTextNode(''))); |
| 755 |
assertTrue( |
| 756 |
'an element created with createElement() is an element', |
| 757 |
goog.dom.isElement(goog.dom.createElement(goog.dom.TagName.A))); |
| 758 |
|
| 759 |
assertFalse('null is not an element', goog.dom.isElement(null)); |
| 760 |
assertFalse('a string is not an element', goog.dom.isElement('abcd')); |
| 761 |
|
| 762 |
assertTrue('custom object is an element', goog.dom.isElement({nodeType: 1})); |
| 763 |
assertFalse( |
| 764 |
'custom non-element object is a not an element', |
| 765 |
goog.dom.isElement({someProperty: 'somevalue'})); |
| 766 |
} |
| 767 |
|
| 768 |
function testIsWindow() { |
| 769 |
var global = goog.global; |
| 770 |
var frame = window.frames['frame']; |
| 771 |
var otherWindow = window.open('', 'blank'); |
| 772 |
var object = {window: goog.global}; |
| 773 |
var nullVar = null; |
| 774 |
var notDefined; |
| 775 |
|
| 776 |
try { |
| 777 |
// Use try/finally to ensure that we clean up the window we open, even if an |
| 778 |
// assertion fails or something else goes wrong. |
| 779 |
assertTrue( |
| 780 |
'global object in HTML context should be a window', |
| 781 |
goog.dom.isWindow(goog.global)); |
| 782 |
assertTrue('iframe window should be a window', goog.dom.isWindow(frame)); |
| 783 |
if (otherWindow) { |
| 784 |
assertTrue( |
| 785 |
'other window should be a window', goog.dom.isWindow(otherWindow)); |
| 786 |
} |
| 787 |
assertFalse('object should not be a window', goog.dom.isWindow(object)); |
| 788 |
assertFalse('null should not be a window', goog.dom.isWindow(nullVar)); |
| 789 |
assertFalse( |
| 790 |
'undefined should not be a window', goog.dom.isWindow(notDefined)); |
| 791 |
} finally { |
| 792 |
if (otherWindow) { |
| 793 |
otherWindow.close(); |
| 794 |
} |
| 795 |
} |
| 796 |
} |
| 797 |
|
| 798 |
function testGetOwnerDocument() { |
| 799 |
assertEquals(goog.dom.getOwnerDocument($('p1')), document); |
| 800 |
assertEquals(goog.dom.getOwnerDocument(document.body), document); |
| 801 |
assertEquals(goog.dom.getOwnerDocument(document.documentElement), document); |
| 802 |
} |
| 803 |
|
| 804 |
// Tests the breakages resulting in rollback cl/64715474 |
| 805 |
function testGetOwnerDocumentNonNodeInput() { |
| 806 |
// We should fail on null. |
| 807 |
assertThrows(function() { goog.dom.getOwnerDocument(null); }); |
| 808 |
assertEquals(document, goog.dom.getOwnerDocument(window)); |
| 809 |
} |
| 810 |
|
| 811 |
function testDomHelper() { |
| 812 |
var x = new goog.dom.DomHelper(window.frames['frame'].document); |
| 813 |
assertTrue( |
| 814 |
'Should have some HTML', x.getDocument().body.innerHTML.length > 0); |
| 815 |
} |
| 816 |
|
| 817 |
function testGetFirstElementChild() { |
| 818 |
var p2 = $('p2'); |
| 819 |
var b1 = goog.dom.getFirstElementChild(p2); |
| 820 |
assertNotNull('First element child of p2 should not be null', b1); |
| 821 |
assertEquals('First element child is b1', 'b1', b1.id); |
| 822 |
|
| 823 |
var c = goog.dom.getFirstElementChild(b1); |
| 824 |
assertNull('First element child of b1 should be null', c); |
| 825 |
|
| 826 |
// Test with an undefined firstElementChild attribute. |
| 827 |
var b2 = $('b2'); |
| 828 |
var mockP2 = { |
| 829 |
childNodes: [b1, b2], |
| 830 |
firstChild: b1, |
| 831 |
firstElementChild: undefined |
| 832 |
}; |
| 833 |
|
| 834 |
b1 = goog.dom.getFirstElementChild(mockP2); |
| 835 |
assertNotNull('First element child of mockP2 should not be null', b1); |
| 836 |
assertEquals('First element child is b1', 'b1', b1.id); |
| 837 |
} |
| 838 |
|
| 839 |
function testGetLastElementChild() { |
| 840 |
var p2 = $('p2'); |
| 841 |
var b2 = goog.dom.getLastElementChild(p2); |
| 842 |
assertNotNull('Last element child of p2 should not be null', b2); |
| 843 |
assertEquals('Last element child is b2', 'b2', b2.id); |
| 844 |
|
| 845 |
var c = goog.dom.getLastElementChild(b2); |
| 846 |
assertNull('Last element child of b2 should be null', c); |
| 847 |
|
| 848 |
// Test with an undefined lastElementChild attribute. |
| 849 |
var b1 = $('b1'); |
| 850 |
var mockP2 = { |
| 851 |
childNodes: [b1, b2], |
| 852 |
lastChild: b2, |
| 853 |
lastElementChild: undefined |
| 854 |
}; |
| 855 |
|
| 856 |
b2 = goog.dom.getLastElementChild(mockP2); |
| 857 |
assertNotNull('Last element child of mockP2 should not be null', b2); |
| 858 |
assertEquals('Last element child is b2', 'b2', b2.id); |
| 859 |
} |
| 860 |
|
| 861 |
function testGetNextElementSibling() { |
| 862 |
var b1 = $('b1'); |
| 863 |
var b2 = goog.dom.getNextElementSibling(b1); |
| 864 |
assertNotNull('Next element sibling of b1 should not be null', b1); |
| 865 |
assertEquals('Next element sibling is b2', 'b2', b2.id); |
| 866 |
|
| 867 |
var c = goog.dom.getNextElementSibling(b2); |
| 868 |
assertNull('Next element sibling of b2 should be null', c); |
| 869 |
|
| 870 |
// Test with an undefined nextElementSibling attribute. |
| 871 |
var mockB1 = {nextSibling: b2, nextElementSibling: undefined}; |
| 872 |
|
| 873 |
b2 = goog.dom.getNextElementSibling(mockB1); |
| 874 |
assertNotNull('Next element sibling of mockB1 should not be null', b1); |
| 875 |
assertEquals('Next element sibling is b2', 'b2', b2.id); |
| 876 |
} |
| 877 |
|
| 878 |
function testGetPreviousElementSibling() { |
| 879 |
var b2 = $('b2'); |
| 880 |
var b1 = goog.dom.getPreviousElementSibling(b2); |
| 881 |
assertNotNull('Previous element sibling of b2 should not be null', b1); |
| 882 |
assertEquals('Previous element sibling is b1', 'b1', b1.id); |
| 883 |
|
| 884 |
var c = goog.dom.getPreviousElementSibling(b1); |
| 885 |
assertNull('Previous element sibling of b1 should be null', c); |
| 886 |
|
| 887 |
// Test with an undefined previousElementSibling attribute. |
| 888 |
var mockB2 = {previousSibling: b1, previousElementSibling: undefined}; |
| 889 |
|
| 890 |
b1 = goog.dom.getPreviousElementSibling(mockB2); |
| 891 |
assertNotNull('Previous element sibling of mockB2 should not be null', b1); |
| 892 |
assertEquals('Previous element sibling is b1', 'b1', b1.id); |
| 893 |
} |
| 894 |
|
| 895 |
function testGetChildren() { |
| 896 |
var p2 = $('p2'); |
| 897 |
var children = goog.dom.getChildren(p2); |
| 898 |
assertNotNull('Elements array should not be null', children); |
| 899 |
assertEquals( |
| 900 |
'List of element children should be length two.', 2, children.length); |
| 901 |
|
| 902 |
var b1 = $('b1'); |
| 903 |
var b2 = $('b2'); |
| 904 |
assertObjectEquals('First element child should be b1.', b1, children[0]); |
| 905 |
assertObjectEquals('Second element child should be b2.', b2, children[1]); |
| 906 |
|
| 907 |
var noChildren = goog.dom.getChildren(b1); |
| 908 |
assertNotNull('Element children array should not be null', noChildren); |
| 909 |
assertEquals( |
| 910 |
'List of element children should be length zero.', 0, noChildren.length); |
| 911 |
|
| 912 |
// Test with an undefined children attribute. |
| 913 |
var mockP2 = {childNodes: [b1, b2], children: undefined}; |
| 914 |
|
| 915 |
children = goog.dom.getChildren(mockP2); |
| 916 |
assertNotNull('Elements array should not be null', children); |
| 917 |
assertEquals( |
| 918 |
'List of element children should be length two.', 2, children.length); |
| 919 |
|
| 920 |
assertObjectEquals('First element child should be b1.', b1, children[0]); |
| 921 |
assertObjectEquals('Second element child should be b2.', b2, children[1]); |
| 922 |
} |
| 923 |
|
| 924 |
function testGetNextNode() { |
| 925 |
var tree = goog.dom.safeHtmlToNode(goog.html.testing.newSafeHtmlForTest( |
| 926 |
'<div>' + |
| 927 |
'<p>Some text</p>' + |
| 928 |
'<blockquote>Some <i>special</i> <b>text</b></blockquote>' + |
| 929 |
'<address><!-- comment -->Foo</address>' + |
| 930 |
'</div>')); |
| 931 |
|
| 932 |
assertNull(goog.dom.getNextNode(null)); |
| 933 |
|
| 934 |
var node = tree; |
| 935 |
var next = function() { return node = goog.dom.getNextNode(node); }; |
| 936 |
|
| 937 |
assertEquals(String(goog.dom.TagName.P), next().tagName); |
| 938 |
assertEquals('Some text', next().nodeValue); |
| 939 |
assertEquals(String(goog.dom.TagName.BLOCKQUOTE), next().tagName); |
| 940 |
assertEquals('Some ', next().nodeValue); |
| 941 |
assertEquals(String(goog.dom.TagName.I), next().tagName); |
| 942 |
assertEquals('special', next().nodeValue); |
| 943 |
assertEquals(' ', next().nodeValue); |
| 944 |
assertEquals(String(goog.dom.TagName.B), next().tagName); |
| 945 |
assertEquals('text', next().nodeValue); |
| 946 |
assertEquals(String(goog.dom.TagName.ADDRESS), next().tagName); |
| 947 |
assertEquals(goog.dom.NodeType.COMMENT, next().nodeType); |
| 948 |
assertEquals('Foo', next().nodeValue); |
| 949 |
|
| 950 |
assertNull(next()); |
| 951 |
} |
| 952 |
|
| 953 |
function testGetPreviousNode() { |
| 954 |
var tree = goog.dom.safeHtmlToNode(goog.html.testing.newSafeHtmlForTest( |
| 955 |
'<div>' + |
| 956 |
'<p>Some text</p>' + |
| 957 |
'<blockquote>Some <i>special</i> <b>text</b></blockquote>' + |
| 958 |
'<address><!-- comment -->Foo</address>' + |
| 959 |
'</div>')); |
| 960 |
|
| 961 |
assertNull(goog.dom.getPreviousNode(null)); |
| 962 |
|
| 963 |
var node = tree.lastChild.lastChild; |
| 964 |
var previous = function() { return node = goog.dom.getPreviousNode(node); }; |
| 965 |
|
| 966 |
assertEquals(goog.dom.NodeType.COMMENT, previous().nodeType); |
| 967 |
assertEquals(String(goog.dom.TagName.ADDRESS), previous().tagName); |
| 968 |
assertEquals('text', previous().nodeValue); |
| 969 |
assertEquals(String(goog.dom.TagName.B), previous().tagName); |
| 970 |
assertEquals(' ', previous().nodeValue); |
| 971 |
assertEquals('special', previous().nodeValue); |
| 972 |
assertEquals(String(goog.dom.TagName.I), previous().tagName); |
| 973 |
assertEquals('Some ', previous().nodeValue); |
| 974 |
assertEquals(String(goog.dom.TagName.BLOCKQUOTE), previous().tagName); |
| 975 |
assertEquals('Some text', previous().nodeValue); |
| 976 |
assertEquals(String(goog.dom.TagName.P), previous().tagName); |
| 977 |
assertEquals(String(goog.dom.TagName.DIV), previous().tagName); |
| 978 |
|
| 979 |
if (!goog.userAgent.IE) { |
| 980 |
// Internet Explorer maintains a parentNode for Elements after they are |
| 981 |
// removed from the hierarchy. Everyone else agrees on a null parentNode. |
| 982 |
assertNull(previous()); |
| 983 |
} |
| 984 |
} |
| 985 |
|
| 986 |
function testSetTextContent() { |
| 987 |
var p1 = $('p1'); |
| 988 |
var s = 'hello world'; |
| 989 |
goog.dom.setTextContent(p1, s); |
| 990 |
assertEquals( |
| 991 |
'We should have one childNode after setTextContent', 1, |
| 992 |
p1.childNodes.length); |
| 993 |
assertEquals(s, p1.firstChild.data); |
| 994 |
assertEquals(s, p1.innerHTML); |
| 995 |
|
| 996 |
s = 'four elefants < five ants'; |
| 997 |
var sHtml = 'four elefants < five ants'; |
| 998 |
goog.dom.setTextContent(p1, s); |
| 999 |
assertEquals( |
| 1000 |
'We should have one childNode after setTextContent', 1, |
| 1001 |
p1.childNodes.length); |
| 1002 |
assertEquals(s, p1.firstChild.data); |
| 1003 |
assertEquals(sHtml, p1.innerHTML); |
| 1004 |
|
| 1005 |
// ensure that we remove existing children |
| 1006 |
p1.innerHTML = 'a<b>b</b>c'; |
| 1007 |
s = 'hello world'; |
| 1008 |
goog.dom.setTextContent(p1, s); |
| 1009 |
assertEquals( |
| 1010 |
'We should have one childNode after setTextContent', 1, |
| 1011 |
p1.childNodes.length); |
| 1012 |
assertEquals(s, p1.firstChild.data); |
| 1013 |
|
| 1014 |
// same but start with an element |
| 1015 |
p1.innerHTML = '<b>a</b>b<i>c</i>'; |
| 1016 |
s = 'hello world'; |
| 1017 |
goog.dom.setTextContent(p1, s); |
| 1018 |
assertEquals( |
| 1019 |
'We should have one childNode after setTextContent', 1, |
| 1020 |
p1.childNodes.length); |
| 1021 |
assertEquals(s, p1.firstChild.data); |
| 1022 |
|
| 1023 |
// Text/CharacterData |
| 1024 |
goog.dom.setTextContent(p1, 'before'); |
| 1025 |
s = 'after'; |
| 1026 |
goog.dom.setTextContent(p1.firstChild, s); |
| 1027 |
assertEquals( |
| 1028 |
'We should have one childNode after setTextContent', 1, |
| 1029 |
p1.childNodes.length); |
| 1030 |
assertEquals(s, p1.firstChild.data); |
| 1031 |
|
| 1032 |
// DocumentFragment |
| 1033 |
var df = document.createDocumentFragment(); |
| 1034 |
s = 'hello world'; |
| 1035 |
goog.dom.setTextContent(df, s); |
| 1036 |
assertEquals( |
| 1037 |
'We should have one childNode after setTextContent', 1, |
| 1038 |
df.childNodes.length); |
| 1039 |
assertEquals(s, df.firstChild.data); |
| 1040 |
|
| 1041 |
// clean up |
| 1042 |
goog.dom.removeChildren(p1); |
| 1043 |
} |
| 1044 |
|
| 1045 |
function testFindNode() { |
| 1046 |
var expected = document.body; |
| 1047 |
var result = goog.dom.findNode(document, function(n) { |
| 1048 |
return n.nodeType == goog.dom.NodeType.ELEMENT && |
| 1049 |
n.tagName == goog.dom.TagName.BODY; |
| 1050 |
}); |
| 1051 |
assertEquals(expected, result); |
| 1052 |
|
| 1053 |
expected = goog.dom.getElementsByTagName(goog.dom.TagName.P)[0]; |
| 1054 |
result = goog.dom.findNode(document, function(n) { |
| 1055 |
return n.nodeType == goog.dom.NodeType.ELEMENT && |
| 1056 |
n.tagName == goog.dom.TagName.P; |
| 1057 |
}); |
| 1058 |
assertEquals(expected, result); |
| 1059 |
|
| 1060 |
result = goog.dom.findNode(document, function(n) { return false; }); |
| 1061 |
assertUndefined(result); |
| 1062 |
} |
| 1063 |
|
| 1064 |
function testFindNodes() { |
| 1065 |
var expected = goog.dom.getElementsByTagName(goog.dom.TagName.P); |
| 1066 |
var result = goog.dom.findNodes(document, function(n) { |
| 1067 |
return n.nodeType == goog.dom.NodeType.ELEMENT && |
| 1068 |
n.tagName == goog.dom.TagName.P; |
| 1069 |
}); |
| 1070 |
assertEquals(expected.length, result.length); |
| 1071 |
assertEquals(expected[0], result[0]); |
| 1072 |
assertEquals(expected[1], result[1]); |
| 1073 |
|
| 1074 |
result = goog.dom.findNodes(document, function(n) { return false; }).length; |
| 1075 |
assertEquals(0, result); |
| 1076 |
} |
| 1077 |
|
| 1078 |
function createTestDom(txt) { |
| 1079 |
var dom = goog.dom.createDom(goog.dom.TagName.DIV); |
| 1080 |
dom.innerHTML = txt; |
| 1081 |
return dom; |
| 1082 |
} |
| 1083 |
|
| 1084 |
function testIsFocusableTabIndex() { |
| 1085 |
assertFalse( |
| 1086 |
'isFocusableTabIndex() must be false for no tab index', |
| 1087 |
goog.dom.isFocusableTabIndex(goog.dom.getElement('noTabIndex'))); |
| 1088 |
assertFalse( |
| 1089 |
'isFocusableTabIndex() must be false for tab index -2', |
| 1090 |
goog.dom.isFocusableTabIndex(goog.dom.getElement('tabIndexNegative2'))); |
| 1091 |
assertFalse( |
| 1092 |
'isFocusableTabIndex() must be false for tab index -1', |
| 1093 |
goog.dom.isFocusableTabIndex(goog.dom.getElement('tabIndexNegative1'))); |
| 1094 |
|
| 1095 |
// WebKit on Mac doesn't support focusable DIVs until version 526 and later. |
| 1096 |
if (!goog.userAgent.WEBKIT || !goog.userAgent.MAC || |
| 1097 |
goog.userAgent.isVersionOrHigher('526')) { |
| 1098 |
assertTrue( |
| 1099 |
'isFocusableTabIndex() must be true for tab index 0', |
| 1100 |
goog.dom.isFocusableTabIndex(goog.dom.getElement('tabIndex0'))); |
| 1101 |
assertTrue( |
| 1102 |
'isFocusableTabIndex() must be true for tab index 1', |
| 1103 |
goog.dom.isFocusableTabIndex(goog.dom.getElement('tabIndex1'))); |
| 1104 |
assertTrue( |
| 1105 |
'isFocusableTabIndex() must be true for tab index 2', |
| 1106 |
goog.dom.isFocusableTabIndex(goog.dom.getElement('tabIndex2'))); |
| 1107 |
} |
| 1108 |
} |
| 1109 |
|
| 1110 |
function testSetFocusableTabIndex() { |
| 1111 |
// WebKit on Mac doesn't support focusable DIVs until version 526 and later. |
| 1112 |
if (!goog.userAgent.WEBKIT || !goog.userAgent.MAC || |
| 1113 |
goog.userAgent.isVersionOrHigher('526')) { |
| 1114 |
// Test enabling focusable tab index. |
| 1115 |
goog.dom.setFocusableTabIndex(goog.dom.getElement('noTabIndex'), true); |
| 1116 |
assertTrue( |
| 1117 |
'isFocusableTabIndex() must be true after enabling tab index', |
| 1118 |
goog.dom.isFocusableTabIndex(goog.dom.getElement('noTabIndex'))); |
| 1119 |
|
| 1120 |
// Test disabling focusable tab index that was added programmatically. |
| 1121 |
goog.dom.setFocusableTabIndex(goog.dom.getElement('noTabIndex'), false); |
| 1122 |
assertFalse( |
| 1123 |
'isFocusableTabIndex() must be false after disabling tab ' + |
| 1124 |
'index that was programmatically added', |
| 1125 |
goog.dom.isFocusableTabIndex(goog.dom.getElement('noTabIndex'))); |
| 1126 |
|
| 1127 |
// Test disabling focusable tab index that was specified in markup. |
| 1128 |
goog.dom.setFocusableTabIndex(goog.dom.getElement('tabIndex0'), false); |
| 1129 |
assertFalse( |
| 1130 |
'isFocusableTabIndex() must be false after disabling tab ' + |
| 1131 |
'index that was specified in markup', |
| 1132 |
goog.dom.isFocusableTabIndex(goog.dom.getElement('tabIndex0'))); |
| 1133 |
|
| 1134 |
// Test re-enabling focusable tab index. |
| 1135 |
goog.dom.setFocusableTabIndex(goog.dom.getElement('tabIndex0'), true); |
| 1136 |
assertTrue( |
| 1137 |
'isFocusableTabIndex() must be true after reenabling tabindex', |
| 1138 |
goog.dom.isFocusableTabIndex(goog.dom.getElement('tabIndex0'))); |
| 1139 |
} |
| 1140 |
} |
| 1141 |
|
| 1142 |
function testIsFocusable() { |
| 1143 |
// Test all types of form elements with no tab index specified are focusable. |
| 1144 |
assertTrue( |
| 1145 |
'isFocusable() must be true for anchor elements with ' + |
| 1146 |
'no tab index', |
| 1147 |
goog.dom.isFocusable(goog.dom.getElement('noTabIndexAnchor'))); |
| 1148 |
assertTrue( |
| 1149 |
'isFocusable() must be true for input elements with ' + |
| 1150 |
'no tab index', |
| 1151 |
goog.dom.isFocusable(goog.dom.getElement('noTabIndexInput'))); |
| 1152 |
assertTrue( |
| 1153 |
'isFocusable() must be true for textarea elements with ' + |
| 1154 |
'no tab index', |
| 1155 |
goog.dom.isFocusable(goog.dom.getElement('noTabIndexTextArea'))); |
| 1156 |
assertTrue( |
| 1157 |
'isFocusable() must be true for select elements with ' + |
| 1158 |
'no tab index', |
| 1159 |
goog.dom.isFocusable(goog.dom.getElement('noTabIndexSelect'))); |
| 1160 |
assertTrue( |
| 1161 |
'isFocusable() must be true for button elements with ' + |
| 1162 |
'no tab index', |
| 1163 |
goog.dom.isFocusable(goog.dom.getElement('noTabIndexButton'))); |
| 1164 |
|
| 1165 |
// Test form element with negative tab index is not focusable. |
| 1166 |
assertFalse( |
| 1167 |
'isFocusable() must be false for form elements with ' + |
| 1168 |
'negative tab index', |
| 1169 |
goog.dom.isFocusable(goog.dom.getElement('negTabIndexButton'))); |
| 1170 |
|
| 1171 |
// Test form element with zero tab index is focusable. |
| 1172 |
assertTrue( |
| 1173 |
'isFocusable() must be true for form elements with ' + |
| 1174 |
'zero tab index', |
| 1175 |
goog.dom.isFocusable(goog.dom.getElement('zeroTabIndexButton'))); |
| 1176 |
|
| 1177 |
// Test form element with positive tab index is focusable. |
| 1178 |
assertTrue( |
| 1179 |
'isFocusable() must be true for form elements with ' + |
| 1180 |
'positive tab index', |
| 1181 |
goog.dom.isFocusable(goog.dom.getElement('posTabIndexButton'))); |
| 1182 |
|
| 1183 |
// Test disabled form element with no tab index is not focusable. |
| 1184 |
assertFalse( |
| 1185 |
'isFocusable() must be false for disabled form elements with ' + |
| 1186 |
'no tab index', |
| 1187 |
goog.dom.isFocusable(goog.dom.getElement('disabledNoTabIndexButton'))); |
| 1188 |
|
| 1189 |
// Test disabled form element with negative tab index is not focusable. |
| 1190 |
assertFalse( |
| 1191 |
'isFocusable() must be false for disabled form elements with ' + |
| 1192 |
'negative tab index', |
| 1193 |
goog.dom.isFocusable(goog.dom.getElement('disabledNegTabIndexButton'))); |
| 1194 |
|
| 1195 |
// Test disabled form element with zero tab index is not focusable. |
| 1196 |
assertFalse( |
| 1197 |
'isFocusable() must be false for disabled form elements with ' + |
| 1198 |
'zero tab index', |
| 1199 |
goog.dom.isFocusable(goog.dom.getElement('disabledZeroTabIndexButton'))); |
| 1200 |
|
| 1201 |
// Test disabled form element with positive tab index is not focusable. |
| 1202 |
assertFalse( |
| 1203 |
'isFocusable() must be false for disabled form elements with ' + |
| 1204 |
'positive tab index', |
| 1205 |
goog.dom.isFocusable(goog.dom.getElement('disabledPosTabIndexButton'))); |
| 1206 |
|
| 1207 |
// Test non-form types should return same value as isFocusableTabIndex() |
| 1208 |
assertEquals( |
| 1209 |
'isFocusable() and isFocusableTabIndex() must agree for ' + |
| 1210 |
' no tab index', |
| 1211 |
goog.dom.isFocusableTabIndex(goog.dom.getElement('noTabIndex')), |
| 1212 |
goog.dom.isFocusable(goog.dom.getElement('noTabIndex'))); |
| 1213 |
assertEquals( |
| 1214 |
'isFocusable() and isFocusableTabIndex() must agree for ' + |
| 1215 |
' tab index -2', |
| 1216 |
goog.dom.isFocusableTabIndex(goog.dom.getElement('tabIndexNegative2')), |
| 1217 |
goog.dom.isFocusable(goog.dom.getElement('tabIndexNegative2'))); |
| 1218 |
assertEquals( |
| 1219 |
'isFocusable() and isFocusableTabIndex() must agree for ' + |
| 1220 |
' tab index -1', |
| 1221 |
goog.dom.isFocusableTabIndex(goog.dom.getElement('tabIndexNegative1')), |
| 1222 |
goog.dom.isFocusable(goog.dom.getElement('tabIndexNegative1'))); |
| 1223 |
|
| 1224 |
// Make sure IE doesn't throw for detached elements. IE can't measure detached |
| 1225 |
// elements, and calling getBoundingClientRect() will throw Unspecified Error. |
| 1226 |
goog.dom.isFocusable(goog.dom.createDom(goog.dom.TagName.BUTTON)); |
| 1227 |
} |
| 1228 |
|
| 1229 |
function testGetTextContent() { |
| 1230 |
function t(inp, out) { |
| 1231 |
assertEquals( |
| 1232 |
out.replace(/ /g, '_'), |
| 1233 |
goog.dom.getTextContent(createTestDom(inp)).replace(/ /g, '_')); |
| 1234 |
} |
| 1235 |
|
| 1236 |
t('abcde', 'abcde'); |
| 1237 |
t('a<b>bcd</b>efgh', 'abcdefgh'); |
| 1238 |
t('a<script type="text/javascript' + |
| 1239 |
'">var a=1;<' + |
| 1240 |
'/script>h', |
| 1241 |
'ah'); |
| 1242 |
t('<html><head><style type="text/css">' + |
| 1243 |
'p{margin:100%;padding:5px}\n.class{background-color:red;}</style>' + |
| 1244 |
'</head><body><h1>Hello</h1>\n<p>One two three</p>\n<table><tr><td>a' + |
| 1245 |
'<td>b</table><' + |
| 1246 |
'script>var a = \'foo\';' + |
| 1247 |
'</scrip' + |
| 1248 |
't></body></html>', |
| 1249 |
'HelloOne two threeab'); |
| 1250 |
t('abc<br>def', 'abc\ndef'); |
| 1251 |
t('abc<br>\ndef', 'abc\ndef'); |
| 1252 |
t('abc<br>\n\ndef', 'abc\ndef'); |
| 1253 |
t('abc<br><br>\ndef', 'abc\n\ndef'); |
| 1254 |
t(' <b>abcde </b> ', 'abcde '); |
| 1255 |
t(' <b>abcde </b> hi ', 'abcde hi '); |
| 1256 |
t(' \n<b>abcde </b> ', 'abcde '); |
| 1257 |
t(' \n<b>abcde </b> \n\n\n', 'abcde '); |
| 1258 |
t('<p>abcde</p>\nfg', 'abcdefg'); |
| 1259 |
t('\n <div> <b>abcde </b> ', 'abcde '); |
| 1260 |
t(' \n­<b>abcde ­ </b> \n\n\n­', 'abcde '); |
| 1261 |
t(' \n­\n\n­\na ', 'a '); |
| 1262 |
t(' \n<wbr></wbr><b>abcde <wbr></wbr> </b> \n\n\n<wbr></wbr>', 'abcde '); |
| 1263 |
t('a b', |
| 1264 |
goog.dom.BrowserFeature.CAN_USE_INNER_TEXT ? 'a b' : |
| 1265 |
'a\xA0\xA0\xA0\xA0\xA0b'); |
| 1266 |
} |
| 1267 |
|
| 1268 |
function testGetNodeTextLength() { |
| 1269 |
assertEquals(6, goog.dom.getNodeTextLength(createTestDom('abcdef'))); |
| 1270 |
assertEquals(8, goog.dom.getNodeTextLength(createTestDom('a<b>bcd</b>efgh'))); |
| 1271 |
assertEquals( |
| 1272 |
2, goog.dom.getNodeTextLength( |
| 1273 |
createTestDom( |
| 1274 |
'a<script type="text/javascript' + |
| 1275 |
'">var a = 1234;<' + |
| 1276 |
'/script>h'))); |
| 1277 |
assertEquals( |
| 1278 |
4, goog.dom.getNodeTextLength( |
| 1279 |
createTestDom('a<br>\n<!-- some comments -->\nfo'))); |
| 1280 |
assertEquals( |
| 1281 |
20, |
| 1282 |
goog.dom.getNodeTextLength( |
| 1283 |
createTestDom( |
| 1284 |
'<html><head><style type="text/css">' + |
| 1285 |
'p{margin:100%;padding:5px}\n.class{background-color:red;}</style>' + |
| 1286 |
'</head><body><h1>Hello</h1><p>One two three</p><table><tr><td>a<td>b' + |
| 1287 |
'</table><' + |
| 1288 |
'script>var a = \'foo\';</scrip' + |
| 1289 |
't></body></html>'))); |
| 1290 |
assertEquals( |
| 1291 |
10, goog.dom.getNodeTextLength(createTestDom('a<b>bcd</b><br />efghi'))); |
| 1292 |
} |
| 1293 |
|
| 1294 |
function testGetNodeTextOffset() { |
| 1295 |
assertEquals( |
| 1296 |
4, goog.dom.getNodeTextOffset($('offsetTest1'), $('offsetParent1'))); |
| 1297 |
assertEquals(12, goog.dom.getNodeTextOffset($('offsetTest1'))); |
| 1298 |
} |
| 1299 |
|
| 1300 |
function testGetNodeAtOffset() { |
| 1301 |
var html = '<div id=a>123<b id=b>45</b><span id=c>67<b id=d>89<i id=e>01' + |
| 1302 |
'</i>23<i id=f>45</i>67</b>890<i id=g>123</i><b id=h>456</b>' + |
| 1303 |
'</span></div><div id=i>7890<i id=j>123</i></div>'; |
| 1304 |
var node = goog.dom.createElement(goog.dom.TagName.DIV); |
| 1305 |
node.innerHTML = html; |
| 1306 |
var rv = {}; |
| 1307 |
|
| 1308 |
goog.dom.getNodeAtOffset(node, 2, rv); |
| 1309 |
assertEquals('123', rv.node.nodeValue); |
| 1310 |
assertEquals('a', rv.node.parentNode.id); |
| 1311 |
assertEquals(1, rv.remainder); |
| 1312 |
|
| 1313 |
goog.dom.getNodeAtOffset(node, 3, rv); |
| 1314 |
assertEquals('123', rv.node.nodeValue); |
| 1315 |
assertEquals('a', rv.node.parentNode.id); |
| 1316 |
assertEquals(2, rv.remainder); |
| 1317 |
|
| 1318 |
goog.dom.getNodeAtOffset(node, 5, rv); |
| 1319 |
assertEquals('45', rv.node.nodeValue); |
| 1320 |
assertEquals('b', rv.node.parentNode.id); |
| 1321 |
assertEquals(1, rv.remainder); |
| 1322 |
|
| 1323 |
goog.dom.getNodeAtOffset(node, 6, rv); |
| 1324 |
assertEquals('67', rv.node.nodeValue); |
| 1325 |
assertEquals('c', rv.node.parentNode.id); |
| 1326 |
assertEquals(0, rv.remainder); |
| 1327 |
|
| 1328 |
goog.dom.getNodeAtOffset(node, 23, rv); |
| 1329 |
assertEquals('123', rv.node.nodeValue); |
| 1330 |
assertEquals('g', rv.node.parentNode.id); |
| 1331 |
assertEquals(2, rv.remainder); |
| 1332 |
|
| 1333 |
goog.dom.getNodeAtOffset(node, 30, rv); |
| 1334 |
assertEquals('7890', rv.node.nodeValue); |
| 1335 |
assertEquals('i', rv.node.parentNode.id); |
| 1336 |
assertEquals(3, rv.remainder); |
| 1337 |
} |
| 1338 |
|
| 1339 |
// IE inserts line breaks and capitalizes nodenames. |
| 1340 |
function assertEqualsCaseAndLeadingWhitespaceInsensitive(value1, value2) { |
| 1341 |
value1 = value1.replace(/^\s+|\s+$/g, '').toLowerCase(); |
| 1342 |
value2 = value2.replace(/^\s+|\s+$/g, '').toLowerCase(); |
| 1343 |
assertEquals(value1, value2); |
| 1344 |
} |
| 1345 |
|
| 1346 |
function testGetOuterHtml() { |
| 1347 |
var contents = '<b>foo</b>'; |
| 1348 |
var node = goog.dom.createElement(goog.dom.TagName.DIV); |
| 1349 |
node.setAttribute('foo', 'bar'); |
| 1350 |
node.innerHTML = contents; |
| 1351 |
assertEqualsCaseAndLeadingWhitespaceInsensitive( |
| 1352 |
goog.dom.getOuterHtml(node), '<div foo="bar">' + contents + '</div>'); |
| 1353 |
|
| 1354 |
var imgNode = goog.dom.createElement(goog.dom.TagName.IMG); |
| 1355 |
imgNode.setAttribute('foo', 'bar'); |
| 1356 |
assertEqualsCaseAndLeadingWhitespaceInsensitive( |
| 1357 |
goog.dom.getOuterHtml(imgNode), '<img foo="bar">'); |
| 1358 |
} |
| 1359 |
|
| 1360 |
|
| 1361 |
function testGetWindowFrame() { |
| 1362 |
var frameWindow = window.frames['frame']; |
| 1363 |
var frameDocument = frameWindow.document; |
| 1364 |
var frameDomHelper = new goog.dom.DomHelper(frameDocument); |
| 1365 |
|
| 1366 |
// Cannot use assertEquals since IE fails on === |
| 1367 |
assertTrue(frameWindow == frameDomHelper.getWindow()); |
| 1368 |
} |
| 1369 |
|
| 1370 |
function testGetWindow() { |
| 1371 |
var domHelper = new goog.dom.DomHelper(); |
| 1372 |
// Cannot use assertEquals since IE fails on === |
| 1373 |
assertTrue(window == domHelper.getWindow()); |
| 1374 |
} |
| 1375 |
|
| 1376 |
function testGetWindowStatic() { |
| 1377 |
// Cannot use assertEquals since IE fails on === |
| 1378 |
assertTrue(window == goog.dom.getWindow()); |
| 1379 |
} |
| 1380 |
|
| 1381 |
function testIsNodeList() { |
| 1382 |
var elem = document.getElementById('p2'); |
| 1383 |
var text = document.getElementById('b2').firstChild; |
| 1384 |
|
| 1385 |
assertTrue( |
| 1386 |
'NodeList should be a node list', goog.dom.isNodeList(elem.childNodes)); |
| 1387 |
assertFalse('TextNode should not be a node list', goog.dom.isNodeList(text)); |
| 1388 |
assertFalse( |
| 1389 |
'Array of nodes should not be a node list', |
| 1390 |
goog.dom.isNodeList([elem.firstChild, elem.lastChild])); |
| 1391 |
} |
| 1392 |
|
| 1393 |
function testGetFrameContentDocument() { |
| 1394 |
var iframe = goog.dom.getElementsByTagName(goog.dom.TagName.IFRAME)[0]; |
| 1395 |
var name = iframe.name; |
| 1396 |
var iframeDoc = goog.dom.getFrameContentDocument(iframe); |
| 1397 |
assertEquals(window.frames[name].document, iframeDoc); |
| 1398 |
} |
| 1399 |
|
| 1400 |
function testGetFrameContentWindow() { |
| 1401 |
var iframe = goog.dom.getElementsByTagName(goog.dom.TagName.IFRAME)[0]; |
| 1402 |
var name = iframe.name; |
| 1403 |
var iframeWin = goog.dom.getFrameContentWindow(iframe); |
| 1404 |
assertEquals(window.frames[name], iframeWin); |
| 1405 |
} |
| 1406 |
|
| 1407 |
function testGetFrameContentWindowNotInitialized() { |
| 1408 |
var iframe = goog.dom.createDom(goog.dom.TagName.IFRAME); |
| 1409 |
assertNull(goog.dom.getFrameContentWindow(iframe)); |
| 1410 |
} |
| 1411 |
|
| 1412 |
function testCanHaveChildren() { |
| 1413 |
var EMPTY_ELEMENTS = goog.object.createSet( |
| 1414 |
goog.dom.TagName.APPLET, goog.dom.TagName.AREA, goog.dom.TagName.BASE, |
| 1415 |
goog.dom.TagName.BR, goog.dom.TagName.COL, goog.dom.TagName.COMMAND, |
| 1416 |
goog.dom.TagName.EMBED, goog.dom.TagName.FRAME, goog.dom.TagName.HR, |
| 1417 |
goog.dom.TagName.IMG, goog.dom.TagName.INPUT, goog.dom.TagName.IFRAME, |
| 1418 |
goog.dom.TagName.ISINDEX, goog.dom.TagName.KEYGEN, goog.dom.TagName.LINK, |
| 1419 |
goog.dom.TagName.NOFRAMES, goog.dom.TagName.NOSCRIPT, |
| 1420 |
goog.dom.TagName.META, goog.dom.TagName.OBJECT, goog.dom.TagName.PARAM, |
| 1421 |
goog.dom.TagName.SCRIPT, goog.dom.TagName.SOURCE, goog.dom.TagName.STYLE, |
| 1422 |
goog.dom.TagName.TRACK, goog.dom.TagName.WBR); |
| 1423 |
|
| 1424 |
// IE opens a dialog warning about using Java content if the following |
| 1425 |
// elements are created. |
| 1426 |
var IE_ILLEGAL_ELEMENTS = |
| 1427 |
goog.object.createSet(goog.dom.TagName.APPLET, goog.dom.TagName.EMBED); |
| 1428 |
|
| 1429 |
for (var tag in goog.dom.TagName) { |
| 1430 |
if (goog.userAgent.IE && tag in IE_ILLEGAL_ELEMENTS) { |
| 1431 |
continue; |
| 1432 |
} |
| 1433 |
|
| 1434 |
var expected = !(tag in EMPTY_ELEMENTS); |
| 1435 |
var node = goog.dom.createElement(tag); |
| 1436 |
assertEquals( |
| 1437 |
tag + ' should ' + (expected ? '' : 'not ') + 'have children', expected, |
| 1438 |
goog.dom.canHaveChildren(node)); |
| 1439 |
|
| 1440 |
// Make sure we can _actually_ add a child if we identify the node as |
| 1441 |
// allowing children. |
| 1442 |
if (goog.dom.canHaveChildren(node)) { |
| 1443 |
node.appendChild(goog.dom.createDom(goog.dom.TagName.DIV, null, 'foo')); |
| 1444 |
} |
| 1445 |
} |
| 1446 |
} |
| 1447 |
|
| 1448 |
function testGetAncestorNoElement() { |
| 1449 |
assertNull(goog.dom.getAncestor( |
| 1450 |
null /* element */, goog.functions.TRUE /* matcher */)); |
| 1451 |
assertNull(goog.dom.getAncestor( |
| 1452 |
null /* element */, goog.functions.TRUE /* matcher */, |
| 1453 |
true /* opt_includeNode */)); |
| 1454 |
} |
| 1455 |
|
| 1456 |
function testGetAncestorNoMatch() { |
| 1457 |
var elem = goog.dom.getElement('nestedElement'); |
| 1458 |
assertNull(goog.dom.getAncestor(elem, function() { return false; })); |
| 1459 |
} |
| 1460 |
|
| 1461 |
function testGetAncestorMatchSelf() { |
| 1462 |
var elem = goog.dom.getElement('nestedElement'); |
| 1463 |
var matched = goog.dom.getAncestor(elem, function() { return true; }, true); |
| 1464 |
assertEquals(elem, matched); |
| 1465 |
} |
| 1466 |
|
| 1467 |
function testGetAncestorNoMatchSelf() { |
| 1468 |
var elem = goog.dom.getElement('nestedElement'); |
| 1469 |
var matched = goog.dom.getAncestor(elem, function() { return true; }); |
| 1470 |
assertEquals(elem.parentNode, matched); |
| 1471 |
} |
| 1472 |
|
| 1473 |
function testGetAncestorWithMaxSearchStepsMatchSelf() { |
| 1474 |
var elem = goog.dom.getElement('nestedElement'); |
| 1475 |
var matched = |
| 1476 |
goog.dom.getAncestor(elem, function() { return true; }, true, 2); |
| 1477 |
assertEquals(elem, matched); |
| 1478 |
} |
| 1479 |
|
| 1480 |
function testGetAncestorWithMaxSearchStepsMatch() { |
| 1481 |
var elem = goog.dom.getElement('nestedElement'); |
| 1482 |
var searchEl = elem.parentNode.parentNode; |
| 1483 |
var matched = goog.dom.getAncestor( |
| 1484 |
elem, function(el) { return el == searchEl; }, false, 1); |
| 1485 |
assertEquals(searchEl, matched); |
| 1486 |
} |
| 1487 |
|
| 1488 |
function testGetAncestorWithMaxSearchStepsNoMatch() { |
| 1489 |
var elem = goog.dom.getElement('nestedElement'); |
| 1490 |
var searchEl = elem.parentNode.parentNode; |
| 1491 |
var matched = goog.dom.getAncestor( |
| 1492 |
elem, function(el) { return el == searchEl; }, false, 0); |
| 1493 |
assertNull(matched); |
| 1494 |
} |
| 1495 |
|
| 1496 |
function testGetAncestorByTagWithMaxSearchStepsNoMatch() { |
| 1497 |
var elem = goog.dom.getElement('nestedElement'); |
| 1498 |
var searchEl = elem.parentNode.parentNode; |
| 1499 |
var matched = goog.dom.getAncestorByTagNameAndClass( |
| 1500 |
elem, goog.dom.TagName.DIV, /* class */ undefined, 0); |
| 1501 |
assertNull(matched); |
| 1502 |
} |
| 1503 |
|
| 1504 |
function testGetAncestorByTagNameNoMatch() { |
| 1505 |
var elem = goog.dom.getElement('nestedElement'); |
| 1506 |
assertNull(goog.dom.getAncestorByTagNameAndClass(elem, goog.dom.TagName.IMG)); |
| 1507 |
} |
| 1508 |
|
| 1509 |
function testGetAncestorByTagNameOnly() { |
| 1510 |
var elem = goog.dom.getElement('nestedElement'); |
| 1511 |
var expected = goog.dom.getElement('testAncestorDiv'); |
| 1512 |
assertEquals( |
| 1513 |
expected, |
| 1514 |
goog.dom.getAncestorByTagNameAndClass(elem, goog.dom.TagName.DIV)); |
| 1515 |
assertEquals(expected, goog.dom.getAncestorByTagNameAndClass(elem, 'div')); |
| 1516 |
} |
| 1517 |
|
| 1518 |
function testGetAncestorByClassWithMaxSearchStepsNoMatch() { |
| 1519 |
var elem = goog.dom.getElement('nestedElement'); |
| 1520 |
var searchEl = elem.parentNode.parentNode; |
| 1521 |
var matched = goog.dom.getAncestorByClass(elem, 'testAncestor', 0); |
| 1522 |
assertNull(matched); |
| 1523 |
} |
| 1524 |
|
| 1525 |
function testGetAncestorByClassNameNoMatch() { |
| 1526 |
var elem = goog.dom.getElement('nestedElement'); |
| 1527 |
assertNull(goog.dom.getAncestorByClass(elem, 'bogusClassName')); |
| 1528 |
} |
| 1529 |
|
| 1530 |
function testGetAncestorByClassName() { |
| 1531 |
var elem = goog.dom.getElement('nestedElement'); |
| 1532 |
var expected = goog.dom.getElement('testAncestorP'); |
| 1533 |
assertEquals(expected, goog.dom.getAncestorByClass(elem, 'testAncestor')); |
| 1534 |
} |
| 1535 |
|
| 1536 |
function testGetAncestorByTagNameAndClass() { |
| 1537 |
var elem = goog.dom.getElement('nestedElement'); |
| 1538 |
var expected = goog.dom.getElement('testAncestorDiv'); |
| 1539 |
assertEquals( |
| 1540 |
expected, goog.dom.getAncestorByTagNameAndClass( |
| 1541 |
elem, goog.dom.TagName.DIV, 'testAncestor')); |
| 1542 |
assertNull( |
| 1543 |
'Should return null if no search criteria are given', |
| 1544 |
goog.dom.getAncestorByTagNameAndClass(elem)); |
| 1545 |
} |
| 1546 |
|
| 1547 |
function testCreateTable() { |
| 1548 |
var table = goog.dom.createTable(2, 3, true); |
| 1549 |
assertEquals( |
| 1550 |
2, goog.dom.getElementsByTagName(goog.dom.TagName.TR, table).length); |
| 1551 |
assertEquals( |
| 1552 |
3, goog.dom.getElementsByTagName(goog.dom.TagName.TR, table)[0] |
| 1553 |
.childNodes.length); |
| 1554 |
assertEquals( |
| 1555 |
6, goog.dom.getElementsByTagName(goog.dom.TagName.TD, table).length); |
| 1556 |
assertEquals( |
| 1557 |
goog.string.Unicode.NBSP, |
| 1558 |
goog.dom.getElementsByTagName(goog.dom.TagName.TD, table)[0] |
| 1559 |
.firstChild.nodeValue); |
| 1560 |
|
| 1561 |
table = goog.dom.createTable(2, 3, false); |
| 1562 |
assertEquals( |
| 1563 |
2, goog.dom.getElementsByTagName(goog.dom.TagName.TR, table).length); |
| 1564 |
assertEquals( |
| 1565 |
3, goog.dom.getElementsByTagName(goog.dom.TagName.TR, table)[0] |
| 1566 |
.childNodes.length); |
| 1567 |
assertEquals( |
| 1568 |
6, goog.dom.getElementsByTagName(goog.dom.TagName.TD, table).length); |
| 1569 |
assertEquals( |
| 1570 |
0, goog.dom.getElementsByTagName(goog.dom.TagName.TD, table)[0] |
| 1571 |
.childNodes.length); |
| 1572 |
} |
| 1573 |
|
| 1574 |
function testSafeHtmlToNode() { |
| 1575 |
var docFragment = goog.dom.safeHtmlToNode( |
| 1576 |
goog.html.testing.newSafeHtmlForTest('<a>1</a><b>2</b>')); |
| 1577 |
assertNull(docFragment.parentNode); |
| 1578 |
assertEquals(2, docFragment.childNodes.length); |
| 1579 |
|
| 1580 |
var div = goog.dom.safeHtmlToNode( |
| 1581 |
goog.html.testing.newSafeHtmlForTest('<div>3</div>')); |
| 1582 |
assertEquals(String(goog.dom.TagName.DIV), div.tagName); |
| 1583 |
|
| 1584 |
var script = goog.dom.safeHtmlToNode( |
| 1585 |
goog.html.testing.newSafeHtmlForTest('<script></script>')); |
| 1586 |
assertEquals(String(goog.dom.TagName.SCRIPT), script.tagName); |
| 1587 |
|
| 1588 |
if (goog.userAgent.IE && !goog.userAgent.isDocumentModeOrHigher(9)) { |
| 1589 |
// Removing an Element from a DOM tree in IE sets its parentNode to a new |
| 1590 |
// DocumentFragment. Bizarre! |
| 1591 |
assertEquals( |
| 1592 |
goog.dom.NodeType.DOCUMENT_FRAGMENT, |
| 1593 |
goog.dom.removeNode(div).parentNode.nodeType); |
| 1594 |
} else { |
| 1595 |
assertNull(div.parentNode); |
| 1596 |
} |
| 1597 |
} |
| 1598 |
|
| 1599 |
/** |
| 1600 |
* Assert that the given goog.string.Const, when converted to a Node, |
| 1601 |
* stringifies in one of the specified ways. |
| 1602 |
* |
| 1603 |
* @param{!Array<string>} potentialStringifications |
| 1604 |
* @param{...!goog.string.Const} var_args The constants to use. |
| 1605 |
*/ |
| 1606 |
function assertConstHtmlToNodeStringifiesToOneOf( |
| 1607 |
potentialStringifications, var_args) { |
| 1608 |
var node = |
| 1609 |
goog.dom.constHtmlToNode.apply(undefined, goog.array.slice(arguments, 1)); |
| 1610 |
var stringified = goog.dom.getOuterHtml(node); |
| 1611 |
if (goog.array.find(potentialStringifications, function(element) { |
| 1612 |
return element == stringified; |
| 1613 |
}) === null) { |
| 1614 |
fail( |
| 1615 |
'Unexpected stringification for a node built from "' + |
| 1616 |
goog.array.map(goog.array.slice(arguments, 1), goog.string.Const.unwrap) |
| 1617 |
.join('') + |
| 1618 |
'": "' + stringified + '"'); |
| 1619 |
} |
| 1620 |
} |
| 1621 |
|
| 1622 |
function testRegularConstHtmlToNodeStringifications() { |
| 1623 |
assertConstHtmlToNodeStringifiesToOneOf( |
| 1624 |
['<b>foo</b>', '<B>foo</B>'], goog.string.Const.from('<b>foo</b>')); |
| 1625 |
|
| 1626 |
assertConstHtmlToNodeStringifiesToOneOf( |
| 1627 |
['<br>', '<BR>'], goog.string.Const.from('<br>')); |
| 1628 |
|
| 1629 |
assertConstHtmlToNodeStringifiesToOneOf( |
| 1630 |
[ |
| 1631 |
'<SVG></B>', '<svg></svg>', '<svg xmlns="http://www.w3.org/2000/svg" />' |
| 1632 |
], |
| 1633 |
goog.string.Const.from('<svg></b>')); |
| 1634 |
|
| 1635 |
assertConstHtmlToNodeStringifiesToOneOf( |
| 1636 |
['<unknown></unknown>', '<unknown>', '<UNKNOWN />'], |
| 1637 |
goog.string.Const.from('<unknown />')); |
| 1638 |
|
| 1639 |
assertConstHtmlToNodeStringifiesToOneOf( |
| 1640 |
['<"&', '<"'], goog.string.Const.from('<"&')); |
| 1641 |
} |
| 1642 |
|
| 1643 |
function testConcatenatedConstHtmlToNodeStringifications() { |
| 1644 |
assertConstHtmlToNodeStringifiesToOneOf( |
| 1645 |
['<b>foo</b>', '<B>foo</B>'], goog.string.Const.from('<b>foo<'), |
| 1646 |
goog.string.Const.from('/b>')); |
| 1647 |
|
| 1648 |
assertConstHtmlToNodeStringifiesToOneOf( |
| 1649 |
['<b>foo</b>', '<B>foo</B>'], goog.string.Const.from('<b>foo</b>'), |
| 1650 |
goog.string.Const.from('')); |
| 1651 |
|
| 1652 |
assertConstHtmlToNodeStringifiesToOneOf(['']); |
| 1653 |
} |
| 1654 |
|
| 1655 |
function testSpecialConstHtmlToNodeStringifications() { |
| 1656 |
// body one is IE8, \r\n is opera. |
| 1657 |
assertConstHtmlToNodeStringifiesToOneOf( |
| 1658 |
[ |
| 1659 |
'<script></script>', '<SCRIPT></SCRIPT>', '<script></body></script>', |
| 1660 |
'\r\n' + |
| 1661 |
'<SCRIPT></SCRIPT>' |
| 1662 |
], |
| 1663 |
goog.string.Const.from('<script>')); |
| 1664 |
|
| 1665 |
assertConstHtmlToNodeStringifiesToOneOf( |
| 1666 |
['<% %>', '<% %>'], goog.string.Const.from('<% %>')); |
| 1667 |
|
| 1668 |
assertConstHtmlToNodeStringifiesToOneOf( |
| 1669 |
['<% <script> %></script>', '<% <script> %>'], |
| 1670 |
goog.string.Const.from('<% <script> %>')); |
| 1671 |
|
| 1672 |
assertConstHtmlToNodeStringifiesToOneOf( |
| 1673 |
['</ hi />', '<!-- hi /-->', ''], goog.string.Const.from('</ hi />')); |
| 1674 |
|
| 1675 |
assertConstHtmlToNodeStringifiesToOneOf( |
| 1676 |
['<!-- <script --> />', '</ <script>/>', ' />'], |
| 1677 |
goog.string.Const.from('</ <script > />')); |
| 1678 |
} |
| 1679 |
|
| 1680 |
function testAppend() { |
| 1681 |
var div = goog.dom.createElement(goog.dom.TagName.DIV); |
| 1682 |
var b = goog.dom.createElement(goog.dom.TagName.B); |
| 1683 |
var c = document.createTextNode('c'); |
| 1684 |
goog.dom.append(div, 'a', b, c); |
| 1685 |
assertEqualsCaseAndLeadingWhitespaceInsensitive('a<b></b>c', div.innerHTML); |
| 1686 |
} |
| 1687 |
|
| 1688 |
function testAppend2() { |
| 1689 |
var dom = new goog.dom.DomHelper(myIframeDoc); |
| 1690 |
var div = dom.createElement(goog.dom.TagName.DIV); |
| 1691 |
var b = dom.createElement(goog.dom.TagName.B); |
| 1692 |
var c = myIframeDoc.createTextNode('c'); |
| 1693 |
goog.dom.append(div, 'a', b, c); |
| 1694 |
assertEqualsCaseAndLeadingWhitespaceInsensitive('a<b></b>c', div.innerHTML); |
| 1695 |
} |
| 1696 |
|
| 1697 |
function testAppend3() { |
| 1698 |
var div = goog.dom.createElement(goog.dom.TagName.DIV); |
| 1699 |
var b = goog.dom.createElement(goog.dom.TagName.B); |
| 1700 |
var c = document.createTextNode('c'); |
| 1701 |
goog.dom.append(div, ['a', b, c]); |
| 1702 |
assertEqualsCaseAndLeadingWhitespaceInsensitive('a<b></b>c', div.innerHTML); |
| 1703 |
} |
| 1704 |
|
| 1705 |
function testAppend4() { |
| 1706 |
var div = goog.dom.createElement(goog.dom.TagName.DIV); |
| 1707 |
var div2 = goog.dom.createElement(goog.dom.TagName.DIV); |
| 1708 |
div2.innerHTML = 'a<b></b>c'; |
| 1709 |
goog.dom.append(div, div2.childNodes); |
| 1710 |
assertEqualsCaseAndLeadingWhitespaceInsensitive('a<b></b>c', div.innerHTML); |
| 1711 |
assertFalse(div2.hasChildNodes()); |
| 1712 |
} |
| 1713 |
|
| 1714 |
function testGetDocumentScroll() { |
| 1715 |
// setUpPage added divForTestingScrolling to the DOM. It's not init'd here so |
| 1716 |
// it can be shared amonst other tests. |
| 1717 |
window.scrollTo(100, 100); |
| 1718 |
|
| 1719 |
assertEquals(100, goog.dom.getDocumentScroll().x); |
| 1720 |
assertEquals(100, goog.dom.getDocumentScroll().y); |
| 1721 |
} |
| 1722 |
|
| 1723 |
function testGetDocumentScrollOfFixedViewport() { |
| 1724 |
// iOS and perhaps other environments don't actually support scrolling. |
| 1725 |
// Instead, you view the document's fixed layout through a screen viewport. |
| 1726 |
// We need getDocumentScroll to handle this case though. |
| 1727 |
// In case of IE10 though, we do want to use scrollLeft/scrollTop |
| 1728 |
// because the rest of the positioning is done off the scrolled away origin. |
| 1729 |
var fakeDocumentScrollElement = {scrollLeft: 0, scrollTop: 0}; |
| 1730 |
var fakeDocument = { |
| 1731 |
defaultView: {pageXOffset: 100, pageYOffset: 100}, |
| 1732 |
documentElement: fakeDocumentScrollElement, |
| 1733 |
body: fakeDocumentScrollElement |
| 1734 |
}; |
| 1735 |
var dh = goog.dom.getDomHelper(document); |
| 1736 |
dh.setDocument(fakeDocument); |
| 1737 |
if (goog.userAgent.IE && goog.userAgent.isVersionOrHigher(10)) { |
| 1738 |
assertEquals(0, dh.getDocumentScroll().x); |
| 1739 |
assertEquals(0, dh.getDocumentScroll().y); |
| 1740 |
} else { |
| 1741 |
assertEquals(100, dh.getDocumentScroll().x); |
| 1742 |
assertEquals(100, dh.getDocumentScroll().y); |
| 1743 |
} |
| 1744 |
} |
| 1745 |
|
| 1746 |
|
| 1747 |
function testGetDocumentScrollFromDocumentWithoutABody() { |
| 1748 |
// Some documents, like SVG docs, do not have a body element. The document |
| 1749 |
// element should be used when computing the document scroll for these |
| 1750 |
// documents. |
| 1751 |
var fakeDocument = { |
| 1752 |
defaultView: {pageXOffset: 0, pageYOffset: 0}, |
| 1753 |
documentElement: {scrollLeft: 0, scrollTop: 0} |
| 1754 |
}; |
| 1755 |
|
| 1756 |
var dh = new goog.dom.DomHelper(fakeDocument); |
| 1757 |
assertEquals(fakeDocument.documentElement, dh.getDocumentScrollElement()); |
| 1758 |
assertEquals(0, dh.getDocumentScroll().x); |
| 1759 |
assertEquals(0, dh.getDocumentScroll().y); |
| 1760 |
// OK if this does not throw. |
| 1761 |
} |
| 1762 |
|
| 1763 |
function testDefaultToScrollingElement() { |
| 1764 |
var fakeDocument = {documentElement: {}, body: {}}; |
| 1765 |
var dh = new goog.dom.DomHelper(fakeDocument); |
| 1766 |
|
| 1767 |
// When scrollingElement isn't supported or is null (no element causes |
| 1768 |
// scrolling), then behavior is UA-dependent for maximum compatibility. |
| 1769 |
assertTrue( |
| 1770 |
dh.getDocumentScrollElement() == fakeDocument.body || |
| 1771 |
dh.getDocumentScrollElement() == fakeDocument.documentElement); |
| 1772 |
fakeDocument.scrollingElement = null; |
| 1773 |
assertTrue( |
| 1774 |
dh.getDocumentScrollElement() == fakeDocument.body || |
| 1775 |
dh.getDocumentScrollElement() == fakeDocument.documentElement); |
| 1776 |
|
| 1777 |
// But when scrollingElement is set, we use it directly. |
| 1778 |
fakeDocument.scrollingElement = fakeDocument.documentElement; |
| 1779 |
assertEquals(fakeDocument.documentElement, dh.getDocumentScrollElement()); |
| 1780 |
fakeDocument.scrollingElement = fakeDocument.body; |
| 1781 |
assertEquals(fakeDocument.body, dh.getDocumentScrollElement()); |
| 1782 |
} |
| 1783 |
|
| 1784 |
function testActiveElementIE() { |
| 1785 |
if (!goog.userAgent.IE) { |
| 1786 |
return; |
| 1787 |
} |
| 1788 |
|
| 1789 |
var link = goog.dom.getElement('link'); |
| 1790 |
link.focus(); |
| 1791 |
|
| 1792 |
assertEquals(link.tagName, goog.dom.getActiveElement(document).tagName); |
| 1793 |
assertEquals(link, goog.dom.getActiveElement(document)); |
| 1794 |
} |
| 1795 |
|
| 1796 |
function testParentElement() { |
| 1797 |
var testEl = $('testEl'); |
| 1798 |
var bodyEl = goog.dom.getParentElement(testEl); |
| 1799 |
assertNotNull(bodyEl); |
| 1800 |
var htmlEl = goog.dom.getParentElement(bodyEl); |
| 1801 |
assertNotNull(htmlEl); |
| 1802 |
var documentNotAnElement = goog.dom.getParentElement(htmlEl); |
| 1803 |
assertNull(documentNotAnElement); |
| 1804 |
|
| 1805 |
var tree = goog.dom.safeHtmlToNode(goog.html.testing.newSafeHtmlForTest( |
| 1806 |
'<div>' + |
| 1807 |
'<p>Some text</p>' + |
| 1808 |
'<blockquote>Some <i>special</i> <b>text</b></blockquote>' + |
| 1809 |
'<address><!-- comment -->Foo</address>' + |
| 1810 |
'</div>')); |
| 1811 |
assertNull(goog.dom.getParentElement(tree)); |
| 1812 |
pEl = goog.dom.getNextNode(tree); |
| 1813 |
var fragmentRootEl = goog.dom.getParentElement(pEl); |
| 1814 |
assertEquals(tree, fragmentRootEl); |
| 1815 |
|
| 1816 |
var detachedEl = goog.dom.createDom(goog.dom.TagName.DIV); |
| 1817 |
var detachedHasNoParent = goog.dom.getParentElement(detachedEl); |
| 1818 |
assertNull(detachedHasNoParent); |
| 1819 |
|
| 1820 |
// svg is not supported in IE8 and below or in IE9 quirks mode |
| 1821 |
var supported = !goog.userAgent.IE || |
| 1822 |
goog.userAgent.isDocumentModeOrHigher(10) || |
| 1823 |
(goog.dom.isCss1CompatMode() && goog.userAgent.isDocumentModeOrHigher(9)); |
| 1824 |
if (!supported) { |
| 1825 |
return; |
| 1826 |
} |
| 1827 |
|
| 1828 |
var svg = $('testSvg'); |
| 1829 |
assertNotNull(svg); |
| 1830 |
var rect = $('testRect'); |
| 1831 |
assertNotNull(rect); |
| 1832 |
var g = $('testG'); |
| 1833 |
assertNotNull(g); |
| 1834 |
|
| 1835 |
if (goog.userAgent.IE && goog.userAgent.isVersionOrHigher('9')) { |
| 1836 |
// test to make sure IE9 is returning undefined for .parentElement |
| 1837 |
assertUndefined(g.parentElement); |
| 1838 |
assertUndefined(rect.parentElement); |
| 1839 |
assertUndefined(svg.parentElement); |
| 1840 |
} |
| 1841 |
var shouldBeG = goog.dom.getParentElement(rect); |
| 1842 |
assertEquals(g, shouldBeG); |
| 1843 |
var shouldBeSvg = goog.dom.getParentElement(g); |
| 1844 |
assertEquals(svg, shouldBeSvg); |
| 1845 |
var shouldBeBody = goog.dom.getParentElement(svg); |
| 1846 |
assertEquals(bodyEl, shouldBeBody); |
| 1847 |
} |
| 1848 |
|
| 1849 |
|
| 1850 |
/** |
| 1851 |
* @return {boolean} Returns true if the userAgent is IE8 or higher. |
| 1852 |
*/ |
| 1853 |
function isIE8OrHigher() { |
| 1854 |
return goog.userAgent.IE && goog.userAgent.product.isVersion('8'); |
| 1855 |
} |
| 1856 |
|
| 1857 |
/** |
| 1858 |
* Stub out goog.dom.getWindow with passed object. |
| 1859 |
* @param {!Object} win Fake window object. |
| 1860 |
*/ |
| 1861 |
function setWindow(win) { |
| 1862 |
stubs.set(goog.dom, 'getWindow', goog.functions.constant(win)); |
| 1863 |
} |
| 1864 |
|
| 1865 |
function testDevicePixelRatio() { |
| 1866 |
var devicePixelRatio = 1.5; |
| 1867 |
setWindow({ |
| 1868 |
'matchMedia': function(query) { |
| 1869 |
return { |
| 1870 |
'matches': devicePixelRatio >= parseFloat(query.split(': ')[1], 10) |
| 1871 |
}; |
| 1872 |
} |
| 1873 |
}); |
| 1874 |
|
| 1875 |
assertEquals(devicePixelRatio, goog.dom.getPixelRatio()); |
| 1876 |
|
| 1877 |
setWindow({'devicePixelRatio': 2.0}); |
| 1878 |
assertEquals(2, goog.dom.getPixelRatio()); |
| 1879 |
|
| 1880 |
setWindow({}); |
| 1881 |
assertEquals(1, goog.dom.getPixelRatio()); |
| 1882 |
} |