From: <rb...@us...> - 2017-11-18 15:57:36
|
Revision: 14966 http://sourceforge.net/p/htmlunit/code/14966 Author: rbri Date: 2017-11-18 15:57:33 +0000 (Sat, 18 Nov 2017) Log Message: ----------- URLSearchParams getAll/has Modified Paths: -------------- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/URLSearchParams.java trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/URLSearchParamsTest.java Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/URLSearchParams.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/URLSearchParams.java 2017-11-18 15:44:45 UTC (rev 14965) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/URLSearchParams.java 2017-11-18 15:57:33 UTC (rev 14966) @@ -30,6 +30,9 @@ import com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction; import net.sourceforge.htmlunit.corejs.javascript.Context; +import net.sourceforge.htmlunit.corejs.javascript.NativeArray; +import net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime; +import net.sourceforge.htmlunit.corejs.javascript.TopLevel; import net.sourceforge.htmlunit.corejs.javascript.Undefined; /** @@ -142,6 +145,44 @@ } /** + * The getAll() method of the URLSearchParams interface returns all the values + * associated with a given search parameter as an array. + * + * @param name The name of the parameter to return. + * @return An array of USVStrings. + */ + @JsxFunction + public NativeArray getAll(final String name) { + final List<String> result = new LinkedList<>(); + for (Entry<String, String> param : params_) { + if (param.getKey().equals(name)) { + result.add(param.getValue()); + } + } + + final NativeArray jsValues = new NativeArray(result.toArray()); + ScriptRuntime.setBuiltinProtoAndParent(jsValues, getWindow(this), TopLevel.Builtins.Array); + return jsValues; + } + + /** + * The has() method of the URLSearchParams interface returns a Boolean that + * indicates whether a parameter with the specified name exists. + * + * @param name The name of the parameter to find. + * @return A Boolean. + */ + @JsxFunction + public boolean has(final String name) { + for (Entry<String, String> param : params_) { + if (param.getKey().equals(name)) { + return true; + } + } + return false; + } + + /** * Calls for instance for implicit conversion to string. * @see com.gargoylesoftware.htmlunit.javascript.SimpleScriptable#getDefaultValue(java.lang.Class) * @param hint the type hint Modified: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/URLSearchParamsTest.java =================================================================== --- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/URLSearchParamsTest.java 2017-11-18 15:44:45 UTC (rev 14965) +++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/URLSearchParamsTest.java 2017-11-18 15:57:33 UTC (rev 14966) @@ -146,4 +146,58 @@ + "</html>"; loadPageWithAlerts2(html); } + + /** + * @throws Exception if an error occurs + */ + @Test + @Alerts(DEFAULT = { "true", "true", "false"}, + IE = {}) + public void has() throws Exception { + final String html = + "<html>\n" + + "<head>\n" + + " <script>\n" + + " function test() {\n" + + " if (self.URLSearchParams) {\n" + + " var param = new URLSearchParams('key1=val1&key2=val2&key1=val3');\n" + + " alert(param.has('key1'));\n" + + " alert(param.has('key2'));\n" + + " alert(param.has('key3'));\n" + + " }\n" + + " }\n" + + " </script>\n" + + "</head>\n" + + "<body onload='test()'>\n" + + "</body>\n" + + "</html>"; + loadPageWithAlerts2(html); + } + + /** + * @throws Exception if an error occurs + */ + @Test + @Alerts(DEFAULT = { "val1,val3", "val2", ""}, + IE = {}) + public void getAll() throws Exception { + final String html = + "<html>\n" + + "<head>\n" + + " <script>\n" + + " function test() {\n" + + " if (self.URLSearchParams) {\n" + + " var param = new URLSearchParams('key1=val1&key2=val2&key1=val3');\n" + + " alert(param.getAll('key1'));\n" + + " alert(param.getAll('key2'));\n" + + " alert(param.getAll('key3'));\n" + + " }\n" + + " }\n" + + " </script>\n" + + "</head>\n" + + "<body onload='test()'>\n" + + "</body>\n" + + "</html>"; + loadPageWithAlerts2(html); + } } |