[HtmlUnit] SVN: [14119] trunk/htmlunit/src

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[HtmlUnit] SVN: [14119] trunk/htmlunit/src

asashour-2
Revision: 14119
          http://sourceforge.net/p/htmlunit/code/14119
Author:   asashour
Date:     2017-04-16 08:24:19 +0000 (Sun, 16 Apr 2017)
Log Message:
-----------
Implement NodeList.forEach

Modified Paths:
--------------
    trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/dom/NodeList.java
    trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/dom/NodeListTest.java

Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/dom/NodeList.java
===================================================================
--- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/dom/NodeList.java 2017-04-16 08:10:51 UTC (rev 14118)
+++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/dom/NodeList.java 2017-04-16 08:24:19 UTC (rev 14119)
@@ -29,6 +29,9 @@
 import com.gargoylesoftware.htmlunit.javascript.configuration.WebBrowser;
 import com.gargoylesoftware.htmlunit.javascript.host.Iterator;
 
+import net.sourceforge.htmlunit.corejs.javascript.Context;
+import net.sourceforge.htmlunit.corejs.javascript.Function;
+import net.sourceforge.htmlunit.corejs.javascript.Scriptable;
 import net.sourceforge.htmlunit.corejs.javascript.ScriptableObject;
 
 /**
@@ -105,8 +108,9 @@
      */
     @JsxFunction({@WebBrowser(CHROME), @WebBrowser(value = FF, minVersion = 52)})
     public Iterator keys() {
+        final int length = getElements().size();
         final List<Integer> list = new ArrayList<>();
-        for (int i = 0; i < getLength(); i++) {
+        for (int i = 0; i < length; i++) {
             list.add(i);
         }
         final Iterator object = new Iterator("Iterator", list.iterator());
@@ -120,12 +124,30 @@
      */
     @JsxFunction({@WebBrowser(CHROME), @WebBrowser(value = FF, minVersion = 52)})
     public Iterator values() {
-        final List<Object> list = new ArrayList<>();
-        for (int i = 0; i < getLength(); i++) {
-            list.add(item(i));
-        }
+        final List<DomNode> list = getElements();
         final Iterator object = new Iterator("Iterator", list.iterator());
         object.setParentScope(getParentScope());
         return object;
     }
+
+    /**
+     * Calls the {@code callback} given in parameter once for each value pair in the list, in insertion order.
+     * @param callback function to execute for each element
+     */
+    @JsxFunction({@WebBrowser(CHROME), @WebBrowser(value = FF, minVersion = 52)})
+    public void forEach(final Object callback) {
+        final List<DomNode> nodes = getElements();
+        final Context context = Context.enter();
+        try {
+            final Function function = (Function) callback;
+            final Scriptable scope = getParentScope();
+            for (int i = 0; i < nodes.size(); i++) {
+                function.call(context, scope, this, new Object[] {
+                        nodes.get(i).getScriptableObject(), i, this});
+            }
+        }
+        finally {
+            Context.exit();
+        }
+    }
 }

Modified: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/dom/NodeListTest.java
===================================================================
--- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/dom/NodeListTest.java 2017-04-16 08:10:51 UTC (rev 14118)
+++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/dom/NodeListTest.java 2017-04-16 08:24:19 UTC (rev 14119)
@@ -115,7 +115,6 @@
             FF45 = "no forEach",
             IE = "no forEach",
             EDGE = "no forEach")
-    @NotYetImplemented({CHROME, FF52})
     public void forEach() throws Exception {
         final String html = "<html><head><title>test</title>\n"
             + "<script>\n"


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
HtmlUnit-develop mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/htmlunit-develop