| 1 |
// Copyright 2013 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 Unit tests for goog.dom.safe. |
| 17 |
*/ |
| 18 |
|
| 19 |
goog.provide('goog.dom.safeTest'); |
| 20 |
goog.setTestOnly('goog.dom.safeTest'); |
| 21 |
|
| 22 |
goog.require('goog.asserts'); |
| 23 |
goog.require('goog.dom'); |
| 24 |
goog.require('goog.dom.TagName'); |
| 25 |
goog.require('goog.dom.safe'); |
| 26 |
goog.require('goog.dom.safe.InsertAdjacentHtmlPosition'); |
| 27 |
goog.require('goog.html.SafeHtml'); |
| 28 |
goog.require('goog.html.SafeScript'); |
| 29 |
goog.require('goog.html.SafeStyle'); |
| 30 |
goog.require('goog.html.SafeUrl'); |
| 31 |
goog.require('goog.html.TrustedResourceUrl'); |
| 32 |
goog.require('goog.html.testing'); |
| 33 |
goog.require('goog.string'); |
| 34 |
goog.require('goog.string.Const'); |
| 35 |
goog.require('goog.testing'); |
| 36 |
goog.require('goog.testing.jsunit'); |
| 37 |
goog.require('goog.userAgent'); |
| 38 |
|
| 39 |
|
| 40 |
var mockWindowOpen; |
| 41 |
|
| 42 |
|
| 43 |
function tearDown() { |
| 44 |
if (mockWindowOpen) { |
| 45 |
mockWindowOpen.$tearDown(); |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
function testInsertAdjacentHtml() { |
| 51 |
var writtenHtml; |
| 52 |
var writtenPosition; |
| 53 |
var mockNode = /** @type {!Node} */ ({ |
| 54 |
'insertAdjacentHTML': function(position, html) { |
| 55 |
writtenPosition = position; |
| 56 |
writtenHtml = html; |
| 57 |
} |
| 58 |
}); |
| 59 |
|
| 60 |
goog.dom.safe.insertAdjacentHtml( |
| 61 |
mockNode, goog.dom.safe.InsertAdjacentHtmlPosition.BEFOREBEGIN, |
| 62 |
goog.html.SafeHtml.create('div', {}, 'foobar')); |
| 63 |
assertEquals('<div>foobar</div>', writtenHtml); |
| 64 |
assertEquals('beforebegin', writtenPosition); |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
function testSetInnerHtml() { |
| 69 |
var mockElement = |
| 70 |
/** @type {!Element} */ ({'tagName': 'DIV', 'innerHTML': 'blarg'}); |
| 71 |
var html = '<script>somethingTrusted();<' + |
| 72 |
'/script>'; |
| 73 |
var safeHtml = goog.html.testing.newSafeHtmlForTest(html); |
| 74 |
goog.dom.safe.setInnerHtml(mockElement, safeHtml); |
| 75 |
assertEquals(html, mockElement.innerHTML); |
| 76 |
} |
| 77 |
|
| 78 |
function testSetInnerHtml_doesntAllowScript() { |
| 79 |
var script = |
| 80 |
/** @type {!Element} */ ({'tagName': 'SCRIPT', 'innerHTML': 'blarg'}); |
| 81 |
var safeHtml = goog.html.SafeHtml.htmlEscape('alert(1);'); |
| 82 |
assertThrows(function() { goog.dom.safe.setInnerHtml(script, safeHtml); }); |
| 83 |
} |
| 84 |
|
| 85 |
function testSetInnerHtml_doesntAllowStyle() { |
| 86 |
var style = |
| 87 |
/** @type {!Element} */ ({'tagName': 'STYLE', 'innerHTML': 'blarg'}); |
| 88 |
var safeHtml = goog.html.SafeHtml.htmlEscape('A { color: red; }'); |
| 89 |
assertThrows(function() { goog.dom.safe.setInnerHtml(style, safeHtml); }); |
| 90 |
} |
| 91 |
|
| 92 |
function testSetStyle() { |
| 93 |
var style = |
| 94 |
goog.html.SafeStyle.fromConstant(goog.string.Const.from('color: red;')); |
| 95 |
var elem = document.createElement('div'); |
| 96 |
assertEquals('', elem.style.color); // sanity check |
| 97 |
|
| 98 |
goog.dom.safe.setStyle(elem, style); |
| 99 |
assertEquals('red', elem.style.color); |
| 100 |
} |
| 101 |
|
| 102 |
function testDocumentWrite() { |
| 103 |
var mockDoc = /** @type {!Document} */ ({ |
| 104 |
'html': null, |
| 105 |
/** @suppress {globalThis} */ |
| 106 |
'write': function(html) { this['html'] = html; } |
| 107 |
}); |
| 108 |
var html = '<script>somethingTrusted();<' + |
| 109 |
'/script>'; |
| 110 |
var safeHtml = goog.html.testing.newSafeHtmlForTest(html); |
| 111 |
goog.dom.safe.documentWrite(mockDoc, safeHtml); |
| 112 |
assertEquals(html, mockDoc.html); |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
function testsetLinkHrefAndRel_trustedResourceUrl() { |
| 117 |
var mockLink = /** @type {!HTMLLinkElement} */ ({'href': null, 'rel': null}); |
| 118 |
|
| 119 |
var url = goog.html.TrustedResourceUrl.fromConstant( |
| 120 |
goog.string.Const.from('javascript:trusted();')); |
| 121 |
// Test case-insensitive too. |
| 122 |
goog.dom.safe.setLinkHrefAndRel(mockLink, url, 'foo, Stylesheet, bar'); |
| 123 |
assertEquals('javascript:trusted();', mockLink.href); |
| 124 |
|
| 125 |
goog.dom.safe.setLinkHrefAndRel(mockLink, url, 'foo, bar'); |
| 126 |
assertEquals('javascript:trusted();', mockLink.href); |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
function testsetLinkHrefAndRel_safeUrl() { |
| 131 |
var mockLink = /** @type {!HTMLLinkElement} */ ({'href': null, 'rel': null}); |
| 132 |
|
| 133 |
var url = goog.html.SafeUrl.fromConstant( |
| 134 |
goog.string.Const.from('javascript:trusted();')); |
| 135 |
assertThrows(function() { |
| 136 |
goog.dom.safe.setLinkHrefAndRel(mockLink, url, 'foo, stylesheet, bar'); |
| 137 |
}); |
| 138 |
|
| 139 |
goog.dom.safe.setLinkHrefAndRel(mockLink, url, 'foo, bar'); |
| 140 |
assertEquals('javascript:trusted();', mockLink.href); |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
function testsetLinkHrefAndRel_string() { |
| 145 |
var mockLink = /** @type {!HTMLLinkElement} */ ({'href': null, 'rel': null}); |
| 146 |
|
| 147 |
assertThrows(function() { |
| 148 |
goog.dom.safe.setLinkHrefAndRel( |
| 149 |
mockLink, 'javascript:evil();', 'foo, stylesheet, bar'); |
| 150 |
}); |
| 151 |
withAssertionFailure(function() { |
| 152 |
goog.dom.safe.setLinkHrefAndRel(mockLink, 'javascript:evil();', 'foo, bar'); |
| 153 |
}); |
| 154 |
assertEquals('about:invalid#zClosurez', mockLink.href); |
| 155 |
} |
| 156 |
|
| 157 |
function testsetLinkHrefAndRel_assertsType() { |
| 158 |
if (!goog.userAgent.IE || goog.userAgent.isVersionOrHigher(10)) { |
| 159 |
var otherElement = document.createElement('A'); |
| 160 |
var ex = assertThrows(function() { |
| 161 |
goog.dom.safe.setLinkHrefAndRel( |
| 162 |
/** @type {!HTMLLinkElement} */ (otherElement), 'http://example.com/', |
| 163 |
'author'); |
| 164 |
}); |
| 165 |
assert( |
| 166 |
goog.string.contains(ex.message, 'Argument is not a HTMLLinkElement')); |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Returns a link element, incorrectly typed as a Location. |
| 172 |
* @return {!Location} |
| 173 |
* @suppress {checkTypes} |
| 174 |
*/ |
| 175 |
function makeLinkElementTypedAsLocation() { |
| 176 |
return document.createElement('LINK'); |
| 177 |
} |
| 178 |
|
| 179 |
function testSetLocationHref() { |
| 180 |
var mockLoc = /** @type {!Location} */ ({'href': 'blarg'}); |
| 181 |
withAssertionFailure(function() { |
| 182 |
goog.dom.safe.setLocationHref(mockLoc, 'javascript:evil();'); |
| 183 |
}); |
| 184 |
assertEquals('about:invalid#zClosurez', mockLoc.href); |
| 185 |
|
| 186 |
mockLoc = /** @type {!Location} */ ({'href': 'blarg'}); |
| 187 |
var safeUrl = goog.html.SafeUrl.fromConstant( |
| 188 |
goog.string.Const.from('javascript:trusted();')); |
| 189 |
goog.dom.safe.setLocationHref(mockLoc, safeUrl); |
| 190 |
assertEquals('javascript:trusted();', mockLoc.href); |
| 191 |
|
| 192 |
// Asserts correct runtime type. |
| 193 |
if (!goog.userAgent.IE || goog.userAgent.isVersionOrHigher(10)) { |
| 194 |
var ex = assertThrows(function() { |
| 195 |
goog.dom.safe.setLocationHref(makeLinkElementTypedAsLocation(), safeUrl); |
| 196 |
}); |
| 197 |
assert(goog.string.contains(ex.message, 'Argument is not a Location')); |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
function testReplaceLocationSafeString() { |
| 202 |
// TODO(bangert): the mocks don't work on IE 8 |
| 203 |
if (!goog.userAgent.IE || goog.userAgent.isVersionOrHigher(10)) { |
| 204 |
/** @type {?} */ |
| 205 |
var mockLoc = new goog.testing.StrictMock(window.location); |
| 206 |
mockLoc.replace('http://example.com/'); |
| 207 |
mockLoc.$replay(); |
| 208 |
goog.dom.safe.replaceLocation(mockLoc, 'http://example.com/'); |
| 209 |
mockLoc.$verify(); |
| 210 |
mockLoc.$reset(); |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
function testReplaceLocationEvilString() { |
| 215 |
// TODO(bangert): the mocks don't work on IE 8 |
| 216 |
if (!goog.userAgent.IE || goog.userAgent.isVersionOrHigher(10)) { |
| 217 |
/** @type {?} */ |
| 218 |
var mockLoc = new goog.testing.StrictMock(window.location); |
| 219 |
mockLoc.replace('about:invalid#zClosurez'); |
| 220 |
mockLoc.$replay(); |
| 221 |
withAssertionFailure(function() { |
| 222 |
goog.dom.safe.replaceLocation(mockLoc, 'javascript:evil();'); |
| 223 |
}); |
| 224 |
mockLoc.$verify(); |
| 225 |
mockLoc.$reset(); |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
function testReplaceLocationSafeUrl() { |
| 230 |
// TODO(bangert): the mocks don't work on IE 8 |
| 231 |
if (!goog.userAgent.IE || goog.userAgent.isVersionOrHigher(10)) { |
| 232 |
var safeUrl = goog.html.SafeUrl.fromConstant( |
| 233 |
goog.string.Const.from('javascript:trusted();')); |
| 234 |
/** @type {?} */ |
| 235 |
var mockLoc = new goog.testing.StrictMock(window.location); |
| 236 |
mockLoc.replace('javascript:trusted();'); |
| 237 |
mockLoc.$replay(); |
| 238 |
goog.dom.safe.replaceLocation(mockLoc, safeUrl); |
| 239 |
mockLoc.$verify(); |
| 240 |
mockLoc.$reset(); |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
function testSetAnchorHref() { |
| 245 |
var anchor = /** @type {!HTMLAnchorElement} */ (document.createElement('A')); |
| 246 |
withAssertionFailure(function() { |
| 247 |
goog.dom.safe.setAnchorHref(anchor, 'javascript:evil();'); |
| 248 |
}); |
| 249 |
assertEquals('about:invalid#zClosurez', anchor.href); |
| 250 |
|
| 251 |
anchor = /** @type {!HTMLAnchorElement} */ (document.createElement('A')); |
| 252 |
var safeUrl = goog.html.SafeUrl.fromConstant( |
| 253 |
goog.string.Const.from('javascript:trusted();')); |
| 254 |
goog.dom.safe.setAnchorHref(anchor, safeUrl); |
| 255 |
assertEquals('javascript:trusted();', anchor.href); |
| 256 |
|
| 257 |
// Works with mocks too. |
| 258 |
var mockAnchor = /** @type {!HTMLAnchorElement} */ ({'href': 'blarg'}); |
| 259 |
withAssertionFailure(function() { |
| 260 |
goog.dom.safe.setAnchorHref(mockAnchor, 'javascript:evil();'); |
| 261 |
}); |
| 262 |
assertEquals('about:invalid#zClosurez', mockAnchor.href); |
| 263 |
|
| 264 |
mockAnchor = /** @type {!HTMLAnchorElement} */ ({'href': 'blarg'}); |
| 265 |
safeUrl = goog.html.SafeUrl.fromConstant( |
| 266 |
goog.string.Const.from('javascript:trusted();')); |
| 267 |
goog.dom.safe.setAnchorHref(mockAnchor, safeUrl); |
| 268 |
assertEquals('javascript:trusted();', mockAnchor.href); |
| 269 |
|
| 270 |
// Asserts correct runtime type. |
| 271 |
if (!goog.userAgent.IE || goog.userAgent.isVersionOrHigher(10)) { |
| 272 |
var otherElement = document.createElement('LINK'); |
| 273 |
var ex = assertThrows(function() { |
| 274 |
goog.dom.safe.setAnchorHref( |
| 275 |
/** @type {!HTMLAnchorElement} */ (otherElement), safeUrl); |
| 276 |
}); |
| 277 |
assert(goog.string.contains( |
| 278 |
ex.message, 'Argument is not a HTMLAnchorElement')); |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
function testSetInputFormActionHarmlessString() { |
| 283 |
var element = goog.dom.createElement(goog.dom.TagName.INPUT); |
| 284 |
goog.dom.safe.setInputFormAction(element, 'foo.com'); |
| 285 |
assertEquals('foo.com', element.formaction); |
| 286 |
} |
| 287 |
|
| 288 |
function testSetInputFormActionEvilString() { |
| 289 |
var element = goog.dom.createElement(goog.dom.TagName.INPUT); |
| 290 |
withAssertionFailure(function() { |
| 291 |
goog.dom.safe.setInputFormAction(element, 'javascript:evil();'); |
| 292 |
}); |
| 293 |
assertEquals('about:invalid#zClosurez', element.formaction); |
| 294 |
} |
| 295 |
|
| 296 |
function testSetInputFormActionSafeUrl() { |
| 297 |
var element = goog.dom.createElement(goog.dom.TagName.INPUT); |
| 298 |
goog.dom.safe.setInputFormAction( |
| 299 |
element, |
| 300 |
goog.html.SafeUrl.fromConstant( |
| 301 |
goog.string.Const.from('javascript:trusted();'))); |
| 302 |
assertEquals('javascript:trusted();', element.formaction); |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
function testSetInputFormActionAssertsType() { |
| 307 |
/** @type {?} */ |
| 308 |
var element = goog.dom.createElement(goog.dom.TagName.FORM); |
| 309 |
withAssertionFailure(function() { |
| 310 |
goog.dom.safe.setInputFormAction(element, 'foo'); |
| 311 |
}); |
| 312 |
assertEquals('foo', element.formaction); |
| 313 |
} |
| 314 |
|
| 315 |
function testSetButtonFormActionHarmlessString() { |
| 316 |
var element = goog.dom.createElement(goog.dom.TagName.BUTTON); |
| 317 |
goog.dom.safe.setButtonFormAction(element, 'http://foo.com'); |
| 318 |
assertEquals('http://foo.com', element.formaction); |
| 319 |
} |
| 320 |
|
| 321 |
function testSetButtonFormActionEvilString() { |
| 322 |
var element = goog.dom.createElement(goog.dom.TagName.BUTTON); |
| 323 |
withAssertionFailure(function() { |
| 324 |
goog.dom.safe.setButtonFormAction(element, 'javascript:evil();'); |
| 325 |
}); |
| 326 |
assertEquals('about:invalid#zClosurez', element.formaction); |
| 327 |
} |
| 328 |
|
| 329 |
function testSetButtonFormActionSafeUrl() { |
| 330 |
var element = goog.dom.createElement(goog.dom.TagName.BUTTON); |
| 331 |
goog.dom.safe.setButtonFormAction( |
| 332 |
element, |
| 333 |
goog.html.SafeUrl.fromConstant( |
| 334 |
goog.string.Const.from('javascript:trusted();'))); |
| 335 |
assertEquals('javascript:trusted();', element.formaction); |
| 336 |
} |
| 337 |
|
| 338 |
function testSetFormElementActionAssertsType() { |
| 339 |
/** @type {?} */ |
| 340 |
var element = goog.dom.createElement(goog.dom.TagName.INPUT); |
| 341 |
withAssertionFailure(function() { |
| 342 |
goog.dom.safe.setFormElementAction(element, 'javascript:evil();'); |
| 343 |
}); |
| 344 |
assertEquals('about:invalid#zClosurez', element.action); |
| 345 |
} |
| 346 |
|
| 347 |
function testSetFormElementActionHarmlessString() { |
| 348 |
var element = goog.dom.createElement(goog.dom.TagName.FORM); |
| 349 |
goog.dom.safe.setFormElementAction(element, 'http://foo.com'); |
| 350 |
assertEquals('http://foo.com/', element.action); // url is normalized |
| 351 |
} |
| 352 |
|
| 353 |
function testSetFormElementActionEvilString() { |
| 354 |
var element = goog.dom.createElement(goog.dom.TagName.FORM); |
| 355 |
withAssertionFailure(function() { |
| 356 |
goog.dom.safe.setFormElementAction(element, 'javascript:evil();'); |
| 357 |
}); |
| 358 |
assertEquals('about:invalid#zClosurez', element.action); |
| 359 |
} |
| 360 |
|
| 361 |
function testSetFormElementActionSafeUrl() { |
| 362 |
var element = goog.dom.createElement(goog.dom.TagName.FORM); |
| 363 |
goog.dom.safe.setFormElementAction( |
| 364 |
element, |
| 365 |
goog.html.SafeUrl.fromConstant( |
| 366 |
goog.string.Const.from('javascript:trusted();'))); |
| 367 |
assertEquals('javascript:trusted();', element.action); |
| 368 |
} |
| 369 |
|
| 370 |
function testSetImageSrc_withSafeUrlObject() { |
| 371 |
var mockImageElement = /** @type {!HTMLImageElement} */ ({'src': 'blarg'}); |
| 372 |
withAssertionFailure(function() { |
| 373 |
goog.dom.safe.setImageSrc(mockImageElement, 'javascript:evil();'); |
| 374 |
}); |
| 375 |
assertEquals('about:invalid#zClosurez', mockImageElement.src); |
| 376 |
|
| 377 |
mockImageElement = /** @type {!HTMLImageElement} */ ({'src': 'blarg'}); |
| 378 |
var safeUrl = goog.html.SafeUrl.fromConstant( |
| 379 |
goog.string.Const.from('javascript:trusted();')); |
| 380 |
goog.dom.safe.setImageSrc(mockImageElement, safeUrl); |
| 381 |
assertEquals('javascript:trusted();', mockImageElement.src); |
| 382 |
|
| 383 |
// Asserts correct runtime type. |
| 384 |
if (!goog.userAgent.IE || goog.userAgent.isVersionOrHigher(10)) { |
| 385 |
var otherElement = document.createElement('SCRIPT'); |
| 386 |
var ex = assertThrows(function() { |
| 387 |
goog.dom.safe.setImageSrc( |
| 388 |
/** @type {!HTMLImageElement} */ (otherElement), safeUrl); |
| 389 |
}); |
| 390 |
assert( |
| 391 |
goog.string.contains(ex.message, 'Argument is not a HTMLImageElement')); |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
function testSetImageSrc_withHttpsUrl() { |
| 396 |
var mockImageElement = /** @type {!HTMLImageElement} */ ({'src': 'blarg'}); |
| 397 |
|
| 398 |
var safeUrl = 'https://trusted_url'; |
| 399 |
goog.dom.safe.setImageSrc(mockImageElement, safeUrl); |
| 400 |
assertEquals(safeUrl, mockImageElement.src); |
| 401 |
} |
| 402 |
|
| 403 |
function testSetVideoSrc() { |
| 404 |
var mockVideoElement = /** @type {!HTMLVideoElement} */ ({'src': 'blarg'}); |
| 405 |
var safeUrl = 'https://trusted_url'; |
| 406 |
goog.dom.safe.setVideoSrc(mockVideoElement, safeUrl); |
| 407 |
assertEquals(safeUrl, mockVideoElement.src); |
| 408 |
|
| 409 |
mockVideoElement = /** @type {!HTMLVideoElement} */ ({'src': 'blarg'}); |
| 410 |
withAssertionFailure(function() { |
| 411 |
goog.dom.safe.setVideoSrc(mockVideoElement, 'javascript:evil();'); |
| 412 |
}); |
| 413 |
assertEquals('about:invalid#zClosurez', mockVideoElement.src); |
| 414 |
|
| 415 |
mockVideoElement = /** @type {!HTMLVideoElement} */ ({'src': 'blarg'}); |
| 416 |
var safeUrl = goog.html.SafeUrl.fromConstant( |
| 417 |
goog.string.Const.from('javascript:trusted();')); |
| 418 |
goog.dom.safe.setVideoSrc(mockVideoElement, safeUrl); |
| 419 |
assertEquals('javascript:trusted();', mockVideoElement.src); |
| 420 |
|
| 421 |
// Asserts correct runtime type. |
| 422 |
if (!goog.userAgent.IE || goog.userAgent.isVersionOrHigher(10)) { |
| 423 |
var otherElement = document.createElement('SCRIPT'); |
| 424 |
var ex = assertThrows(function() { |
| 425 |
goog.dom.safe.setVideoSrc( |
| 426 |
/** @type {!HTMLVideoElement} */ (otherElement), safeUrl); |
| 427 |
}); |
| 428 |
assert( |
| 429 |
goog.string.contains(ex.message, 'Argument is not a HTMLVideoElement')); |
| 430 |
} |
| 431 |
} |
| 432 |
|
| 433 |
function testSetEmbedSrc() { |
| 434 |
var url = goog.html.TrustedResourceUrl.fromConstant( |
| 435 |
goog.string.Const.from('javascript:trusted();')); |
| 436 |
var mockElement = /** @type {!HTMLEmbedElement} */ ({'src': 'blarg'}); |
| 437 |
goog.dom.safe.setEmbedSrc(mockElement, url); |
| 438 |
assertEquals('javascript:trusted();', mockElement.src); |
| 439 |
|
| 440 |
// Asserts correct runtime type. |
| 441 |
if (!goog.userAgent.IE || goog.userAgent.isVersionOrHigher(10)) { |
| 442 |
var otherElement = document.createElement('IMAGE'); |
| 443 |
var ex = assertThrows(function() { |
| 444 |
goog.dom.safe.setEmbedSrc( |
| 445 |
/** @type {!HTMLEmbedElement} */ (otherElement), url); |
| 446 |
}); |
| 447 |
assert( |
| 448 |
goog.string.contains(ex.message, 'Argument is not a HTMLEmbedElement')); |
| 449 |
} |
| 450 |
} |
| 451 |
|
| 452 |
function testSetFrameSrc() { |
| 453 |
var url = goog.html.TrustedResourceUrl.fromConstant( |
| 454 |
goog.string.Const.from('javascript:trusted();')); |
| 455 |
var mockElement = /** @type {!HTMLFrameElement} */ ({'src': 'blarg'}); |
| 456 |
goog.dom.safe.setFrameSrc(mockElement, url); |
| 457 |
assertEquals('javascript:trusted();', mockElement.src); |
| 458 |
|
| 459 |
// Asserts correct runtime type. |
| 460 |
if (!goog.userAgent.IE || goog.userAgent.isVersionOrHigher(10)) { |
| 461 |
var otherElement = document.createElement('IMAGE'); |
| 462 |
var ex = assertThrows(function() { |
| 463 |
goog.dom.safe.setFrameSrc( |
| 464 |
/** @type {!HTMLFrameElement} */ (otherElement), url); |
| 465 |
}); |
| 466 |
assert( |
| 467 |
goog.string.contains(ex.message, 'Argument is not a HTMLFrameElement')); |
| 468 |
} |
| 469 |
} |
| 470 |
|
| 471 |
function testSetIframeSrc() { |
| 472 |
var url = goog.html.TrustedResourceUrl.fromConstant( |
| 473 |
goog.string.Const.from('javascript:trusted();')); |
| 474 |
var mockElement = /** @type {!HTMLIFrameElement} */ ({'src': 'blarg'}); |
| 475 |
goog.dom.safe.setIframeSrc(mockElement, url); |
| 476 |
assertEquals('javascript:trusted();', mockElement.src); |
| 477 |
|
| 478 |
// Asserts correct runtime type. |
| 479 |
if (!goog.userAgent.IE || goog.userAgent.isVersionOrHigher(10)) { |
| 480 |
var otherElement = document.createElement('IMAGE'); |
| 481 |
var ex = assertThrows(function() { |
| 482 |
goog.dom.safe.setIframeSrc( |
| 483 |
/** @type {!HTMLIFrameElement} */ (otherElement), url); |
| 484 |
}); |
| 485 |
assert(goog.string.contains( |
| 486 |
ex.message, 'Argument is not a HTMLIFrameElement')); |
| 487 |
} |
| 488 |
} |
| 489 |
|
| 490 |
function testSetIframeSrcdoc() { |
| 491 |
var html = goog.html.SafeHtml.create('div', {}, 'foobar'); |
| 492 |
var mockIframe = /** @type {!HTMLIFrameElement} */ ({'srcdoc': ''}); |
| 493 |
goog.dom.safe.setIframeSrcdoc(mockIframe, html); |
| 494 |
assertEquals('<div>foobar</div>', mockIframe.srcdoc); |
| 495 |
|
| 496 |
// Asserts correct runtime type. |
| 497 |
if (!goog.userAgent.IE || goog.userAgent.isVersionOrHigher(10)) { |
| 498 |
var otherElement = document.createElement('IMAGE'); |
| 499 |
var ex = assertThrows(function() { |
| 500 |
goog.dom.safe.setIframeSrcdoc( |
| 501 |
/** @type {!HTMLIFrameElement} */ (otherElement), html); |
| 502 |
}); |
| 503 |
assert(goog.string.contains( |
| 504 |
ex.message, 'Argument is not a HTMLIFrameElement')); |
| 505 |
} |
| 506 |
} |
| 507 |
|
| 508 |
function testSetObjectData() { |
| 509 |
var url = goog.html.TrustedResourceUrl.fromConstant( |
| 510 |
goog.string.Const.from('javascript:trusted();')); |
| 511 |
var mockElement = /** @type {!HTMLObjectElement} */ ({'data': 'blarg'}); |
| 512 |
goog.dom.safe.setObjectData(mockElement, url); |
| 513 |
assertEquals('javascript:trusted();', mockElement.data); |
| 514 |
|
| 515 |
// Asserts correct runtime type. |
| 516 |
if (!goog.userAgent.IE || goog.userAgent.isVersionOrHigher(10)) { |
| 517 |
var otherElement = document.createElement('IMAGE'); |
| 518 |
var ex = assertThrows(function() { |
| 519 |
goog.dom.safe.setObjectData( |
| 520 |
/** @type {!HTMLObjectElement} */ (otherElement), url); |
| 521 |
}); |
| 522 |
assert(goog.string.contains( |
| 523 |
ex.message, 'Argument is not a HTMLObjectElement')); |
| 524 |
} |
| 525 |
} |
| 526 |
|
| 527 |
function testSetScriptSrc() { |
| 528 |
var url = goog.html.TrustedResourceUrl.fromConstant( |
| 529 |
goog.string.Const.from('javascript:trusted();')); |
| 530 |
var mockElement = /** @type {!HTMLScriptElement} */ ({'src': 'blarg'}); |
| 531 |
goog.dom.safe.setScriptSrc(mockElement, url); |
| 532 |
assertEquals('javascript:trusted();', mockElement.src); |
| 533 |
|
| 534 |
// Asserts correct runtime type. |
| 535 |
if (!goog.userAgent.IE || goog.userAgent.isVersionOrHigher(10)) { |
| 536 |
var otherElement = document.createElement('IMAGE'); |
| 537 |
var ex = assertThrows(function() { |
| 538 |
goog.dom.safe.setScriptSrc( |
| 539 |
/** @type {!HTMLScriptElement} */ (otherElement), url); |
| 540 |
}); |
| 541 |
assert(goog.string.contains( |
| 542 |
ex.message, 'Argument is not a HTMLScriptElement')); |
| 543 |
} |
| 544 |
} |
| 545 |
|
| 546 |
function testSetScriptContent() { |
| 547 |
var mockScriptElement = /** @type {!HTMLScriptElement} */ ({}); |
| 548 |
|
| 549 |
var content = |
| 550 |
goog.html.SafeScript.fromConstant(goog.string.Const.from('alert(1);')); |
| 551 |
goog.dom.safe.setScriptContent(mockScriptElement, content); |
| 552 |
assertEquals(goog.html.SafeScript.unwrap(content), mockScriptElement.text); |
| 553 |
} |
| 554 |
|
| 555 |
function testOpenInWindow() { |
| 556 |
mockWindowOpen = |
| 557 |
/** @type {?} */ (goog.testing.createMethodMock(window, 'open')); |
| 558 |
var fakeWindow = {}; |
| 559 |
|
| 560 |
mockWindowOpen('about:invalid#zClosurez', 'name', 'specs', true) |
| 561 |
.$returns(fakeWindow); |
| 562 |
mockWindowOpen.$replay(); |
| 563 |
var retVal = withAssertionFailure(function() { |
| 564 |
return goog.dom.safe.openInWindow( |
| 565 |
'javascript:evil();', window, goog.string.Const.from('name'), 'specs', |
| 566 |
true); |
| 567 |
}); |
| 568 |
mockWindowOpen.$verify(); |
| 569 |
assertEquals( |
| 570 |
'openInWindow should return the created window', fakeWindow, retVal); |
| 571 |
|
| 572 |
mockWindowOpen.$reset(); |
| 573 |
retVal = null; |
| 574 |
|
| 575 |
var safeUrl = goog.html.SafeUrl.fromConstant( |
| 576 |
goog.string.Const.from('javascript:trusted();')); |
| 577 |
mockWindowOpen('javascript:trusted();', 'name', 'specs', true) |
| 578 |
.$returns(fakeWindow); |
| 579 |
mockWindowOpen.$replay(); |
| 580 |
retVal = goog.dom.safe.openInWindow( |
| 581 |
safeUrl, window, goog.string.Const.from('name'), 'specs', true); |
| 582 |
mockWindowOpen.$verify(); |
| 583 |
assertEquals( |
| 584 |
'openInWindow should return the created window', fakeWindow, retVal); |
| 585 |
} |
| 586 |
|
| 587 |
/** |
| 588 |
* Tests that f raises an AssertionError and runs f while disabling assertion |
| 589 |
* errors. |
| 590 |
* |
| 591 |
* @param {function():*} f function with a failing assertion. |
| 592 |
* @return {*} the return value of f. |
| 593 |
*/ |
| 594 |
function withAssertionFailure(f) { |
| 595 |
assertThrows(f); |
| 596 |
goog.asserts.setErrorHandler(function(error) {}); |
| 597 |
try { |
| 598 |
return f(); |
| 599 |
} finally { |
| 600 |
goog.asserts.setErrorHandler(goog.asserts.DEFAULT_ERROR_HANDLER); |
| 601 |
} |
| 602 |
} |