| 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.html.SafeHtml and its builders. |
| 17 |
*/ |
| 18 |
|
| 19 |
goog.provide('goog.html.safeHtmlTest'); |
| 20 |
|
| 21 |
goog.require('goog.html.SafeHtml'); |
| 22 |
goog.require('goog.html.SafeScript'); |
| 23 |
goog.require('goog.html.SafeStyle'); |
| 24 |
goog.require('goog.html.SafeStyleSheet'); |
| 25 |
goog.require('goog.html.SafeUrl'); |
| 26 |
goog.require('goog.html.TrustedResourceUrl'); |
| 27 |
goog.require('goog.html.testing'); |
| 28 |
goog.require('goog.i18n.bidi.Dir'); |
| 29 |
goog.require('goog.labs.userAgent.browser'); |
| 30 |
goog.require('goog.object'); |
| 31 |
goog.require('goog.string.Const'); |
| 32 |
goog.require('goog.testing.jsunit'); |
| 33 |
|
| 34 |
goog.setTestOnly('goog.html.safeHtmlTest'); |
| 35 |
|
| 36 |
|
| 37 |
function testSafeHtml() { |
| 38 |
// TODO(xtof): Consider using SafeHtmlBuilder instead of newSafeHtmlForTest, |
| 39 |
// when available. |
| 40 |
var safeHtml = goog.html.testing.newSafeHtmlForTest('Hello <em>World</em>'); |
| 41 |
assertSameHtml('Hello <em>World</em>', safeHtml); |
| 42 |
assertEquals('Hello <em>World</em>', goog.html.SafeHtml.unwrap(safeHtml)); |
| 43 |
assertEquals('SafeHtml{Hello <em>World</em>}', String(safeHtml)); |
| 44 |
assertNull(safeHtml.getDirection()); |
| 45 |
|
| 46 |
safeHtml = goog.html.testing.newSafeHtmlForTest( |
| 47 |
'World <em>Hello</em>', goog.i18n.bidi.Dir.RTL); |
| 48 |
assertSameHtml('World <em>Hello</em>', safeHtml); |
| 49 |
assertEquals('World <em>Hello</em>', goog.html.SafeHtml.unwrap(safeHtml)); |
| 50 |
assertEquals('SafeHtml{World <em>Hello</em>}', String(safeHtml)); |
| 51 |
assertEquals(goog.i18n.bidi.Dir.RTL, safeHtml.getDirection()); |
| 52 |
|
| 53 |
// Interface markers are present. |
| 54 |
assertTrue(safeHtml.implementsGoogStringTypedString); |
| 55 |
assertTrue(safeHtml.implementsGoogI18nBidiDirectionalString); |
| 56 |
|
| 57 |
// Pre-defined constant. |
| 58 |
assertSameHtml('', goog.html.SafeHtml.EMPTY); |
| 59 |
assertSameHtml('<br>', goog.html.SafeHtml.BR); |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
/** @suppress {checkTypes} */ |
| 64 |
function testUnwrap() { |
| 65 |
var privateFieldName = 'privateDoNotAccessOrElseSafeHtmlWrappedValue_'; |
| 66 |
var markerFieldName = 'SAFE_HTML_TYPE_MARKER_GOOG_HTML_SECURITY_PRIVATE_'; |
| 67 |
var propNames = goog.object.getKeys(goog.html.SafeHtml.htmlEscape('')); |
| 68 |
assertContains(privateFieldName, propNames); |
| 69 |
assertContains(markerFieldName, propNames); |
| 70 |
var evil = {}; |
| 71 |
evil[privateFieldName] = '<script>evil()</script'; |
| 72 |
evil[markerFieldName] = {}; |
| 73 |
|
| 74 |
var exception = assertThrows(function() { goog.html.SafeHtml.unwrap(evil); }); |
| 75 |
assertContains('expected object of type SafeHtml', exception.message); |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
function testHtmlEscape() { |
| 80 |
// goog.html.SafeHtml passes through unchanged. |
| 81 |
var safeHtmlIn = goog.html.SafeHtml.htmlEscape('<b>in</b>'); |
| 82 |
assertTrue(safeHtmlIn === goog.html.SafeHtml.htmlEscape(safeHtmlIn)); |
| 83 |
|
| 84 |
// Plain strings are escaped. |
| 85 |
var safeHtml = goog.html.SafeHtml.htmlEscape('Hello <em>"\'&World</em>'); |
| 86 |
assertSameHtml('Hello <em>"'&World</em>', safeHtml); |
| 87 |
assertEquals( |
| 88 |
'SafeHtml{Hello <em>"'&World</em>}', |
| 89 |
String(safeHtml)); |
| 90 |
|
| 91 |
// Creating from a SafeUrl escapes and retains the known direction (which is |
| 92 |
// fixed to RTL for URLs). |
| 93 |
var safeUrl = goog.html.SafeUrl.fromConstant( |
| 94 |
goog.string.Const.from('http://example.com/?foo&bar')); |
| 95 |
var escapedUrl = goog.html.SafeHtml.htmlEscape(safeUrl); |
| 96 |
assertSameHtml('http://example.com/?foo&bar', escapedUrl); |
| 97 |
assertEquals(goog.i18n.bidi.Dir.LTR, escapedUrl.getDirection()); |
| 98 |
|
| 99 |
// Creating SafeHtml from a goog.string.Const escapes as well (i.e., the |
| 100 |
// value is treated like any other string). To create HTML markup from |
| 101 |
// program literals, SafeHtmlBuilder should be used. |
| 102 |
assertSameHtml( |
| 103 |
'this & that', |
| 104 |
goog.html.SafeHtml.htmlEscape(goog.string.Const.from('this & that'))); |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
function testSafeHtmlCreate() { |
| 109 |
var br = goog.html.SafeHtml.create('br'); |
| 110 |
|
| 111 |
assertSameHtml('<br>', br); |
| 112 |
|
| 113 |
assertSameHtml( |
| 114 |
'<span title="""></span>', |
| 115 |
goog.html.SafeHtml.create('span', {'title': '"'})); |
| 116 |
|
| 117 |
assertSameHtml( |
| 118 |
'<span><</span>', goog.html.SafeHtml.create('span', {}, '<')); |
| 119 |
|
| 120 |
assertSameHtml( |
| 121 |
'<span><br></span>', goog.html.SafeHtml.create('span', {}, br)); |
| 122 |
|
| 123 |
assertSameHtml('<span></span>', goog.html.SafeHtml.create('span', {}, [])); |
| 124 |
|
| 125 |
assertSameHtml( |
| 126 |
'<span></span>', |
| 127 |
goog.html.SafeHtml.create('span', {'title': null, 'class': undefined})); |
| 128 |
|
| 129 |
assertSameHtml( |
| 130 |
'<span>x<br>y</span>', |
| 131 |
goog.html.SafeHtml.create('span', {}, ['x', br, 'y'])); |
| 132 |
|
| 133 |
assertSameHtml( |
| 134 |
'<table border="0"></table>', |
| 135 |
goog.html.SafeHtml.create('table', {'border': 0})); |
| 136 |
|
| 137 |
var onclick = goog.string.Const.from('alert(/"/)'); |
| 138 |
assertSameHtml( |
| 139 |
'<span onclick="alert(/"/)"></span>', |
| 140 |
goog.html.SafeHtml.create('span', {'onclick': onclick})); |
| 141 |
|
| 142 |
var href = goog.html.testing.newSafeUrlForTest('?a&b'); |
| 143 |
assertSameHtml( |
| 144 |
'<a href="?a&b"></a>', |
| 145 |
goog.html.SafeHtml.create('a', {'href': href})); |
| 146 |
|
| 147 |
var style = goog.html.testing.newSafeStyleForTest('border: /* " */ 0;'); |
| 148 |
assertSameHtml( |
| 149 |
'<hr style="border: /* " */ 0;">', |
| 150 |
goog.html.SafeHtml.create('hr', {'style': style})); |
| 151 |
|
| 152 |
assertEquals( |
| 153 |
goog.i18n.bidi.Dir.NEUTRAL, |
| 154 |
goog.html.SafeHtml.create('span').getDirection()); |
| 155 |
assertNull(goog.html.SafeHtml.create('span', {'dir': 'x'}).getDirection()); |
| 156 |
assertEquals( |
| 157 |
goog.i18n.bidi.Dir.NEUTRAL, |
| 158 |
goog.html.SafeHtml.create('span', {'dir': 'ltr'}, 'a').getDirection()); |
| 159 |
|
| 160 |
assertThrows(function() { goog.html.SafeHtml.create('script'); }); |
| 161 |
|
| 162 |
assertThrows(function() { goog.html.SafeHtml.create('br', {}, 'x'); }); |
| 163 |
|
| 164 |
assertThrows(function() { |
| 165 |
goog.html.SafeHtml.create('img', {'onerror': ''}); |
| 166 |
}); |
| 167 |
|
| 168 |
assertThrows(function() { |
| 169 |
goog.html.SafeHtml.create('img', {'OnError': ''}); |
| 170 |
}); |
| 171 |
|
| 172 |
assertThrows(function() { goog.html.SafeHtml.create('a href=""'); }); |
| 173 |
|
| 174 |
assertThrows(function() { |
| 175 |
goog.html.SafeHtml.create('a', {'title="" href': ''}); |
| 176 |
}); |
| 177 |
|
| 178 |
assertThrows(function() { goog.html.SafeHtml.create('applet'); }); |
| 179 |
|
| 180 |
assertThrows(function() { |
| 181 |
goog.html.SafeHtml.create('applet', {'code': 'kittens.class'}); |
| 182 |
}); |
| 183 |
|
| 184 |
assertThrows(function() { goog.html.SafeHtml.create('base'); }); |
| 185 |
|
| 186 |
assertThrows(function() { |
| 187 |
goog.html.SafeHtml.create('base', {'href': 'http://example.org'}); |
| 188 |
}); |
| 189 |
|
| 190 |
assertThrows(function() { goog.html.SafeHtml.create('math'); }); |
| 191 |
|
| 192 |
assertThrows(function() { goog.html.SafeHtml.create('meta'); }); |
| 193 |
|
| 194 |
assertThrows(function() { goog.html.SafeHtml.create('svg'); }); |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
function testSafeHtmlCreate_styleAttribute() { |
| 199 |
var style = 'color:red;'; |
| 200 |
var expected = '<hr style="' + style + '">'; |
| 201 |
assertThrows(function() { |
| 202 |
goog.html.SafeHtml.create('hr', {'style': style}); |
| 203 |
}); |
| 204 |
assertSameHtml(expected, goog.html.SafeHtml.create('hr', { |
| 205 |
'style': goog.html.SafeStyle.fromConstant(goog.string.Const.from(style)) |
| 206 |
})); |
| 207 |
assertSameHtml( |
| 208 |
expected, goog.html.SafeHtml.create('hr', {'style': {'color': 'red'}})); |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
function testSafeHtmlCreate_urlAttributes() { |
| 213 |
// TrustedResourceUrl is allowed. |
| 214 |
var trustedResourceUrl = goog.html.TrustedResourceUrl.fromConstant( |
| 215 |
goog.string.Const.from('https://google.com/trusted')); |
| 216 |
assertSameHtml( |
| 217 |
'<img src="https://google.com/trusted">', |
| 218 |
goog.html.SafeHtml.create('img', {'src': trustedResourceUrl})); |
| 219 |
// SafeUrl is allowed. |
| 220 |
var safeUrl = goog.html.SafeUrl.sanitize('https://google.com/safe'); |
| 221 |
assertSameHtml( |
| 222 |
'<imG src="https://google.com/safe">', |
| 223 |
goog.html.SafeHtml.create('imG', {'src': safeUrl})); |
| 224 |
// Const is allowed. |
| 225 |
var constUrl = goog.string.Const.from('https://google.com/const'); |
| 226 |
assertSameHtml( |
| 227 |
'<a href="https://google.com/const"></a>', |
| 228 |
goog.html.SafeHtml.create('a', {'href': constUrl})); |
| 229 |
|
| 230 |
// string is allowed but escaped. |
| 231 |
assertSameHtml( |
| 232 |
'<a href="http://google.com/safe""></a>', |
| 233 |
goog.html.SafeHtml.create('a', {'href': 'http://google.com/safe"'})); |
| 234 |
|
| 235 |
// string is allowed but sanitized. |
| 236 |
var badUrl = 'javascript:evil();'; |
| 237 |
var sanitizedUrl = |
| 238 |
goog.html.SafeUrl.unwrap(goog.html.SafeUrl.sanitize(badUrl)); |
| 239 |
|
| 240 |
assertTrue(typeof sanitizedUrl == 'string'); |
| 241 |
assertNotEquals(badUrl, sanitizedUrl); |
| 242 |
|
| 243 |
assertSameHtml( |
| 244 |
'<a href="' + sanitizedUrl + '"></a>', |
| 245 |
goog.html.SafeHtml.create('a', {'href': badUrl})); |
| 246 |
|
| 247 |
// attribute case is ignored for url attributes purposes |
| 248 |
assertSameHtml( |
| 249 |
'<a hReF="' + sanitizedUrl + '"></a>', |
| 250 |
goog.html.SafeHtml.create('a', {'hReF': badUrl})); |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
/** @suppress {checkTypes} */ |
| 255 |
function testSafeHtmlCreateIframe() { |
| 256 |
// Setting src and srcdoc. |
| 257 |
var url = goog.html.TrustedResourceUrl.fromConstant( |
| 258 |
goog.string.Const.from('https://google.com/trusted<')); |
| 259 |
assertSameHtml( |
| 260 |
'<iframe src="https://google.com/trusted<"></iframe>', |
| 261 |
goog.html.SafeHtml.createIframe(url, null, {'sandbox': null})); |
| 262 |
var srcdoc = goog.html.SafeHtml.BR; |
| 263 |
assertSameHtml( |
| 264 |
'<iframe srcdoc="<br>"></iframe>', |
| 265 |
goog.html.SafeHtml.createIframe(null, srcdoc, {'sandbox': null})); |
| 266 |
|
| 267 |
// sandbox default and overriding it. |
| 268 |
assertSameHtml( |
| 269 |
'<iframe sandbox=""></iframe>', goog.html.SafeHtml.createIframe()); |
| 270 |
assertSameHtml( |
| 271 |
'<iframe Sandbox="allow-same-origin allow-top-navigation"></iframe>', |
| 272 |
goog.html.SafeHtml.createIframe( |
| 273 |
null, null, {'Sandbox': 'allow-same-origin allow-top-navigation'})); |
| 274 |
|
| 275 |
// Cannot override src and srddoc. |
| 276 |
assertThrows(function() { |
| 277 |
goog.html.SafeHtml.createIframe(null, null, {'Src': url}); |
| 278 |
}); |
| 279 |
assertThrows(function() { |
| 280 |
goog.html.SafeHtml.createIframe(null, null, {'Srcdoc': url}); |
| 281 |
}); |
| 282 |
|
| 283 |
// Unsafe src and srcdoc. |
| 284 |
assertThrows(function() { |
| 285 |
goog.html.SafeHtml.createIframe('http://example.com'); |
| 286 |
}); |
| 287 |
assertThrows(function() { |
| 288 |
goog.html.SafeHtml.createIframe(null, '<script>alert(1)</script>'); |
| 289 |
}); |
| 290 |
|
| 291 |
// Can set content. |
| 292 |
assertSameHtml( |
| 293 |
'<iframe><</iframe>', |
| 294 |
goog.html.SafeHtml.createIframe(null, null, {'sandbox': null}, '<')); |
| 295 |
} |
| 296 |
|
| 297 |
/** @suppress {checkTypes} */ |
| 298 |
function testSafeHtmlcreateSandboxIframe() { |
| 299 |
function assertSameHtmlIfSupportsSandbox(referenceHtml, testedHtmlFunction) { |
| 300 |
if (!goog.html.SafeHtml.canUseSandboxIframe()) { |
| 301 |
assertThrows(testedHtmlFunction); |
| 302 |
} else { |
| 303 |
assertSameHtml(referenceHtml, testedHtmlFunction()); |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
// Setting src and srcdoc. |
| 308 |
var url = goog.html.SafeUrl.fromConstant( |
| 309 |
goog.string.Const.from('https://google.com/trusted<')); |
| 310 |
assertSameHtmlIfSupportsSandbox( |
| 311 |
'<iframe src="https://google.com/trusted<" sandbox=""></iframe>', |
| 312 |
function() { return goog.html.SafeHtml.createSandboxIframe(url, null); }); |
| 313 |
|
| 314 |
// If set with a string, src is sanitized. |
| 315 |
assertSameHtmlIfSupportsSandbox( |
| 316 |
'<iframe src="' + goog.html.SafeUrl.INNOCUOUS_STRING + |
| 317 |
'" sandbox=""></iframe>', |
| 318 |
function() { |
| 319 |
return goog.html.SafeHtml.createSandboxIframe( |
| 320 |
"javascript:evil();", null); |
| 321 |
}); |
| 322 |
|
| 323 |
var srcdoc = '<br>'; |
| 324 |
assertSameHtmlIfSupportsSandbox( |
| 325 |
'<iframe srcdoc="<br>" sandbox=""></iframe>', function() { |
| 326 |
return goog.html.SafeHtml.createSandboxIframe(null, srcdoc); |
| 327 |
}); |
| 328 |
|
| 329 |
// Cannot override src, srcdoc. |
| 330 |
assertThrows(function() { |
| 331 |
goog.html.SafeHtml.createSandboxIframe(null, null, {'Src': url}); |
| 332 |
}); |
| 333 |
assertThrows(function() { |
| 334 |
goog.html.SafeHtml.createSandboxIframe(null, null, {'Srcdoc': url}); |
| 335 |
}); |
| 336 |
|
| 337 |
|
| 338 |
// Sandboxed by default, and can't be overriden. |
| 339 |
assertSameHtmlIfSupportsSandbox('<iframe sandbox=""></iframe>', function() { |
| 340 |
return goog.html.SafeHtml.createSandboxIframe(); |
| 341 |
}); |
| 342 |
|
| 343 |
assertThrows(function() { |
| 344 |
goog.html.SafeHtml.createSandboxIframe(null, null, {'sandbox': ''}); |
| 345 |
}); |
| 346 |
assertThrows(function() { |
| 347 |
goog.html.SafeHtml.createSandboxIframe( |
| 348 |
null, null, {'SaNdBoX': 'allow-scripts'}); |
| 349 |
}); |
| 350 |
assertThrows(function() { |
| 351 |
goog.html.SafeHtml.createSandboxIframe( |
| 352 |
null, null, {'sandbox': 'allow-same-origin allow-top-navigation'}); |
| 353 |
}); |
| 354 |
|
| 355 |
// Can set content. |
| 356 |
assertSameHtmlIfSupportsSandbox( |
| 357 |
'<iframe sandbox=""><</iframe>', function() { |
| 358 |
return goog.html.SafeHtml.createSandboxIframe(null, null, null, '<'); |
| 359 |
}); |
| 360 |
} |
| 361 |
|
| 362 |
|
| 363 |
function testSafeHtmlCanUseIframeSandbox() { |
| 364 |
// We know that the IE < 10 do not support the sandbox attribute, so use them |
| 365 |
// as a reference. |
| 366 |
if (goog.labs.userAgent.browser.isIE() && |
| 367 |
goog.labs.userAgent.browser.getVersion() < 10) { |
| 368 |
assertEquals(false, goog.html.SafeHtml.canUseSandboxIframe()); |
| 369 |
} else { |
| 370 |
assertEquals(true, goog.html.SafeHtml.canUseSandboxIframe()); |
| 371 |
} |
| 372 |
} |
| 373 |
|
| 374 |
|
| 375 |
function testSafeHtmlCreateScript() { |
| 376 |
var script = |
| 377 |
goog.html.SafeScript.fromConstant(goog.string.Const.from('function1();')); |
| 378 |
var scriptHtml = goog.html.SafeHtml.createScript(script); |
| 379 |
assertSameHtml('<script>function1();</script>', scriptHtml); |
| 380 |
|
| 381 |
// Two pieces of script. |
| 382 |
var otherScript = |
| 383 |
goog.html.SafeScript.fromConstant(goog.string.Const.from('function2();')); |
| 384 |
scriptHtml = goog.html.SafeHtml.createScript([script, otherScript]); |
| 385 |
assertSameHtml('<script>function1();function2();</script>', scriptHtml); |
| 386 |
|
| 387 |
// Set attribute. |
| 388 |
scriptHtml = goog.html.SafeHtml.createScript(script, {'id': 'test'}); |
| 389 |
assertContains('id="test"', goog.html.SafeHtml.unwrap(scriptHtml)); |
| 390 |
|
| 391 |
// Set attribute to null. |
| 392 |
scriptHtml = |
| 393 |
goog.html.SafeHtml.createScript(goog.html.SafeScript.EMPTY, {'id': null}); |
| 394 |
assertSameHtml('<script></script>', scriptHtml); |
| 395 |
|
| 396 |
// Set attribute to invalid value. |
| 397 |
var exception = assertThrows(function() { |
| 398 |
goog.html.SafeHtml.createScript( |
| 399 |
goog.html.SafeScript.EMPTY, {'invalid.': 'cantdothis'}); |
| 400 |
}); |
| 401 |
assertContains('Invalid attribute name', exception.message); |
| 402 |
|
| 403 |
// Cannot override type attribute. |
| 404 |
exception = assertThrows(function() { |
| 405 |
goog.html.SafeHtml.createScript( |
| 406 |
goog.html.SafeScript.EMPTY, {'Type': 'cantdothis'}); |
| 407 |
}); |
| 408 |
assertContains('Cannot set "type"', exception.message); |
| 409 |
|
| 410 |
// Cannot set src attribute. |
| 411 |
exception = assertThrows(function() { |
| 412 |
goog.html.SafeHtml.createScript( |
| 413 |
goog.html.SafeScript.EMPTY, {'src': 'cantdothis'}); |
| 414 |
}); |
| 415 |
assertContains('Cannot set "src"', exception.message); |
| 416 |
|
| 417 |
// Directionality. |
| 418 |
assertEquals(goog.i18n.bidi.Dir.NEUTRAL, scriptHtml.getDirection()); |
| 419 |
} |
| 420 |
|
| 421 |
|
| 422 |
/** @suppress {checkTypes} */ |
| 423 |
function testSafeHtmlCreateScriptSrc() { |
| 424 |
var url = goog.html.TrustedResourceUrl.fromConstant( |
| 425 |
goog.string.Const.from('https://google.com/trusted<')); |
| 426 |
|
| 427 |
assertSameHtml( |
| 428 |
'<script src="https://google.com/trusted<"></script>', |
| 429 |
goog.html.SafeHtml.createScriptSrc(url)); |
| 430 |
|
| 431 |
assertSameHtml( |
| 432 |
'<script src="https://google.com/trusted<" defer="defer"></script>', |
| 433 |
goog.html.SafeHtml.createScriptSrc(url, {'defer': 'defer'})); |
| 434 |
|
| 435 |
// Unsafe src. |
| 436 |
assertThrows(function() { |
| 437 |
goog.html.SafeHtml.createScriptSrc('http://example.com'); |
| 438 |
}); |
| 439 |
|
| 440 |
// Unsafe attribute. |
| 441 |
assertThrows(function() { |
| 442 |
goog.html.SafeHtml.createScriptSrc(url, {'onerror': 'alert(1)'}); |
| 443 |
}); |
| 444 |
|
| 445 |
// Cannot override src. |
| 446 |
assertThrows(function() { |
| 447 |
goog.html.SafeHtml.createScriptSrc(url, {'Src': url}); |
| 448 |
}); |
| 449 |
} |
| 450 |
|
| 451 |
|
| 452 |
function testSafeHtmlCreateMeta() { |
| 453 |
var url = goog.html.SafeUrl.fromConstant( |
| 454 |
goog.string.Const.from('https://google.com/trusted<')); |
| 455 |
|
| 456 |
// SafeUrl with no timeout gets properly escaped. |
| 457 |
assertSameHtml( |
| 458 |
'<meta http-equiv="refresh" ' + |
| 459 |
'content="0; url=https://google.com/trusted<">', |
| 460 |
goog.html.SafeHtml.createMetaRefresh(url)); |
| 461 |
|
| 462 |
// SafeUrl with 0 timeout also gets properly escaped. |
| 463 |
assertSameHtml( |
| 464 |
'<meta http-equiv="refresh" ' + |
| 465 |
'content="0; url=https://google.com/trusted<">', |
| 466 |
goog.html.SafeHtml.createMetaRefresh(url, 0)); |
| 467 |
|
| 468 |
// Positive timeouts are supported. |
| 469 |
assertSameHtml( |
| 470 |
'<meta http-equiv="refresh" ' + |
| 471 |
'content="1337; url=https://google.com/trusted<">', |
| 472 |
goog.html.SafeHtml.createMetaRefresh(url, 1337)); |
| 473 |
|
| 474 |
// Negative timeouts are also kept, though they're not correct HTML. |
| 475 |
assertSameHtml( |
| 476 |
'<meta http-equiv="refresh" ' + |
| 477 |
'content="-1337; url=https://google.com/trusted<">', |
| 478 |
goog.html.SafeHtml.createMetaRefresh(url, -1337)); |
| 479 |
|
| 480 |
// String-based URLs work out of the box. |
| 481 |
assertSameHtml( |
| 482 |
'<meta http-equiv="refresh" ' + |
| 483 |
'content="0; url=https://google.com/trusted<">', |
| 484 |
goog.html.SafeHtml.createMetaRefresh('https://google.com/trusted<')); |
| 485 |
|
| 486 |
// Sanitization happens. |
| 487 |
assertSameHtml( |
| 488 |
'<meta http-equiv="refresh" ' + |
| 489 |
'content="0; url=about:invalid#zClosurez">', |
| 490 |
goog.html.SafeHtml.createMetaRefresh('javascript:alert(1)')); |
| 491 |
} |
| 492 |
|
| 493 |
function testSafeHtmlCreateStyle() { |
| 494 |
var styleSheet = goog.html.SafeStyleSheet.fromConstant( |
| 495 |
goog.string.Const.from('P.special { color:"red" ; }')); |
| 496 |
var styleHtml = goog.html.SafeHtml.createStyle(styleSheet); |
| 497 |
assertSameHtml( |
| 498 |
'<style type="text/css">P.special { color:"red" ; }</style>', styleHtml); |
| 499 |
|
| 500 |
// Two stylesheets. |
| 501 |
var otherStyleSheet = goog.html.SafeStyleSheet.fromConstant( |
| 502 |
goog.string.Const.from('P.regular { color:blue ; }')); |
| 503 |
styleHtml = goog.html.SafeHtml.createStyle([styleSheet, otherStyleSheet]); |
| 504 |
assertSameHtml( |
| 505 |
'<style type="text/css">P.special { color:"red" ; }' + |
| 506 |
'P.regular { color:blue ; }</style>', |
| 507 |
styleHtml); |
| 508 |
|
| 509 |
// Set attribute. |
| 510 |
styleHtml = goog.html.SafeHtml.createStyle(styleSheet, {'id': 'test'}); |
| 511 |
var styleHtmlString = goog.html.SafeHtml.unwrap(styleHtml); |
| 512 |
assertContains('id="test"', styleHtmlString); |
| 513 |
assertContains('type="text/css"', styleHtmlString); |
| 514 |
|
| 515 |
// Set attribute to null. |
| 516 |
styleHtml = goog.html.SafeHtml.createStyle( |
| 517 |
goog.html.SafeStyleSheet.EMPTY, {'id': null}); |
| 518 |
assertSameHtml('<style type="text/css"></style>', styleHtml); |
| 519 |
|
| 520 |
// Set attribute to invalid value. |
| 521 |
var exception = assertThrows(function() { |
| 522 |
goog.html.SafeHtml.createStyle( |
| 523 |
goog.html.SafeStyleSheet.EMPTY, {'invalid.': 'cantdothis'}); |
| 524 |
}); |
| 525 |
assertContains('Invalid attribute name', exception.message); |
| 526 |
|
| 527 |
// Cannot override type attribute. |
| 528 |
exception = assertThrows(function() { |
| 529 |
goog.html.SafeHtml.createStyle( |
| 530 |
goog.html.SafeStyleSheet.EMPTY, {'Type': 'cantdothis'}); |
| 531 |
}); |
| 532 |
assertContains('Cannot override "type"', exception.message); |
| 533 |
|
| 534 |
// Directionality. |
| 535 |
assertEquals(goog.i18n.bidi.Dir.NEUTRAL, styleHtml.getDirection()); |
| 536 |
} |
| 537 |
|
| 538 |
|
| 539 |
function testSafeHtmlCreateWithDir() { |
| 540 |
var ltr = goog.i18n.bidi.Dir.LTR; |
| 541 |
|
| 542 |
assertEquals(ltr, goog.html.SafeHtml.createWithDir(ltr, 'br').getDirection()); |
| 543 |
} |
| 544 |
|
| 545 |
|
| 546 |
function testSafeHtmlConcat() { |
| 547 |
var br = goog.html.testing.newSafeHtmlForTest('<br>'); |
| 548 |
|
| 549 |
var html = goog.html.SafeHtml.htmlEscape('Hello'); |
| 550 |
assertSameHtml('Hello<br>', goog.html.SafeHtml.concat(html, br)); |
| 551 |
|
| 552 |
assertSameHtml('', goog.html.SafeHtml.concat()); |
| 553 |
assertSameHtml('', goog.html.SafeHtml.concat([])); |
| 554 |
|
| 555 |
assertSameHtml('a<br>c', goog.html.SafeHtml.concat('a', br, 'c')); |
| 556 |
assertSameHtml('a<br>c', goog.html.SafeHtml.concat(['a', br, 'c'])); |
| 557 |
assertSameHtml('a<br>c', goog.html.SafeHtml.concat('a', [br, 'c'])); |
| 558 |
assertSameHtml('a<br>c', goog.html.SafeHtml.concat(['a'], br, ['c'])); |
| 559 |
|
| 560 |
var ltr = goog.html.testing.newSafeHtmlForTest('', goog.i18n.bidi.Dir.LTR); |
| 561 |
var rtl = goog.html.testing.newSafeHtmlForTest('', goog.i18n.bidi.Dir.RTL); |
| 562 |
var neutral = |
| 563 |
goog.html.testing.newSafeHtmlForTest('', goog.i18n.bidi.Dir.NEUTRAL); |
| 564 |
var unknown = goog.html.testing.newSafeHtmlForTest(''); |
| 565 |
assertEquals( |
| 566 |
goog.i18n.bidi.Dir.NEUTRAL, goog.html.SafeHtml.concat().getDirection()); |
| 567 |
assertEquals( |
| 568 |
goog.i18n.bidi.Dir.LTR, |
| 569 |
goog.html.SafeHtml.concat(ltr, ltr).getDirection()); |
| 570 |
assertEquals( |
| 571 |
goog.i18n.bidi.Dir.LTR, |
| 572 |
goog.html.SafeHtml.concat(ltr, neutral, ltr).getDirection()); |
| 573 |
assertNull(goog.html.SafeHtml.concat(ltr, unknown).getDirection()); |
| 574 |
assertNull(goog.html.SafeHtml.concat(ltr, rtl).getDirection()); |
| 575 |
assertNull(goog.html.SafeHtml.concat(ltr, [rtl]).getDirection()); |
| 576 |
} |
| 577 |
|
| 578 |
|
| 579 |
function testHtmlEscapePreservingNewlines() { |
| 580 |
// goog.html.SafeHtml passes through unchanged. |
| 581 |
var safeHtmlIn = goog.html.SafeHtml.htmlEscapePreservingNewlines('<b>in</b>'); |
| 582 |
assertTrue( |
| 583 |
safeHtmlIn === |
| 584 |
goog.html.SafeHtml.htmlEscapePreservingNewlines(safeHtmlIn)); |
| 585 |
|
| 586 |
assertSameHtml( |
| 587 |
'a<br>c', goog.html.SafeHtml.htmlEscapePreservingNewlines('a\nc')); |
| 588 |
assertSameHtml( |
| 589 |
'<<br>', goog.html.SafeHtml.htmlEscapePreservingNewlines('<\n')); |
| 590 |
assertSameHtml( |
| 591 |
'<br>', goog.html.SafeHtml.htmlEscapePreservingNewlines('\r\n')); |
| 592 |
assertSameHtml('<br>', goog.html.SafeHtml.htmlEscapePreservingNewlines('\r')); |
| 593 |
assertSameHtml('', goog.html.SafeHtml.htmlEscapePreservingNewlines('')); |
| 594 |
} |
| 595 |
|
| 596 |
|
| 597 |
function testHtmlEscapePreservingNewlinesAndSpaces() { |
| 598 |
// goog.html.SafeHtml passes through unchanged. |
| 599 |
var safeHtmlIn = |
| 600 |
goog.html.SafeHtml.htmlEscapePreservingNewlinesAndSpaces('<b>in</b>'); |
| 601 |
assertTrue( |
| 602 |
safeHtmlIn === |
| 603 |
goog.html.SafeHtml.htmlEscapePreservingNewlinesAndSpaces(safeHtmlIn)); |
| 604 |
|
| 605 |
assertSameHtml( |
| 606 |
'a<br>c', |
| 607 |
goog.html.SafeHtml.htmlEscapePreservingNewlinesAndSpaces('a\nc')); |
| 608 |
assertSameHtml( |
| 609 |
'<<br>', |
| 610 |
goog.html.SafeHtml.htmlEscapePreservingNewlinesAndSpaces('<\n')); |
| 611 |
assertSameHtml( |
| 612 |
'<br>', goog.html.SafeHtml.htmlEscapePreservingNewlinesAndSpaces('\r\n')); |
| 613 |
assertSameHtml( |
| 614 |
'<br>', goog.html.SafeHtml.htmlEscapePreservingNewlinesAndSpaces('\r')); |
| 615 |
assertSameHtml( |
| 616 |
'', goog.html.SafeHtml.htmlEscapePreservingNewlinesAndSpaces('')); |
| 617 |
|
| 618 |
assertSameHtml( |
| 619 |
'a  b', |
| 620 |
goog.html.SafeHtml.htmlEscapePreservingNewlinesAndSpaces('a b')); |
| 621 |
} |
| 622 |
|
| 623 |
|
| 624 |
function testSafeHtmlConcatWithDir() { |
| 625 |
var ltr = goog.i18n.bidi.Dir.LTR; |
| 626 |
var rtl = goog.i18n.bidi.Dir.RTL; |
| 627 |
var br = goog.html.testing.newSafeHtmlForTest('<br>'); |
| 628 |
|
| 629 |
assertEquals(ltr, goog.html.SafeHtml.concatWithDir(ltr).getDirection()); |
| 630 |
assertEquals( |
| 631 |
ltr, |
| 632 |
goog.html.SafeHtml |
| 633 |
.concatWithDir(ltr, goog.html.testing.newSafeHtmlForTest('', rtl)) |
| 634 |
.getDirection()); |
| 635 |
|
| 636 |
assertSameHtml('a<br>c', goog.html.SafeHtml.concatWithDir(ltr, 'a', br, 'c')); |
| 637 |
} |
| 638 |
|
| 639 |
|
| 640 |
function assertSameHtml(expected, html) { |
| 641 |
assertEquals(expected, goog.html.SafeHtml.unwrap(html)); |
| 642 |
} |