OLD | NEW |
1 description("This tests element.dataset."); | 1 description("This tests element.dataset."); |
2 | 2 |
3 function testGet(attr, expected) | 3 function testGet(attr, expected) |
4 { | 4 { |
5 var d = document.createElement("div"); | 5 var d = document.createElement("div"); |
6 d.setAttribute(attr, "value"); | 6 d.setAttribute(attr, "value"); |
7 return d.dataset[expected] == "value"; | 7 return d.dataset[expected] == "value"; |
8 } | 8 } |
9 | 9 |
10 shouldBeTrue("testGet('data-foo', 'foo')"); | 10 shouldBeTrue("testGet('data-foo', 'foo')"); |
11 shouldBeTrue("testGet('data-foo-bar', 'fooBar')"); | 11 shouldBeTrue("testGet('data-foo-bar', 'fooBar')"); |
12 shouldBeTrue("testGet('data--', '-')"); | 12 shouldBeTrue("testGet('data--', '-')"); |
13 shouldBeTrue("testGet('data--foo', 'Foo')"); | 13 shouldBeTrue("testGet('data--foo', 'Foo')"); |
14 shouldBeTrue("testGet('data---foo', '-Foo')"); | 14 shouldBeTrue("testGet('data---foo', '-Foo')"); |
15 shouldBeTrue("testGet('data---foo--bar', '-Foo-Bar')"); | 15 shouldBeTrue("testGet('data---foo--bar', '-Foo-Bar')"); |
16 shouldBeTrue("testGet('data---foo---bar', '-Foo--Bar')"); | 16 shouldBeTrue("testGet('data---foo---bar', '-Foo--Bar')"); |
17 shouldBeTrue("testGet('data-foo-', 'foo-')"); | 17 shouldBeTrue("testGet('data-foo-', 'foo-')"); |
18 shouldBeTrue("testGet('data-foo--', 'foo--')"); | 18 shouldBeTrue("testGet('data-foo--', 'foo--')"); |
19 shouldBeTrue("testGet('data-Foo', 'foo')"); // HTML lowercases all attributes. | 19 shouldBeTrue("testGet('data-Foo', 'foo')"); // HTML lowercases all attributes. |
20 shouldBeTrue("testGet('data-', '')"); | 20 shouldBeTrue("testGet('data-', '')"); |
21 shouldBeTrue("testGet('data-\xE0', '\xE0')"); | 21 shouldBeTrue("testGet('data-\xE0', '\xE0')"); |
| 22 shouldBeTrue("testGet('data-1', '1')"); |
| 23 shouldBeTrue("testGet('data-01', '01')"); |
| 24 shouldBeTrue("testGet('data-zx81', 'zx81')"); |
| 25 shouldBeTrue("testGet('data-i4770k', 'i4770k')"); |
| 26 shouldBeTrue("testGet('data-r-7', 'r-7')"); |
| 27 shouldBeTrue("testGet('data-r-7-k', 'r-7K')"); |
22 shouldBeUndefined("document.body.dataset.nonExisting"); | 28 shouldBeUndefined("document.body.dataset.nonExisting"); |
23 debug(""); | 29 debug(""); |
24 | 30 |
| 31 function testIsUndefined(attr, prop) |
| 32 { |
| 33 var d = document.createElement("div"); |
| 34 d.setAttribute(attr, "value"); |
| 35 return d.dataset[prop] === undefined; |
| 36 } |
| 37 |
| 38 shouldBeTrue("testIsUndefined('data-022', '22')"); |
| 39 shouldBeTrue("testIsUndefined('data-22', '022')"); |
| 40 |
| 41 debug(""); |
| 42 |
25 function matchesNothingInDataset(attr) | 43 function matchesNothingInDataset(attr) |
26 { | 44 { |
27 var d = document.createElement("div"); | 45 var d = document.createElement("div"); |
28 d.setAttribute(attr, "value"); | 46 d.setAttribute(attr, "value"); |
29 | 47 |
30 var count = 0; | 48 var count = 0; |
31 for (var item in d.dataset) | 49 for (var item in d.dataset) |
32 count++; | 50 count++; |
33 return count == 0; | 51 return count == 0; |
34 } | 52 } |
35 | 53 |
36 shouldBeTrue("matchesNothingInDataset('dataFoo')"); | 54 shouldBeTrue("matchesNothingInDataset('dataFoo')"); |
37 debug(""); | 55 debug(""); |
38 | 56 |
39 function testSet(prop, expected) | 57 function testSet(prop, expected) |
40 { | 58 { |
41 var d = document.createElement("div"); | 59 var d = document.createElement("div"); |
42 d.dataset[prop] = "value"; | 60 d.dataset[prop] = "value"; |
43 return d.getAttribute(expected) == "value"; | 61 return d.getAttribute(expected) == "value"; |
44 } | 62 } |
45 | 63 |
46 shouldBeTrue("testSet('foo', 'data-foo')"); | 64 shouldBeTrue("testSet('foo', 'data-foo')"); |
47 shouldBeTrue("testSet('fooBar', 'data-foo-bar')"); | 65 shouldBeTrue("testSet('fooBar', 'data-foo-bar')"); |
48 shouldBeTrue("testSet('-', 'data--')"); | 66 shouldBeTrue("testSet('-', 'data--')"); |
49 shouldBeTrue("testSet('Foo', 'data--foo')"); | 67 shouldBeTrue("testSet('Foo', 'data--foo')"); |
50 shouldBeTrue("testSet('-Foo', 'data---foo')"); | 68 shouldBeTrue("testSet('-Foo', 'data---foo')"); |
51 shouldBeTrue("testSet('', 'data-')"); | 69 shouldBeTrue("testSet('', 'data-')"); |
52 shouldBeTrue("testSet('\xE0', 'data-\xE0')"); | 70 shouldBeTrue("testSet('\xE0', 'data-\xE0')"); |
| 71 shouldBeTrue("testSet('32', 'data-32')"); |
| 72 shouldBeTrue("testSet('0032', 'data-0032')"); |
| 73 shouldBeTrue("testSet('i18n', 'data-i18n')"); |
| 74 shouldBeTrue("testSet('d2', 'data-d2')"); |
| 75 shouldBeTrue("testSet('2d', 'data-2d')"); |
| 76 shouldBeTrue("testSet('d-2', 'data-d-2')"); |
| 77 shouldBeTrue("testSet('A--S', 'data--a---s')"); |
| 78 debug(""); |
| 79 |
| 80 function testIsNull(prop, attr) |
| 81 { |
| 82 var d = document.createElement("div"); |
| 83 d.dataset[prop] = "value"; |
| 84 return d.getAttribute(attr) === null; |
| 85 } |
| 86 |
| 87 shouldBeTrue("testIsNull('0123', 'data-123')"); |
| 88 shouldBeTrue("testIsNull('123', 'data-0123')"); |
53 debug(""); | 89 debug(""); |
54 | 90 |
55 shouldThrow("testSet('-foo', 'dummy')", '"SyntaxError: Failed to set the \'-foo\
' property on \'DOMStringMap\': \'-foo\' is not a valid property name."'); | 91 shouldThrow("testSet('-foo', 'dummy')", '"SyntaxError: Failed to set the \'-foo\
' property on \'DOMStringMap\': \'-foo\' is not a valid property name."'); |
56 shouldThrow("testSet('foo\x20', 'dummy')", "'InvalidCharacterError: The string c
ontains invalid characters.'"); | 92 shouldThrow("testSet('foo\x20', 'dummy')", "'InvalidCharacterError: The string c
ontains invalid characters.'"); |
57 shouldThrow("testSet('foo\uF900', 'dummy')", "'InvalidCharacterError: The string
contains invalid characters.'"); | 93 shouldThrow("testSet('foo\uF900', 'dummy')", "'InvalidCharacterError: The string
contains invalid characters.'"); |
58 debug(""); | 94 debug(""); |
59 | 95 |
60 function testDelete(attr, prop) | 96 function testDelete(attr, prop) |
61 { | 97 { |
62 var d = document.createElement("div"); | 98 var d = document.createElement("div"); |
63 d.setAttribute(attr, "value"); | 99 d.setAttribute(attr, "value"); |
64 delete d.dataset[prop]; | 100 delete d.dataset[prop]; |
65 return d.getAttribute(attr) != "value"; | 101 return d.getAttribute(attr) != "value"; |
66 } | 102 } |
67 | 103 |
68 shouldBeTrue("testDelete('data-foo', 'foo')"); | 104 shouldBeTrue("testDelete('data-foo', 'foo')"); |
69 shouldBeTrue("testDelete('data-foo-bar', 'fooBar')"); | 105 shouldBeTrue("testDelete('data-foo-bar', 'fooBar')"); |
70 shouldBeTrue("testDelete('data--', '-')"); | 106 shouldBeTrue("testDelete('data--', '-')"); |
71 shouldBeTrue("testDelete('data--foo', 'Foo')"); | 107 shouldBeTrue("testDelete('data--foo', 'Foo')"); |
72 shouldBeTrue("testDelete('data---foo', '-Foo')"); | 108 shouldBeTrue("testDelete('data---foo', '-Foo')"); |
73 shouldBeTrue("testDelete('data-', '')"); | 109 shouldBeTrue("testDelete('data-', '')"); |
74 shouldBeTrue("testDelete('data-\xE0', '\xE0')"); | 110 shouldBeTrue("testDelete('data-\xE0', '\xE0')"); |
| 111 shouldBeTrue("testDelete('data-33', '33')"); |
| 112 shouldBeTrue("testDelete('data-00033', '00033')"); |
| 113 shouldBeTrue("testDelete('data-r2', 'r2')"); |
| 114 shouldBeTrue("testDelete('data-2r', '2r')"); |
| 115 shouldBeTrue("testDelete('data-r-2', 'r-2')"); |
| 116 shouldBeTrue("testDelete('data--r-2-', 'R-2-')"); |
| 117 shouldBeTrue("testDelete('data--r-2r', 'R-2r')"); |
| 118 shouldBeTrue("testDelete('data--r-2-----r', 'R-2----R')"); |
75 debug(""); | 119 debug(""); |
76 | 120 |
77 shouldBeFalse("testDelete('dummy', '-foo')"); | 121 shouldBeFalse("testDelete('dummy', '-foo')"); |
78 debug(""); | 122 debug(""); |
79 | 123 |
80 function testForIn(array) | 124 function testForIn(array) |
81 { | 125 { |
82 var d = document.createElement("div"); | 126 var d = document.createElement("div"); |
83 for (var i = 0; i < array.length; ++i) { | 127 for (var i = 0; i < array.length; ++i) { |
84 d.setAttribute(array[i], "value"); | 128 d.setAttribute(array[i], "value"); |
85 } | 129 } |
86 | 130 |
87 var count = 0; | 131 var count = 0; |
88 for (var item in d.dataset) | 132 for (var item in d.dataset) |
89 count++; | 133 count++; |
90 | 134 |
91 return count; | 135 return count; |
92 } | 136 } |
93 | 137 |
94 shouldBe("testForIn(['data-foo', 'data-bar', 'data-baz'])", "3"); | 138 shouldBe("testForIn(['data-foo', 'data-bar', 'data-baz'])", "3"); |
95 shouldBe("testForIn(['data-foo', 'data-bar', 'dataFoo'])", "2"); | 139 shouldBe("testForIn(['data-foo', 'data-bar', 'dataFoo'])", "2"); |
96 shouldBe("testForIn(['data-foo', 'data-bar', 'style'])", "2"); | 140 shouldBe("testForIn(['data-foo', 'data-bar', 'style'])", "2"); |
97 shouldBe("testForIn(['data-foo', 'data-bar', 'data-'])", "3"); | 141 shouldBe("testForIn(['data-foo', 'data-bar', 'data-'])", "3"); |
| 142 shouldBe("testForIn(['data-foo', 'data-bar', 'data-43'])", "3"); |
| 143 shouldBe("testForIn(['data-foo', 'data-oric1', 'data-bar'])", "3"); |
| 144 shouldBe("testForIn(['data-foo', 'data-oric-1', 'data-bar'])", "3"); |
| 145 shouldBe("testForIn(['data-foo', 'data-oric-1x', 'data-bar'])", "3"); |
98 | 146 |
99 | 147 |
100 debug(""); | 148 debug(""); |
101 debug("Property override:"); | 149 debug("Property override:"); |
102 var div = document.createElement("div"); | 150 var div = document.createElement("div"); |
103 // If the Object prorotype already has "foo", dataset doesn't create the | 151 // If the Object prototype already has "foo", dataset doesn't create the |
104 // corresponding attribute for "foo". | 152 // corresponding attribute for "foo". |
105 shouldBe("Object.prototype.foo = 'on Object'; div.dataset.foo", "'on Object'"); | 153 shouldBe("Object.prototype.foo = 'on Object'; div.dataset.foo", "'on Object'"); |
106 shouldBe("div.dataset['foo'] = 'on dataset'; div.dataset.foo", "'on dataset'"); | 154 shouldBe("div.dataset['foo'] = 'on dataset'; div.dataset.foo", "'on dataset'"); |
107 shouldBeTrue("div.hasAttribute('data-foo')"); | 155 shouldBeTrue("div.hasAttribute('data-foo')"); |
108 shouldBe("div.setAttribute('data-foo', 'attr'); div.dataset.foo", "'attr'"); | 156 shouldBe("div.setAttribute('data-foo', 'attr'); div.dataset.foo", "'attr'"); |
109 debug("Update the JavaScript property:"); | 157 debug("Update the JavaScript property:"); |
110 shouldBe("div.dataset.foo = 'updated'; div.dataset.foo", "'updated'"); | 158 shouldBe("div.dataset.foo = 'updated'; div.dataset.foo", "'updated'"); |
111 shouldBe("div.getAttribute('data-foo')", "'updated'"); | 159 shouldBe("div.getAttribute('data-foo')", "'updated'"); |
112 // "Bar" can't be represented as a data- attribute. | 160 // "Bar" can't be represented as a data- attribute. |
113 shouldBe("div.dataset.Bar = 'on dataset'; div.dataset.Bar", "'on dataset'"); | 161 shouldBe("div.dataset.Bar = 'on dataset'; div.dataset.Bar", "'on dataset'"); |
114 shouldBeFalse("div.hasAttribute('data-Bar')"); | 162 shouldBeFalse("div.hasAttribute('data-Bar')"); |
115 debug("Make the JavaScript property empty:"); | 163 debug("Make the JavaScript property empty:"); |
116 shouldBe("div.dataset.foo = ''; div.dataset.foo", "''"); | 164 shouldBe("div.dataset.foo = ''; div.dataset.foo", "''"); |
117 shouldBe("div.getAttribute('data-foo')", "''"); | 165 shouldBe("div.getAttribute('data-foo')", "''"); |
118 debug("Remove the attribute:"); | 166 debug("Remove the attribute:"); |
119 shouldBe("div.removeAttribute('data-foo'); div.dataset.foo", "'on Object'"); | 167 shouldBe("div.removeAttribute('data-foo'); div.dataset.foo", "'on Object'"); |
120 debug("Remove the JavaScript property:"); | 168 debug("Remove the JavaScript property:"); |
121 shouldBe("div.setAttribute('data-foo', 'attr'); delete div.dataset.foo; div.data
set.foo", "'on Object'"); | 169 shouldBe("div.setAttribute('data-foo', 'attr'); delete div.dataset.foo; div.data
set.foo", "'on Object'"); |
122 shouldBeFalse("div.hasAttribute('foo')"); | 170 shouldBeFalse("div.hasAttribute('foo')"); |
123 shouldBeUndefined("delete div.dataset.Bar; div.dataset.Bar"); | 171 shouldBeUndefined("delete div.dataset.Bar; div.dataset.Bar"); |
124 | 172 |
| 173 shouldBe("Object.prototype[11] = 'on Object'; div.dataset[11]", "'on Object'"); |
| 174 shouldBe("div.dataset['11'] = 'on dataset'; div.dataset[11]", "'on dataset'"); |
| 175 shouldBeTrue("div.hasAttribute('data-11')"); |
| 176 shouldBe("div.setAttribute('data-11', 'attr'); div.dataset[11]", "'attr'"); |
| 177 debug("Update the JavaScript property:"); |
| 178 shouldBe("div.dataset[11] = 'updated'; div.dataset[11]", "'updated'"); |
| 179 shouldBe("div.getAttribute('data-11')", "'updated'"); |
| 180 |
| 181 shouldBe("Object.prototype['a500'] = 'on Object'; div.dataset['a500']", "'on Obj
ect'"); |
| 182 shouldBe("div.dataset['a500'] = 'on dataset'; div.dataset['a500']", "'on dataset
'"); |
| 183 shouldBeTrue("div.hasAttribute('data-a500')"); |
| 184 shouldBe("div.setAttribute('data-a500', 'attr'); div.dataset['a500']", "'attr'")
; |
| 185 debug("Update the JavaScript property:"); |
| 186 shouldBe("div.dataset['a500'] = 'updated'; div.dataset['a500']", "'updated'"); |
| 187 shouldBe("div.getAttribute('data-a500')", "'updated'"); |
| 188 |
| 189 shouldBe("Object.prototype['a-500k'] = 'on Object'; div.dataset['a-500k']", "'on
Object'"); |
| 190 shouldBe("div.dataset['a-500k'] = 'on dataset'; div.dataset['a-500k']", "'on dat
aset'"); |
| 191 shouldBeTrue("div.hasAttribute('data-a-500k')"); |
| 192 shouldBe("div.setAttribute('data-a-500k', 'attr'); div.dataset['a-500k']", "'att
r'"); |
| 193 debug("Update the JavaScript property:"); |
| 194 shouldBe("div.dataset['a-500k'] = 'updated'; div.dataset['a-500k']", "'updated'"
); |
| 195 shouldBe("div.getAttribute('data-a-500k')", "'updated'"); |
| 196 |
125 debug(""); | 197 debug(""); |
126 debug("Set null:"); | 198 debug("Set null:"); |
127 | 199 |
128 var d = document.createElement("div"); | 200 var d = document.createElement("div"); |
129 d.dataset.foo = null; | 201 d.dataset.foo = null; |
130 shouldBe("d.dataset.foo", "'null'"); | 202 shouldBe("d.dataset.foo", "'null'"); |
131 | 203 |
132 debug(""); | 204 debug(""); |
OLD | NEW |