Source code

Revision control

Copy as Markdown

Other Tools

<!DOCTYPE html>
<html>
<head>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
</head>
<body>
<script>
let constructor_called = false;
let connectedCallback_called = false;
class FooBarElement extends HTMLDivElement {
constructor() {
super();
constructor_called = true;
}
connectedCallback() {
connectedCallback_called = true;
}
}
customElements.define("foo-bar", FooBarElement, { extends: "div" });
function assert_removed(sanitizer) {
constructor_called = connectedCallback_called = false;
let d = document.createElement("div");
document.body.append(d);
d.setHTML(`<div is="foo-bar">hello</div>`, { sanitizer } );
assert_equals(d.innerHTML, `<div>hello</div>`);
assert_false(d.firstChild.hasAttribute("is"));
assert_true(d.firstChild.matches(":defined"));
assert_false(constructor_called);
assert_false(connectedCallback_called);
d.remove();
}
test(t => {
assert_removed("default");
}, "The is= attribute is removed by the default sanitizer config.");
test(t => {
assert_removed({ removeAttributes: ["is"] });
}, "The is= attribute is removed by the global removeAttributes");
test(t => {
assert_removed({ elements: [{ name: "div", removeAttributes: ["is"] }], removeAttributes: [] });
}, "The is= attribute is removed by the local removeAttributes");
test(t => {
assert_removed({ elements: [{ name: "div", attributes: [] }], attributes: [] });
}, "The is= attribute is removed by the local missing attributes");
function assert_kept(sanitizer) {
constructor_called = connectedCallback_called = false;
let d = document.createElement("div");
document.body.append(d);
d.setHTML(`<div is="foo-bar">hello</div>`, { sanitizer });
assert_equals(d.innerHTML, `<div is="foo-bar">hello</div>`);
assert_true(d.firstChild.hasAttribute("is"));
assert_true(d.firstChild.matches(":defined"));
// "is" is re-created even after removing the attribute.
d.firstChild.removeAttribute("is");
assert_equals(d.innerHTML, `<div is="foo-bar">hello</div>`);
assert_true(constructor_called);
assert_true(connectedCallback_called);
d.remove();
}
test(t => {
assert_kept({ })
}, "The is= attribute is kept when allowed with an empty config.");
test(t => {
assert_kept({ attributes: ["is"] })
}, "The is= attribute is kept when allowed by global attributes.");
test(t => {
assert_kept({ removeAttributes: [] })
}, "The is= attribute is kept when allowed by global removeAttributes.");
test(t => {
assert_kept({ elements: [{ name: "div", attributes: ["is"] }], attributes: [] });
}, "The is= attribute is kept when allowed by local attributes.");
function assert_removed_parseHTML(sanitizer, element) {
constructor_called = connectedCallback_called = false;
// To have meaningful test results with parseHTML, we need to always allow
// "html" and "body". But since those might also be in the config, we need
// to be a bit careful in when to allow them.
if (sanitizer && sanitizer.elements && sanitizer.elements[0].name != "html")
sanitizer.elements.push("html");
if (sanitizer && sanitizer.elements && sanitizer.elements[0].name != "body")
sanitizer.elements.push("body");
const doc = Document.parseHTML(`<${element} is="foo-bar">hello</${element}>`, { sanitizer } );
assert_equals(doc.body.innerText, `hello`);
assert_equals(doc.getElementsByTagName(element).length, 1);
const parsed_element = doc.getElementsByTagName(element)[0];
assert_false(parsed_element.hasAttribute("is"));
assert_true(parsed_element.matches(":defined"));
assert_false(constructor_called);
assert_false(connectedCallback_called);
}
for (element of ["html", "body", "div"]) {
test(t => {
assert_removed_parseHTML("default", element);
}, `parseHTML removes ${element} is= attribute with the default sanitizer config.`);
test(t => {
assert_removed_parseHTML({ removeAttributes: ["is"] }, element);
}, `parseHTML removes ${element} is= attribute with global removeAttributes`);
test(t => {
assert_removed_parseHTML({ elements: [{ name: element, removeAttributes: ["is"] }], removeAttributes: [] }, element);
}, `parseHTML removes ${element} is= attribute with local removeAttributes`);
test(t => {
assert_removed_parseHTML({ elements: [{ name: element, attributes: [] }], attributes: [] }, element);
}, `parseHTML removes ${element} is= attribute with local allow attributes`);
}
</script>
</body>
</html>