From: <rb...@us...> - 2017-11-18 15:44:48
|
Revision: 14965 http://sourceforge.net/p/htmlunit/code/14965 Author: rbri Date: 2017-11-18 15:44:45 +0000 (Sat, 18 Nov 2017) Log Message: ----------- URLSearchParams append/delete/get 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:25:50 UTC (rev 14964) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/URLSearchParams.java 2017-11-18 15:44:45 UTC (rev 14965) @@ -18,7 +18,8 @@ import static com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.FF; import java.util.AbstractMap; -import java.util.LinkedHashSet; +import java.util.LinkedList; +import java.util.List; import java.util.Map.Entry; import org.apache.commons.lang3.StringUtils; @@ -26,6 +27,7 @@ import com.gargoylesoftware.htmlunit.javascript.SimpleScriptable; import com.gargoylesoftware.htmlunit.javascript.configuration.JsxClass; import com.gargoylesoftware.htmlunit.javascript.configuration.JsxConstructor; +import com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction; import net.sourceforge.htmlunit.corejs.javascript.Context; import net.sourceforge.htmlunit.corejs.javascript.Undefined; @@ -40,7 +42,7 @@ @JsxClass({CHROME, FF}) public class URLSearchParams extends SimpleScriptable { - private final LinkedHashSet<Entry<String, String>> params_ = new LinkedHashSet<>(); + private final List<Entry<String, String>> params_ = new LinkedList<>(); /** * Constructs a new instance. @@ -96,6 +98,50 @@ } /** + * The append() method of the URLSearchParams interface appends a specified + * key/value pair as a new search parameter. + * + * @param name The name of the parameter to append. + * @param value The value of the parameter to append. + */ + @JsxFunction + public void append(final String name, final String value) { + params_.add(new AbstractMap.SimpleEntry<>(name, value)); + } + + /** + * The delete() method of the URLSearchParams interface deletes the given search + * parameter and its associated value, from the list of all search parameters. + * + * @param name The name of the parameter to be deleted. + */ + @JsxFunction + public void delete(final String name) { + for (Entry<String, String> param : params_) { + if (param.getKey().equals(name)) { + params_.remove(param); + } + } + } + + /** + * The get() method of the URLSearchParams interface returns the + * first value associated to the given search parameter. + * + * @param name The name of the parameter to return. + * @return An array of USVStrings. + */ + @JsxFunction + public String get(final String name) { + for (Entry<String, String> param : params_) { + if (param.getKey().equals(name)) { + return param.getValue(); + } + } + return null; + } + + /** * 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:25:50 UTC (rev 14964) +++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/URLSearchParamsTest.java 2017-11-18 15:44:45 UTC (rev 14965) @@ -58,4 +58,92 @@ + "</html>"; loadPageWithAlerts2(html); } + + /** + * @throws Exception if an error occurs + */ + @Test + @Alerts(DEFAULT = { "key=value", "key=value&empty-key=undefined", + "key=value&empty-key=undefined&key=overwrite"}, + IE = {}) + public void append() throws Exception { + final String html = + "<html>\n" + + "<head>\n" + + " <script>\n" + + " function test() {\n" + + " if (self.URLSearchParams) {\n" + + " var param = new URLSearchParams();\n" + + " param.append('key', 'value');\n" + + " alert(param);\n" + + " param.append('empty-key', undefined);\n" + + " alert(param);\n" + + " param.append('key', 'overwrite');\n" + + " alert(param);\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 = { "key2=val2", "key2=val2", "key2=val2"}, + IE = {}) + public void delete() 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');\n" + + " param.delete('key1');\n" + + " alert(param);\n" + + " param.delete('key3');\n" + + " alert(param);\n" + + " param.delete('key1');\n" + + " alert(param);\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", "val2", "null"}, + IE = {}) + public void get() 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.get('key1'));\n" + + " alert(param.get('key2'));\n" + + " alert(param.get('key3'));\n" + + " }\n" + + " }\n" + + " </script>\n" + + "</head>\n" + + "<body onload='test()'>\n" + + "</body>\n" + + "</html>"; + loadPageWithAlerts2(html); + } } |