| Ryosuke Niwa | e09776a | 2016-09-29 07:14:40 | [diff] [blame] | 1 | <!DOCTYPE html> |
| 2 | <html> |
| 3 | <head> |
| 4 | <title>Custom Elements: attributeChangedCallback</title> |
| 5 | <meta name="author" title="Ryosuke Niwa" href="mailto:rniwa@webkit.org"> |
| 6 | <meta name="assert" content="attributeChangedCallback must be enqueued whenever custom element's attribute is added, changed or removed"> |
| 7 | <link rel="help" href="https://w3c.github.io/webcomponents/spec/custom/#dfn-attribute-changed-callback"> |
| 8 | <script src="/resources/testharness.js"></script> |
| 9 | <script src="/resources/testharnessreport.js"></script> |
| Ryosuke Niwa | c7fbe2d | 2016-10-06 06:19:47 | [diff] [blame] | 10 | <script src="resources/custom-elements-helpers.js"></script> |
| Ryosuke Niwa | e09776a | 2016-09-29 07:14:40 | [diff] [blame] | 11 | </head> |
| 12 | <body> |
| 13 | <div id="log"></div> |
| Edgar Chen | d4888ba | 2018-01-23 15:27:55 | [diff] [blame^] | 14 | <parser-created-element title></parser-created-element> |
| Ryosuke Niwa | e09776a | 2016-09-29 07:14:40 | [diff] [blame] | 15 | <script> |
| 16 | |
| Ryosuke Niwa | c7fbe2d | 2016-10-06 06:19:47 | [diff] [blame] | 17 | var customElement = define_new_custom_element(['title', 'id', 'r']); |
| Ryosuke Niwa | e09776a | 2016-09-29 07:14:40 | [diff] [blame] | 18 | |
| 19 | test(function () { |
| Ryosuke Niwa | c7fbe2d | 2016-10-06 06:19:47 | [diff] [blame] | 20 | const instance = document.createElement(customElement.name); |
| 21 | assert_array_equals(customElement.takeLog().types(), ['constructed']); |
| Ryosuke Niwa | e09776a | 2016-09-29 07:14:40 | [diff] [blame] | 22 | |
| 23 | instance.setAttribute('title', 'foo'); |
| 24 | assert_equals(instance.getAttribute('title'), 'foo'); |
| Ryosuke Niwa | c7fbe2d | 2016-10-06 06:19:47 | [diff] [blame] | 25 | var logEntries = customElement.takeLog(); |
| 26 | assert_array_equals(logEntries.types(), ['attributeChanged']); |
| 27 | assert_attribute_log_entry(logEntries.last(), {name: 'title', oldValue: null, newValue: 'foo', namespace: null}); |
| Ryosuke Niwa | e09776a | 2016-09-29 07:14:40 | [diff] [blame] | 28 | |
| 29 | instance.removeAttribute('title'); |
| 30 | assert_equals(instance.getAttribute('title'), null); |
| Ryosuke Niwa | c7fbe2d | 2016-10-06 06:19:47 | [diff] [blame] | 31 | var logEntries = customElement.takeLog(); |
| 32 | assert_array_equals(logEntries.types(), ['attributeChanged']); |
| 33 | assert_attribute_log_entry(logEntries.last(), {name: 'title', oldValue: 'foo', newValue: null, namespace: null}); |
| Ryosuke Niwa | e09776a | 2016-09-29 07:14:40 | [diff] [blame] | 34 | }, 'setAttribute and removeAttribute must enqueue and invoke attributeChangedCallback'); |
| 35 | |
| 36 | test(function () { |
| Ryosuke Niwa | c7fbe2d | 2016-10-06 06:19:47 | [diff] [blame] | 37 | var instance = document.createElement(customElement.name); |
| 38 | assert_array_equals(customElement.takeLog().types(), ['constructed']); |
| Ryosuke Niwa | e09776a | 2016-09-29 07:14:40 | [diff] [blame] | 39 | |
| 40 | instance.setAttributeNS('http://www.w3.org/2000/svg', 'title', 'hello'); |
| 41 | assert_equals(instance.getAttribute('title'), 'hello'); |
| Ryosuke Niwa | c7fbe2d | 2016-10-06 06:19:47 | [diff] [blame] | 42 | var logEntries = customElement.takeLog(); |
| 43 | assert_array_equals(logEntries.types(), ['attributeChanged']); |
| 44 | assert_attribute_log_entry(logEntries.last(), {name: 'title', oldValue: null, newValue: 'hello', namespace: 'http://www.w3.org/2000/svg'}); |
| Ryosuke Niwa | e09776a | 2016-09-29 07:14:40 | [diff] [blame] | 45 | |
| 46 | instance.removeAttributeNS('http://www.w3.org/2000/svg', 'title'); |
| 47 | assert_equals(instance.getAttribute('title'), null); |
| Ryosuke Niwa | c7fbe2d | 2016-10-06 06:19:47 | [diff] [blame] | 48 | var logEntries = customElement.takeLog(); |
| 49 | assert_array_equals(logEntries.types(), ['attributeChanged']); |
| 50 | assert_attribute_log_entry(logEntries.last(), {name: 'title', oldValue: 'hello', newValue: null, namespace: 'http://www.w3.org/2000/svg'}); |
| Ryosuke Niwa | e09776a | 2016-09-29 07:14:40 | [diff] [blame] | 51 | }, 'setAttributeNS and removeAttributeNS must enqueue and invoke attributeChangedCallback'); |
| 52 | |
| 53 | test(function () { |
| Ryosuke Niwa | c7fbe2d | 2016-10-06 06:19:47 | [diff] [blame] | 54 | var instance = document.createElement(customElement.name); |
| 55 | assert_array_equals(customElement.takeLog().types(), ['constructed']); |
| Ryosuke Niwa | e09776a | 2016-09-29 07:14:40 | [diff] [blame] | 56 | |
| 57 | var attr = document.createAttribute('id'); |
| 58 | attr.value = 'bar'; |
| 59 | instance.setAttributeNode(attr); |
| 60 | |
| 61 | assert_equals(instance.getAttribute('id'), 'bar'); |
| Ryosuke Niwa | c7fbe2d | 2016-10-06 06:19:47 | [diff] [blame] | 62 | var logEntries = customElement.takeLog(); |
| 63 | assert_array_equals(logEntries.types(), ['attributeChanged']); |
| 64 | assert_attribute_log_entry(logEntries.last(), {name: 'id', oldValue: null, newValue: 'bar', namespace: null}); |
| Ryosuke Niwa | e09776a | 2016-09-29 07:14:40 | [diff] [blame] | 65 | |
| 66 | instance.removeAttributeNode(attr); |
| 67 | assert_equals(instance.getAttribute('id'), null); |
| Ryosuke Niwa | c7fbe2d | 2016-10-06 06:19:47 | [diff] [blame] | 68 | var logEntries = customElement.takeLog(); |
| 69 | assert_array_equals(logEntries.types(), ['attributeChanged']); |
| 70 | assert_attribute_log_entry(logEntries.last(), {name: 'id', oldValue: 'bar', newValue: null, namespace: null}); |
| 71 | }, 'setAttributeNode and removeAttributeNode must enqueue and invoke attributeChangedCallback for an HTML attribute'); |
| Ryosuke Niwa | e09776a | 2016-09-29 07:14:40 | [diff] [blame] | 72 | |
| 73 | test(function () { |
| Ryosuke Niwa | c7fbe2d | 2016-10-06 06:19:47 | [diff] [blame] | 74 | const instance = document.createElement(customElement.name); |
| 75 | assert_array_equals(customElement.takeLog().types(), ['constructed']); |
| Ryosuke Niwa | e09776a | 2016-09-29 07:14:40 | [diff] [blame] | 76 | |
| Ryosuke Niwa | c7fbe2d | 2016-10-06 06:19:47 | [diff] [blame] | 77 | const attr = document.createAttributeNS('http://www.w3.org/2000/svg', 'r'); |
| Ryosuke Niwa | e09776a | 2016-09-29 07:14:40 | [diff] [blame] | 78 | attr.value = '100'; |
| 79 | instance.setAttributeNode(attr); |
| 80 | |
| 81 | assert_equals(instance.getAttribute('r'), '100'); |
| Ryosuke Niwa | c7fbe2d | 2016-10-06 06:19:47 | [diff] [blame] | 82 | var logEntries = customElement.takeLog(); |
| 83 | assert_array_equals(logEntries.types(), ['attributeChanged']); |
| 84 | assert_attribute_log_entry(logEntries.last(), {name: 'r', oldValue: null, newValue: '100', namespace: 'http://www.w3.org/2000/svg'}); |
| Ryosuke Niwa | e09776a | 2016-09-29 07:14:40 | [diff] [blame] | 85 | |
| 86 | instance.removeAttributeNode(attr); |
| 87 | assert_equals(instance.getAttribute('r'), null); |
| Ryosuke Niwa | c7fbe2d | 2016-10-06 06:19:47 | [diff] [blame] | 88 | var logEntries = customElement.takeLog(); |
| 89 | assert_array_equals(logEntries.types(), ['attributeChanged']); |
| 90 | assert_attribute_log_entry(logEntries.last(), {name: 'r', oldValue: '100', newValue: null, namespace: 'http://www.w3.org/2000/svg'}); |
| 91 | }, 'setAttributeNode and removeAttributeNS must enqueue and invoke attributeChangedCallback for an SVG attribute'); |
| Ryosuke Niwa | e09776a | 2016-09-29 07:14:40 | [diff] [blame] | 92 | |
| 93 | test(function () { |
| Ryosuke Niwa | c7fbe2d | 2016-10-06 06:19:47 | [diff] [blame] | 94 | const callsToOld = []; |
| 95 | const callsToNew = []; |
| Ryosuke Niwa | e09776a | 2016-09-29 07:14:40 | [diff] [blame] | 96 | class CustomElement extends HTMLElement { } |
| Ryosuke Niwa | c7fbe2d | 2016-10-06 06:19:47 | [diff] [blame] | 97 | CustomElement.prototype.attributeChangedCallback = function (...args) { |
| 98 | callsToOld.push(create_attribute_changed_callback_log(this, ...args)); |
| Ryosuke Niwa | e09776a | 2016-09-29 07:14:40 | [diff] [blame] | 99 | } |
| 100 | CustomElement.observedAttributes = ['title']; |
| 101 | customElements.define('element-with-mutated-attribute-changed-callback', CustomElement); |
| Ryosuke Niwa | c7fbe2d | 2016-10-06 06:19:47 | [diff] [blame] | 102 | CustomElement.prototype.attributeChangedCallback = function (...args) { |
| 103 | callsToNew.push(create_attribute_changed_callback_log(this, ...args)); |
| Ryosuke Niwa | e09776a | 2016-09-29 07:14:40 | [diff] [blame] | 104 | } |
| 105 | |
| Ryosuke Niwa | c7fbe2d | 2016-10-06 06:19:47 | [diff] [blame] | 106 | const instance = document.createElement('element-with-mutated-attribute-changed-callback'); |
| Ryosuke Niwa | e09776a | 2016-09-29 07:14:40 | [diff] [blame] | 107 | instance.setAttribute('title', 'hi'); |
| 108 | assert_equals(instance.getAttribute('title'), 'hi'); |
| 109 | assert_array_equals(callsToNew, []); |
| 110 | assert_equals(callsToOld.length, 1); |
| Ryosuke Niwa | c7fbe2d | 2016-10-06 06:19:47 | [diff] [blame] | 111 | assert_attribute_log_entry(callsToOld[0], {name: 'title', oldValue: null, newValue: 'hi', namespace: null}); |
| Ryosuke Niwa | e09776a | 2016-09-29 07:14:40 | [diff] [blame] | 112 | }, 'Mutating attributeChangedCallback after calling customElements.define must not affect the callback being invoked'); |
| 113 | |
| 114 | test(function () { |
| Ryosuke Niwa | c7fbe2d | 2016-10-06 06:19:47 | [diff] [blame] | 115 | const calls = []; |
| Ryosuke Niwa | e09776a | 2016-09-29 07:14:40 | [diff] [blame] | 116 | class CustomElement extends HTMLElement { |
| Ryosuke Niwa | c7fbe2d | 2016-10-06 06:19:47 | [diff] [blame] | 117 | attributeChangedCallback(...args) { |
| 118 | calls.push(create_attribute_changed_callback_log(this, ...args)); |
| Ryosuke Niwa | e09776a | 2016-09-29 07:14:40 | [diff] [blame] | 119 | } |
| 120 | } |
| 121 | CustomElement.observedAttributes = ['title']; |
| 122 | customElements.define('element-not-observing-id-attribute', CustomElement); |
| 123 | |
| Ryosuke Niwa | c7fbe2d | 2016-10-06 06:19:47 | [diff] [blame] | 124 | const instance = document.createElement('element-not-observing-id-attribute'); |
| 125 | assert_equals(calls.length, 0); |
| Ryosuke Niwa | e09776a | 2016-09-29 07:14:40 | [diff] [blame] | 126 | instance.setAttribute('title', 'hi'); |
| 127 | assert_equals(calls.length, 1); |
| Ryosuke Niwa | c7fbe2d | 2016-10-06 06:19:47 | [diff] [blame] | 128 | assert_attribute_log_entry(calls[0], {name: 'title', oldValue: null, newValue: 'hi', namespace: null}); |
| Ryosuke Niwa | e09776a | 2016-09-29 07:14:40 | [diff] [blame] | 129 | instance.setAttribute('id', 'some'); |
| 130 | assert_equals(calls.length, 1); |
| Ryosuke Niwa | c7fbe2d | 2016-10-06 06:19:47 | [diff] [blame] | 131 | assert_attribute_log_entry(calls[0], {name: 'title', oldValue: null, newValue: 'hi', namespace: null}); |
| 132 | }, 'attributedChangedCallback must not be invoked when the observed attributes does not contain the attribute'); |
| Ryosuke Niwa | e09776a | 2016-09-29 07:14:40 | [diff] [blame] | 133 | |
| 134 | test(function () { |
| Ryosuke Niwa | c7fbe2d | 2016-10-06 06:19:47 | [diff] [blame] | 135 | const calls = []; |
| Ryosuke Niwa | e09776a | 2016-09-29 07:14:40 | [diff] [blame] | 136 | class CustomElement extends HTMLElement { } |
| Ryosuke Niwa | c7fbe2d | 2016-10-06 06:19:47 | [diff] [blame] | 137 | CustomElement.prototype.attributeChangedCallback = function (...args) { |
| 138 | calls.push(create_attribute_changed_callback_log(this, ...args)); |
| Ryosuke Niwa | e09776a | 2016-09-29 07:14:40 | [diff] [blame] | 139 | } |
| 140 | CustomElement.observedAttributes = ['title', 'lang']; |
| 141 | customElements.define('element-with-mutated-observed-attributes', CustomElement); |
| 142 | CustomElement.observedAttributes = ['title', 'id']; |
| 143 | |
| Ryosuke Niwa | c7fbe2d | 2016-10-06 06:19:47 | [diff] [blame] | 144 | const instance = document.createElement('element-with-mutated-observed-attributes'); |
| Ryosuke Niwa | e09776a | 2016-09-29 07:14:40 | [diff] [blame] | 145 | instance.setAttribute('title', 'hi'); |
| 146 | assert_equals(calls.length, 1); |
| Ryosuke Niwa | c7fbe2d | 2016-10-06 06:19:47 | [diff] [blame] | 147 | assert_attribute_log_entry(calls[0], {name: 'title', oldValue: null, newValue: 'hi', namespace: null}); |
| Ryosuke Niwa | e09776a | 2016-09-29 07:14:40 | [diff] [blame] | 148 | |
| 149 | instance.setAttribute('id', 'some'); |
| 150 | assert_equals(calls.length, 1); |
| 151 | |
| 152 | instance.setAttribute('lang', 'en'); |
| 153 | assert_equals(calls.length, 2); |
| Ryosuke Niwa | c7fbe2d | 2016-10-06 06:19:47 | [diff] [blame] | 154 | assert_attribute_log_entry(calls[1], {name: 'lang', oldValue: null, newValue: 'en', namespace: null}); |
| Ryosuke Niwa | e09776a | 2016-09-29 07:14:40 | [diff] [blame] | 155 | }, 'Mutating observedAttributes after calling customElements.define must not affect the set of attributes for which attributedChangedCallback is invoked'); |
| 156 | |
| 157 | test(function () { |
| 158 | var calls = []; |
| 159 | class CustomElement extends HTMLElement { } |
| Ryosuke Niwa | c7fbe2d | 2016-10-06 06:19:47 | [diff] [blame] | 160 | CustomElement.prototype.attributeChangedCallback = function (...args) { |
| 161 | calls.push(create_attribute_changed_callback_log(this, ...args)); |
| Ryosuke Niwa | e09776a | 2016-09-29 07:14:40 | [diff] [blame] | 162 | } |
| 163 | CustomElement.observedAttributes = { [Symbol.iterator]: function *() { yield 'lang'; yield 'style'; } }; |
| 164 | customElements.define('element-with-generator-observed-attributes', CustomElement); |
| 165 | |
| 166 | var instance = document.createElement('element-with-generator-observed-attributes'); |
| 167 | instance.setAttribute('lang', 'en'); |
| 168 | assert_equals(calls.length, 1); |
| Ryosuke Niwa | c7fbe2d | 2016-10-06 06:19:47 | [diff] [blame] | 169 | assert_attribute_log_entry(calls[0], {name: 'lang', oldValue: null, newValue: 'en', namespace: null}); |
| Ryosuke Niwa | e09776a | 2016-09-29 07:14:40 | [diff] [blame] | 170 | |
| 171 | instance.setAttribute('lang', 'ja'); |
| 172 | assert_equals(calls.length, 2); |
| Ryosuke Niwa | c7fbe2d | 2016-10-06 06:19:47 | [diff] [blame] | 173 | assert_attribute_log_entry(calls[1], {name: 'lang', oldValue: 'en', newValue: 'ja', namespace: null}); |
| Ryosuke Niwa | e09776a | 2016-09-29 07:14:40 | [diff] [blame] | 174 | |
| 175 | instance.setAttribute('title', 'hello'); |
| 176 | assert_equals(calls.length, 2); |
| 177 | |
| 178 | instance.setAttribute('style', 'font-size: 2rem'); |
| 179 | assert_equals(calls.length, 3); |
| Ryosuke Niwa | c7fbe2d | 2016-10-06 06:19:47 | [diff] [blame] | 180 | assert_attribute_log_entry(calls[2], {name: 'style', oldValue: null, newValue: 'font-size: 2rem', namespace: null}); |
| Ryosuke Niwa | e09776a | 2016-09-29 07:14:40 | [diff] [blame] | 181 | }, 'attributedChangedCallback must be enqueued for attributes specified in a non-Array iterable observedAttributes'); |
| 182 | |
| Ryosuke Niwa | c7fbe2d | 2016-10-06 06:19:47 | [diff] [blame] | 183 | test(function () { |
| 184 | var calls = []; |
| 185 | class CustomElement extends HTMLElement { } |
| 186 | CustomElement.prototype.attributeChangedCallback = function (...args) { |
| 187 | calls.push(create_attribute_changed_callback_log(this, ...args)); |
| 188 | } |
| 189 | CustomElement.observedAttributes = ['style']; |
| 190 | customElements.define('element-with-style-attribute-observation', CustomElement); |
| 191 | |
| 192 | var instance = document.createElement('element-with-style-attribute-observation'); |
| 193 | assert_equals(calls.length, 0); |
| 194 | |
| 195 | instance.style.fontSize = '10px'; |
| 196 | assert_equals(calls.length, 1); |
| 197 | assert_attribute_log_entry(calls[0], {name: 'style', oldValue: null, newValue: 'font-size: 10px;', namespace: null}); |
| 198 | |
| 199 | instance.style.fontSize = '20px'; |
| 200 | assert_equals(calls.length, 2); |
| 201 | assert_attribute_log_entry(calls[1], {name: 'style', oldValue: 'font-size: 10px;', newValue: 'font-size: 20px;', namespace: null}); |
| 202 | |
| 203 | }, 'attributedChangedCallback must be enqueued for style attribute change by mutating inline style declaration'); |
| 204 | |
| 205 | test(function () { |
| 206 | var calls = []; |
| 207 | class CustomElement extends HTMLElement { } |
| 208 | CustomElement.prototype.attributeChangedCallback = function (...args) { |
| 209 | calls.push(create_attribute_changed_callback_log(this, ...args)); |
| 210 | } |
| 211 | CustomElement.observedAttributes = ['title']; |
| 212 | customElements.define('element-with-no-style-attribute-observation', CustomElement); |
| 213 | |
| 214 | var instance = document.createElement('element-with-no-style-attribute-observation'); |
| 215 | assert_equals(calls.length, 0); |
| 216 | instance.style.fontSize = '10px'; |
| 217 | assert_equals(calls.length, 0); |
| 218 | instance.title = 'hello'; |
| 219 | assert_attribute_log_entry(calls[0], {name: 'title', oldValue: null, newValue: 'hello', namespace: null}); |
| 220 | }, 'attributedChangedCallback must not be enqueued when mutating inline style declaration if the style attribute is not observed'); |
| 221 | |
| Edgar Chen | d4888ba | 2018-01-23 15:27:55 | [diff] [blame^] | 222 | test(function () { |
| 223 | var calls = []; |
| 224 | class CustomElement extends HTMLElement { } |
| 225 | CustomElement.prototype.attributeChangedCallback = function (...args) { |
| 226 | calls.push(create_attribute_changed_callback_log(this, ...args)); |
| 227 | } |
| 228 | CustomElement.observedAttributes = ['title']; |
| 229 | customElements.define('parser-created-element', CustomElement); |
| 230 | assert_attribute_log_entry(calls[0], {name: 'title', oldValue: null, newValue: '', namespace: null}); |
| 231 | }, 'Upgrading a parser created element must enqueue and invoke attributeChangedCallback for an HTML attribute'); |
| 232 | |
| 233 | test(function () { |
| 234 | var calls = []; |
| 235 | class CustomElement extends HTMLElement { } |
| 236 | CustomElement.prototype.attributeChangedCallback = function (...args) { |
| 237 | calls.push(create_attribute_changed_callback_log(this, ...args)); |
| 238 | } |
| 239 | CustomElement.observedAttributes = ['title']; |
| 240 | customElements.define('cloned-element-with-attribute', CustomElement); |
| 241 | |
| 242 | var instance = document.createElement('cloned-element-with-attribute'); |
| 243 | assert_equals(calls.length, 0); |
| 244 | instance.title = ''; |
| 245 | assert_attribute_log_entry(calls[0], {name: 'title', oldValue: null, newValue: '', namespace: null}); |
| 246 | |
| 247 | calls = []; |
| 248 | var clone = instance.cloneNode(false); |
| 249 | assert_attribute_log_entry(calls[0], {name: 'title', oldValue: null, newValue: '', namespace: null}); |
| 250 | }, 'Upgrading a cloned element must enqueue and invoke attributeChangedCallback for an HTML attribute'); |
| 251 | |
| Ryosuke Niwa | e09776a | 2016-09-29 07:14:40 | [diff] [blame] | 252 | </script> |
| 253 | </body> |
| 254 | </html> |