Bug 1290636. Expose iterator stuff on NodeList and DOMStringList. r=qdot
authorBoris Zbarsky <bzbarsky@mit.edu>
Sat, 30 Jul 2016 01:00:18 -0400
changeset 347418 49efb446f5b0977be697e0277cb27a2d5502fb76
parent 347417 6b62785dbfe3b989a089ca7192e224725cc2d61f
child 347419 974cfc29c1e30561d40882c051f07724eff99491
push id6389
push userraliiev@mozilla.com
push dateMon, 19 Sep 2016 13:38:22 +0000
treeherdermozilla-beta@01d67bfe6c81 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersqdot
bugs1290636
milestone50.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1290636. Expose iterator stuff on NodeList and DOMStringList. r=qdot
dom/webidl/DOMTokenList.webidl
dom/webidl/NodeList.webidl
testing/web-platform/meta/MANIFEST.json
testing/web-platform/tests/dom/lists/DOMTokenList-iteration.html
testing/web-platform/tests/dom/nodes/Node-childNodes.html
--- a/dom/webidl/DOMTokenList.webidl
+++ b/dom/webidl/DOMTokenList.webidl
@@ -22,9 +22,10 @@ interface DOMTokenList {
   void replace(DOMString token, DOMString newToken);
   [Throws]
   boolean toggle(DOMString token, optional boolean force);
   [Throws]
   boolean supports(DOMString token);
   [SetterThrows]
   attribute DOMString value;
   stringifier DOMString ();
+  iterable<DOMString?>;
 };
--- a/dom/webidl/NodeList.webidl
+++ b/dom/webidl/NodeList.webidl
@@ -8,9 +8,10 @@
  *
  * Copyright © 2012 W3C® (MIT, ERCIM, Keio), All Rights Reserved. W3C
  * liability, trademark and document use rules apply.
  */
 
 interface NodeList {
   getter Node? item(unsigned long index);
   readonly attribute unsigned long length;
+  iterable<Node?>;
 };
--- a/testing/web-platform/meta/MANIFEST.json
+++ b/testing/web-platform/meta/MANIFEST.json
@@ -36388,16 +36388,22 @@
           }
         ],
         "dom/events/AddEventListenerOptions-once.html": [
           {
             "path": "dom/events/AddEventListenerOptions-once.html",
             "url": "/dom/events/AddEventListenerOptions-once.html"
           }
         ],
+        "dom/lists/DOMTokenList-iteration.html": [
+          {
+            "path": "dom/lists/DOMTokenList-iteration.html",
+            "url": "/dom/lists/DOMTokenList-iteration.html"
+          }
+        ],
         "editing/run/justifycenter.html": [
           {
             "path": "editing/run/justifycenter.html",
             "timeout": "long",
             "url": "/editing/run/justifycenter.html"
           }
         ],
         "editing/run/multitest.html": [
new file mode 100644
--- /dev/null
+++ b/testing/web-platform/tests/dom/lists/DOMTokenList-iteration.html
@@ -0,0 +1,48 @@
+<!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);
+    assert_equals(list.values, Array.prototype.values);
+    assert_equals(list.entries, Array.prototype.entries);
+    assert_equals(list.forEach, Array.prototype.forEach);
+  });
+</script>
--- a/testing/web-platform/tests/dom/nodes/Node-childNodes.html
+++ b/testing/web-platform/tests/dom/nodes/Node-childNodes.html
@@ -40,9 +40,58 @@ test(function() {
 
 test(function() {
   check_parent_node(document.createDocumentFragment());
 }, "Node.childNodes on a DocumentFragment.");
 
 test(function() {
   check_parent_node(new Document());
 }, "Node.childNodes on a Document.");
+
+test(function() {
+  var node = document.createElement("div");
+  var kid1 = document.createElement("p");
+  var kid2 = document.createTextNode("hey");
+  var kid3 = document.createElement("span");
+  node.appendChild(kid1);
+  node.appendChild(kid2);
+  node.appendChild(kid3);
+
+  var list = node.childNodes;
+  assert_array_equals([...list], [kid1, kid2, kid3]);
+
+  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, [kid1, kid2, kid3]);
+
+  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);
+  assert_equals(list.values, Array.prototype.values);
+  assert_equals(list.entries, Array.prototype.entries);
+  assert_equals(list.forEach, Array.prototype.forEach);
+}, "Iterator behavior of Node.childNodes");
 </script>