From: <rb...@us...> - 2018-01-20 23:42:46
|
Revision: 15087 http://sourceforge.net/p/htmlunit/code/15087 Author: rbri Date: 2018-01-20 23:42:44 +0000 (Sat, 20 Jan 2018) Log Message: ----------- some improvements Modified Paths: -------------- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/WeakSet.java trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/WeakSetTest.java Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/WeakSet.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/WeakSet.java 2018-01-20 22:49:57 UTC (rev 15086) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/WeakSet.java 2018-01-20 23:42:44 UTC (rev 15087) @@ -29,6 +29,7 @@ import net.sourceforge.htmlunit.corejs.javascript.Context; import net.sourceforge.htmlunit.corejs.javascript.Delegator; import net.sourceforge.htmlunit.corejs.javascript.NativeArray; +import net.sourceforge.htmlunit.corejs.javascript.Scriptable; import net.sourceforge.htmlunit.corejs.javascript.ScriptableObject; import net.sourceforge.htmlunit.corejs.javascript.Undefined; @@ -36,6 +37,7 @@ * A JavaScript object for {@code WeakSet}. * * @author Ahmed Ashour + * @author Ronald Brill */ @JsxClass({CHROME, FF, EDGE}) public class WeakSet extends SimpleScriptable { @@ -54,18 +56,36 @@ */ @JsxConstructor public WeakSet(final Object iterable) { - if (iterable != Undefined.instance) { - if (iterable instanceof NativeArray) { - final NativeArray array = (NativeArray) iterable; - for (int i = 0; i < array.getLength(); i++) { - add(array.get(i)); + if (iterable == Undefined.instance) { + return; + } + + if (iterable instanceof NativeArray) { + final NativeArray array = (NativeArray) iterable; + for (int i = 0; i < array.getLength(); i++) { + final Object value = ScriptableObject.getProperty(array, i); + if (Undefined.instance != value + && value instanceof ScriptableObject) { + add(ScriptableObject.getProperty(array, i)); } } - else { - throw Context.reportRuntimeError("TypeError: object is not iterablee (" - + iterable.getClass().getName() + ")"); + return; + } + + if (iterable instanceof Scriptable) { + final Scriptable scriptable = (Scriptable) iterable; + if (Iterator.iterate(Context.getCurrentContext(), this, scriptable, + value -> { + if (Undefined.instance != value + && value instanceof ScriptableObject) { + add(value); + } + })) { + return; } } + + throw Context.reportRuntimeError("TypeError: object is not iterable (" + iterable.getClass().getName() + ")"); } /** @@ -78,9 +98,11 @@ if (value instanceof Delegator) { value = ((Delegator) value).getDelegee(); } + if (!(value instanceof ScriptableObject)) { throw Context.reportRuntimeError("TypeError: key is not an object"); } + set_.add(value); return this; } Modified: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/WeakSetTest.java =================================================================== --- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/WeakSetTest.java 2018-01-20 22:49:57 UTC (rev 15086) +++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/WeakSetTest.java 2018-01-20 23:42:44 UTC (rev 15087) @@ -25,6 +25,7 @@ * Tests for {@link WeakSet}. * * @author Ahmed Ashour + * @author Ronald Brill */ @RunWith(BrowserRunner.class) public class WeakSetTest extends WebDriverTestCase { @@ -33,6 +34,121 @@ * @throws Exception if the test fails */ @Test + @Alerts(DEFAULT = "true", + IE = {}) + public void constructorArray() throws Exception { + final String html + = "<html><head><title>foo</title><script>\n" + + "function test() {\n" + + " if (window.WeakSet) {\n" + + " var obj = {};\n" + + " var foo = {};\n" + + " var mySet = new WeakSet([obj, foo]);\n" + + " alert(mySet.has(foo));\n" + + " }\n" + + "}\n" + + "</script></head><body onload='test()'>\n" + + "</body></html>"; + + loadPageWithAlerts2(html); + } + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts(DEFAULT = { "false", "true"}, + IE = {}) + public void constructorSetParam() throws Exception { + final String html + = "<html><head><title>foo</title><script>\n" + + "function test() {\n" + + " if (window.WeakSet) {\n" + + " var obj = {};\n" + + " var foo = {};\n" + + " var mySet = new WeakSet(new Set([foo]));\n" + + " alert(mySet.has(obj));\n" + + " alert(mySet.has(foo));\n" + + " }\n" + + "}\n" + + "</script></head><body onload='test()'>\n" + + "</body></html>"; + + loadPageWithAlerts2(html); + } + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts(DEFAULT = {"false", "false", "false"}, + IE = {}) + public void constructorMapParam() throws Exception { + final String html + = "<html><head><title>foo</title><script>\n" + + "function test() {\n" + + " if (window.WeakSet) {\n" + + " var obj = {};\n" + + " var foo = {};\n" + + " var kvArray = [['key1', obj], ['key2', foo]];\n" + + " var myMap = new Map(kvArray);\n" + + " var mySet = new WeakSet(myMap);\n" + + " alert(mySet.has('key1'));\n" + + " alert(mySet.has(obj));\n" + + " alert(mySet.has(kvArray[1]));\n" + + " }\n" + + "}\n" + + "</script></head><body onload='test()'>\n" + + "</body></html>"; + + loadPageWithAlerts2(html); + } + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts(DEFAULT = {"true", "false"}, + IE = {}) + public void constructorIteratorParam() throws Exception { + final String html + = "<html><head><title>foo</title><script>\n" + + "function logElement(value) {\n" + + " alert(value);\n" + + "}\n" + + "function test() {\n" + + " if (window.WeakSet) {\n" + + " var obj = {};\n" + + " var foo = {};\n" + + " var myIterable = {};\n" + + " myIterable[Symbol.iterator] = function() {\n" + + " return {\n" + + " next: function() {\n" + + " if (this._first) {;\n" + + " this._first = false;\n" + + " return { value: foo, done: false };\n" + + " }\n" + + " return { done: true };\n" + + " },\n" + + " _first: true\n" + + " };\n" + + " };\n" + + " var mySet = new WeakSet(myIterable);\n" + + " alert(mySet.has(foo));\n" + + " alert(mySet.has(obj));\n" + + " }\n" + + "}\n" + + "</script></head>\n" + + "<body onload='test()'>\n" + + "</body></html>"; + + loadPageWithAlerts2(html); + } + + /** + * @throws Exception if the test fails + */ + @Test @Alerts(DEFAULT = {"undefined", "true"}, IE = {}) public void has() throws Exception { @@ -52,4 +168,34 @@ + "</body></html>"; loadPageWithAlerts2(html); } + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts(DEFAULT = {"Type error", "Type error", "true"}, + IE = {}) + public void add() throws Exception { + final String html = "<html><head><script>\n" + + " function test() {\n" + + " if (window.WeakSet) {\n" + + " var mySet = new WeakSet();\n" + + + " try {\n" + + " mySet.add(7);\n" + + " } catch(e) { alert('Type error'); }\n" + + + " try {\n" + + " mySet.add('seven');\n" + + " } catch(e) { alert('Type error'); }\n" + + + " var foo = {};\n" + + " mySet.add(foo);\n" + + " alert(mySet.has(foo));\n" + + " }\n" + + " }\n" + + "</script></head><body onload='test()'>\n" + + "</body></html>"; + loadPageWithAlerts2(html); + } } |