| OLD | NEW |
| (Empty) | |
| 1 QUnit.test('windowDepth: no parent window', function(assert) { |
| 2 var serializer = new HTMLSerializer(); |
| 3 assert.equal(serializer.windowDepth(window), 0); |
| 4 }); |
| 5 |
| 6 QUnit.test('windowDepth: multiple parent windows', function(assert) { |
| 7 var serializer = new HTMLSerializer(); |
| 8 var fixture = document.getElementById('qunit-fixture'); |
| 9 var childFrame = document.createElement('iframe'); |
| 10 fixture.appendChild(childFrame); |
| 11 var childFrameBody = childFrame.contentDocument.body; |
| 12 var grandChildFrame = document.createElement('iframe'); |
| 13 childFrameBody.appendChild(grandChildFrame); |
| 14 assert.equal(serializer.windowDepth(childFrame.contentWindow), 1); |
| 15 assert.equal(serializer.windowDepth(grandChildFrame.contentWindow), 2); |
| 16 }); |
| 17 |
| 18 QUnit.test('escapedCharacter: zero depth', function(assert) { |
| 19 var serializer = new HTMLSerializer(); |
| 20 assert.equal(serializer.escapedCharacter('"', 0), '"'); |
| 21 assert.equal(serializer.escapedCharacter("'", 0), "'"); |
| 22 assert.equal(serializer.escapedCharacter('<', 0), '<'); |
| 23 assert.equal(serializer.escapedCharacter('>', 0), '>'); |
| 24 assert.equal(serializer.escapedCharacter('&', 0), '&'); |
| 25 }); |
| 26 |
| 27 QUnit.test('escapedCharacter: nonzero depth', function(assert) { |
| 28 var serializer = new HTMLSerializer(); |
| 29 assert.equal(serializer.escapedCharacter('"', 1), '"'); |
| 30 assert.equal(serializer.escapedCharacter('"', 2), '&quot;'); |
| 31 assert.equal(serializer.escapedCharacter("'", 1), '''); |
| 32 assert.equal(serializer.escapedCharacter("'", 2), '&#39;'); |
| 33 assert.equal(serializer.escapedCharacter('<', 1), '<'); |
| 34 assert.equal(serializer.escapedCharacter('<', 2), '&lt;'); |
| 35 assert.equal(serializer.escapedCharacter('>', 1), '>'); |
| 36 assert.equal(serializer.escapedCharacter('>', 2), '&gt;'); |
| 37 assert.equal(serializer.escapedCharacter('&', 1), '&'); |
| 38 assert.equal(serializer.escapedCharacter('&', 2), '&amp;'); |
| 39 }); |
| 40 |
| 41 QUnit.test('iframeIndex: single layer', function(assert) { |
| 42 var serializer = new HTMLSerializer(); |
| 43 var fixture = document.getElementById('qunit-fixture'); |
| 44 var childFrame1 = document.createElement('iframe'); |
| 45 var childFrame2 = document.createElement('iframe'); |
| 46 fixture.appendChild(childFrame1); |
| 47 fixture.appendChild(childFrame2); |
| 48 assert.equal(serializer.iframeIndex(window), -1); |
| 49 assert.equal(serializer.iframeIndex(childFrame1.contentWindow), 0); |
| 50 assert.equal(serializer.iframeIndex(childFrame2.contentWindow), 1); |
| 51 }); |
| 52 |
| 53 QUnit.test('iframeIndex: multiple layers', function(assert) { |
| 54 var serializer = new HTMLSerializer(); |
| 55 var fixture = document.getElementById('qunit-fixture'); |
| 56 var childFrame = document.createElement('iframe'); |
| 57 var grandChildFrame1 = document.createElement('iframe'); |
| 58 var grandChildFrame2 = document.createElement('iframe'); |
| 59 fixture.appendChild(childFrame); |
| 60 var childFrameBody = childFrame.contentDocument.body; |
| 61 childFrameBody.appendChild(grandChildFrame1); |
| 62 childFrameBody.appendChild(grandChildFrame2); |
| 63 assert.equal(serializer.iframeIndex(grandChildFrame1.contentWindow), 0); |
| 64 assert.equal(serializer.iframeIndex(grandChildFrame2.contentWindow), 1); |
| 65 }); |
| 66 |
| 67 QUnit.test('iframeFullyQualifiedName: single layer', function(assert) { |
| 68 var serializer = new HTMLSerializer(); |
| 69 var fixture = document.getElementById('qunit-fixture'); |
| 70 var childFrame1 = document.createElement('iframe'); |
| 71 var childFrame2 = document.createElement('iframe'); |
| 72 fixture.appendChild(childFrame1); |
| 73 fixture.appendChild(childFrame2); |
| 74 assert.equal(serializer.iframeFullyQualifiedName(window), '0'); |
| 75 assert.equal( |
| 76 serializer.iframeFullyQualifiedName(childFrame1.contentWindow), |
| 77 '0.0'); |
| 78 assert.equal( |
| 79 serializer.iframeFullyQualifiedName(childFrame2.contentWindow), |
| 80 '0.1'); |
| 81 }); |
| 82 |
| 83 QUnit.test('iframeFullyQualifiedName: multiple layers', function(assert) { |
| 84 var serializer = new HTMLSerializer(); |
| 85 var fixture = document.getElementById('qunit-fixture'); |
| 86 var childFrame = document.createElement('iframe'); |
| 87 var grandChildFrame1 = document.createElement('iframe'); |
| 88 var grandChildFrame2 = document.createElement('iframe'); |
| 89 fixture.appendChild(childFrame); |
| 90 var childFrameBody = childFrame.contentDocument.body; |
| 91 childFrameBody.appendChild(grandChildFrame1); |
| 92 childFrameBody.appendChild(grandChildFrame2); |
| 93 assert.equal( |
| 94 serializer.iframeFullyQualifiedName(grandChildFrame1.contentWindow), |
| 95 '0.0.0'); |
| 96 assert.equal( |
| 97 serializer.iframeFullyQualifiedName(grandChildFrame2.contentWindow), |
| 98 '0.0.1'); |
| 99 }); |
| 100 |
| 101 QUnit.test('processSimpleAttribute: top window', function(assert) { |
| 102 var serializer = new HTMLSerializer(); |
| 103 var fixture = document.getElementById('qunit-fixture'); |
| 104 var win = fixture.ownerDocument.defaultView; |
| 105 serializer.processSimpleAttribute(win, 'height', '5'); |
| 106 assert.equal(serializer.html[0], 'height="5" '); |
| 107 }); |
| 108 |
| 109 QUnit.test('processSimpleAttribute: nested window', function(assert) { |
| 110 var serializer = new HTMLSerializer(); |
| 111 var fixture = document.getElementById('qunit-fixture'); |
| 112 var childFrame = document.createElement('iframe'); |
| 113 var grandChildFrame = document.createElement('iframe'); |
| 114 fixture.appendChild(childFrame); |
| 115 var childFrameBody = childFrame.contentDocument.body; |
| 116 childFrameBody.appendChild(grandChildFrame); |
| 117 var childFrameWindow = childFrame.contentDocument.defaultView; |
| 118 var grandChildFrameWindow = grandChildFrame.contentDocument.defaultView; |
| 119 serializer.processSimpleAttribute(childFrameWindow, 'height', '5'); |
| 120 serializer.processSimpleAttribute(grandChildFrameWindow, 'width', '2'); |
| 121 assert.equal(serializer.html[0], 'height="5" '); |
| 122 assert.equal(serializer.html[1], 'width=&quot;2&quot; '); |
| 123 }); |
| 124 |
| 125 QUnit.test('processHoleAttribute: top window', function(assert) { |
| 126 var serializer = new HTMLSerializer(); |
| 127 var fixture = document.getElementById('qunit-fixture'); |
| 128 var win = fixture.ownerDocument.defaultView; |
| 129 var valueIndex = serializer.processHoleAttribute(win, 'height'); |
| 130 assert.equal(valueIndex, 1); |
| 131 assert.equal(serializer.html[0], 'height="'); |
| 132 assert.equal(serializer.html[2], '" '); |
| 133 }); |
| 134 |
| 135 QUnit.test('processHoleAttribute: nested window', function(assert) { |
| 136 var serializer = new HTMLSerializer(); |
| 137 var fixture = document.getElementById('qunit-fixture'); |
| 138 var childFrame = document.createElement('iframe'); |
| 139 var grandChildFrame = document.createElement('iframe'); |
| 140 fixture.appendChild(childFrame); |
| 141 var childFrameBody = childFrame.contentDocument.body; |
| 142 childFrameBody.appendChild(grandChildFrame); |
| 143 var childFrameWindow = childFrame.contentDocument.defaultView; |
| 144 var grandChildFrameWindow = grandChildFrame.contentDocument.defaultView; |
| 145 var childValueIndex = serializer.processHoleAttribute( |
| 146 childFrameWindow, |
| 147 'height'); |
| 148 var grandChildValueIndex = serializer.processHoleAttribute( |
| 149 grandChildFrameWindow, |
| 150 'width'); |
| 151 assert.equal(childValueIndex, 1); |
| 152 assert.equal(serializer.html[0], 'height="'); |
| 153 assert.equal(serializer.html[2], '" '); |
| 154 assert.equal(grandChildValueIndex, 4); |
| 155 assert.equal(serializer.html[3], 'width=&quot;'); |
| 156 assert.equal(serializer.html[5], '&quot; '); |
| 157 }); |
| 158 |
| 159 QUnit.test('fullyQualifiedURL', function(assert) { |
| 160 var serializer = new HTMLSerializer(); |
| 161 var iframe = document.createElement('iframe'); |
| 162 iframe.setAttribute('src', 'page.html'); |
| 163 var url = serializer.fullyQualifiedURL(iframe); |
| 164 var href = window.location.href; |
| 165 var path = href.slice(0, href.lastIndexOf('/')); |
| 166 assert.equal(path + '/page.html', url.href); |
| 167 }); |
| 168 |
| 169 QUnit.test('processSrcHole: top window', function(assert) { |
| 170 var serializer = new HTMLSerializer(); |
| 171 var iframe = document.createElement('iframe'); |
| 172 iframe.setAttribute('src', 'tests.html'); |
| 173 serializer.processSrcHole(iframe); |
| 174 assert.equal(serializer.html[0], 'src="'); |
| 175 assert.equal(serializer.html[1], ''); |
| 176 assert.equal(serializer.html[2], '" '); |
| 177 assert.equal(serializer.srcHoles[1], window.location.href); |
| 178 assert.equal(Object.keys(serializer.srcHoles).length, 1); |
| 179 }); |
| 180 |
| 181 QUnit.test('processSrcAttribute: iframe', function(assert) { |
| 182 var serializer = new HTMLSerializer(); |
| 183 var iframe = document.createElement('iframe'); |
| 184 iframe.setAttribute('src', 'tests.html'); |
| 185 serializer.processSrcAttribute(iframe); |
| 186 assert.equal(serializer.html.length, 0); |
| 187 assert.equal(Object.keys(serializer.srcHoles).length, 0); |
| 188 }); |
| 189 |
| 190 QUnit.test('processSrcAttribute: audio', function(assert) { |
| 191 var serializer = new HTMLSerializer(); |
| 192 var audio = document.createElement('audio'); |
| 193 audio.setAttribute('src', 'tests.html'); |
| 194 serializer.processSrcAttribute(audio); |
| 195 assert.equal(serializer.html[0], `src="${window.location.href}" `); |
| 196 assert.equal(serializer.html.length, 1); |
| 197 assert.equal(Object.keys(serializer.srcHoles).length, 0); |
| 198 }); |
| 199 |
| 200 QUnit.test('processSrcAttribute: img', function(assert) { |
| 201 var serializer = new HTMLSerializer(); |
| 202 var img = document.createElement('img'); |
| 203 img.setAttribute('src', 'tests.html'); |
| 204 serializer.processSrcAttribute(img); |
| 205 assert.equal(serializer.html[0], 'src="'); |
| 206 assert.equal(serializer.html[1], ''); |
| 207 assert.equal(serializer.html[2], '" '); |
| 208 assert.equal(serializer.srcHoles[1], window.location.href); |
| 209 assert.equal(Object.keys(serializer.srcHoles).length, 1); |
| 210 }); |
| 211 |
| 212 QUnit.test('processTree: single node', function(assert) { |
| 213 var fixture = document.getElementById('qunit-fixture'); |
| 214 var node = document.createElement('div'); |
| 215 node.appendChild(document.createTextNode('test')); |
| 216 fixture.appendChild(node); |
| 217 var serializer = new HTMLSerializer(); |
| 218 serializer.processTree(node); |
| 219 assert.equal(Object.keys(serializer.srcHoles).length, 0); |
| 220 assert.equal(Object.keys(serializer.frameHoles).length, 0); |
| 221 }); |
| 222 |
| 223 QUnit.test('HTMLSerializer class loaded twice', function(assert) { |
| 224 assert.expect(0); |
| 225 var done = assert.async(); |
| 226 var fixture = document.getElementById('qunit-fixture'); |
| 227 var script1 = document.createElement('script'); |
| 228 var script2 = document.createElement('script'); |
| 229 script1.setAttribute('src', '../HTMLSerializer.js'); |
| 230 script2.setAttribute('src', '../HTMLSerializer.js'); |
| 231 fixture.appendChild(script1); |
| 232 fixture.appendChild(script2); |
| 233 setTimeout(function() { |
| 234 done(); |
| 235 }, 0); |
| 236 }); |
| 237 |
| 238 QUnit.test('processTree: no closing tag', function(assert) { |
| 239 var serializer = new HTMLSerializer(); |
| 240 var img = document.createElement('img'); |
| 241 serializer.processTree(img); |
| 242 assert.equal(serializer.html[0], '<img '); |
| 243 assert.equal( |
| 244 serializer.html[1], |
| 245 `style="${window.getComputedStyle(img, null).cssText}" `); |
| 246 assert.equal(serializer.html[3], '>') |
| 247 assert.equal(serializer.html.length, 4); |
| 248 }); |
| 249 |
| 250 QUnit.test('processTree: closing tag', function(assert) { |
| 251 var serializer = new HTMLSerializer(); |
| 252 var p = document.createElement('p'); |
| 253 serializer.processTree(p); |
| 254 assert.equal(serializer.html[0], '<p '); |
| 255 assert.equal( |
| 256 serializer.html[1], |
| 257 `style="${window.getComputedStyle(p, null).cssText}" `); |
| 258 assert.equal(serializer.html[3], '>'); |
| 259 assert.equal(serializer.html[4], '</p>'); |
| 260 assert.equal(serializer.html.length, 5); |
| 261 }); |
| 262 |
| 263 QUnit.test( |
| 264 'processAttributes: img with height and width attributes', |
| 265 function(assert) { |
| 266 var serializer = new HTMLSerializer(); |
| 267 var fixture = document.getElementById('qunit-fixture'); |
| 268 var img = document.createElement('img'); |
| 269 img.setAttribute('height', 5); |
| 270 img.setAttribute('width', 5); |
| 271 fixture.appendChild(img); |
| 272 serializer.processAttributes(img, 'id'); |
| 273 var styleText = serializer.html[0]; |
| 274 assert.ok(styleText.includes(' height: 5px;')); |
| 275 assert.ok(styleText.includes(' width: 5px;')); |
| 276 } |
| 277 ); |
| 278 |
| 279 QUnit.test( |
| 280 'processAttributes: img without height and width attributes', |
| 281 function(assert) { |
| 282 var serializer = new HTMLSerializer(); |
| 283 var fixture = document.getElementById('qunit-fixture'); |
| 284 var img = document.createElement('img'); |
| 285 fixture.appendChild(img); |
| 286 var style = window.getComputedStyle(img, null); |
| 287 serializer.processAttributes(img, 'id'); |
| 288 var styleText = serializer.html[0]; |
| 289 assert.ok(styleText.includes(` height: ${style.height};`)); |
| 290 assert.ok(styleText.includes(` width: ${style.width};`)); |
| 291 } |
| 292 ); |
| 293 |
| 294 QUnit.test( |
| 295 'processAttributes: img with height and width attributes and inline style', |
| 296 function(assert) { |
| 297 var serializer = new HTMLSerializer(); |
| 298 var fixture = document.getElementById('qunit-fixture'); |
| 299 var img = document.createElement('img'); |
| 300 img.setAttribute('height', 5); |
| 301 img.setAttribute('width', 5); |
| 302 img.setAttribute('style', 'height: 10px; width: 10px;'); |
| 303 fixture.appendChild(img); |
| 304 serializer.processAttributes(img, 'id'); |
| 305 var styleText = serializer.html[0]; |
| 306 assert.ok(styleText.includes(' height: 10px;')); |
| 307 assert.ok(styleText.includes(' width: 10px;')); |
| 308 assert.notOk(styleText.includes(' height: 5px;')); |
| 309 assert.notOk(styleText.includes(' width: 5px;')); |
| 310 } |
| 311 ); |
| 312 |
| 313 QUnit.test('processText: simple text node', function(assert) { |
| 314 var serializer = new HTMLSerializer(); |
| 315 var fixture = document.getElementById('qunit-fixture'); |
| 316 var node = document.createTextNode('Simple text'); |
| 317 fixture.appendChild(node); |
| 318 serializer.processText(node); |
| 319 assert.equal(serializer.html[0], 'Simple text'); |
| 320 }); |
| 321 |
| 322 QUnit.test('processText: escaped characters', function(assert) { |
| 323 var serializer = new HTMLSerializer(); |
| 324 var fixture = document.getElementById('qunit-fixture'); |
| 325 var node = document.createTextNode(`<div> with '&"`); |
| 326 fixture.appendChild(node); |
| 327 serializer.processText(node); |
| 328 assert.equal( |
| 329 serializer.html[0], |
| 330 '<div> with '&"'); |
| 331 }); |
| 332 |
| 333 QUnit.test('processText: nested escaped characters', function(assert) { |
| 334 var serializer = new HTMLSerializer(); |
| 335 var fixture = document.getElementById('qunit-fixture'); |
| 336 var childFrame = document.createElement('iframe'); |
| 337 var grandChildFrame = document.createElement('iframe'); |
| 338 fixture.appendChild(childFrame); |
| 339 var childFrameBody = childFrame.contentDocument.body; |
| 340 childFrameBody.appendChild(grandChildFrame); |
| 341 var grandChildFrameBody = grandChildFrame.contentDocument.body; |
| 342 var node1 = document.createTextNode(`<div> with '&"`); |
| 343 var node2 = document.createTextNode(`<div> with '&"`); |
| 344 childFrameBody.appendChild(node1); |
| 345 grandChildFrameBody.appendChild(node2); |
| 346 serializer.processText(node1); |
| 347 serializer.processText(node2); |
| 348 assert.equal( |
| 349 serializer.html[0], |
| 350 '&lt;div&gt; with &#39;&amp;&quot;'); |
| 351 assert.equal( |
| 352 serializer.html[1], |
| 353 '&amp;lt;div&amp;gt; with &amp;#39;&amp;amp;&amp;quot;
'); |
| 354 }); |
| 355 |
| 356 QUnit.test('processPseudoElements', function(assert) { |
| 357 var serializer = new HTMLSerializer(); |
| 358 var fixture = document.getElementById('qunit-fixture'); |
| 359 var element = document.createElement('div'); |
| 360 var style = document.createElement('style'); |
| 361 style.appendChild(document.createTextNode('div::before{content:"test";}')); |
| 362 fixture.appendChild(style); |
| 363 fixture.appendChild(element); |
| 364 serializer.processPseudoElements(element, 'myId'); |
| 365 var styleText = window.getComputedStyle(element, ':before').cssText; |
| 366 assert.equal(serializer.pseudoElementCSS.length, 1); |
| 367 assert.equal(serializer.pseudoElementCSS[0], `#myId::before{${styleText}} `); |
| 368 }); |
| 369 |
| 370 QUnit.test('processTree: element without id', function(assert) { |
| 371 var serializer = new HTMLSerializer(); |
| 372 var fixture = document.getElementById('qunit-fixture'); |
| 373 var element = document.createElement('div'); |
| 374 fixture.appendChild(element); |
| 375 serializer.processTree(element); |
| 376 assert.equal(serializer.html[0], '<div '); |
| 377 assert.equal(serializer.html[2], 'id="snap-it0" '); |
| 378 assert.equal(serializer.html[3], '>'); |
| 379 }); |
| 380 |
| 381 QUnit.test('processTree: element with id', function(assert) { |
| 382 var serializer = new HTMLSerializer(); |
| 383 var fixture = document.getElementById('qunit-fixture'); |
| 384 var element = document.createElement('div'); |
| 385 element.setAttribute('id', 'myId'); |
| 386 fixture.appendChild(element); |
| 387 serializer.processTree(element); |
| 388 assert.equal(serializer.html[0], '<div '); |
| 389 assert.equal(serializer.html[2], 'id="myId" '); |
| 390 assert.equal(serializer.html[3], '>'); |
| 391 }); |
| 392 |
| 393 QUnit.test('processTree: generated id exists', function(assert) { |
| 394 var serializer = new HTMLSerializer(); |
| 395 var fixture = document.getElementById('qunit-fixture'); |
| 396 var span = document.createElement('span'); |
| 397 span.setAttribute('id', 'snap-it0'); |
| 398 fixture.appendChild(span); |
| 399 var element = document.createElement('div'); |
| 400 fixture.appendChild(element); |
| 401 serializer.processTree(element); |
| 402 assert.equal(serializer.html[0], '<div '); |
| 403 assert.equal(serializer.html[2], 'id="snap-it1" '); |
| 404 assert.equal(serializer.html[3], '>'); |
| 405 }); |
| 406 |
| 407 QUnit.test('generateIdGenerator', function(assert) { |
| 408 var serializer = new HTMLSerializer(); |
| 409 var fixture = document.getElementById('qunit-fixture'); |
| 410 var div = document.createElement('div'); |
| 411 fixture.appendChild(div); |
| 412 var generateId = serializer.generateIdGenerator(); |
| 413 assert.equal(generateId(document), 'snap-it0'); |
| 414 assert.equal(generateId(document), 'snap-it1'); |
| 415 div.setAttribute('id', 'snap-it2'); |
| 416 assert.equal(generateId(document), 'snap-it3'); |
| 417 }); |
| 418 |
| 419 QUnit.test('escapedUnicodeString: html', function(assert) { |
| 420 var serializer = new HTMLSerializer(); |
| 421 var fixture = document.getElementById('qunit-fixture'); |
| 422 var div = document.createElement('div'); |
| 423 div.appendChild(document.createTextNode('i \u2665 \u0073f')); |
| 424 var string = div.childNodes[0].textContent; |
| 425 assert.equal( |
| 426 serializer.escapedUnicodeString(string, serializer.INPUT_TEXT_TYPE.HTML), |
| 427 'i ♥ sf'); |
| 428 }); |
| 429 |
| 430 QUnit.test('escapedUnicodeString: css', function(assert) { |
| 431 var serializer = new HTMLSerializer(); |
| 432 var fixture = document.getElementById('qunit-fixture'); |
| 433 var div = document.createElement('div'); |
| 434 div.appendChild(document.createTextNode('i \u2665 \u0073f')); |
| 435 var string = div.childNodes[0].textContent; |
| 436 assert.equal( |
| 437 serializer.escapedUnicodeString(string, serializer.INPUT_TEXT_TYPE.CSS), |
| 438 'i \\2665 sf'); |
| 439 }); |
| 440 |
| 441 QUnit.test('fullyQualifiedFontURL', function(assert) { |
| 442 var serializer = new HTMLSerializer(); |
| 443 var href = 'http://www.example.com/path/page/'; |
| 444 var url1 = '/hello/world/'; |
| 445 assert.equal( |
| 446 serializer.fullyQualifiedFontURL(href, url1), |
| 447 'http://www.example.com/hello/world/'); |
| 448 var url2 = './hello/world/'; |
| 449 assert.equal( |
| 450 serializer.fullyQualifiedFontURL(href, url2), |
| 451 'http://www.example.com/path/./hello/world/'); |
| 452 var url3 = '../hello/world/'; |
| 453 assert.equal( |
| 454 serializer.fullyQualifiedFontURL(href, url3), |
| 455 'http://www.example.com/path/../hello/world/'); |
| 456 var url4 = 'http://www.google.com/'; |
| 457 assert.equal( |
| 458 serializer.fullyQualifiedFontURL(href, url4), |
| 459 'http://www.google.com/'); |
| 460 var url5 = 'hello/world/'; |
| 461 assert.equal( |
| 462 serializer.fullyQualifiedFontURL(href, url5), |
| 463 'http://www.example.com/path/hello/world/'); |
| 464 }); |
| 465 |
| 466 QUnit.test('processCSSFonts: no line breaks in declaration', function(assert) { |
| 467 var serializer = new HTMLSerializer(); |
| 468 var cssText = 'body{color:red;}' + |
| 469 '@font-face{font-family:Font;src:url("/hello/")}'; |
| 470 var href = 'http://www.example.com/'; |
| 471 serializer.processCSSFonts(window, href, cssText); |
| 472 assert.equal( |
| 473 serializer.fontCSS[0], |
| 474 '@font-face{font-family:Font;src:url("http://www.example.com/hello/")}'); |
| 475 }); |
| 476 |
| 477 QUnit.test('processCSSFonts: line breaks in declaration', function(assert) { |
| 478 var serializer = new HTMLSerializer(); |
| 479 var cssText = 'body{color:red;}' + |
| 480 '@font-face { font-family:Font;\nsrc:url("/goodbye/")}'; |
| 481 var href = 'http://www.url.com/'; |
| 482 serializer.processCSSFonts(window, href, cssText); |
| 483 assert.equal( |
| 484 serializer.fontCSS[0], |
| 485 '@font-face { font-family:Font;\nsrc:url("http://www.url.com/goodbye/")}')
; |
| 486 }); |
| 487 |
| 488 QUnit.test('loadFonts', function(assert) { |
| 489 var serializer = new HTMLSerializer(); |
| 490 serializer.loadFonts(document); |
| 491 assert.equal(serializer.html[0], ''); |
| 492 assert.equal(serializer.fontPlaceHolderIndex, 0); |
| 493 assert.equal( |
| 494 serializer.crossOriginStyleSheets[0], |
| 495 'https://code.jquery.com/qunit/qunit-2.0.0.css'); |
| 496 }); |
| 497 |
| 498 QUnit.test('escapedCharacterString', function(assert) { |
| 499 var serializer = new HTMLSerializer(); |
| 500 var str = serializer.escapedCharacterString(`hello &>'<& "`, 2); |
| 501 assert.equal( |
| 502 str, |
| 503 'hello &amp;&gt;&#39;&lt;&amp; &quot;'); |
| 504 }); |
| 505 |
| 506 QUnit.test('unescapeHTML', function(assert) { |
| 507 var html = '&lt;div&gt;&lt;/div&gt;'; |
| 508 var unescapedHTML = unescapeHTML(html, 2); |
| 509 assert.equal(unescapedHTML, '<div></div>'); |
| 510 }); |
| 511 |
| 512 QUnit.test('minimizeStyles', function(assert) { |
| 513 var message = { |
| 514 'html': [ |
| 515 '<div id="myId"', |
| 516 'style="animation-delay: 0s; width: 5px;" ', |
| 517 '></div>' |
| 518 ], |
| 519 'frameHoles': null, |
| 520 'idToStyleIndex': {"myId": 1}, |
| 521 'idToStyleMap': { |
| 522 'myId': { |
| 523 'animation-delay': '0s', |
| 524 'width': '5px' |
| 525 } |
| 526 }, |
| 527 'windowHeight': 5, |
| 528 'windowWidth': 5, |
| 529 'frameIndex': '0' |
| 530 }; |
| 531 minimizeStyles(message); |
| 532 assert.equal(message.html[1], 'style="width: 5px;" '); |
| 533 }); |
| 534 |
| 535 QUnit.test('minimizeStyle', function(assert) { |
| 536 var fixture = document.getElementById('qunit-fixture'); |
| 537 var div = document.createElement('div'); |
| 538 div.setAttribute('id', 'myId'); |
| 539 div.setAttribute('style', 'animation-delay: 0s; width: 5px;'); |
| 540 fixture.appendChild(div); |
| 541 var message = { |
| 542 'html': [ |
| 543 '<div id="myId"', |
| 544 'style="animation-delay: 0s; width: 5px;" ', |
| 545 '></div>' |
| 546 ], |
| 547 'frameHoles': null, |
| 548 'idToStyleIndex': {"myId": 1}, |
| 549 'idToStyleMap': { |
| 550 'myId': { |
| 551 'animation-delay': '0s', |
| 552 'width': '5px' |
| 553 } |
| 554 }, |
| 555 'windowHeight': 5, |
| 556 'windowWidth': 5, |
| 557 'frameIndex': '0' |
| 558 }; |
| 559 minimizeStyle(message, document, div, 'myId', 1); |
| 560 assert.equal(message.html[1], 'style="width: 5px;" '); |
| 561 }); |
| 562 |
| 563 QUnit.test('serialize tree: end-to-end, no style', function(assert) { |
| 564 var serializer = new HTMLSerializer(); |
| 565 var fixture = document.getElementById('qunit-fixture'); |
| 566 var iframe = document.createElement('iframe'); |
| 567 fixture.appendChild(iframe); |
| 568 var div = document.createElement('div'); |
| 569 div.appendChild(document.createTextNode('hello world')); |
| 570 iframe.contentDocument.body.appendChild(div); |
| 571 serializer.processTree(div); |
| 572 var win = div.ownerDocument.defaultView; |
| 573 var message = { |
| 574 'html': serializer.html, |
| 575 'frameHoles': serializer.frameHoles, |
| 576 'idToStyleIndex': serializer.idToStyleIndex, |
| 577 'idToStyleMap': { |
| 578 'snap-it0': { |
| 579 'animation-delay': '0s' |
| 580 } |
| 581 }, |
| 582 'windowHeight': serializer.windowHeight, |
| 583 'windowWidth': serializer.windowWidth, |
| 584 'frameIndex': serializer.iframeFullyQualifiedName(win) |
| 585 }; |
| 586 var html = unescapeHTML(outputHTMLString([message]), 1); |
| 587 assert.equal(html, '<div id="snap-it0" >hello world</div>'); |
| 588 }); |
| 589 |
| 590 QUnit.test('serialize tree: end-to-end, style', function(assert) { |
| 591 var serializer = new HTMLSerializer(); |
| 592 var fixture = document.getElementById('qunit-fixture'); |
| 593 var iframe = document.createElement('iframe'); |
| 594 fixture.appendChild(iframe); |
| 595 var div = document.createElement('div'); |
| 596 div.appendChild(document.createTextNode('hello world')); |
| 597 iframe.contentDocument.body.appendChild(div); |
| 598 var style = document.createElement('style'); |
| 599 style.innerHTML = 'div { border: 1px solid blue; }'; |
| 600 iframe.contentDocument.body.appendChild(style); |
| 601 serializer.processTree(div); |
| 602 var win = div.ownerDocument.defaultView; |
| 603 var message = { |
| 604 'html': serializer.html, |
| 605 'frameHoles': serializer.frameHoles, |
| 606 'idToStyleIndex': serializer.idToStyleIndex, |
| 607 'idToStyleMap': { |
| 608 'snap-it0': { |
| 609 'animation-delay': '0s', |
| 610 'border-bottom-color': 'rgb(0, 0, 255)', |
| 611 'border-bottom-style': 'solid', |
| 612 'border-bottom-width': '4px', |
| 613 'border-left-color': 'rgb(0, 0, 255)', |
| 614 'border-left-style': 'solid', |
| 615 'border-left-width': '4px', |
| 616 'border-right-color': 'rgb(0, 0, 255)', |
| 617 'border-right-style': 'solid', |
| 618 'border-right-width': '4px', |
| 619 'border-top-color': 'rgb(0, 0, 255)', |
| 620 'border-top-style': 'solid', |
| 621 'border-top-width': '4px', |
| 622 'width': '276px', |
| 623 'perspective-origin': '142px 24px', |
| 624 'transform-origin': '142px 24px' |
| 625 } |
| 626 }, |
| 627 'windowHeight': serializer.windowHeight, |
| 628 'windowWidth': serializer.windowWidth, |
| 629 'frameIndex': serializer.iframeFullyQualifiedName(win) |
| 630 }; |
| 631 var html = unescapeHTML(outputHTMLString([message]), 1); |
| 632 assert.equal( |
| 633 html, |
| 634 '<div style="border-bottom-color: rgb(0, 0, 255); border-bottom-style: ' + |
| 635 'solid; border-bottom-width: 4px; border-left-color: rgb(0, 0, 255); ' + |
| 636 'border-left-style: solid; border-left-width: 4px; border-right-color: ' + |
| 637 'rgb(0, 0, 255); border-right-style: solid; border-right-width: 4px; ' + |
| 638 'border-top-color: rgb(0, 0, 255); border-top-style: solid; ' + |
| 639 'border-top-width: 4px; width: 276px; perspective-origin: 142px 24px; ' + |
| 640 'transform-origin: 142px 24px;" id="snap-it0" >hello world</div>'); |
| 641 }); |
| 642 |
| 643 QUnit.test('processTree: head tag', function(assert) { |
| 644 var serializer = new HTMLSerializer(); |
| 645 var head = document.createElement('head'); |
| 646 serializer.processTree(head); |
| 647 assert.equal(serializer.fontPlaceHolderIndex, 4); |
| 648 assert.equal(serializer.pseudoElementPlaceHolderIndex, 5); |
| 649 }); |
| 650 |
| 651 QUnit.test('minimizeStyles: root html tag', function(assert) { |
| 652 var message = { |
| 653 'html': [ |
| 654 '<html id="myId" ', |
| 655 'style="animation-delay: 0s; width: 5px;" ', |
| 656 '></html>' |
| 657 ], |
| 658 'frameHoles': null, |
| 659 'idToStyleIndex': {}, |
| 660 'idToStyleMap': { |
| 661 'myId': { |
| 662 'animation-delay': '0s', |
| 663 'width': '5px' |
| 664 } |
| 665 }, |
| 666 'windowHeight': 5, |
| 667 'windowWidth': 5, |
| 668 'rootId': 'myId', |
| 669 'rootStyleIndex': 1, |
| 670 'frameIndex': '0' |
| 671 }; |
| 672 minimizeStyles(message); |
| 673 assert.equal(message.html[1], 'style="width: 5px;" '); |
| 674 }); |
| 675 |
| 676 QUnit.test('processAttributes: escaping characters', function(assert) { |
| 677 var serializer = new HTMLSerializer(); |
| 678 var fixture = document.getElementById('qunit-fixture'); |
| 679 var div = document.createElement('div'); |
| 680 div.setAttribute('name', '<">'); |
| 681 fixture.appendChild(div); |
| 682 serializer.processAttributes(div, 'myId'); |
| 683 assert.equal(serializer.html[2], 'name="<">" '); |
| 684 }); |
| 685 |
| 686 QUnit.test('window size comment', function(assert) { |
| 687 var serializer = new HTMLSerializer(); |
| 688 serializer.processDocument(document); |
| 689 assert.equal( |
| 690 serializer.html[1], |
| 691 `<!-- Original window height: ${window.innerHeight}. -->\n`); |
| 692 assert.equal( |
| 693 serializer.html[2], |
| 694 `<!-- Original window width: ${window.innerWidth}. -->\n`); |
| 695 }); |
| 696 |
| 697 QUnit.test('processDocument: doctype tag', function(assert) { |
| 698 var serializer = new HTMLSerializer(); |
| 699 serializer.processDocument(document); |
| 700 assert.equal(serializer.html[0], '<!DOCTYPE html>\n'); |
| 701 }); |
| 702 |
| 703 QUnit.test('processDocument: no doctype tag', function(assert) { |
| 704 var serializer = new HTMLSerializer(); |
| 705 var fixture = document.getElementById('qunit-fixture'); |
| 706 var iframe = document.createElement('iframe'); |
| 707 fixture.appendChild(iframe); |
| 708 serializer.processDocument(iframe.contentDocument); |
| 709 assert.notEqual(serializer.html[0], '<!DOCTYPE html>\n'); |
| 710 }); |
| 711 |
| 712 QUnit.test('escapedQuote', function(assert) { |
| 713 assert.equal(escapedQuote(0), '"'); |
| 714 assert.equal(escapedQuote(1), '"'); |
| 715 assert.equal(escapedQuote(3), '&amp;quot;'); |
| 716 }); |
| 717 |
| 718 QUnit.test('buildStyleAttribute', function(assert) { |
| 719 var styleMap = { |
| 720 'color': 'blue', |
| 721 'background-color': 'red' |
| 722 } |
| 723 assert.equal( |
| 724 buildStyleAttribute(styleMap), |
| 725 'color: blue; background-color: red;'); |
| 726 }); |
| 727 |
| 728 QUnit.test('updateMinimizedStyleMap: no update', function(assert) { |
| 729 var fixture = document.getElementById('qunit-fixture'); |
| 730 var div = document.createElement('div'); |
| 731 div.setAttribute('id', 'myId'); |
| 732 div.setAttribute('style', 'width: 5px;'); |
| 733 fixture.appendChild(div); |
| 734 var originalStyleMap = { |
| 735 'animation-delay': '0s', |
| 736 'width': '5px' |
| 737 }; |
| 738 var requiredStyleMap = { |
| 739 'width': '5px' |
| 740 }; |
| 741 var updated = updateMinimizedStyleMap( |
| 742 document, |
| 743 div, |
| 744 originalStyleMap, |
| 745 requiredStyleMap, |
| 746 null); |
| 747 assert.notOk(updated); |
| 748 assert.equal(Object.keys(requiredStyleMap).length, 1); |
| 749 assert.equal(requiredStyleMap.width, '5px'); |
| 750 }); |
| 751 |
| 752 QUnit.test('updateMinimizedStyleMap: update', function(assert) { |
| 753 var fixture = document.getElementById('qunit-fixture'); |
| 754 var div = document.createElement('div'); |
| 755 div.setAttribute('id', 'myId'); |
| 756 fixture.appendChild(div); |
| 757 var originalStyleMap = { |
| 758 'animation-delay': '0s', |
| 759 'width': '5px' |
| 760 }; |
| 761 var requiredStyleMap = {}; |
| 762 var updated = updateMinimizedStyleMap( |
| 763 document, |
| 764 div, |
| 765 originalStyleMap, |
| 766 requiredStyleMap, |
| 767 null); |
| 768 assert.ok(updated); |
| 769 assert.equal(Object.keys(requiredStyleMap).length, 1); |
| 770 assert.equal(requiredStyleMap.width, '5px'); |
| 771 }); |
| 772 |
| 773 QUnit.test('minimizePseudoElementStyle', function(assert) { |
| 774 var fixture = document.getElementById('qunit-fixture'); |
| 775 var div = document.createElement('div'); |
| 776 div.setAttribute('id', 'divId'); |
| 777 var pseudoElementStyle = document.createElement('style'); |
| 778 pseudoElementStyle.innerHTML = |
| 779 '#divId::before{animation-delay: 0s; content:"hello"}'; |
| 780 var testingStyle = document.createElement('style'); |
| 781 testingStyle.setAttribute('id', 'styleId'); |
| 782 fixture.appendChild(div); |
| 783 fixture.appendChild(pseudoElementStyle); |
| 784 fixture.appendChild(testingStyle); |
| 785 var message = { |
| 786 'pseudoElementSelectorToCSSMap': { |
| 787 '#divId::before': { |
| 788 'animation-delay': '0s', |
| 789 'content': 'hello' |
| 790 } |
| 791 }, |
| 792 'pseudoElementTestingStyleId': 'styleId', |
| 793 'unusedId': 'unusedId', |
| 794 'frameIndex': '0' |
| 795 } |
| 796 var finalPseudoElementCSS = []; |
| 797 minimizePseudoElementStyle( |
| 798 message, |
| 799 document, |
| 800 '#divId::before', |
| 801 finalPseudoElementCSS); |
| 802 assert.equal( |
| 803 finalPseudoElementCSS.join(' '), |
| 804 '#divId::before{ content: hello; }'); |
| 805 }); |
| 806 |
| 807 QUnit.test('minimizeStyles: pseudo elements', function(assert) { |
| 808 var message = { |
| 809 'html': [ |
| 810 '<div id="divId"', |
| 811 'style="animation-delay: 0s; width: 5px;" ', |
| 812 '></div>', |
| 813 '<style>#divId::before{animation-delay: 0s; content:"hello"}</style>', |
| 814 '<style id="styleId"></style>' |
| 815 ], |
| 816 'pseudoElementSelectorToCSSMap': { |
| 817 '#divId::before': { |
| 818 'animation-delay': '0s', |
| 819 'content': 'hello' |
| 820 } |
| 821 }, |
| 822 'pseudoElementPlaceHolderIndex': 3, |
| 823 'pseudoElementTestingStyleIndex': 4, |
| 824 'pseudoElementTestingStyleId': 'styleId', |
| 825 'unusedId': 'unusedId', |
| 826 'frameHoles': null, |
| 827 'idToStyleIndex': {"divId": 1}, |
| 828 'idToStyleMap': { |
| 829 'divId': { |
| 830 'animation-delay': '0s', |
| 831 'width': '5px' |
| 832 } |
| 833 }, |
| 834 'windowHeight': 5, |
| 835 'windowWidth': 5, |
| 836 'frameIndex': '0' |
| 837 }; |
| 838 minimizeStyles(message); |
| 839 assert.equal( |
| 840 message.html[3], |
| 841 '<style>#divId::before{ content: hello; }</style>'); |
| 842 }); |
| OLD | NEW |