1 // This test mostly comes from fast/dom/HTMLElement/script-tests/class-list.js
2 description('Tests the htmlFor attribute and its properties.');
4 var container = document.createElement('div');
5 document.body.appendChild(container);
9 function createElement(tokenList)
11 container.innerHTML = '<output for="' + tokenList + '"></output>';
12 element = container.lastChild;
15 debug('- Tests from http://simon.html5.org/test/html/dom/reflecting/DOMTokenList/');
17 // HTMLOutputElement::htmlFor setter is forwarding assignment to DOMSettableTokenList.value attribute.
19 shouldBeEqualToString('element.htmlFor.value', 'x');
20 shouldBeEqualToString('String(element.htmlFor)', 'x');
21 element.htmlFor = 'y';
22 shouldBeEqualToString('element.htmlFor.value', 'y');
23 shouldBeEqualToString('String(element.htmlFor)', 'y');
25 // http://simon.html5.org/test/html/dom/reflecting/DOMTokenList/getting/001.htm
27 shouldEvaluateTo('element.htmlFor.length', 0);
29 // http://simon.html5.org/test/html/dom/reflecting/DOMTokenList/getting/002.htm
31 shouldEvaluateTo('element.htmlFor.length', 1);
33 // http://simon.html5.org/test/html/dom/reflecting/DOMTokenList/getting/003.htm
35 shouldEvaluateTo('element.htmlFor.length', 2);
37 // http://simon.html5.org/test/html/dom/reflecting/DOMTokenList/getting/004.htm
39 shouldEvaluateTo('element.htmlFor.length', 2);
41 // http://simon.html5.org/test/html/dom/reflecting/DOMTokenList/getting/005.htm
43 element.htmlFor.add('x');
44 shouldBeEqualToString('element.htmlFor.toString()', 'x');
46 // http://simon.html5.org/test/html/dom/reflecting/DOMTokenList/getting/006.htm
48 element.htmlFor.add('x');
49 shouldBeEqualToString('element.htmlFor.toString()', 'x');
51 // http://simon.html5.org/test/html/dom/reflecting/DOMTokenList/getting/007.htm
53 element.htmlFor.add('x');
54 shouldBeEqualToString('element.htmlFor.toString()', 'x x');
56 // http://simon.html5.org/test/html/dom/reflecting/DOMTokenList/getting/008.htm
58 element.htmlFor.add('x');
59 shouldBeEqualToString('element.htmlFor.toString()', 'y x');
61 // http://simon.html5.org/test/html/dom/reflecting/DOMTokenList/getting/009.htm
63 element.htmlFor.remove('x');
64 shouldBeEqualToString('element.htmlFor.toString()', '');
66 // http://simon.html5.org/test/html/dom/reflecting/DOMTokenList/getting/010.htm
68 element.htmlFor.remove('x');
69 shouldBeEqualToString('element.htmlFor.toString()', '');
71 // http://simon.html5.org/test/html/dom/reflecting/DOMTokenList/getting/011.htm
72 createElement(' y x y ');
73 element.htmlFor.remove('x');
74 shouldBeEqualToString('element.htmlFor.toString()', ' y y ');
76 // http://simon.html5.org/test/html/dom/reflecting/DOMTokenList/getting/012.htm
77 createElement(' x y x ');
78 element.htmlFor.remove('x');
79 shouldBeEqualToString('element.htmlFor.toString()', 'y');
82 debug('- Ensure that we can handle empty form attribute correctly');
83 element = document.createElement('output');
84 var list = element.htmlFor;
86 shouldBeEqualToString('list.value', 'x');
89 shouldBeEqualToString('list.value', '');
91 element = document.createElement('output');
92 shouldBeFalse('element.htmlFor.contains(\'x\')');
93 shouldBeUndefined('element.htmlFor[1]');
94 element.htmlFor.remove('x');
95 element.htmlFor.add('x')
96 shouldBeTrue('element.htmlFor.contains(\'x\')');
97 shouldBeUndefined('element.htmlFor[1]');
99 debug('- Testing add in presence of trailing white spaces.');
102 element.htmlFor.add('y');
103 shouldBeEqualToString('element.htmlFor.toString()', 'x y');
105 createElement('x\t');
106 element.htmlFor.add('y');
107 shouldBeEqualToString('element.htmlFor.toString()', 'x\ty');
110 element.htmlFor.add('y');
111 shouldBeEqualToString('element.htmlFor.toString()', ' y');
114 debug('- Test invalid tokens');
116 // Testing exception due to invalid token
118 // shouldThrow from js-test.js is not sufficient.
119 function shouldThrowDOMException(f, ec)
123 testFailed('Expected an exception');
125 if (!(ex instanceof DOMException)) {
126 testFailed('Exception is not an instance of DOMException, found: ' +
127 Object.toString.call(ex));
130 if (ec !== ex.code) {
131 testFailed('Wrong exception code: ' + ex.code);
135 var formattedFunction = String(f).replace(/^function.+\{\s*/m, '').
136 replace(/;?\s+\}/m, '');
137 testPassed(formattedFunction + ' threw expected DOMException with code ' + ec);
141 shouldThrowDOMException(function() {
142 element.htmlFor.contains('');
143 }, DOMException.SYNTAX_ERR);
145 createElement('x y');
146 shouldThrowDOMException(function() {
147 element.htmlFor.contains('x y');
148 }, DOMException.INVALID_CHARACTER_ERR);
151 shouldThrowDOMException(function() {
152 element.htmlFor.add('');
153 }, DOMException.SYNTAX_ERR);
156 shouldThrowDOMException(function() {
157 element.htmlFor.add('x y');
158 }, DOMException.INVALID_CHARACTER_ERR);
161 shouldThrowDOMException(function() {
162 element.htmlFor.remove('');
163 }, DOMException.SYNTAX_ERR);
166 shouldThrowDOMException(function() {
167 element.htmlFor.remove('x y');
168 }, DOMException.INVALID_CHARACTER_ERR);
171 shouldThrowDOMException(function() {
172 element.htmlFor.toggle('');
173 }, DOMException.SYNTAX_ERR);
175 createElement('x y');
176 shouldThrowDOMException(function() {
177 element.htmlFor.toggle('x y');
178 }, DOMException.INVALID_CHARACTER_ERR);
184 shouldBeEqualToString('element.htmlFor[0]', 'x');
185 shouldBeEqualToString('element.htmlFor.item(0)', 'x');
187 createElement('x x');
188 shouldBeEqualToString('element.htmlFor[1]', 'x');
189 shouldBeEqualToString('element.htmlFor.item(1)', 'x');
191 createElement('x y');
192 shouldBeEqualToString('element.htmlFor[1]', 'y');
193 shouldBeEqualToString('element.htmlFor.item(1)', 'y');
196 shouldBeUndefined('element.htmlFor[0]');
197 shouldBeNull('element.htmlFor.item(0)');
199 createElement('x y z');
200 shouldBeUndefined('element.htmlFor[4]');
201 shouldBeNull('element.htmlFor.item(4)');
202 shouldBeUndefined('element.htmlFor[-1]'); // Not a valid index so should not trigger item().
203 shouldBeNull('element.htmlFor.item(-1)');
205 debug('- Test case since DOMTokenList is case sensitive');
208 shouldBeTrue('element.htmlFor.contains(\'x\')');
209 shouldBeFalse('element.htmlFor.contains(\'X\')');
210 shouldBeEqualToString('element.htmlFor[0]', 'x');
213 shouldBeTrue('element.htmlFor.contains(\'X\')');
214 shouldBeFalse('element.htmlFor.contains(\'x\')');
215 shouldBeEqualToString('element.htmlFor[0]', 'X');
218 debug('- Testing whitespace');
219 // U+0020 SPACE, U+0009 CHARACTER TABULATION (tab), U+000A LINE FEED (LF),
220 // U+000C FORM FEED (FF), and U+000D CARRIAGE RETURN (CR)
222 createElement('x\u0020y');
223 shouldEvaluateTo('element.htmlFor.length', 2);
225 createElement('x\u0009y');
226 shouldEvaluateTo('element.htmlFor.length', 2);
228 createElement('x\u000Ay');
229 shouldEvaluateTo('element.htmlFor.length', 2);
231 createElement('x\u000Cy');
232 shouldEvaluateTo('element.htmlFor.length', 2);
234 createElement('x\u000Dy');
235 shouldEvaluateTo('element.htmlFor.length', 2);
238 debug('- DOMSettableTokenList presence and type');
241 // Safari returns object
242 // Firefox returns object
243 // IE8 returns object
244 // Chrome returns function
245 // assertEquals('object', typeof DOMSettableTokenList);
246 shouldBeTrue('\'undefined\' != typeof DOMSettableTokenList');
248 shouldBeEqualToString('typeof DOMSettableTokenList.prototype', 'object');
251 shouldBeEqualToString('typeof element.htmlFor', 'object');
253 shouldEvaluateTo('element.htmlFor.constructor', 'DOMSettableTokenList');
255 shouldBeTrue('element.htmlFor === element.htmlFor');