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

Unified Diff: qunit/tests/cssEscaping.js

Issue 4935175632846848: Issue 1527 - Properly escape generated CSS selectors (Closed)
Patch Set: Renamed function and added test case Created Nov. 6, 2014, 2:59 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« include.postload.js ('K') | « qunit/index.html ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: qunit/tests/cssEscaping.js
===================================================================
new file mode 100644
--- /dev/null
+++ b/qunit/tests/cssEscaping.js
@@ -0,0 +1,100 @@
+module("CSS escaping");
+asyncTest("CSS escaping", function()
+{
+ var frame = document.createElement("iframe");
+ frame.srcdoc='<script src="../include.postload.js"></script>';
+
+ 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
+ {
+ start();
+
+ var escapeCSS = frame.contentWindow.escapeCSS;
+ var quote = frame.contentWindow.quote;
+
+ 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
+
+ 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.
+ var mustMatch = opts.mustMatch !== false;
+ var doc = document.implementation.createHTMLDocument();
+
+ var style = doc.createElement("style");
+ doc.documentElement.appendChild(style);
+ style.sheet.insertRule(opts.selector + " {}");
+
+ var element;
+ try
+ {
+ element = doc.createElement(opts.tagName || "div");
+ }
+ catch (e)
+ {
+ // Some characters we are going to test can not occur in tag names,
+ // but we still have to make sure that no exception is thrown when
+ // calling .querySelector() and .insertRule()
+ element = null;
+ mustMatch = false;
+ }
+
+ if (element)
+ {
+ for (var attr in opts.attributes)
+ element.setAttribute(attr, opts.attributes[attr]);
+
+ doc.documentElement.appendChild(element);
+ }
+
+ var foundElement = doc.querySelector(opts.selector);
+ if (mustMatch)
+ equal(foundElement, element, opts.selector);
+ else
+ ok(true, opts.selector);
+ }
+
+ function testEscape(s)
+ {
+ testSelector({
+ selector: escapeCSS(s),
+ tagName: s
+ });
+
+ testSelector({
+ selector: "#" + escapeCSS(s),
+ attributes: {id: s}
+ });
+
+ testSelector({
+ selector: "." + escapeCSS(s),
+ attributes: {class: s},
+
+ // Whitespace characters split the class name, hence the selector
+ // won't match. But we still have to make sure that no exception
+ // is thrown when calling .querySelector() and .insertRule()
+ mustMatch: !/\s/.test(s)
+ });
+
+ testSelector({
+ selector: "[foo=" + quote(s) + "]",
+ attributes: {foo: s}
+ });
+ }
+
+ for (var i = 0; i < 0x80; i++)
+ {
+ var chr = String.fromCharCode(i);
+
+ // Make sure that all ASCII characters are correctly escaped.
+ testEscape(chr);
+
+ // Some characters are only escaped when in the first positon,
+ // so we still have to make sure that everything is correctly escaped
+ // in subsequent positions. At the same time we make sure that
+ // leading dashes are escaped, when followed by certain characters.
+ 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.
+ }
+
+ // Test some non-ASCII characters. However, those shouldn't require escaping.
+ 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
+ });
+
+ document.body.appendChild(frame);
+});
« include.postload.js ('K') | « qunit/index.html ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld