Skip to content

Commit

Permalink
fixup! fixup! Add tests for element/attribute name selectors in HTML
Browse files Browse the repository at this point in the history
  • Loading branch information
gsnedders committed Nov 1, 2018
1 parent de5e2a0 commit 455f020
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<!doctype html>
<meta charset=utf8>
<title>CSS attribute name selector case-sensitivity</title>
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<body>
<script>
const xhtmlns = "http://www.w3.org/1999/xhtml";
const svgns = "http://www.w3.org/2000/svg";
const fakens = "http://example.com/i.made.this.up";

const xhtml_doc = document.implementation.createDocument(xhtmlns, "html", null);
const svg_doc = document.implementation.createDocument(svgns, "svg", null);

function pretty_print_name(ns, ln) {
if (ns === xhtmlns)
return "xhtml:" + ln;
if (ns === svgns)
return "svg:" + ln;
if (ns === fakens)
return "fakens:" + ln;

return ns + ":" + ln;
}

function format_element(el) {
return pretty_print_name(el.namespaceURI, el.localName);
}

function get_strings(s) {
switch(s) {
case "lower":
return ["a", "tspan", "madeupelement", "a\u0301"];
case "upper":
return ["A", "TSPAN", "MADEUPELEMENT", "A\u0301"];
case "lower-nonascii":
return ["\u00e5", "\u00e9"];
case "upper-nonascii":
return ["\u00c5", "\u00c9"];
}
}

function get_test_pair(local, selector) {
let locals = get_strings(local);
let selectors = get_strings(selector);

let rv = [];
for (let i = 0; i < locals.length; i++)
rv.push([locals[i], selectors[i]]);

return rv;
}

for (let [doc, isHTML] of [[document, true], [xhtml_doc, false], [svg_doc, false]]) {
let doc_name = format_element(doc.documentElement) + " (" + (isHTML ? "HTML" : "XML") + ")";

let tests = [
{
local: "lower",
selector: "lower",
matches: true,
},
{
local: "lower",
selector: "upper",
matches: false,
matchesHTML: true,
},
{
local: "upper",
selector: "lower",
matches: false,
},
{
local: "upper",
selector: "upper",
matches: true,
matchesHTML: false,
},
{
local: "lower-nonascii",
selector: "lower-nonascii",
matches: true,
},
{
local: "lower-nonascii",
selector: "upper-nonascii",
matches: false,
},
{
local: "upper-nonascii",
selector: "lower-nonascii",
matches: false,
},
{
local: "upper-nonascii",
selector: "upper-nonascii",
matches: true,
},
];

for (let t of tests) {
for (let element_ns of [null, xhtmlns, svgns, fakens]) {
for (let ns of [null, xhtmlns, svgns, fakens]) {
for (let [local, attr_selector] of get_test_pair(t.local, t.selector)) {
selector = "span[" + attr_selector + "]";
test(function() {
let el = doc.createElementNS(element_ns, "span");
doc.documentElement.appendChild(el);
this.add_cleanup(function() { el.remove(); });
el.setAttributeNS(ns, local, "foobar");
let expect_match;
if (ns !== null)
expect_match = false;
else if (element_ns === xhtmlns)
expect_match = (isHTML && "matchesHTML" in t ? t.matchesHTML : t.matches);
else
expect_match = attr_selector === local;
assert_equals(doc.querySelector(selector), expect_match ? el : null);
}, "match " + selector + " with " + pretty_print_name(ns, local) + " attribute on " + pretty_print_name(element_ns, "span") + " element in " + doc_name);
}
}
}
}
}
</script>
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<!doctype html>
<meta charset=utf8>
<title>CSS element/attribute name selector case-sensitivity</title>
<title>CSS element selector case-sensitivity</title>
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<body>
<script>
const xhtmlns = "http://www.w3.org/1999/xhtml";
const svgns = "http://www.w3.org/2000/svg";
const fakens = "http://web-platform.test/i.made.this.up";
const fakens = "http://example.com/i.made.this.up";

const xhtml_doc = document.implementation.createDocument(xhtmlns, "html", null);
const svg_doc = document.implementation.createDocument(svgns, "svg", null);
Expand Down Expand Up @@ -102,25 +102,13 @@
for (let t of tests) {
for (let ns of [null, xhtmlns, svgns, fakens]) {
for (let [local, selector] of get_test_pair(t.local, t.selector)) {
// element name
test(function() {
let el = doc.createElementNS(ns, local);
doc.documentElement.appendChild(el);
this.add_cleanup(function() { el.remove(); });
let expect_match = (isHTML && ns === xhtmlns && "matchesHTML" in t ? t.matchesHTML : t.matches);
assert_equals(doc.querySelector(selector), expect_match ? el : null);
}, "match " + selector + " with " + pretty_print_name(ns, local) + " in " + doc_name);

// attribute name
selector = "span[" + selector + "]";
test(function() {
let el = doc.createElementNS(xhtmlns, "span");
doc.documentElement.appendChild(el);
this.add_cleanup(function() { el.remove(); });
el.setAttributeNS(ns, local, "foobar");
let expect_match = ns === null && (isHTML && "matchesHTML" in t ? t.matchesHTML : t.matches);
assert_equals(doc.querySelector(selector), expect_match ? el : null);
}, "match " + selector + " with " + pretty_print_name(ns, local) + " attribute in " + doc_name);
}
}
}
Expand Down

0 comments on commit 455f020

Please sign in to comment.