aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCYBAI <cyb.ai.815@gmail.com>2018-12-25 21:45:39 +0800
committerCYBAI <cyb.ai.815@gmail.com>2018-12-26 18:05:42 +0800
commit4ba0ab7bd01482649ce9beff905d35c7471f3e1b (patch)
tree07b3233c0d06d26f16c0f2ada19f49182a5284c4
parent47a308b277f1023f5fda0c3db465317a8691818a (diff)
downloadservo-4ba0ab7bd01482649ce9beff905d35c7471f3e1b.tar.gz
servo-4ba0ab7bd01482649ce9beff905d35c7471f3e1b.zip
Construct URLSearchParams from array or object
-rw-r--r--components/script/dom/urlsearchparams.rs32
-rw-r--r--components/script/dom/webidls/URLSearchParams.webidl4
-rw-r--r--tests/wpt/metadata/url/urlsearchparams-constructor.any.js.ini43
-rw-r--r--tests/wpt/metadata/xhr/send-entity-body-basic.htm.ini5
4 files changed, 26 insertions, 58 deletions
diff --git a/components/script/dom/urlsearchparams.rs b/components/script/dom/urlsearchparams.rs
index 5be91c9304c..9a4e6b54765 100644
--- a/components/script/dom/urlsearchparams.rs
+++ b/components/script/dom/urlsearchparams.rs
@@ -5,8 +5,8 @@
use crate::dom::bindings::cell::DomRefCell;
use crate::dom::bindings::codegen::Bindings::URLSearchParamsBinding::URLSearchParamsMethods;
use crate::dom::bindings::codegen::Bindings::URLSearchParamsBinding::URLSearchParamsWrap;
-use crate::dom::bindings::codegen::UnionTypes::USVStringOrURLSearchParams;
-use crate::dom::bindings::error::Fallible;
+use crate::dom::bindings::codegen::UnionTypes::USVStringSequenceSequenceOrUSVStringUSVStringRecordOrUSVString;
+use crate::dom::bindings::error::{Error, Fallible};
use crate::dom::bindings::iterable::Iterable;
use crate::dom::bindings::reflector::{reflect_dom_object, Reflector};
use crate::dom::bindings::root::DomRoot;
@@ -47,13 +47,30 @@ impl URLSearchParams {
// https://url.spec.whatwg.org/#dom-urlsearchparams-urlsearchparams
pub fn Constructor(
global: &GlobalScope,
- init: Option<USVStringOrURLSearchParams>,
+ init: Option<USVStringSequenceSequenceOrUSVStringUSVStringRecordOrUSVString>,
) -> Fallible<DomRoot<URLSearchParams>> {
// Step 1.
let query = URLSearchParams::new(global, None);
match init {
- Some(USVStringOrURLSearchParams::USVString(init)) => {
+ Some(USVStringSequenceSequenceOrUSVStringUSVStringRecordOrUSVString::USVStringSequenceSequence(init)) => {
// Step 2.
+
+ // Step 2-1.
+ if init.iter().any(|pair| pair.len() != 2) {
+ return Err(Error::Type("Sequence initializer must only contain pair elements.".to_string()));
+ }
+
+ // Step 2-2.
+ *query.list.borrow_mut() =
+ init.iter().map(|pair| (pair[0].to_string(), pair[1].to_string())).collect::<Vec<_>>();
+ },
+ Some(USVStringSequenceSequenceOrUSVStringUSVStringRecordOrUSVString::USVStringUSVStringRecord(init)) => {
+ // Step 3.
+ *query.list.borrow_mut() =
+ (*init).iter().map(|(name, value)| (name.to_string(), value.to_string())).collect::<Vec<_>>();
+ },
+ Some(USVStringSequenceSequenceOrUSVStringUSVStringRecordOrUSVString::USVString(init)) => {
+ // Step 4.
let init_bytes = match init.0.chars().next() {
Some(first_char) if first_char == '?' => {
let (_, other_bytes) = init.0.as_bytes().split_at(1);
@@ -66,13 +83,10 @@ impl URLSearchParams {
*query.list.borrow_mut() =
form_urlencoded::parse(init_bytes).into_owned().collect();
},
- Some(USVStringOrURLSearchParams::URLSearchParams(init)) => {
- // Step 3.
- *query.list.borrow_mut() = init.list.borrow().clone();
- },
None => {},
}
- // Step 4.
+
+ // Step 5.
Ok(query)
}
diff --git a/components/script/dom/webidls/URLSearchParams.webidl b/components/script/dom/webidls/URLSearchParams.webidl
index 341e6fb2fba..8e72b207c0c 100644
--- a/components/script/dom/webidls/URLSearchParams.webidl
+++ b/components/script/dom/webidls/URLSearchParams.webidl
@@ -6,7 +6,9 @@
* https://url.spec.whatwg.org/#interface-urlsearchparams
*/
-[Constructor(optional (USVString or URLSearchParams) init/* = ""*/), Exposed=(Window,Worker)]
+[Constructor(
+ optional (sequence<sequence<USVString>> or record<USVString, USVString> or USVString) init/* = ""*/
+), Exposed=(Window,Worker)]
interface URLSearchParams {
void append(USVString name, USVString value);
void delete(USVString name);
diff --git a/tests/wpt/metadata/url/urlsearchparams-constructor.any.js.ini b/tests/wpt/metadata/url/urlsearchparams-constructor.any.js.ini
index f62df6a0576..09df91b6479 100644
--- a/tests/wpt/metadata/url/urlsearchparams-constructor.any.js.ini
+++ b/tests/wpt/metadata/url/urlsearchparams-constructor.any.js.ini
@@ -1,51 +1,8 @@
[urlsearchparams-constructor.any.worker.html]
- [Construct with object with two keys]
- expected: FAIL
-
- [Construct with object with NULL, non-ASCII, and surrogate keys]
- expected: FAIL
-
- [URLSearchParams constructor, {} as argument]
- expected: FAIL
-
- [Custom [Symbol.iterator\]]
- expected: FAIL
-
[URLSearchParams constructor, DOMException as argument]
expected: FAIL
- [Constructor with sequence of sequences of strings]
- expected: FAIL
-
- [Construct with object with +]
- expected: FAIL
-
- [Construct with array with two keys]
- expected: FAIL
-
[urlsearchparams-constructor.any.html]
- [Construct with object with two keys]
- expected: FAIL
-
- [Construct with object with NULL, non-ASCII, and surrogate keys]
- expected: FAIL
-
- [URLSearchParams constructor, {} as argument]
- expected: FAIL
-
- [Custom [Symbol.iterator\]]
- expected: FAIL
-
[URLSearchParams constructor, DOMException as argument]
expected: FAIL
-
- [Constructor with sequence of sequences of strings]
- expected: FAIL
-
- [Construct with object with +]
- expected: FAIL
-
- [Construct with array with two keys]
- expected: FAIL
-
diff --git a/tests/wpt/metadata/xhr/send-entity-body-basic.htm.ini b/tests/wpt/metadata/xhr/send-entity-body-basic.htm.ini
deleted file mode 100644
index 17882081cb3..00000000000
--- a/tests/wpt/metadata/xhr/send-entity-body-basic.htm.ini
+++ /dev/null
@@ -1,5 +0,0 @@
-[send-entity-body-basic.htm]
- type: testharness
- [XMLHttpRequest: send() - data argument (1=2&3=4)]
- expected: FAIL
-