| 1 |
// Copyright 2014 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.SafeStyle and its builders. |
| 17 |
*/ |
| 18 |
|
| 19 |
goog.provide('goog.html.safeStyleTest'); |
| 20 |
|
| 21 |
goog.require('goog.html.SafeStyle'); |
| 22 |
goog.require('goog.html.SafeUrl'); |
| 23 |
goog.require('goog.object'); |
| 24 |
goog.require('goog.string.Const'); |
| 25 |
goog.require('goog.testing.jsunit'); |
| 26 |
|
| 27 |
goog.setTestOnly('goog.html.safeStyleTest'); |
| 28 |
|
| 29 |
|
| 30 |
function testSafeStyle() { |
| 31 |
var style = 'width: 1em;height: 1em;'; |
| 32 |
var safeStyle = |
| 33 |
goog.html.SafeStyle.fromConstant(goog.string.Const.from(style)); |
| 34 |
var extracted = goog.html.SafeStyle.unwrap(safeStyle); |
| 35 |
assertEquals(style, extracted); |
| 36 |
assertEquals(style, safeStyle.getTypedStringValue()); |
| 37 |
assertEquals('SafeStyle{' + style + '}', String(safeStyle)); |
| 38 |
|
| 39 |
// Interface marker is present. |
| 40 |
assertTrue(safeStyle.implementsGoogStringTypedString); |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
/** @suppress {checkTypes} */ |
| 45 |
function testUnwrap() { |
| 46 |
var privateFieldName = 'privateDoNotAccessOrElseSafeStyleWrappedValue_'; |
| 47 |
var markerFieldName = 'SAFE_STYLE_TYPE_MARKER_GOOG_HTML_SECURITY_PRIVATE_'; |
| 48 |
var propNames = goog.object.getKeys( |
| 49 |
goog.html.SafeStyle.fromConstant(goog.string.Const.from(''))); |
| 50 |
assertContains(privateFieldName, propNames); |
| 51 |
assertContains(markerFieldName, propNames); |
| 52 |
var evil = {}; |
| 53 |
evil[privateFieldName] = 'width: expression(evil);'; |
| 54 |
evil[markerFieldName] = {}; |
| 55 |
|
| 56 |
var exception = |
| 57 |
assertThrows(function() { goog.html.SafeStyle.unwrap(evil); }); |
| 58 |
assertContains('expected object of type SafeStyle', exception.message); |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
function testFromConstant_allowsEmptyString() { |
| 63 |
assertEquals( |
| 64 |
goog.html.SafeStyle.EMPTY, |
| 65 |
goog.html.SafeStyle.fromConstant(goog.string.Const.from(''))); |
| 66 |
} |
| 67 |
|
| 68 |
function testFromConstant_throwsOnForbiddenCharacters() { |
| 69 |
assertThrows(function() { |
| 70 |
goog.html.SafeStyle.fromConstant(goog.string.Const.from('width: x<;')); |
| 71 |
}); |
| 72 |
assertThrows(function() { |
| 73 |
goog.html.SafeStyle.fromConstant(goog.string.Const.from('width: x>;')); |
| 74 |
}); |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
function testFromConstant_throwsIfNoFinalSemicolon() { |
| 79 |
assertThrows(function() { |
| 80 |
goog.html.SafeStyle.fromConstant(goog.string.Const.from('width: 1em')); |
| 81 |
}); |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
function testFromConstant_throwsIfNoColon() { |
| 86 |
assertThrows(function() { |
| 87 |
goog.html.SafeStyle.fromConstant(goog.string.Const.from('width= 1em;')); |
| 88 |
}); |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
function testEmpty() { |
| 93 |
assertEquals('', goog.html.SafeStyle.unwrap(goog.html.SafeStyle.EMPTY)); |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
function testCreate() { |
| 98 |
assertCreateEquals( |
| 99 |
'background:url(i.png);margin:0;', |
| 100 |
{'background': goog.string.Const.from('url(i.png)'), 'margin': '0'}); |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
function testCreate_allowsEmpty() { |
| 105 |
assertEquals(goog.html.SafeStyle.EMPTY, goog.html.SafeStyle.create({})); |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
function testCreate_skipsNull() { |
| 110 |
var style = goog.html.SafeStyle.create({'background': null}); |
| 111 |
assertEquals(goog.html.SafeStyle.EMPTY, style); |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
function testCreate_allowsLengths() { |
| 116 |
assertCreateEquals( |
| 117 |
'padding:0 1px .2% 3.4em;', // expected |
| 118 |
{'padding': '0 1px .2% 3.4em'}); |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
function testCreate_allowsRgb() { |
| 123 |
assertCreateEquals( |
| 124 |
'color:rgb(10,20,30);', // expected |
| 125 |
{'color': 'rgb(10,20,30)'}); |
| 126 |
assertCreateEquals( |
| 127 |
'color:rgb(10%, 20%, 30%);', // expected |
| 128 |
{'color': 'rgb(10%, 20%, 30%)'}); |
| 129 |
assertCreateEquals( |
| 130 |
'background:0 5px rgb(10,20,30);', // expected |
| 131 |
{'background': '0 5px rgb(10,20,30)'}); |
| 132 |
assertCreateEquals( |
| 133 |
'background:rgb(10,0,0), rgb(0,0,30);', |
| 134 |
{'background': 'rgb(10,0,0), rgb(0,0,30)'}); |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
function testCreate_allowsRgba() { |
| 139 |
assertCreateEquals( |
| 140 |
'color:rgba(10,20,30,0.1);', // expected |
| 141 |
{'color': 'rgba(10,20,30,0.1)'}); |
| 142 |
assertCreateEquals( |
| 143 |
'color:rgba(10%, 20%, 30%, .5);', // expected |
| 144 |
{'color': 'rgba(10%, 20%, 30%, .5)'}); |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
function testCreate_allowsScale() { |
| 149 |
assertCreateEquals( |
| 150 |
'transform:scale(.5, 2);', // expected |
| 151 |
{'transform': 'scale(.5, 2)'}); |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
function testCreate_allowsRotate() { |
| 156 |
assertCreateEquals( |
| 157 |
'transform:rotate(45deg);', // expected |
| 158 |
{'transform': 'rotate(45deg)'}); |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
function testCreate_allowsTranslate() { |
| 163 |
assertCreateEquals( |
| 164 |
'transform:translate(10px);', // expected |
| 165 |
{'transform': 'translate(10px)'}); |
| 166 |
assertCreateEquals( |
| 167 |
'transform:translateX(5px);', // expected |
| 168 |
{'transform': 'translateX(5px)'}); |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
function testCreate_allowsSafeUrl() { |
| 173 |
assertCreateEquals('background:url("http://example.com");', { |
| 174 |
'background': goog.html.SafeUrl.fromConstant( |
| 175 |
goog.string.Const.from('http://example.com')) |
| 176 |
}); |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
function testCreate_allowsSafeUrlWithSpecialCharacters() { |
| 181 |
assertCreateEquals('background:url("http://example.com/\\"");', { |
| 182 |
'background': goog.html.SafeUrl.fromConstant( |
| 183 |
goog.string.Const.from('http://example.com/"')) |
| 184 |
}); |
| 185 |
assertCreateEquals('background:url("http://example.com/%3c");', { |
| 186 |
'background': goog.html.SafeUrl.fromConstant( |
| 187 |
goog.string.Const.from('http://example.com/<')) |
| 188 |
}); |
| 189 |
assertCreateEquals('background:url("http://example.com/;");', { |
| 190 |
'background': goog.html.SafeUrl.fromConstant( |
| 191 |
goog.string.Const.from('http://example.com/;')) |
| 192 |
}); |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
function testCreate_allowsArray() { |
| 197 |
var url = goog.html.SafeUrl.fromConstant( |
| 198 |
goog.string.Const.from('http://example.com')); |
| 199 |
assertCreateEquals( |
| 200 |
'background:red url("http://example.com") repeat-y;', |
| 201 |
{'background': ['red', url, 'repeat-y']}); |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
function testCreate_allowsUrl() { |
| 206 |
assertCreateEquals( |
| 207 |
'background:url(http://example.com);', |
| 208 |
{'background': 'url(http://example.com)'}); |
| 209 |
assertCreateEquals( |
| 210 |
'background:url("http://example.com");', |
| 211 |
{'background': 'url("http://example.com")'}); |
| 212 |
assertCreateEquals( |
| 213 |
'background:url( \'http://example.com\' );', |
| 214 |
{'background': 'url( \'http://example.com\' )'}); |
| 215 |
assertCreateEquals( |
| 216 |
'background:url(http://example.com) red;', |
| 217 |
{'background': 'url(http://example.com) red'}); |
| 218 |
assertCreateEquals( |
| 219 |
'background:url(' + goog.html.SafeUrl.INNOCUOUS_STRING + ');', |
| 220 |
{'background': 'url(javascript:alert)'}); |
| 221 |
assertCreateEquals( |
| 222 |
'background:url(")");', // Expected. |
| 223 |
{'background': 'url(")")'}); |
| 224 |
assertCreateEquals( |
| 225 |
'background:url(" ");', // Expected. |
| 226 |
{'background': 'url(" ")'}); |
| 227 |
assertThrows(function() { |
| 228 |
goog.html.SafeStyle.create({'background': 'url(\'http://example.com\'"")'}); |
| 229 |
}); |
| 230 |
assertThrows(function() { |
| 231 |
goog.html.SafeStyle.create({'background': 'url("\\\\")'}); |
| 232 |
}); |
| 233 |
assertThrows(function() { |
| 234 |
goog.html.SafeStyle.create({'background': 'url(a""b)'}); |
| 235 |
}); |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
function testCreate_throwsOnForbiddenCharacters() { |
| 240 |
assertThrows(function() { goog.html.SafeStyle.create({'<': '0'}); }); |
| 241 |
assertThrows(function() { |
| 242 |
goog.html.SafeStyle.create({'color': goog.string.Const.from('<')}); |
| 243 |
}); |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
function testCreate_values() { |
| 248 |
var valids = [ |
| 249 |
'0', '0 0', '1px', '100%', '2.3px', '.1em', 'red', '#f00', 'red !important', |
| 250 |
'"Times New Roman"', "'Times New Roman'", '"Bold \'nuff"', |
| 251 |
'"O\'Connor\'s Revenge"' |
| 252 |
]; |
| 253 |
for (var i = 0; i < valids.length; i++) { |
| 254 |
var value = valids[i]; |
| 255 |
assertCreateEquals( |
| 256 |
'background:' + value + ';', // expected |
| 257 |
{'background': value}); |
| 258 |
} |
| 259 |
|
| 260 |
var invalids = [ |
| 261 |
'', 'expression(alert(1))', '"', '"\'"\'', goog.string.Const.from('red;') |
| 262 |
]; |
| 263 |
for (var i = 0; i < invalids.length; i++) { |
| 264 |
var value = invalids[i]; |
| 265 |
assertThrows(function() { |
| 266 |
goog.html.SafeStyle.create({'background': value}); |
| 267 |
}); |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
/** |
| 273 |
* Asserts that created SafeStyle matches expected value. |
| 274 |
* @param {string} expected |
| 275 |
* @param {!goog.html.SafeStyle.PropertyMap} style |
| 276 |
*/ |
| 277 |
function assertCreateEquals(expected, style) { |
| 278 |
var style = goog.html.SafeStyle.create(style); |
| 279 |
assertEquals(expected, goog.html.SafeStyle.unwrap(style)); |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
function testConcat() { |
| 284 |
var width = |
| 285 |
goog.html.SafeStyle.fromConstant(goog.string.Const.from('width: 1em;')); |
| 286 |
var margin = goog.html.SafeStyle.create({'margin': '0'}); |
| 287 |
var padding = goog.html.SafeStyle.create({'padding': '0'}); |
| 288 |
|
| 289 |
var style = goog.html.SafeStyle.concat(width, margin); |
| 290 |
assertEquals('width: 1em;margin:0;', goog.html.SafeStyle.unwrap(style)); |
| 291 |
|
| 292 |
style = goog.html.SafeStyle.concat([width, margin]); |
| 293 |
assertEquals('width: 1em;margin:0;', goog.html.SafeStyle.unwrap(style)); |
| 294 |
|
| 295 |
style = goog.html.SafeStyle.concat([width], [padding, margin]); |
| 296 |
assertEquals( |
| 297 |
'width: 1em;padding:0;margin:0;', goog.html.SafeStyle.unwrap(style)); |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
function testConcat_allowsEmpty() { |
| 302 |
var empty = goog.html.SafeStyle.EMPTY; |
| 303 |
assertEquals(empty, goog.html.SafeStyle.concat()); |
| 304 |
assertEquals(empty, goog.html.SafeStyle.concat([])); |
| 305 |
assertEquals(empty, goog.html.SafeStyle.concat(empty)); |
| 306 |
} |