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