Index: qunit/tests/cssEscaping.js |
=================================================================== |
--- a/qunit/tests/cssEscaping.js |
+++ b/qunit/tests/cssEscaping.js |
@@ -1,114 +1,130 @@ |
-module( |
- "CSS escaping", |
+(function() |
+{ |
+ var filterClasses = require("filterClasses"); |
+ var Filter = filterClasses.Filter; |
+ var ElemHideFilter = filterClasses.ElemHideFilter; |
+ |
+ module( |
+ "CSS escaping", |
+ { |
+ setup: function() |
+ { |
+ var frame = document.createElement("iframe"); |
+ frame.srcdoc = '<script src="../include.postload.js"></script>'; |
+ |
+ stop(); |
+ frame.addEventListener("load", function() |
+ { |
+ start(); |
+ |
+ this.escapeCSS = frame.contentWindow.escapeCSS; |
+ this.quote = frame.contentWindow.quote; |
+ |
+ document.body.removeChild(frame); |
+ }.bind(this)); |
+ |
+ document.body.appendChild(frame); |
+ } |
+ } |
+ ); |
+ |
+ test("CSS escaping", function() |
{ |
- setup: function() |
+ var escapeCSS = this.escapeCSS; |
+ var quote = this.quote; |
+ |
+ function testSelector(opts) |
{ |
- var frame = document.createElement("iframe"); |
- frame.srcdoc = '<script src="../include.postload.js"></script>'; |
+ var mustMatch = opts.mustMatch !== false; |
+ var doc = document.implementation.createHTMLDocument(); |
- stop(); |
- frame.addEventListener("load", function() |
+ var style = doc.createElement("style"); |
+ doc.documentElement.appendChild(style); |
+ style.sheet.insertRule(opts.selector + " {}", 0); |
+ |
+ var element; |
+ try |
{ |
- start(); |
+ 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; |
+ } |
- this.escapeCSS = frame.contentWindow.escapeCSS; |
- this.quote = frame.contentWindow.quote; |
+ if (element) |
+ { |
+ for (var attr in opts.attributes) |
+ element.setAttribute(attr, opts.attributes[attr]); |
- document.body.removeChild(frame); |
- }.bind(this)); |
+ doc.documentElement.appendChild(element); |
+ } |
- document.body.appendChild(frame); |
- } |
- } |
-); |
+ var foundElement = doc.querySelector(opts.selector); |
+ var filter = Filter.fromText("##" + opts.selector); |
-test("CSS escaping", function() |
-{ |
- var escapeCSS = this.escapeCSS; |
- var quote = this.quote; |
- |
- function testSelector(opts) |
- { |
- var mustMatch = opts.mustMatch !== false; |
- var doc = document.implementation.createHTMLDocument(); |
- |
- var style = doc.createElement("style"); |
- doc.documentElement.appendChild(style); |
- style.sheet.insertRule(opts.selector + " {}", 0); |
- |
- 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 (!(filter instanceof ElemHideFilter)) |
+ { |
+ ok(false, opts.selector + " (not allowed in elemhide filters)"); |
+ } |
+ else |
+ { |
+ if (mustMatch) |
+ equal(foundElement, element, opts.selector); |
+ else |
+ ok(true, opts.selector); |
+ } |
} |
- if (element) |
+ function testEscape(s) |
{ |
- for (var attr in opts.attributes) |
- element.setAttribute(attr, opts.attributes[attr]); |
+ testSelector({ |
+ selector: escapeCSS(s), |
+ tagName: s |
+ }); |
- doc.documentElement.appendChild(element); |
+ 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} |
+ }); |
} |
- var foundElement = doc.querySelector(opts.selector); |
- if (mustMatch) |
- equal(foundElement, element, opts.selector); |
- else |
- ok(true, opts.selector); |
- } |
+ for (var i = 0; i < 0x80; i++) |
+ { |
+ var chr = String.fromCharCode(i); |
- function testEscape(s) |
- { |
- testSelector({ |
- selector: escapeCSS(s), |
- tagName: s |
- }); |
+ // Make sure that all ASCII characters are correctly escaped. |
+ testEscape(chr); |
- testSelector({ |
- selector: "#" + escapeCSS(s), |
- attributes: {id: s} |
- }); |
+ // 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. |
+ testEscape("x" + chr); |
- testSelector({ |
- selector: "." + escapeCSS(s), |
- attributes: {class: s}, |
+ // Leading dashes must be escaped, when followed by certain characters. |
+ testEscape("-" + chr); |
+ } |
- // 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. |
- testEscape("x" + chr); |
- |
- // Leading dashes must be escaped, when followed by certain characters. |
- testEscape("-" + chr); |
- } |
- |
- // Test some non-ASCII characters. However, those shouldn't require escaping. |
- testEscape("\uD83D\uDE3B\u2665\u00E4"); |
-}); |
+ // Test some non-ASCII characters. However, those shouldn't require escaping. |
+ testEscape("\uD83D\uDE3B\u2665\u00E4"); |
+ }); |
+})(); |