OLD | NEW |
1 <!DOCTYPE html> | 1 <!DOCTYPE html> |
2 <script src="../../../resources/testharness.js"></script> | 2 <script src="../../../resources/testharness.js"></script> |
3 <script src="../../../resources/testharnessreport.js"></script> | 3 <script src="../../../resources/testharnessreport.js"></script> |
4 <body> | 4 <body> |
5 <script> | 5 <script> |
6 test(function () { | 6 test(function () { |
7 var attributeChangedInvocations = 0; | 7 var attributeChangedInvocations = 0; |
8 function attributeChanged(_, _, _) { | 8 function attributeChanged(_, _, _) { |
9 attributeChangedInvocations++; | 9 attributeChangedInvocations++; |
10 } | 10 } |
11 | 11 |
12 var getterInvocations = 0; | 12 var getterInvocations = 0; |
13 function getter() { | 13 function getter() { |
14 getterInvocations++; | 14 getterInvocations++; |
15 return attributeChanged; | 15 return attributeChanged; |
16 } | 16 } |
17 | 17 |
18 function failer() { | 18 function failer() { |
19 assert_unreached('the attribute changed callback must not be retrieved a
fter registration'); | 19 assert_unreached('the attribute changed callback must not be retrieved a
fter registration'); |
20 } | 20 } |
21 | 21 |
22 var proto = Object.create(HTMLElement.prototype, { | 22 var proto = Object.create(HTMLElement.prototype, { |
23 attributeChangedCallback: { | 23 attributeChangedCallback: { |
24 get: getter | 24 get: getter |
25 } | 25 } |
26 }); | 26 }); |
27 var ctor = document.register('x-a', {prototype: proto}); | 27 var ctor = document.registerElement('x-a', {prototype: proto}); |
28 assert_equals(getterInvocations, 1, 'the attribute changed callback must hav
e been retrieved'); | 28 assert_equals(getterInvocations, 1, 'the attribute changed callback must hav
e been retrieved'); |
29 | 29 |
30 proto.attributeChangedCallback = failer; | 30 proto.attributeChangedCallback = failer; |
31 var element = new ctor(); | 31 var element = new ctor(); |
32 element.setAttribute('a', 'b'); | 32 element.setAttribute('a', 'b'); |
33 assert_equals(attributeChangedInvocations, 1, 'the attribute changed callbac
k retrieved at registration must be invoked'); | 33 assert_equals(attributeChangedInvocations, 1, 'the attribute changed callbac
k retrieved at registration must be invoked'); |
34 }, 'transfer attribute changed callback'); | 34 }, 'transfer attribute changed callback'); |
35 | 35 |
36 test(function () { | 36 test(function () { |
37 var invocations = []; | 37 var invocations = []; |
38 function created() { | 38 function created() { |
39 invocations.push('created'); | 39 invocations.push('created'); |
40 } | 40 } |
41 function attributeChanged(name, oldValue, newValue) { | 41 function attributeChanged(name, oldValue, newValue) { |
42 invocations.push(name + ': ' + oldValue + ' => ' + newValue); | 42 invocations.push(name + ': ' + oldValue + ' => ' + newValue); |
43 } | 43 } |
44 | 44 |
45 var proto = Object.create(HTMLElement.prototype); | 45 var proto = Object.create(HTMLElement.prototype); |
46 proto.createdCallback = created; | 46 proto.createdCallback = created; |
47 proto.attributeChangedCallback = attributeChanged; | 47 proto.attributeChangedCallback = attributeChanged; |
48 var B = document.register('x-b', {prototype: proto}); | 48 var B = document.registerElement('x-b', {prototype: proto}); |
49 | 49 |
50 var b = new B(); | 50 var b = new B(); |
51 b.id = 'x'; | 51 b.id = 'x'; |
52 assert_array_equals(invocations, ['created', 'id: null => x'], 'setting a re
flected attribute should invoke the attributeChanged callback'); | 52 assert_array_equals(invocations, ['created', 'id: null => x'], 'setting a re
flected attribute should invoke the attributeChanged callback'); |
53 | 53 |
54 invocations = []; | 54 invocations = []; |
55 b.removeAttribute('id'); | 55 b.removeAttribute('id'); |
56 assert_array_equals(invocations, ['id: x => null'], 'removing an attribute s
hould invoke the attributeChangedCallback'); | 56 assert_array_equals(invocations, ['id: x => null'], 'removing an attribute s
hould invoke the attributeChangedCallback'); |
57 | 57 |
58 invocations = []; | 58 invocations = []; |
59 b.setAttribute('data-s', 't'); | 59 b.setAttribute('data-s', 't'); |
60 assert_array_equals(invocations, ['data-s: null => t'], 'adding an attribute
with setAttribute should invoke the attributeChangedCallback'); | 60 assert_array_equals(invocations, ['data-s: null => t'], 'adding an attribute
with setAttribute should invoke the attributeChangedCallback'); |
61 | 61 |
62 invocations = []; | 62 invocations = []; |
63 b.classList.toggle('u'); | 63 b.classList.toggle('u'); |
64 assert_array_equals(invocations, ['class: null => u'], 'adding a class attri
bute through classList should invoke the attributeChangedCallback'); | 64 assert_array_equals(invocations, ['class: null => u'], 'adding a class attri
bute through classList should invoke the attributeChangedCallback'); |
65 | 65 |
66 b.setAttribute('data-v', 'w'); | 66 b.setAttribute('data-v', 'w'); |
67 invocations = []; | 67 invocations = []; |
68 b.setAttribute('data-v', 'x'); | 68 b.setAttribute('data-v', 'x'); |
69 assert_array_equals(invocations, ['data-v: w => x'], 'changing an attribute
with setAttribute should invoke the attributeChangedCallback'); | 69 assert_array_equals(invocations, ['data-v: w => x'], 'changing an attribute
with setAttribute should invoke the attributeChangedCallback'); |
70 | 70 |
71 invocations = []; | 71 invocations = []; |
72 b.setAttribute('data-v', 'x'); | 72 b.setAttribute('data-v', 'x'); |
73 assert_array_equals(invocations, [], 'setting an attribute to the existing v
alue with setAttribute should not invoke the attributeChangedCallback'); | 73 assert_array_equals(invocations, [], 'setting an attribute to the existing v
alue with setAttribute should not invoke the attributeChangedCallback'); |
74 }, 'add, change and remove an attribute'); | 74 }, 'add, change and remove an attribute'); |
75 </script> | 75 </script> |
OLD | NEW |