1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
|
<!DOCTYPE html>
<meta charset=utf-8>
<title>Document.getElementById</title>
<link rel="author" title="Tetsuharu OHZEKI" href="mailto:saneyuki.snyk@gmail.com">
<link rel=help href="https://dom.spec.whatwg.org/#dom-document-getelementbyid">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<body>
<div id="log"></div>
<!-- test 0 -->
<div id=""></div>
<!-- test 1 -->
<div id="test1"></div>
<!-- test 5 -->
<div id="test5" data-name="1st">
<p id="test5" data-name="2nd">P</p>
<input id="test5" type="submit" value="Submit" data-name="3rd">
</div>
<!-- test 15 -->
<div id="outer">
<div id="middle">
<div id="inner"></div>
</div>
</div>
<script>
var gBody = document.getElementsByTagName("body")[0];
test(function() {
assert_equals(document.getElementById(""), null);
}, "Calling document.getElementById with an empty string argument.");
test(function() {
var element = document.createElement("div");
element.setAttribute("id", "null");
document.body.appendChild(element);
this.add_cleanup(function() { document.body.removeChild(element) });
assert_equals(document.getElementById(null), element);
}, "Calling document.getElementById with a null argument.");
test(function() {
var element = document.createElement("div");
element.setAttribute("id", "undefined");
document.body.appendChild(element);
this.add_cleanup(function() { document.body.removeChild(element) });
assert_equals(document.getElementById(undefined), element);
}, "Calling document.getElementById with an undefined argument.");
test(function() {
var bar = document.getElementById("test1");
assert_not_equals(bar, null, "should not be null");
assert_equals(bar.tagName, "DIV", "should have expected tag name.");
assert_true(bar instanceof HTMLDivElement, "should be a valid Element instance");
}, "on static page");
test(function() {
var TEST_ID = "test2";
var test = document.createElement("div");
test.setAttribute("id", TEST_ID);
gBody.appendChild(test);
// test: appended element
var result = document.getElementById(TEST_ID);
assert_not_equals(result, null, "should not be null.");
assert_equals(result.tagName, "DIV", "should have appended element's tag name");
assert_true(result instanceof HTMLDivElement, "should be a valid Element instance");
// test: removed element
gBody.removeChild(test);
var removed = document.getElementById(TEST_ID);
// `document.getElementById()` returns `null` if there is none.
// https://dom.spec.whatwg.org/#dom-nonelementparentnode-getelementbyid
assert_equals(removed, null, "should not get removed element.");
}, "Document.getElementById with a script-inserted element");
test(function() {
// setup fixtures.
var TEST_ID = "test3";
var test = document.createElement("div");
test.setAttribute("id", TEST_ID);
gBody.appendChild(test);
// update id
var UPDATED_ID = "test3-updated";
test.setAttribute("id", UPDATED_ID);
var e = document.getElementById(UPDATED_ID);
assert_equals(e, test, "should get the element with id.");
var old = document.getElementById(TEST_ID);
assert_equals(old, null, "shouldn't get the element by the old id.");
// remove id.
test.removeAttribute("id");
var e2 = document.getElementById(UPDATED_ID);
assert_equals(e2, null, "should return null when the passed id is none in document.");
}, "update `id` attribute via setAttribute/removeAttribute");
test(function() {
var TEST_ID = "test4-should-not-exist";
var e = document.createElement('div');
e.setAttribute("id", TEST_ID);
assert_equals(document.getElementById(TEST_ID), null, "should be null");
document.body.appendChild(e);
assert_equals(document.getElementById(TEST_ID), e, "should be the appended element");
}, "Ensure that the id attribute only affects elements present in a document");
test(function() {
// the method should return the 1st element.
var TEST_ID = "test5";
var target = document.getElementById(TEST_ID);
assert_not_equals(target, null, "should not be null");
assert_equals(target.getAttribute("data-name"), "1st", "should return the 1st");
// even if after the new element was appended.
var element4 = document.createElement("div");
element4.setAttribute("id", TEST_ID);
element4.setAttribute("data-name", "4th");
gBody.appendChild(element4);
var target2 = document.getElementById(TEST_ID);
assert_not_equals(target2, null, "should not be null");
assert_equals(target2.getAttribute("data-name"), "1st", "should be the 1st");
// should return the next element after removed the subtree including the 1st element.
target2.parentNode.removeChild(target2);
var target3 = document.getElementById(TEST_ID);
assert_not_equals(target3, null, "should not be null");
assert_equals(target3.getAttribute("data-name"), "4th", "should be the 4th");
}, "in tree order, within the context object's tree");
test(function() {
var TEST_ID = "test6";
var s = document.createElement("div");
s.setAttribute("id", TEST_ID);
// append to Element, not Document.
document.createElement("div").appendChild(s);
assert_equals(document.getElementById(TEST_ID), null, "should be null");
}, "Modern browsers optimize this method with using internal id cache. " +
"This test checks that their optimization should effect only append to `Document`, not append to `Node`.");
test(function() {
var TEST_ID = "test7"
var element = document.createElement("div");
element.setAttribute("id", TEST_ID);
gBody.appendChild(element);
var target = document.getElementById(TEST_ID);
assert_equals(target, element, "should return the element before changing the value");
element.attributes[0].value = TEST_ID + "-updated";
var target2 = document.getElementById(TEST_ID);
assert_equals(target2, null, "should return null after updated id via Attr.value");
var target3 = document.getElementById(TEST_ID + "-updated");
assert_equals(target3, element, "should be equal to the updated element.");
}, "changing attribute's value via `Attr` gotten from `Element.attribute`.");
test(function() {
var TEST_ID = "test8";
// setup fixture
var element = document.createElement("div");
element.setAttribute("id", TEST_ID + "-fixture");
gBody.appendChild(element);
// add id-ed element with using innerHTML
element.innerHTML = "<div id='"+ TEST_ID +"'></div>";
var test = document.getElementById(TEST_ID);
assert_equals(test, element.firstChild, "should not be null");
assert_equals(test.tagName, "DIV", "should have expected tag name.");
assert_true(test instanceof HTMLDivElement, "should be a valid Element instance");
}, "add id attribute via innerHTML");
test(function() {
var TEST_ID = "test9";
// add fixture
var fixture = document.createElement("div");
fixture.setAttribute("id", TEST_ID + "-fixture");
gBody.appendChild(fixture);
var element = document.createElement("div");
element.setAttribute("id", TEST_ID);
fixture.appendChild(element);
// check 'getElementById' should get the 'element'
assert_equals(document.getElementById(TEST_ID), element, "should not be null");
// remove id-ed element with using innerHTML (clear 'element')
fixture.innerHTML = "";
var test = document.getElementById(TEST_ID);
assert_equals(test, null, "should be null.");
}, "remove id attribute via innerHTML");
test(function() {
var TEST_ID = "test10";
// setup fixture
var element = document.createElement("div");
element.setAttribute("id", TEST_ID + "-fixture");
gBody.appendChild(element);
// add id-ed element with using outerHTML
element.outerHTML = "<div id='"+ TEST_ID +"'></div>";
var test = document.getElementById(TEST_ID);
assert_not_equals(test, null, "should not be null");
assert_equals(test.tagName, "DIV", "should have expected tag name.");
assert_true(test instanceof HTMLDivElement,"should be a valid Element instance");
}, "add id attribute via outerHTML");
test(function() {
var TEST_ID = "test11";
var element = document.createElement("div");
element.setAttribute("id", TEST_ID);
gBody.appendChild(element);
var test = document.getElementById(TEST_ID);
assert_equals(test, element, "should be equal to the appended element.");
// remove id-ed element with using outerHTML
element.outerHTML = "<div></div>";
var test = document.getElementById(TEST_ID);
assert_equals(test, null, "should be null.");
}, "remove id attribute via outerHTML");
test(function() {
// setup fixtures.
var TEST_ID = "test12";
var test = document.createElement("div");
test.id = TEST_ID;
gBody.appendChild(test);
// update id
var UPDATED_ID = TEST_ID + "-updated";
test.id = UPDATED_ID;
var e = document.getElementById(UPDATED_ID);
assert_equals(e, test, "should get the element with id.");
var old = document.getElementById(TEST_ID);
assert_equals(old, null, "shouldn't get the element by the old id.");
// remove id.
test.id = "";
var e2 = document.getElementById(UPDATED_ID);
assert_equals(e2, null, "should return null when the passed id is none in document.");
}, "update `id` attribute via element.id");
test(function() {
var TEST_ID = "test13";
var create_same_id_element = function (order) {
var element = document.createElement("div");
element.setAttribute("id", TEST_ID);
element.setAttribute("data-order", order);// for debug
return element;
};
// create fixture
var container = document.createElement("div");
container.setAttribute("id", TEST_ID + "-fixture");
gBody.appendChild(container);
var element1 = create_same_id_element("1");
var element2 = create_same_id_element("2");
var element3 = create_same_id_element("3");
var element4 = create_same_id_element("4");
// append element: 2 -> 4 -> 3 -> 1
container.appendChild(element2);
container.appendChild(element4);
container.insertBefore(element3, element4);
container.insertBefore(element1, element2);
var test = document.getElementById(TEST_ID);
assert_equals(test, element1, "should return 1st element");
container.removeChild(element1);
test = document.getElementById(TEST_ID);
assert_equals(test, element2, "should return 2nd element");
container.removeChild(element2);
test = document.getElementById(TEST_ID);
assert_equals(test, element3, "should return 3rd element");
container.removeChild(element3);
test = document.getElementById(TEST_ID);
assert_equals(test, element4, "should return 4th element");
container.removeChild(element4);
}, "where insertion order and tree order don't match");
test(function() {
var TEST_ID = "test14";
var a = document.createElement("a");
var b = document.createElement("b");
a.appendChild(b);
b.id = TEST_ID;
assert_equals(document.getElementById(TEST_ID), null);
gBody.appendChild(a);
assert_equals(document.getElementById(TEST_ID), b);
}, "Inserting an id by inserting its parent node");
test(function () {
var TEST_ID = "test15"
var outer = document.getElementById("outer");
var middle = document.getElementById("middle");
var inner = document.getElementById("inner");
outer.removeChild(middle);
var new_el = document.createElement("h1");
new_el.id = "heading";
inner.appendChild(new_el);
// the new element is not part of the document since
// "middle" element was removed previously
assert_equals(document.getElementById("heading"), null);
}, "Document.getElementById must not return nodes not present in document");
// TODO:
// id attribute in a namespace
// TODO:
// SVG + MathML elements with id attributes
</script>
</body>
</html>
|