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: Created Nov. 5, 2014, 2:21 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:
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
(no file at all)
1 module(
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()
26 {
27 var escapeCSS = this.escapeCSS;
28 var quote = this.quote;
29
30 function testSelector(opts)
31 {
32 var mustMatch = opts.mustMatch !== false;
33 var doc = document.implementation.createHTMLDocument();
34
35 var style = doc.createElement("style");
36 doc.documentElement.appendChild(style);
37 style.sheet.insertRule(opts.selector + " {}");
38
39 var element;
40 try
41 {
42 element = doc.createElement(opts.tagName || "div");
43 }
44 catch (e)
45 {
46 // Some characters we are going to test can not occur in tag names,
47 // but we still have to make sure that no exception is thrown when
48 // calling .querySelector() and .insertRule()
49 element = null;
50 mustMatch = false;
51 }
52
53 if (element)
54 {
55 for (var attr in opts.attributes)
56 element.setAttribute(attr, opts.attributes[attr]);
57
58 doc.documentElement.appendChild(element);
59 }
60
61 var foundElement = doc.querySelector(opts.selector);
62 if (mustMatch)
63 equal(foundElement, element, opts.selector);
64 else
65 ok(true, opts.selector);
66 }
67
68 function testEscape(s)
69 {
70 testSelector({
71 selector: escapeCSS(s),
72 tagName: s
73 });
74
75 testSelector({
76 selector: "#" + escapeCSS(s),
77 attributes: {id: s}
78 });
79
80 testSelector({
81 selector: "." + escapeCSS(s),
82 attributes: {class: s},
83
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");
114 });
LEFTRIGHT

Powered by Google App Engine
This is Rietveld