Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Delta Between Two Patch Sets: qunit/tests/cssEscaping.js

Issue 4935175632846848: Issue 1527 - Properly escape generated CSS selectors (Closed)
Left Patch Set: Renamed function and added test case Created Nov. 6, 2014, 2:59 p.m.
Right Patch Set: Addressed comments Created Nov. 10, 2014, 12:10 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « qunit/index.html ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 module("CSS escaping"); 1 module(
2 asyncTest("CSS escaping", function() 2 "CSS escaping",
3 {
4 setup: function()
5 {
6 var frame = document.createElement("iframe");
7 frame.srcdoc='<script src="../include.postload.js"></script>';
Wladimir Palant 2014/11/10 12:34:15 Nit: Missing spaces around =
8
9 stop();
10 frame.addEventListener("load", function()
11 {
12 start();
13
14 this.escapeCSS = frame.contentWindow.escapeCSS;
15 this.quote = frame.contentWindow.quote;
16
17 document.body.removeChild(frame);
18 }.bind(this));
19
20 document.body.appendChild(frame);
21 }
22 }
23 );
24
25 test("CSS escaping", function()
3 { 26 {
4 var frame = document.createElement("iframe"); 27 var escapeCSS = this.escapeCSS;
5 frame.srcdoc='<script src="../include.postload.js"></script>'; 28 var quote = this.quote;
6 29
7 frame.addEventListener("load", function() 30 function testSelector(opts)
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 { 31 {
9 start(); 32 var mustMatch = opts.mustMatch !== false;
33 var doc = document.implementation.createHTMLDocument();
10 34
11 var escapeCSS = frame.contentWindow.escapeCSS; 35 var style = doc.createElement("style");
12 var quote = frame.contentWindow.quote; 36 doc.documentElement.appendChild(style);
37 style.sheet.insertRule(opts.selector + " {}");
13 38
14 document.body.removeChild(frame); 39 var element;
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 40 try
16 function testSelector(opts) { 41 {
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; 42 element = doc.createElement(opts.tagName || "div");
18 var doc = document.implementation.createHTMLDocument(); 43 }
19 44 catch (e)
20 var style = doc.createElement("style"); 45 {
21 doc.documentElement.appendChild(style); 46 // Some characters we are going to test can not occur in tag names,
22 style.sheet.insertRule(opts.selector + " {}"); 47 // but we still have to make sure that no exception is thrown when
23 48 // calling .querySelector() and .insertRule()
24 var element; 49 element = null;
25 try 50 mustMatch = false;
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 } 51 }
52 52
53 function testEscape(s) 53 if (element)
54 { 54 {
55 testSelector({ 55 for (var attr in opts.attributes)
56 selector: escapeCSS(s), 56 element.setAttribute(attr, opts.attributes[attr]);
57 tagName: s
58 });
59 57
60 testSelector({ 58 doc.documentElement.appendChild(element);
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 } 59 }
80 60
81 for (var i = 0; i < 0x80; i++) 61 var foundElement = doc.querySelector(opts.selector);
82 { 62 if (mustMatch)
83 var chr = String.fromCharCode(i); 63 equal(foundElement, element, opts.selector);
64 else
65 ok(true, opts.selector);
66 }
84 67
85 // Make sure that all ASCII characters are correctly escaped. 68 function testEscape(s)
86 testEscape(chr); 69 {
70 testSelector({
71 selector: escapeCSS(s),
72 tagName: s
73 });
87 74
88 // Some characters are only escaped when in the first positon, 75 testSelector({
89 // so we still have to make sure that everything is correctly escaped 76 selector: "#" + escapeCSS(s),
90 // in subsequent positions. At the same time we make sure that 77 attributes: {id: s}
91 // leading dashes are escaped, when followed by certain characters. 78 });
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 79
95 // Test some non-ASCII characters. However, those shouldn't require escaping . 80 testSelector({
96 testEscape("😻♥ä"); 81 selector: "." + escapeCSS(s),
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 }); 82 attributes: {class: s},
98 83
99 document.body.appendChild(frame); 84 // Whitespace characters split the class name, hence the selector
85 // won't match. But we still have to make sure that no exception
86 // is thrown when calling .querySelector() and .insertRule()
87 mustMatch: !/\s/.test(s)
88 });
89
90 testSelector({
91 selector: "[foo=" + quote(s) + "]",
92 attributes: {foo: s}
93 });
94 }
95
96 for (var i = 0; i < 0x80; i++)
97 {
98 var chr = String.fromCharCode(i);
99
100 // Make sure that all ASCII characters are correctly escaped.
101 testEscape(chr);
102
103 // Some characters are only escaped when in the first positon,
104 // so we still have to make sure that everything is correctly escaped
105 // in subsequent positions.
106 testEscape("x" + chr);
107
108 // Leading dashes must be escaped, when followed by certain characters.
109 testEscape("-" + chr);
110 }
111
112 // Test some non-ASCII characters. However, those shouldn't require escaping.
113 testEscape("\uD83D\uDE3B\u2665\u00E4");
100 }); 114 });
LEFTRIGHT

Powered by Google App Engine
This is Rietveld