Document-createElementNS.html (5106B)
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <link rel="help" href="https://github.com/whatwg/html/issues/10854"> 5 <script src="/resources/testharness.js"></script> 6 <script src="/resources/testharnessreport.js"></script> 7 </head> 8 <body> 9 <script> 10 // Keep this ~synchronized with Document-createElement 11 "use strict"; 12 13 const scopedRegistry = new CustomElementRegistry(); 14 const otherScopedRegistry = new CustomElementRegistry(); 15 class GlobalABElement extends HTMLElement {}; 16 class ScopedABElement extends HTMLElement {}; 17 customElements.define('a-b', GlobalABElement); 18 scopedRegistry.define('a-b', ScopedABElement); 19 20 test(() => { 21 assert_true(document.createElementNS('http://www.w3.org/1999/xhtml', 'a-b') instanceof GlobalABElement); 22 }, 'createElementNS should use the global registry by default'); 23 24 test(() => { 25 assert_true(document.createElementNS('http://www.w3.org/1999/xhtml', 'a-b', {customElementRegistry: scopedRegistry}) instanceof ScopedABElement); 26 }, 'createElementNS should use the specified scoped registry'); 27 28 test(() => { 29 const elements = { 30 div: HTMLDivElement, 31 form: HTMLFormElement, 32 span: HTMLSpanElement, 33 table: HTMLTableElement, 34 unknown: HTMLUnknownElement, 35 }; 36 for (const localName in elements) { 37 const scopedElement = document.createElementNS('http://www.w3.org/1999/xhtml', localName, {customElementRegistry: scopedRegistry}); 38 assert_true(scopedElement instanceof elements[localName], localName); 39 assert_equals(scopedElement.customElementRegistry, scopedRegistry); 40 41 const globalExplicitElement = document.createElementNS('http://www.w3.org/1999/xhtml', localName, {customElementRegistry: window.customElements}); 42 assert_true(globalExplicitElement instanceof elements[localName], localName); 43 assert_equals(globalExplicitElement.customElementRegistry, window.customElements); 44 45 const globalImplicitElement = document.createElementNS('http://www.w3.org/1999/xhtml', localName); 46 assert_true(globalImplicitElement instanceof elements[localName], localName); 47 assert_equals(globalImplicitElement.customElementRegistry, window.customElements); 48 } 49 }, 'createElementNS should create a builtin element regardless of a custom element registry specified'); 50 51 test(() => { 52 assert_true(document.createElementNS('http://www.w3.org/1999/xhtml', 'a-b', {customElementRegistry: window.customElements}) instanceof GlobalABElement); 53 }, 'createElementNS should use the specified global registry'); 54 55 test(() => { 56 const element = document.createElementNS('http://www.w3.org/1999/xhtml', 'a-b', {customElementRegistry: otherScopedRegistry}); 57 assert_equals(element.__proto__.constructor.name, 'HTMLElement'); 58 }, 'createElementNS should create an upgrade candidate when there is no matching definition in the specified registry'); 59 60 test((t) => { 61 assert_equals(document.createElementNS('http://www.w3.org/1999/xhtml', 'div', {customElementRegistry: null}).customElementRegistry, null); 62 }, `document.createElement should create a builtin element with null registry if customElement is set to null`); 63 64 test((t) => { 65 assert_equals(document.createElementNS('http://www.w3.org/1999/xhtml', 'c-d', {customElementRegistry: null}).customElementRegistry, null); 66 }, `document.createElement should create a custom element candidate with null registry if customElement is set to null`); 67 68 test((t) => { 69 assert_equals(document.createElementNS('http://www.w3.org/1999/xhtml', 'a-b', {customElementRegistry: null}).customElementRegistry, null); 70 }, `document.createElement should create a defined custom element with null registry if customElement is set to null`); 71 72 test(() => { 73 class CDElement extends HTMLElement { } 74 const registry = new CustomElementRegistry; 75 const cdElement = document.createElementNS('http://www.w3.org/1999/xhtml', 'c-d', {customElementRegistry: registry}); 76 assert_false(cdElement instanceof CDElement); 77 assert_equals(cdElement.__proto__.constructor.name, 'HTMLElement'); 78 registry.define('c-d', CDElement); 79 assert_false(cdElement instanceof CDElement); // Not yet upgraded since it's disconnected. 80 assert_equals(cdElement.__proto__.constructor.name, 'HTMLElement'); 81 document.body.appendChild(cdElement); 82 assert_true(cdElement instanceof CDElement); 83 }, 'createElementNS should create an upgrade candidate and the candidate should be upgraded when the element is defined'); 84 85 test(() => { 86 const doc = new Document(); 87 const scopedElement = doc.createElementNS(null, "time", {customElementRegistry: scopedRegistry}); 88 assert_equals(scopedElement.namespaceURI, null); 89 assert_equals(scopedElement.customElementRegistry, scopedRegistry); 90 91 const abElement = doc.createElementNS(null, "a-b", {customElementRegistry: scopedRegistry}); 92 assert_equals(abElement.namespaceURI, null); 93 assert_equals(abElement.customElementRegistry, scopedRegistry); 94 assert_false(abElement instanceof ScopedABElement); 95 }, 'createElementNS on a non-HTML document should still handle registries correctly'); 96 97 </script> 98 </body> 99 </html>