| OLD | NEW |
| 1 <!DOCTYPE html> | 1 <!DOCTYPE html> |
| 2 <html> | 2 <html> |
| 3 <head> | 3 <head> |
| 4 <script src="../../../resources/js-test.js"></script> | 4 <script src="../../../resources/js-test.js"></script> |
| 5 </head> | 5 </head> |
| 6 <body> | 6 <body> |
| 7 <script src="script-tests/dom-token-list.js"></script> | 7 <script> |
| 8 // This test mostly comes from fast/dom/HTMLElement/script-tests/class-list.js |
| 9 description('Tests the htmlFor attribute and its properties.'); |
| 10 |
| 11 var container = document.createElement('div'); |
| 12 document.body.appendChild(container); |
| 13 |
| 14 var element; |
| 15 |
| 16 function createElement(tokenList) |
| 17 { |
| 18 container.innerHTML = '<output for="' + tokenList + '"></output>'; |
| 19 element = container.lastChild; |
| 20 } |
| 21 |
| 22 debug('- Tests from http://simon.html5.org/test/html/dom/reflecting/DOMTokenList
/'); |
| 23 |
| 24 // HTMLOutputElement::htmlFor setter is forwarding assignment to DOMTokenList.va
lue attribute. |
| 25 createElement('x'); |
| 26 shouldBeEqualToString('element.htmlFor.value', 'x'); |
| 27 shouldBeEqualToString('String(element.htmlFor)', 'x'); |
| 28 element.htmlFor = 'y'; |
| 29 shouldBeEqualToString('element.htmlFor.value', 'y'); |
| 30 shouldBeEqualToString('String(element.htmlFor)', 'y'); |
| 31 |
| 32 // http://simon.html5.org/test/html/dom/reflecting/DOMTokenList/getting/001.htm |
| 33 createElement(''); |
| 34 shouldEvaluateTo('element.htmlFor.length', 0); |
| 35 |
| 36 // http://simon.html5.org/test/html/dom/reflecting/DOMTokenList/getting/002.htm |
| 37 createElement('x'); |
| 38 shouldEvaluateTo('element.htmlFor.length', 1); |
| 39 |
| 40 // http://simon.html5.org/test/html/dom/reflecting/DOMTokenList/getting/003.htm |
| 41 createElement('x x'); |
| 42 shouldEvaluateTo('element.htmlFor.length', 2); |
| 43 |
| 44 // http://simon.html5.org/test/html/dom/reflecting/DOMTokenList/getting/004.htm |
| 45 createElement('x y'); |
| 46 shouldEvaluateTo('element.htmlFor.length', 2); |
| 47 |
| 48 // http://simon.html5.org/test/html/dom/reflecting/DOMTokenList/getting/005.htm |
| 49 createElement(''); |
| 50 element.htmlFor.add('x'); |
| 51 shouldBeEqualToString('element.htmlFor.toString()', 'x'); |
| 52 |
| 53 // http://simon.html5.org/test/html/dom/reflecting/DOMTokenList/getting/006.htm |
| 54 createElement('x'); |
| 55 element.htmlFor.add('x'); |
| 56 shouldBeEqualToString('element.htmlFor.toString()', 'x'); |
| 57 |
| 58 // http://simon.html5.org/test/html/dom/reflecting/DOMTokenList/getting/007.htm |
| 59 createElement('x x'); |
| 60 element.htmlFor.add('x'); |
| 61 shouldBeEqualToString('element.htmlFor.toString()', 'x x'); |
| 62 |
| 63 // http://simon.html5.org/test/html/dom/reflecting/DOMTokenList/getting/008.htm |
| 64 createElement('y'); |
| 65 element.htmlFor.add('x'); |
| 66 shouldBeEqualToString('element.htmlFor.toString()', 'y x'); |
| 67 |
| 68 // http://simon.html5.org/test/html/dom/reflecting/DOMTokenList/getting/009.htm |
| 69 createElement(''); |
| 70 element.htmlFor.remove('x'); |
| 71 shouldBeEqualToString('element.htmlFor.toString()', ''); |
| 72 |
| 73 // http://simon.html5.org/test/html/dom/reflecting/DOMTokenList/getting/010.htm |
| 74 createElement('x'); |
| 75 element.htmlFor.remove('x'); |
| 76 shouldBeEqualToString('element.htmlFor.toString()', ''); |
| 77 |
| 78 // http://simon.html5.org/test/html/dom/reflecting/DOMTokenList/getting/011.htm |
| 79 createElement(' y x y '); |
| 80 element.htmlFor.remove('x'); |
| 81 shouldBeEqualToString('element.htmlFor.toString()', 'y y'); |
| 82 |
| 83 // http://simon.html5.org/test/html/dom/reflecting/DOMTokenList/getting/012.htm |
| 84 createElement(' x y x '); |
| 85 element.htmlFor.remove('x'); |
| 86 shouldBeEqualToString('element.htmlFor.toString()', 'y'); |
| 87 |
| 88 |
| 89 debug('- Ensure that we can handle empty form attribute correctly'); |
| 90 element = document.createElement('output'); |
| 91 var list = element.htmlFor; |
| 92 list.toggle('x'); |
| 93 shouldBeEqualToString('list.value', 'x'); |
| 94 |
| 95 list.toggle('x'); |
| 96 shouldBeEqualToString('list.value', ''); |
| 97 |
| 98 element = document.createElement('output'); |
| 99 shouldBeFalse('element.htmlFor.contains(\'x\')'); |
| 100 shouldBeUndefined('element.htmlFor[1]'); |
| 101 element.htmlFor.remove('x'); |
| 102 element.htmlFor.add('x') |
| 103 shouldBeTrue('element.htmlFor.contains(\'x\')'); |
| 104 shouldBeUndefined('element.htmlFor[1]'); |
| 105 |
| 106 debug('- Testing add in presence of trailing white spaces.'); |
| 107 |
| 108 createElement('x '); |
| 109 element.htmlFor.add('y'); |
| 110 shouldBeEqualToString('element.htmlFor.toString()', 'x y'); |
| 111 |
| 112 createElement('x\t'); |
| 113 element.htmlFor.add('y'); |
| 114 shouldBeEqualToString('element.htmlFor.toString()', 'x\ty'); |
| 115 |
| 116 createElement(' '); |
| 117 element.htmlFor.add('y'); |
| 118 shouldBeEqualToString('element.htmlFor.toString()', ' y'); |
| 119 |
| 120 |
| 121 debug('- Test invalid tokens'); |
| 122 |
| 123 // Testing exception due to invalid token |
| 124 |
| 125 // shouldThrow from js-test.js is not sufficient. |
| 126 function shouldThrowDOMException(f, ec) |
| 127 { |
| 128 try { |
| 129 f(); |
| 130 testFailed('Expected an exception'); |
| 131 } catch (ex) { |
| 132 if (!(ex instanceof DOMException)) { |
| 133 testFailed('Exception is not an instance of DOMException, found: ' + |
| 134 Object.toString.call(ex)); |
| 135 return; |
| 136 } |
| 137 if (ec !== ex.code) { |
| 138 testFailed('Wrong exception code: ' + ex.code); |
| 139 return; |
| 140 } |
| 141 } |
| 142 var formattedFunction = String(f).replace(/^function.+\{\s*/m, ''). |
| 143 replace(/;?\s+\}/m, ''); |
| 144 testPassed(formattedFunction + ' threw expected DOMException with code ' + e
c); |
| 145 } |
| 146 |
| 147 createElement('x'); |
| 148 shouldThrowDOMException(function() { |
| 149 element.htmlFor.contains(''); |
| 150 }, DOMException.SYNTAX_ERR); |
| 151 |
| 152 createElement('x y'); |
| 153 shouldThrowDOMException(function() { |
| 154 element.htmlFor.contains('x y'); |
| 155 }, DOMException.INVALID_CHARACTER_ERR); |
| 156 |
| 157 createElement(''); |
| 158 shouldThrowDOMException(function() { |
| 159 element.htmlFor.add(''); |
| 160 }, DOMException.SYNTAX_ERR); |
| 161 |
| 162 createElement(''); |
| 163 shouldThrowDOMException(function() { |
| 164 element.htmlFor.add('x y'); |
| 165 }, DOMException.INVALID_CHARACTER_ERR); |
| 166 |
| 167 createElement(''); |
| 168 shouldThrowDOMException(function() { |
| 169 element.htmlFor.remove(''); |
| 170 }, DOMException.SYNTAX_ERR); |
| 171 |
| 172 createElement(''); |
| 173 shouldThrowDOMException(function() { |
| 174 element.htmlFor.remove('x y'); |
| 175 }, DOMException.INVALID_CHARACTER_ERR); |
| 176 |
| 177 createElement(''); |
| 178 shouldThrowDOMException(function() { |
| 179 element.htmlFor.toggle(''); |
| 180 }, DOMException.SYNTAX_ERR); |
| 181 |
| 182 createElement('x y'); |
| 183 shouldThrowDOMException(function() { |
| 184 element.htmlFor.toggle('x y'); |
| 185 }, DOMException.INVALID_CHARACTER_ERR); |
| 186 |
| 187 |
| 188 debug('- Indexing'); |
| 189 |
| 190 createElement('x'); |
| 191 shouldBeEqualToString('element.htmlFor[0]', 'x'); |
| 192 shouldBeEqualToString('element.htmlFor.item(0)', 'x'); |
| 193 |
| 194 createElement('x x'); |
| 195 shouldBeEqualToString('element.htmlFor[1]', 'x'); |
| 196 shouldBeEqualToString('element.htmlFor.item(1)', 'x'); |
| 197 |
| 198 createElement('x y'); |
| 199 shouldBeEqualToString('element.htmlFor[1]', 'y'); |
| 200 shouldBeEqualToString('element.htmlFor.item(1)', 'y'); |
| 201 |
| 202 createElement(''); |
| 203 shouldBeUndefined('element.htmlFor[0]'); |
| 204 shouldBeNull('element.htmlFor.item(0)'); |
| 205 |
| 206 createElement('x y z'); |
| 207 shouldBeUndefined('element.htmlFor[4]'); |
| 208 shouldBeNull('element.htmlFor.item(4)'); |
| 209 shouldBeUndefined('element.htmlFor[-1]'); // Not a valid index so should not tr
igger item(). |
| 210 shouldBeNull('element.htmlFor.item(-1)'); |
| 211 |
| 212 debug('- Test case since DOMTokenList is case sensitive'); |
| 213 |
| 214 createElement('x'); |
| 215 shouldBeTrue('element.htmlFor.contains(\'x\')'); |
| 216 shouldBeFalse('element.htmlFor.contains(\'X\')'); |
| 217 shouldBeEqualToString('element.htmlFor[0]', 'x'); |
| 218 |
| 219 createElement('X'); |
| 220 shouldBeTrue('element.htmlFor.contains(\'X\')'); |
| 221 shouldBeFalse('element.htmlFor.contains(\'x\')'); |
| 222 shouldBeEqualToString('element.htmlFor[0]', 'X'); |
| 223 |
| 224 |
| 225 debug('- Testing whitespace'); |
| 226 // U+0020 SPACE, U+0009 CHARACTER TABULATION (tab), U+000A LINE FEED (LF), |
| 227 // U+000C FORM FEED (FF), and U+000D CARRIAGE RETURN (CR) |
| 228 |
| 229 createElement('x\u0020y'); |
| 230 shouldEvaluateTo('element.htmlFor.length', 2); |
| 231 |
| 232 createElement('x\u0009y'); |
| 233 shouldEvaluateTo('element.htmlFor.length', 2); |
| 234 |
| 235 createElement('x\u000Ay'); |
| 236 shouldEvaluateTo('element.htmlFor.length', 2); |
| 237 |
| 238 createElement('x\u000Cy'); |
| 239 shouldEvaluateTo('element.htmlFor.length', 2); |
| 240 |
| 241 createElement('x\u000Dy'); |
| 242 shouldEvaluateTo('element.htmlFor.length', 2); |
| 243 |
| 244 |
| 245 debug('- DOMTokenList presence and type'); |
| 246 |
| 247 |
| 248 // Safari returns object |
| 249 // Firefox returns object |
| 250 // IE8 returns object |
| 251 // Chrome returns function |
| 252 // assertEquals('object', typeof DOMTokenList); |
| 253 shouldBeTrue('\'undefined\' != typeof DOMTokenList'); |
| 254 |
| 255 shouldBeEqualToString('typeof DOMTokenList.prototype', 'object'); |
| 256 |
| 257 createElement('x'); |
| 258 shouldBeEqualToString('typeof element.htmlFor', 'object'); |
| 259 |
| 260 shouldEvaluateTo('element.htmlFor.constructor', 'DOMTokenList'); |
| 261 |
| 262 shouldBeTrue('element.htmlFor === element.htmlFor'); |
| 263 </script> |
| 8 </body> | 264 </body> |
| 9 </html> | 265 </html> |
| OLD | NEW |