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

Side by Side Diff: qunit/tests/cssEscaping.js

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

Powered by Google App Engine
This is Rietveld