summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/dom/lists/DOMTokenList-iteration.html
blob: 321bbe00da4f0d5036120d951343b5215659289a (plain)
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
<!doctype html>
<meta charset=utf-8>
<title>DOMTokenList iteration: keys, values, etc.</title>
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<span class="   a  a b "></span>
<script>
  test(function() {
    var list = document.querySelector("span").classList;
    assert_array_equals([...list], ["a", "a", "b"]);

    var keys = list.keys();
    assert_false(keys instanceof Array);
    keys = [...keys];
    assert_array_equals(keys, [0, 1, 2]);

    var values = list.values();
    assert_false(values instanceof Array);
    values = [...values];
    assert_array_equals(values, ["a", "a", "b"]);

    var entries = list.entries();
    assert_false(entries instanceof Array);
    entries = [...entries];
    assert_equals(entries.length, keys.length);
    assert_equals(entries.length, values.length);
    for (var i = 0; i < entries.length; ++i) {
      assert_array_equals(entries[i], [keys[i], values[i]]);
    }

    var cur = 0;
    var thisObj = {};
    list.forEach(function(value, key, listObj) {
      assert_equals(listObj, list);
      assert_equals(this, thisObj);
      assert_equals(value, values[cur]);
      assert_equals(key, keys[cur]);
      cur++;
    }, thisObj);
    assert_equals(cur, entries.length);

    assert_equals(list[Symbol.iterator], Array.prototype[Symbol.iterator]);
    assert_equals(list.keys, Array.prototype.keys);
    if (Array.prototype.values) {
      assert_equals(list.values, Array.prototype.values);
    }
    assert_equals(list.entries, Array.prototype.entries);
    assert_equals(list.forEach, Array.prototype.forEach);
  });
</script>