|
| 1 | +<!DOCTYPE html> |
| 2 | +<html> |
| 3 | +<head> |
| 4 | + <script src="/resources/testharness.js"></script> |
| 5 | + <script src="/resources/testharnessreport.js"></script> |
| 6 | +</head> |
| 7 | +<body> |
| 8 | +<script> |
| 9 | + |
| 10 | +let constructor_called = false; |
| 11 | +let connectedCallback_called = false; |
| 12 | + |
| 13 | +class FooBarElement extends HTMLDivElement { |
| 14 | + constructor() { |
| 15 | + super(); |
| 16 | + constructor_called = true; |
| 17 | + } |
| 18 | + |
| 19 | + connectedCallback() { |
| 20 | + connectedCallback_called = true; |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +customElements.define("foo-bar", FooBarElement, { extends: "div" }); |
| 25 | + |
| 26 | +function assert_removed(sanitizer) { |
| 27 | + constructor_called = connectedCallback_called = false; |
| 28 | + |
| 29 | + let d = document.createElement("div"); |
| 30 | + document.body.append(d); |
| 31 | + d.setHTML(`<div is="foo-bar">hello</div>`, { sanitizer } ); |
| 32 | + assert_equals(d.innerHTML, `<div>hello</div>`); |
| 33 | + assert_false(d.firstChild.hasAttribute("is")); |
| 34 | + assert_true(d.firstChild.matches(":defined")); |
| 35 | + |
| 36 | + assert_false(constructor_called); |
| 37 | + assert_false(connectedCallback_called); |
| 38 | + |
| 39 | + d.remove(); |
| 40 | +} |
| 41 | + |
| 42 | +test(t => { |
| 43 | + assert_removed("default"); |
| 44 | +}, "The is= attribute is removed by the default sanitizer config."); |
| 45 | + |
| 46 | +test(t => { |
| 47 | + assert_removed({ removeAttributes: ["is"] }); |
| 48 | +}, "The is= attribute is removed by the global removeAttributes"); |
| 49 | + |
| 50 | +test(t => { |
| 51 | + assert_removed({ elements: [{ name: "div", removeAttributes: ["is"] }], removeAttributes: [] }); |
| 52 | +}, "The is= attribute is removed by the local removeAttributes"); |
| 53 | + |
| 54 | +test(t => { |
| 55 | + assert_removed({ elements: [{ name: "div", attributes: [] }], attributes: [] }); |
| 56 | +}, "The is= attribute is removed by the local missing attributes"); |
| 57 | + |
| 58 | +function assert_kept(sanitizer) { |
| 59 | + constructor_called = connectedCallback_called = false; |
| 60 | + |
| 61 | + let d = document.createElement("div"); |
| 62 | + document.body.append(d); |
| 63 | + d.setHTML(`<div is="foo-bar">hello</div>`, { sanitizer }); |
| 64 | + assert_equals(d.innerHTML, `<div is="foo-bar">hello</div>`); |
| 65 | + assert_true(d.firstChild.hasAttribute("is")); |
| 66 | + assert_true(d.firstChild.matches(":defined")); |
| 67 | + |
| 68 | + // "is" is re-created even after removing the attribute. |
| 69 | + d.firstChild.removeAttribute("is"); |
| 70 | + assert_equals(d.innerHTML, `<div is="foo-bar">hello</div>`); |
| 71 | + |
| 72 | + assert_true(constructor_called); |
| 73 | + assert_true(connectedCallback_called); |
| 74 | + |
| 75 | + d.remove(); |
| 76 | +} |
| 77 | + |
| 78 | +test(t => { |
| 79 | + assert_kept({ }) |
| 80 | +}, "The is= attribute is kept when allowed with an empty config."); |
| 81 | + |
| 82 | +test(t => { |
| 83 | + assert_kept({ attributes: ["is"] }) |
| 84 | +}, "The is= attribute is kept when allowed by global attributes."); |
| 85 | + |
| 86 | +test(t => { |
| 87 | + assert_kept({ removeAttributes: [] }) |
| 88 | +}, "The is= attribute is kept when allowed by global removeAttributes."); |
| 89 | + |
| 90 | +test(t => { |
| 91 | + assert_kept({ elements: [{ name: "div", attributes: ["is"] }], attributes: [] }); |
| 92 | +}, "The is= attribute is kept when allowed by local attributes."); |
| 93 | +</script> |
| 94 | +</body> |
| 95 | +</html> |
0 commit comments