From: <rb...@us...> - 2017-11-18 16:59:50
|
Revision: 14967 http://sourceforge.net/p/htmlunit/code/14967 Author: rbri Date: 2017-11-18 16:59:47 +0000 (Sat, 18 Nov 2017) Log Message: ----------- URLSearchParams set 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:57:33 UTC (rev 14966) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/URLSearchParams.java 2017-11-18 16:59:47 UTC (rev 14967) @@ -18,6 +18,7 @@ import static com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.FF; import java.util.AbstractMap; +import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Map.Entry; @@ -120,9 +121,11 @@ */ @JsxFunction public void delete(final String name) { - for (Entry<String, String> param : params_) { - if (param.getKey().equals(name)) { - params_.remove(param); + final Iterator<Entry<String, String>> iter = params_.iterator(); + while (iter.hasNext()) { + final Entry<String, String> entry = iter.next(); + if (entry.getKey().equals(name)) { + iter.remove(); } } } @@ -166,6 +169,37 @@ } /** + * The set() method of the URLSearchParams interface sets the value associated with a + * given search parameter to the given value. If there were several matching values, + * this method deletes the others. If the search parameter doesn't exist, this method + * creates it. + * + * @param name The name of the parameter to set. + * @param value The value of the parameter to set. + */ + @JsxFunction + public void set(final String name, final String value) { + final Iterator<Entry<String, String>> iter = params_.iterator(); + boolean change = true; + while (iter.hasNext()) { + final Entry<String, String> entry = iter.next(); + if (entry.getKey().equals(name)) { + if (change) { + entry.setValue(value); + change = false; + } + else { + iter.remove(); + } + } + } + + if (change) { + append(name, value); + } + } + + /** * The has() method of the URLSearchParams interface returns a Boolean that * indicates whether a parameter with the specified name exists. * 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:57:33 UTC (rev 14966) +++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/URLSearchParamsTest.java 2017-11-18 16:59:47 UTC (rev 14967) @@ -36,8 +36,8 @@ * @throws Exception if an error occurs */ @Test - @Alerts(DEFAULT = { "foo=1&bar=2", ""}, - FF45 = { "%3Ffoo=1&bar=2", ""}, + @Alerts(DEFAULT = {"foo=1&bar=2", ""}, + FF45 = {"%3Ffoo=1&bar=2", ""}, IE = {}) @NotYetImplemented(FF45) public void ctor() throws Exception { @@ -63,7 +63,7 @@ * @throws Exception if an error occurs */ @Test - @Alerts(DEFAULT = { "key=value", "key=value&empty-key=undefined", + @Alerts(DEFAULT = {"key=value", "key=value&empty-key=undefined", "key=value&empty-key=undefined&key=overwrite"}, IE = {}) public void append() throws Exception { @@ -94,7 +94,7 @@ * @throws Exception if an error occurs */ @Test - @Alerts(DEFAULT = { "key2=val2", "key2=val2", "key2=val2"}, + @Alerts(DEFAULT = {"key2=val2&key2=val3", "", ""}, IE = {}) public void delete() throws Exception { final String html = @@ -103,13 +103,13 @@ + " <script>\n" + " function test() {\n" + " if (self.URLSearchParams) {\n" - + " var param = new URLSearchParams('key1=val1&key2=val2');\n" + + " var param = new URLSearchParams('key1=val1&key2=val2&key2=val3');\n" + " param.delete('key1');\n" + " alert(param);\n" + + " param.delete('key2');\n" + + " alert(param);\n" + " param.delete('key3');\n" + " alert(param);\n" - + " param.delete('key1');\n" - + " alert(param);\n" + " }\n" + " }\n" + " </script>\n" @@ -124,7 +124,7 @@ * @throws Exception if an error occurs */ @Test - @Alerts(DEFAULT = { "val1", "val2", "null"}, + @Alerts(DEFAULT = {"val1", "val2", "null"}, IE = {}) public void get() throws Exception { final String html = @@ -151,7 +151,7 @@ * @throws Exception if an error occurs */ @Test - @Alerts(DEFAULT = { "true", "true", "false"}, + @Alerts(DEFAULT = {"true", "true", "false"}, IE = {}) public void has() throws Exception { final String html = @@ -178,7 +178,7 @@ * @throws Exception if an error occurs */ @Test - @Alerts(DEFAULT = { "val1,val3", "val2", ""}, + @Alerts(DEFAULT = {"val1,val3", "val2", ""}, IE = {}) public void getAll() throws Exception { final String html = @@ -200,4 +200,36 @@ + "</html>"; loadPageWithAlerts2(html); } + + /** + * @throws Exception if an error occurs + */ + @Test + @Alerts(DEFAULT = {"key1=val1&key2=val2&key2=val3&key4=val4", + "key1=new1&key2=val2&key2=val3&key4=val4", + "key1=new1&key2=new2&key4=val4"}, + IE = {}) + public void set() 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&key2=val3');\n" + + " param.set('key4', 'val4');\n" + + " alert(param);\n" + + " param.set('key1', 'new1');\n" + + " alert(param);\n" + + " param.set('key2', 'new2');\n" + + " alert(param);\n" + + " }\n" + + " }\n" + + " </script>\n" + + "</head>\n" + + "<body onload='test()'>\n" + + "</body>\n" + + "</html>"; + loadPageWithAlerts2(html); + } } |