Left: | ||
Right: |
OLD | NEW |
---|---|
(Empty) | |
1 module("CSS escaping"); | |
2 asyncTest("CSS escaping", function() | |
3 { | |
4 var frame = document.createElement("iframe"); | |
5 frame.srcdoc='<script src="../include.postload.js"></script>'; | |
6 | |
7 frame.addEventListener("load", function() | |
Wladimir Palant
2014/11/06 21:26:06
Nit: This should be part of test setup (defined in
Sebastian Noack
2014/11/10 12:13:18
I already wondered whether qunit has setup/teardow
| |
8 { | |
9 start(); | |
10 | |
11 var escapeCSS = frame.contentWindow.escapeCSS; | |
12 var quote = frame.contentWindow.quote; | |
13 | |
14 document.body.removeChild(frame); | |
Wladimir Palant
2014/11/06 21:26:06
Won't removing the frame immediately prevent escap
Sebastian Noack
2014/11/10 12:13:18
No, the test passes without any errors. I think it
| |
15 | |
16 function testSelector(opts) { | |
Wladimir Palant
2014/11/06 21:26:06
Nit: opening bracket on next line please.
Sebastian Noack
2014/11/10 12:13:18
Done.
| |
17 var mustMatch = opts.mustMatch !== false; | |
18 var doc = document.implementation.createHTMLDocument(); | |
19 | |
20 var style = doc.createElement("style"); | |
21 doc.documentElement.appendChild(style); | |
22 style.sheet.insertRule(opts.selector + " {}"); | |
23 | |
24 var element; | |
25 try | |
26 { | |
27 element = doc.createElement(opts.tagName || "div"); | |
28 } | |
29 catch (e) | |
30 { | |
31 // Some characters we are going to test can not occur in tag names, | |
32 // but we still have to make sure that no exception is thrown when | |
33 // calling .querySelector() and .insertRule() | |
34 element = null; | |
35 mustMatch = false; | |
36 } | |
37 | |
38 if (element) | |
39 { | |
40 for (var attr in opts.attributes) | |
41 element.setAttribute(attr, opts.attributes[attr]); | |
42 | |
43 doc.documentElement.appendChild(element); | |
44 } | |
45 | |
46 var foundElement = doc.querySelector(opts.selector); | |
47 if (mustMatch) | |
48 equal(foundElement, element, opts.selector); | |
49 else | |
50 ok(true, opts.selector); | |
51 } | |
52 | |
53 function testEscape(s) | |
54 { | |
55 testSelector({ | |
56 selector: escapeCSS(s), | |
57 tagName: s | |
58 }); | |
59 | |
60 testSelector({ | |
61 selector: "#" + escapeCSS(s), | |
62 attributes: {id: s} | |
63 }); | |
64 | |
65 testSelector({ | |
66 selector: "." + escapeCSS(s), | |
67 attributes: {class: s}, | |
68 | |
69 // Whitespace characters split the class name, hence the selector | |
70 // won't match. But we still have to make sure that no exception | |
71 // is thrown when calling .querySelector() and .insertRule() | |
72 mustMatch: !/\s/.test(s) | |
73 }); | |
74 | |
75 testSelector({ | |
76 selector: "[foo=" + quote(s) + "]", | |
77 attributes: {foo: s} | |
78 }); | |
79 } | |
80 | |
81 for (var i = 0; i < 0x80; i++) | |
82 { | |
83 var chr = String.fromCharCode(i); | |
84 | |
85 // Make sure that all ASCII characters are correctly escaped. | |
86 testEscape(chr); | |
87 | |
88 // Some characters are only escaped when in the first positon, | |
89 // so we still have to make sure that everything is correctly escaped | |
90 // in subsequent positions. At the same time we make sure that | |
91 // leading dashes are escaped, when followed by certain characters. | |
92 testEscape("-" + chr); | |
Wladimir Palant
2014/11/06 21:26:06
How about testing a letter as leading character as
Sebastian Noack
2014/11/10 12:13:18
I considered that case redundant. But fair enough.
| |
93 } | |
94 | |
95 // Test some non-ASCII characters. However, those shouldn't require escaping . | |
96 testEscape("😻♥ä"); | |
Wladimir Palant
2014/11/06 21:26:06
Better not have Unicode characters in a JavaScript
Sebastian Noack
2014/11/10 12:13:18
I actually enjoyed seeing a cat with heart-shaped
| |
97 }); | |
98 | |
99 document.body.appendChild(frame); | |
100 }); | |
OLD | NEW |