OLD | NEW |
(Empty) | |
| 1 <!DOCTYPE html> |
| 2 <title>Custom Elements: Create an element when definition is non-null and synchr
onous flag set</title> |
| 3 <script src="../../resources/testharness.js"></script> |
| 4 <script src="../../resources/testharnessreport.js"></script> |
| 5 <script src="resources/custom-elements-helpers.js"></script> |
| 6 <body> |
| 7 <script> |
| 8 'use strict'; |
| 9 |
| 10 function assert_rethrown(func, description) { |
| 11 assert_throws({ name: 'rethrown' }, () => { |
| 12 const err = new Error('check this is rethrown'); |
| 13 err.name = 'rethrown'; |
| 14 func(err); |
| 15 }, description); |
| 16 } |
| 17 |
| 18 const expectTypeError = TypeError.prototype; |
| 19 const expectNotSupportedError = 'NOT_SUPPORTED_ERR'; |
| 20 |
| 21 // https://dom.spec.whatwg.org/#concept-create-element |
| 22 // 6. If definition is non-null, then: |
| 23 // 6.1. If the synchronous custom elements flag is set: |
| 24 |
| 25 test_create_element_synchronous( |
| 26 'createElement(): ', |
| 27 (w, constructor, options) => { |
| 28 w.customElements.define('a-a', constructor, options); |
| 29 return w.document.createElement('a-a'); |
| 30 }); |
| 31 |
| 32 test_create_element_synchronous( |
| 33 'createElementNS(): ', |
| 34 (w, constructor, options) => { |
| 35 w.customElements.define('a-a', constructor, options); |
| 36 return w.document.createElementNS('http://www.w3.org/1999/xhtml', 'a-a'); |
| 37 }); |
| 38 |
| 39 function test_create_element_synchronous(description, define_and_create_element)
{ |
| 40 test_with_window((w) => { |
| 41 let is_constructed = false; |
| 42 define_and_create_element(w, class extends w.HTMLElement { |
| 43 constructor() { super(); is_constructed = true; } |
| 44 }); |
| 45 assert_true(is_constructed, 'custom constructor ran'); |
| 46 }, `${description}Pre-flight check should succeed`); |
| 47 |
| 48 test_with_window((w) => { |
| 49 assert_rethrown(err => { |
| 50 define_and_create_element(w, class extends w.HTMLElement { |
| 51 constructor() { super(); throw err; } |
| 52 }); |
| 53 }); |
| 54 }, `${description}6.1.2. Errors in Construct(C) should be rethrown`); |
| 55 |
| 56 test_with_window((w) => { |
| 57 assert_throws(expectTypeError, () => { |
| 58 define_and_create_element(w, class { |
| 59 constructor() {} |
| 60 }); |
| 61 }); |
| 62 }, `${description}6.1.3. If result does not implement the HTMLElement interfac
e, throw a TypeError`); |
| 63 |
| 64 test_with_window((w) => { |
| 65 assert_throws(expectNotSupportedError, () => { |
| 66 define_and_create_element(w, class extends w.HTMLElement { |
| 67 constructor() { super(); this.setAttribute('a', 'a'); } |
| 68 }); |
| 69 }); |
| 70 }, `${description}6.1.4. If result\'s attribute list is not empty, then throw
a NotSupportedError`); |
| 71 |
| 72 test_with_window((w) => { |
| 73 assert_throws(expectNotSupportedError, () => { |
| 74 define_and_create_element(w, class extends w.HTMLElement { |
| 75 constructor() { super(); this.appendChild(w.document.createElement('a'))
; } |
| 76 }); |
| 77 }, 'should throw if it has a child element'); |
| 78 assert_throws(expectNotSupportedError, () => { |
| 79 define_and_create_element(w, class extends w.HTMLElement { |
| 80 constructor() { super(); this.appendChild(w.document.createTextNode('a')
); } |
| 81 }); |
| 82 }, 'should throw if it has a child text node'); |
| 83 }, `${description}6.1.5. If result has children, then throw a NotSupportedErro
r`); |
| 84 |
| 85 test_with_window((w) => { |
| 86 assert_throws(expectNotSupportedError, () => { |
| 87 define_and_create_element(w, class extends w.HTMLElement { |
| 88 constructor() { super(); w.document.createElement('div').appendChild(thi
s); } |
| 89 }); |
| 90 }); |
| 91 }, `${description}6.1.6. If result\'s parent is not null, then throw a NotSupp
ortedError`); |
| 92 |
| 93 test_with_window((w) => { |
| 94 assert_throws(expectNotSupportedError, () => { |
| 95 define_and_create_element(w, class extends w.HTMLElement { |
| 96 constructor() { super(); return w.document.implementation.createHTMLDocu
ment().createElement('div'); } |
| 97 }); |
| 98 }); |
| 99 }, `${description}6.1.7. If result\'s node document is not document, then thro
w a NotSupportedError`); |
| 100 |
| 101 /* This is not testsable today, see https://github.com/whatwg/html/issues/1402 |
| 102 test_with_window((w) => { |
| 103 assert_throws(expectNotSupportedError, () => { |
| 104 define_and_create_element(w, class extends w.HTMLElement { |
| 105 constructor() { super(); return w.document.createElementNS('http://www.w
3.org/2000/svg', 'g'); } |
| 106 }); |
| 107 }); |
| 108 }, `${description}6.1.8. If result\'s namespace is not the HTML namespace, the
n throw a NotSupportedError`); |
| 109 */ |
| 110 |
| 111 test_with_window((w) => { |
| 112 assert_throws(expectNotSupportedError, () => { |
| 113 define_and_create_element(w, class extends w.HTMLElement { |
| 114 constructor() { super(); return document.createElement('div'); } |
| 115 }); |
| 116 }); |
| 117 }, `${description}6.1.9. If result\'s local name is not equal to localName, th
en throw a NotSupportedError`); |
| 118 } |
| 119 </script> |
| 120 </body> |
OLD | NEW |