getElementsByClassName-32.html (1713B)
1 <!DOCTYPE html> 2 <html> 3 <meta charset="utf-8"> 4 <title>Node.prototype.getElementsByClassName tests imported from jsdom</title> 5 <link rel=help href="https://dom.spec.whatwg.org/#dom-document-getelementsbyclassname"> 6 <script src="/resources/testharness.js"></script> 7 <script src="/resources/testharnessreport.js"></script> 8 9 <div class="df-article" id="1"> 10 </div> 11 <div class="df-article" id="2"> 12 </div> 13 <div class="df-article" id="3"> 14 </div> 15 16 <script> 17 "use strict"; 18 19 test(() => { 20 21 const p = document.createElement("p"); 22 p.className = "unknown"; 23 document.body.appendChild(p); 24 25 const elements = document.getElementsByClassName("first-p"); 26 assert_array_equals(elements, []); 27 28 }, "cannot find the class name"); 29 30 test(() => { 31 32 const p = document.createElement("p"); 33 p.className = "first-p"; 34 document.body.appendChild(p); 35 36 const elements = document.getElementsByClassName("first-p"); 37 assert_array_equals(elements, [p]); 38 39 }, "finds the class name"); 40 41 42 test(() => { 43 44 const p = document.createElement("p"); 45 p.className = "the-p second third"; 46 document.body.appendChild(p); 47 48 const elements1 = document.getElementsByClassName("the-p"); 49 assert_array_equals(elements1, [p]); 50 51 const elements2 = document.getElementsByClassName("second"); 52 assert_array_equals(elements2, [p]); 53 54 const elements3 = document.getElementsByClassName("third"); 55 assert_array_equals(elements3, [p]); 56 57 }, "finds the same element with multiple class names"); 58 59 test(() => { 60 61 const elements = document.getElementsByClassName("df-article"); 62 63 assert_equals(elements.length, 3); 64 assert_array_equals(Array.prototype.map.call(elements, el => el.id), ["1", "2", "3"]); 65 66 }, "does not get confused by numeric IDs"); 67 68 </script>