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

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

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

asashour-2
Revision: 14121
          http://sourceforge.net/p/htmlunit/code/14121
Author:   asashour
Date:     2017-04-16 20:55:10 +0000 (Sun, 16 Apr 2017)
Log Message:
-----------
JavaScript: implement NodeList.entries(), keys(), values() and forEach()

Modified Paths:
--------------
    trunk/htmlunit/src/changes/changes.xml
    trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/Iterator.java
    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/changes/changes.xml
===================================================================
--- trunk/htmlunit/src/changes/changes.xml 2017-04-16 08:59:17 UTC (rev 14120)
+++ trunk/htmlunit/src/changes/changes.xml 2017-04-16 20:55:10 UTC (rev 14121)
@@ -8,6 +8,9 @@
 
     <body>
         <release version="2.27" date="???" description="GAE broken, Bugfixes">
+            <action type="add" dev="asashour">
+                JavaScript: implement NodeList.entries(), keys(), values() and forEach().
+            </action>
             <action type="fix" dev="rbri" issue="1862">
                 Neko: fix handling of strange comment syntax supported by browsers
             </action>

Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/Iterator.java
===================================================================
--- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/Iterator.java 2017-04-16 08:59:17 UTC (rev 14120)
+++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/Iterator.java 2017-04-16 20:55:10 UTC (rev 14121)
@@ -90,8 +90,8 @@
             value = Undefined.instance;
             done = true;
         }
-        object.defineProperty("done", done, ScriptableObject.DONTENUM);
-        object.defineProperty("value", value, ScriptableObject.DONTENUM);
+        object.defineProperty("done", done, ScriptableObject.EMPTY);
+        object.defineProperty("value", value, ScriptableObject.EMPTY);
         return object;
     }
 

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:59:17 UTC (rev 14120)
+++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/dom/NodeList.java 2017-04-16 20:55:10 UTC (rev 14121)
@@ -131,6 +131,24 @@
     }
 
     /**
+     * Returns an {@link Iterator} allowing to go through all key/value pairs contained in this object.
+     * @return an {@link Iterator}
+     */
+    @JsxFunction({@WebBrowser(CHROME), @WebBrowser(value = FF, minVersion = 52)})
+    public Iterator entries() {
+        final List<DomNode> elements = getElements();
+        
+        final List<Scriptable> list = new ArrayList<>();
+        for (int i = 0; i < elements.size(); i++) {
+            final Object[] array = new Object[] {i, elements.get(i).getScriptableObject()};
+            list.add(Context.getCurrentContext().newArray(getParentScope(), array));
+        }
+        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
      */

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:59:17 UTC (rev 14120)
+++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/dom/NodeListTest.java 2017-04-16 20:55:10 UTC (rev 14121)
@@ -14,15 +14,11 @@
  */
 package com.gargoylesoftware.htmlunit.javascript.host.dom;
 
-import static com.gargoylesoftware.htmlunit.BrowserRunner.Browser.CHROME;
-import static com.gargoylesoftware.htmlunit.BrowserRunner.Browser.FF52;
-
 import org.junit.Test;
 import org.junit.runner.RunWith;
 
 import com.gargoylesoftware.htmlunit.BrowserRunner;
 import com.gargoylesoftware.htmlunit.BrowserRunner.Alerts;
-import com.gargoylesoftware.htmlunit.BrowserRunner.NotYetImplemented;
 import com.gargoylesoftware.htmlunit.WebDriverTestCase;
 import com.gargoylesoftware.htmlunit.html.HtmlPageTest;
 
@@ -79,7 +75,6 @@
             FF45 = "0,1,2,3,4,5,item,length",
             IE = "0,1,2,3,4,5,item,length",
             EDGE = "0,1,2,3,4,5,item,length")
-    @NotYetImplemented({CHROME, FF52})
     public void iterator() throws Exception {
         final String html = HtmlPageTest.STANDARDS_MODE_PREFIX_ + "<html><head><title>test</title>\n"
                 + "<script>\n"
@@ -136,4 +131,37 @@
 
         loadPageWithAlerts2(html);
     }
+
+    /**
+     * @throws Exception if an error occurs
+     */
+    @Test
+    @Alerts(DEFAULT = {"done", "value", "object", "0", "[object HTMLHtmlElement]"},
+            FF45 = "not defined",
+            IE = "not defined",
+            EDGE = "not defined")
+    public void entries() throws Exception {
+        final String html = "<html><head><title>test</title>\n"
+            + "<script>\n"
+            + "  function test() {\n"
+            + "    var nodeList = document.querySelectorAll('*');\n"
+            + "    if (!nodeList.entries) {\n"
+            + "      alert('not defined');\n"
+            + "      return;\n"
+            + "    }\n"
+            + "    var i = nodeList.entries().next();\n"
+            + "    for (var x in i) {\n"
+            + "      alert(x);\n"
+            + "    }\n"
+            + "    var v = i.value;\n"
+            + "    alert(typeof v);\n"
+            + "    alert(v[0]);\n"
+            + "    alert(v[1]);\n"
+            + "  }\n"
+            + "</script>\n"
+            + "</head><body onload='test()'>\n"
+            + "</body></html>\n";
+
+        loadPageWithAlerts2(html);
+    }
 }


------------------------------------------------------------------------------
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
Loading...