|
|
|
|
|
|
1 |
<!DOCTYPE HTML> |
| 2 |
<html> |
| 3 |
<!-- |
| 4 |
https://bugzilla.mozilla.org/show_bug.cgi?id=556743 |
| 5 |
--> |
| 6 |
<head> |
| 7 |
<title>Test for Bug 556743</title> |
| 8 |
<script type="application/javascript" src="/MochiKit/packed.js"></script> |
| 9 |
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> |
| 10 |
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> |
| 11 |
</head> |
| 12 |
<body> |
| 13 |
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=556743">Mozilla Bug 556743</a> |
| 14 |
<p id="display"></p> |
| 15 |
<div id="content" style="display: none"> |
| 16 |
</div> |
| 17 |
<pre id="test"> |
| 18 |
<script type="application/javascript"> |
| 19 |
|
| 20 |
/** Test for Bug 556743 **/ |
| 21 |
|
| 22 |
/** |
| 23 |
* TODO: |
| 24 |
* - check <label><element><script>element.labels<\/script><\/label> |
| 25 |
*/ |
| 26 |
|
| 27 |
/** |
| 28 |
* All functions take an array of elements in first argument and an array of |
| 29 |
* labels in second argument. |
| 30 |
* Then, it returns an array containing an array of elements and an array of |
| 31 |
* array of labels. The array represent the label association with elements |
| 32 |
* like this: |
| 33 |
* [ [ form1, form2 ], [ [ elmt1ofForm1, elmt2ofForm2 ], [ elmtofForm2 ] ] ] |
| 34 |
*/ |
| 35 |
|
| 36 |
/** |
| 37 |
* This function test that, when an element is not a descendant of a form |
| 38 |
* element and has @form set to a valid form id, it's form owner is the form |
| 39 |
* which has the id. |
| 40 |
*/ |
| 41 |
function test1(aForms, aElements) |
| 42 |
{ |
| 43 |
// <form id='f'></form><element id='f'> |
| 44 |
aForms[0].id = 'f'; |
| 45 |
aElements[0].setAttribute('form', 'f'); |
| 46 |
|
| 47 |
return [[aForms[0]], [[aElements[0]]]]; |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* This function test that, when an element is a descendant of a form |
| 52 |
* element and has @form set to a valid form id (not it's descendant), it's form |
| 53 |
* owner is the form which has the id. |
| 54 |
*/ |
| 55 |
function test2(aForms, aElements) |
| 56 |
{ |
| 57 |
// <form id='f'></form><form><element form='f'></form> |
| 58 |
aForms[0].id = 'f'; |
| 59 |
aForms[1].appendChild(aElements[0]); |
| 60 |
aElements[0].setAttribute('form', 'f'); |
| 61 |
|
| 62 |
return [[aForms[0], aForms[1]], [[aElements[0]],[]]]; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* This function test that, when an element is a descendant of a form |
| 67 |
* element and has @form set to a valid form id (not it's descendant), then the |
| 68 |
* form attribute is removed, it does not have a form owner. |
| 69 |
*/ |
| 70 |
function test3(aForms, aElements) |
| 71 |
{ |
| 72 |
// <form id='f'></form><form><element form='f'></form> |
| 73 |
aForms[0].id = 'f'; |
| 74 |
aForms[1].appendChild(aElements[0]); |
| 75 |
aElements[0].setAttribute('form', 'f'); |
| 76 |
aElements[0].removeAttribute('form'); |
| 77 |
|
| 78 |
return [[aForms[0], aForms[1]], [[],[aElements[0]]]]; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* This function test that, when an element is a descendant of a form |
| 83 |
* element and has @form set to a valid form id (not it's descendant), then the |
| 84 |
* form's id attribute is removed, it does not have a form owner. |
| 85 |
*/ |
| 86 |
function test4(aForms, aElements) |
| 87 |
{ |
| 88 |
// <form id='f'></form><form><element form='f'></form> |
| 89 |
aForms[0].id = 'f'; |
| 90 |
aForms[1].appendChild(aElements[0]); |
| 91 |
aElements[0].setAttribute('form', 'f'); |
| 92 |
aForms[0].removeAttribute('id'); |
| 93 |
|
| 94 |
return [[aForms[0], aForms[1]], [[],[]]]; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* This function test that, when an element is a descendant of a form |
| 99 |
* element and has @form set to an invalid form id, then it does not have a form |
| 100 |
* owner. |
| 101 |
*/ |
| 102 |
function test5(aForms, aElements) |
| 103 |
{ |
| 104 |
// <form id='f'></form><form><element form='foo'></form> |
| 105 |
aForms[0].id = 'f'; |
| 106 |
aForms[1].appendChild(aElements[0]); |
| 107 |
aElements[0].setAttribute('form', 'foo'); |
| 108 |
|
| 109 |
return [[aForms[0], aForms[1]], [[],[]]]; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* This function test that, when an element is a descendant of a form |
| 114 |
* element and has @form set to a valid form id (not it's descendant), then the |
| 115 |
* form id attribute is changed to an invalid id, it does not have a form owner. |
| 116 |
*/ |
| 117 |
function test6(aForms, aElements) |
| 118 |
{ |
| 119 |
// <form id='f'></form><form><element form='f'></form> |
| 120 |
aForms[0].id = 'f'; |
| 121 |
aForms[1].appendChild(aElements[0]); |
| 122 |
aElements[0].setAttribute('form', 'f'); |
| 123 |
aElements[0].setAttribute('form', 'foo'); |
| 124 |
|
| 125 |
return [[aForms[0], aForms[1]], [[],[]]]; |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* This function test that, when an element is a descendant of a form |
| 130 |
* element and has @form set to an invalid form id, then the form id attribute |
| 131 |
* is changed to a valid form id, it's form owner is the form which has this id. |
| 132 |
*/ |
| 133 |
function test7(aForms, aElements) |
| 134 |
{ |
| 135 |
// <form id='f'></form><form><element form='foo'></form> |
| 136 |
aForms[0].id = 'f'; |
| 137 |
aForms[1].appendChild(aElements[0]); |
| 138 |
aElements[0].setAttribute('form', 'foo'); |
| 139 |
aElements[0].setAttribute('form', 'f'); |
| 140 |
|
| 141 |
return [[aForms[0], aForms[1]], [[aElements[0]],[]]]; |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* This function test that, when an element is a descendant of a form |
| 146 |
* element and has @form set to a list of ids containing one valid form, then |
| 147 |
* it does not have a form owner. |
| 148 |
*/ |
| 149 |
function test8(aForms, aElements) |
| 150 |
{ |
| 151 |
// <form id='f'></form><form><element form='f foo'></form> |
| 152 |
aForms[0].id = 'f'; |
| 153 |
aForms[1].appendChild(aElements[0]); |
| 154 |
aElements[0].setAttribute('form', 'f foo'); |
| 155 |
|
| 156 |
return [[aForms[0], aForms[1]], [[],[]]]; |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* This function test that, when an element is a descendant of a form |
| 161 |
* element and has @form set to a form id which is valid in a case insensitive |
| 162 |
* way, then it does not have a form owner. |
| 163 |
*/ |
| 164 |
function test9(aForms, aElements) |
| 165 |
{ |
| 166 |
// <form id='f'></form><form><element form='F'></form> |
| 167 |
aForms[0].id = 'f'; |
| 168 |
aForms[1].appendChild(aElements[0]); |
| 169 |
aElements[0].setAttribute('form', 'F'); |
| 170 |
|
| 171 |
return [[aForms[0], aForms[1]], [[],[]]]; |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* This function test that, when an element is a descendant of a form |
| 176 |
* element and has @form set to a form id which is not a valid id, then it's |
| 177 |
* form owner is it does not have a form owner. |
| 178 |
*/ |
| 179 |
function test10(aForms, aElements) |
| 180 |
{ |
| 181 |
// <form id='F'></form><form><element form='f'></form> |
| 182 |
aForms[0].id = 'F'; |
| 183 |
aForms[1].appendChild(aElements[0]); |
| 184 |
aElements[0].setAttribute('form', 'f'); |
| 185 |
|
| 186 |
return [[aForms[0], aForms[1]], [[],[]]]; |
| 187 |
} |
| 188 |
|
| 189 |
/** |
| 190 |
* This function test that, when an element is a descendant of a form |
| 191 |
* element and has @form set to a form id which is not a valid id, then it's |
| 192 |
* form owner is it does not have a form owner. |
| 193 |
*/ |
| 194 |
function test11(aForms, aElements) |
| 195 |
{ |
| 196 |
// <form id='foo bar'></form><form><element form='foo bar'></form> |
| 197 |
aForms[0].id = 'foo bar'; |
| 198 |
aForms[1].appendChild(aElements[0]); |
| 199 |
aElements[0].setAttribute('form', 'foo bar'); |
| 200 |
|
| 201 |
return [[aForms[0], aForms[1]], [[aElements[0]],[]]]; |
| 202 |
} |
| 203 |
|
| 204 |
/** |
| 205 |
* This function test that, when an element is a descendant of a form |
| 206 |
* element and has @form set to a valid form id and the form id change, then |
| 207 |
* it does not have a form owner. |
| 208 |
*/ |
| 209 |
function test12(aForms, aElements) |
| 210 |
{ |
| 211 |
// <form id='f'></form><form><element form='f'></form> |
| 212 |
aForms[0].id = 'f'; |
| 213 |
aForms[1].appendChild(aElements[0]); |
| 214 |
aElements[0].setAttribute('form', 'f'); |
| 215 |
aForms[0].id = 'foo'; |
| 216 |
|
| 217 |
return [[aForms[0], aForms[1]], [[],[]]]; |
| 218 |
} |
| 219 |
|
| 220 |
/** |
| 221 |
* This function test that, when an element is a descendant of a form |
| 222 |
* element and has @form set to an invalid form id and the form id change to a |
| 223 |
* valid one, then it's form owner is the form which has the id. |
| 224 |
*/ |
| 225 |
function test13(aForms, aElements) |
| 226 |
{ |
| 227 |
// <form id='foo'></form><form><element form='f'></form> |
| 228 |
aForms[0].id = 'foo'; |
| 229 |
aForms[1].appendChild(aElements[0]); |
| 230 |
aElements[0].setAttribute('form', 'f'); |
| 231 |
aForms[0].id = 'f'; |
| 232 |
|
| 233 |
return [[aForms[0], aForms[1]], [[aElements[0]],[]]]; |
| 234 |
} |
| 235 |
|
| 236 |
/** |
| 237 |
* This function test that, when an element is a descendant of a form |
| 238 |
* element and has @form set to a valid form id and a form with the same id is |
| 239 |
* inserted before in the tree, then it's form owner is the form which has the |
| 240 |
* id. |
| 241 |
*/ |
| 242 |
function test14(aForms, aElements) |
| 243 |
{ |
| 244 |
// <form id='f'></form><form><element form='f'></form> |
| 245 |
aForms[0].id = 'f'; |
| 246 |
aForms[1].appendChild(aElements[0]); |
| 247 |
aElements[0].setAttribute('form', 'f'); |
| 248 |
aForms[2].id = 'f'; |
| 249 |
|
| 250 |
document.getElementById('content').insertBefore(aForms[2], aForms[0]); |
| 251 |
|
| 252 |
return [[aForms[0], aForms[1], aForms[2]], [[],[],[aElements[0]]]]; |
| 253 |
} |
| 254 |
|
| 255 |
/** |
| 256 |
* This function test that, when an element is a descendant of a form |
| 257 |
* element and has @form set to a valid form id and an element with the same id is |
| 258 |
* inserted before in the tree, then it does not have a form owner. |
| 259 |
*/ |
| 260 |
function test15(aForms, aElements) |
| 261 |
{ |
| 262 |
// <form id='f'></form><form><element form='f'></form> |
| 263 |
aForms[0].id = 'f'; |
| 264 |
aForms[1].appendChild(aElements[0]); |
| 265 |
aElements[0].setAttribute('form', 'f'); |
| 266 |
aElements[1].id = 'f'; |
| 267 |
|
| 268 |
document.getElementById('content').insertBefore(aElements[1], aForms[0]); |
| 269 |
|
| 270 |
return [[aForms[0], aForms[1]], [[],[]]]; |
| 271 |
} |
| 272 |
|
| 273 |
/** |
| 274 |
* This function test that, when an element is a descendant of a form |
| 275 |
* element and has @form set to a valid form id and the form is removed from |
| 276 |
* the tree, then it does not have a form owner. |
| 277 |
*/ |
| 278 |
function test16(aForms, aElements) |
| 279 |
{ |
| 280 |
// <form id='f'></form><form><element form='f'></form> |
| 281 |
aForms[0].id = 'f'; |
| 282 |
aForms[1].appendChild(aElements[0]); |
| 283 |
aElements[0].setAttribute('form', 'f'); |
| 284 |
aElements[1].id = 'f'; |
| 285 |
|
| 286 |
document.getElementById('content').removeChild(aForms[0]); |
| 287 |
|
| 288 |
return [[aForms[0], aForms[1]], [[],[]]]; |
| 289 |
} |
| 290 |
|
| 291 |
/** |
| 292 |
* This function test that, when an element is a descendant of a form element |
| 293 |
* and has @form set to the empty string, it does not have a form owner. |
| 294 |
*/ |
| 295 |
function test17(aForms, aElements) |
| 296 |
{ |
| 297 |
// <form><element form=''></form> |
| 298 |
aForms[0].appendChild(aElements[0]); |
| 299 |
aElements[0].setAttribute('form', ''); |
| 300 |
|
| 301 |
return [[aForms[0]], [[]]]; |
| 302 |
} |
| 303 |
|
| 304 |
/** |
| 305 |
* This function test that, when an element is a descendant of a form element |
| 306 |
* and has @form set to the empty string, it does not have a form owner even if |
| 307 |
* it's parent has its id equals to the empty string. |
| 308 |
*/ |
| 309 |
function test18(aForms, aElements) |
| 310 |
{ |
| 311 |
// <form id=''><element form=''></form> |
| 312 |
aForms[0].id = ''; |
| 313 |
aForms[0].appendChild(aElements[0]); |
| 314 |
aElements[0].setAttribute('form', ''); |
| 315 |
|
| 316 |
return [[aForms[0]], [[]]]; |
| 317 |
} |
| 318 |
|
| 319 |
/** |
| 320 |
* This function test that, when an element is a descendant of a form element |
| 321 |
* and has @form set to a valid form id and the element is being moving inside |
| 322 |
* it's parent, it's form owner will remain the form with the id. |
| 323 |
*/ |
| 324 |
function test19(aForms, aElements) |
| 325 |
{ |
| 326 |
// <form id='f'></form><form><element form='f'><element></form> |
| 327 |
aForms[0].id = 'f'; |
| 328 |
aForms[1].appendChild(aElements[0]); |
| 329 |
aForms[1].appendChild(aElements[1]); |
| 330 |
aElements[0].setAttribute('form', 'f'); |
| 331 |
aForms[1].appendChild(aElements[0]); |
| 332 |
|
| 333 |
return [[aForms[0],aForms[1]],[[aElements[0]],[aElements[1]]]]; |
| 334 |
} |
| 335 |
|
| 336 |
/** |
| 337 |
* This function test that, when an element is a descendant of a form element |
| 338 |
* and has @form set to a valid form id and the element is being moving inside |
| 339 |
* another form, it's form owner will remain the form with the id. |
| 340 |
*/ |
| 341 |
function test20(aForms, aElements) |
| 342 |
{ |
| 343 |
// <form id='f'></form><form><element form='f'><element></form> |
| 344 |
aForms[0].id = 'f'; |
| 345 |
aForms[1].appendChild(aElements[0]); |
| 346 |
aForms[1].appendChild(aElements[1]); |
| 347 |
aElements[0].setAttribute('form', 'f'); |
| 348 |
aForms[2].appendChild(aElements[0]); |
| 349 |
|
| 350 |
return [[aForms[0],aForms[1],aForms[2]],[[aElements[0]],[aElements[1]],[]]]; |
| 351 |
} |
| 352 |
|
| 353 |
var functions = [ |
| 354 |
test1, test2, test3, test4, test5, test6, test7, test8, test9, |
| 355 |
test10, test11, test12, test13, test14, test15, test16, test17, test18, test19, |
| 356 |
test20, |
| 357 |
]; |
| 358 |
|
| 359 |
// Global variable to have an easy access to <div id='content'>. |
| 360 |
var content = document.getElementById('content'); |
| 361 |
|
| 362 |
// Initializing the needed labels. |
| 363 |
var labels = [ |
| 364 |
document.createElement('label'), |
| 365 |
document.createElement('label'), |
| 366 |
]; |
| 367 |
|
| 368 |
var elementNames = [ |
| 369 |
'button', 'input', 'output', 'select', 'textarea', |
| 370 |
]; |
| 371 |
|
| 372 |
var notElementNames = [ |
| 373 |
'fieldset', 'label', 'object', 'option', 'optgroup', 'legend', |
| 374 |
]; |
| 375 |
|
| 376 |
var todoElements = [ |
| 377 |
['keygen', 'Keygen'], |
| 378 |
['meter', 'Meter'], |
| 379 |
['progress', 'Progress'], |
| 380 |
]; |
| 381 |
|
| 382 |
for each(var e in todoElements) { |
| 383 |
var node = document.createElement(e[0]); |
| 384 |
var nodeString = HTMLElement.prototype.toString.apply(node); |
| 385 |
nodeString = nodeString.replace(/Element[\] ].*/, "Element"); |
| 386 |
todo_is(nodeString, "[object HTML" + e[1] + "Element", |
| 387 |
e[0] + " should not be implemented"); |
| 388 |
} |
| 389 |
|
| 390 |
for each(var e in notElementNames) { |
| 391 |
var element = document.createElement(e); |
| 392 |
ok(!("labels" in element), "labels should not be an IDL attribute for " + e + |
| 393 |
" elements"); |
| 394 |
} |
| 395 |
|
| 396 |
for each(var name in elementNames) { |
| 397 |
var elements = [ |
| 398 |
document.createElement(name), |
| 399 |
document.createElement(name), |
| 400 |
]; |
| 401 |
|
| 402 |
ok("labels" in elements[0], "labels should be an IDL attribute for " + e + |
| 403 |
" elements"); |
| 404 |
if (!("labels" in elements[0])) { |
| 405 |
// No need to try anything else if labels isn't an IDL attribute. |
| 406 |
continue; |
| 407 |
} |
| 408 |
|
| 409 |
for each(var func in functions) { |
| 410 |
// Clean-up. |
| 411 |
while (content.firstChild) { |
| 412 |
content.removeChild(content.firstChild); |
| 413 |
} |
| 414 |
for each(label in labels) { |
| 415 |
content.appendChild(label); |
| 416 |
label.removeAttribute('for'); |
| 417 |
is(label.control, null, "The label should have no control."); |
| 418 |
} |
| 419 |
for each(e in elements) { |
| 420 |
content.appendChild(e); |
| 421 |
e.removeAttribute('id'); |
| 422 |
is(e.labels,length, 0, "The element should not be labeled"); |
| 423 |
} |
| 424 |
|
| 425 |
// Calling the test. |
| 426 |
var results = func(elements, labels); |
| 427 |
|
| 428 |
// Checking the results. |
| 429 |
var elementsList = results[0]; |
| 430 |
for (var i=0; i<elementsList.length; ++i) { |
| 431 |
var labelsList = results[1][i]; |
| 432 |
is(elementsList[i].labels.length, labelsList.length, |
| 433 |
"The elements should be labeled by " + labelsList.length + " labels"); |
| 434 |
for (var j=0; j<labelsList.length; ++j) { |
| 435 |
is(elementsList[i].labels[j], labelsList[j], |
| 436 |
"The labels list should contain " + elementsList[j]); |
| 437 |
is(labelsList[j].control, elementsList[i], |
| 438 |
"The control labeled by the label should be " + elementsList[i]); |
| 439 |
} |
| 440 |
} |
| 441 |
} |
| 442 |
|
| 443 |
// Cleaning-up. |
| 444 |
for each(e in elements) { |
| 445 |
e.parentNode.removeChild(e); |
| 446 |
e = null; |
| 447 |
} |
| 448 |
} |
| 449 |
|
| 450 |
</script> |
| 451 |
</pre> |
| 452 |
</body> |
| 453 |
</html> |