aboutsummaryrefslogtreecommitdiffstats
path: root/components/script/dom/urlsearchparams.rs
diff options
context:
space:
mode:
Diffstat (limited to 'components/script/dom/urlsearchparams.rs')
-rw-r--r--components/script/dom/urlsearchparams.rs124
1 files changed, 77 insertions, 47 deletions
diff --git a/components/script/dom/urlsearchparams.rs b/components/script/dom/urlsearchparams.rs
index 58391d65151..ef27b969990 100644
--- a/components/script/dom/urlsearchparams.rs
+++ b/components/script/dom/urlsearchparams.rs
@@ -1,21 +1,19 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
- * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
-
-use dom::bindings::cell::DOMRefCell;
-use dom::bindings::codegen::Bindings::URLSearchParamsBinding::URLSearchParamsMethods;
-use dom::bindings::codegen::Bindings::URLSearchParamsBinding::URLSearchParamsWrap;
-use dom::bindings::codegen::UnionTypes::USVStringOrURLSearchParams;
-use dom::bindings::error::Fallible;
-use dom::bindings::iterable::Iterable;
-use dom::bindings::js::Root;
-use dom::bindings::reflector::{Reflector, reflect_dom_object};
-use dom::bindings::str::{DOMString, USVString};
-use dom::bindings::weakref::MutableWeakRef;
-use dom::globalscope::GlobalScope;
-use dom::url::URL;
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
+
+use crate::dom::bindings::cell::DomRefCell;
+use crate::dom::bindings::codegen::Bindings::URLSearchParamsBinding::URLSearchParamsMethods;
+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;
+use crate::dom::bindings::str::{DOMString, USVString};
+use crate::dom::bindings::weakref::MutableWeakRef;
+use crate::dom::globalscope::GlobalScope;
+use crate::dom::url::URL;
use dom_struct::dom_struct;
-use encoding::types::EncodingRef;
use url::form_urlencoded;
// https://url.spec.whatwg.org/#interface-urlsearchparams
@@ -23,7 +21,7 @@ use url::form_urlencoded;
pub struct URLSearchParams {
reflector_: Reflector,
// https://url.spec.whatwg.org/#concept-urlsearchparams-list
- list: DOMRefCell<Vec<(String, String)>>,
+ list: DomRefCell<Vec<(String, String)>>,
// https://url.spec.whatwg.org/#concept-urlsearchparams-url-object
url: MutableWeakRef<URL>,
}
@@ -32,34 +30,58 @@ impl URLSearchParams {
fn new_inherited(url: Option<&URL>) -> URLSearchParams {
URLSearchParams {
reflector_: Reflector::new(),
- list: DOMRefCell::new(url.map_or(Vec::new(), |url| url.query_pairs())),
+ list: DomRefCell::new(url.map_or(Vec::new(), |url| url.query_pairs())),
url: MutableWeakRef::new(url),
}
}
- pub fn new(global: &GlobalScope, url: Option<&URL>) -> Root<URLSearchParams> {
- reflect_dom_object(box URLSearchParams::new_inherited(url), global,
- URLSearchParamsWrap)
+ pub fn new(global: &GlobalScope, url: Option<&URL>) -> DomRoot<URLSearchParams> {
+ reflect_dom_object(Box::new(URLSearchParams::new_inherited(url)), global)
}
// https://url.spec.whatwg.org/#dom-urlsearchparams-urlsearchparams
- pub fn Constructor(global: &GlobalScope, init: Option<USVStringOrURLSearchParams>) ->
- Fallible<Root<URLSearchParams>> {
+ #[allow(non_snake_case)]
+ pub fn Constructor(
+ global: &GlobalScope,
+ init: USVStringSequenceSequenceOrUSVStringUSVStringRecordOrUSVString,
+ ) -> Fallible<DomRoot<URLSearchParams>> {
// Step 1.
let query = URLSearchParams::new(global, None);
match init {
- Some(USVStringOrURLSearchParams::USVString(init)) => {
+ USVStringSequenceSequenceOrUSVStringUSVStringRecordOrUSVString::USVStringSequenceSequence(init) => {
// Step 2.
- *query.list.borrow_mut() = form_urlencoded::parse(init.0.as_bytes())
- .into_owned().collect();
+
+ // 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(USVStringOrURLSearchParams::URLSearchParams(init)) => {
+ USVStringSequenceSequenceOrUSVStringUSVStringRecordOrUSVString::USVStringUSVStringRecord(init) => {
// Step 3.
- *query.list.borrow_mut() = init.list.borrow().clone();
+ *query.list.borrow_mut() =
+ (*init).iter().map(|(name, value)| (name.to_string(), value.to_string())).collect::<Vec<_>>();
},
- None => {}
+ 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);
+
+ other_bytes
+ },
+ _ => init.0.as_bytes(),
+ };
+
+ *query.list.borrow_mut() =
+ form_urlencoded::parse(init_bytes).into_owned().collect();
+ }
}
- // Step 4.
+
+ // Step 5.
Ok(query)
}
@@ -88,20 +110,23 @@ impl URLSearchParamsMethods for URLSearchParams {
// https://url.spec.whatwg.org/#dom-urlsearchparams-get
fn Get(&self, name: USVString) -> Option<USVString> {
let list = self.list.borrow();
- list.iter().find(|&kv| kv.0 == name.0)
+ list.iter()
+ .find(|&kv| kv.0 == name.0)
.map(|ref kv| USVString(kv.1.clone()))
}
// https://url.spec.whatwg.org/#dom-urlsearchparams-getall
fn GetAll(&self, name: USVString) -> Vec<USVString> {
let list = self.list.borrow();
- list.iter().filter_map(|&(ref k, ref v)| {
- if k == &name.0 {
- Some(USVString(v.clone()))
- } else {
- None
- }
- }).collect()
+ list.iter()
+ .filter_map(|&(ref k, ref v)| {
+ if k == &name.0 {
+ Some(USVString(v.clone()))
+ } else {
+ None
+ }
+ })
+ .collect()
}
// https://url.spec.whatwg.org/#dom-urlsearchparams-has
@@ -133,31 +158,37 @@ impl URLSearchParamsMethods for URLSearchParams {
Some(index) => list[index].1 = value.0,
None => list.push((name.0, value.0)), // Step 2.
};
- } // Un-borrow self.list
- // Step 3.
+ } // Un-borrow self.list
+ // Step 3.
+ self.update_steps();
+ }
+
+ // https://url.spec.whatwg.org/#dom-urlsearchparams-sort
+ fn Sort(&self) {
+ // Step 1.
+ self.list
+ .borrow_mut()
+ .sort_by(|(a, _), (b, _)| a.encode_utf16().cmp(b.encode_utf16()));
+
+ // Step 2.
self.update_steps();
}
// https://url.spec.whatwg.org/#stringification-behavior
fn Stringifier(&self) -> DOMString {
- DOMString::from(self.serialize(None))
+ DOMString::from(self.serialize_utf8())
}
}
-
impl URLSearchParams {
// https://url.spec.whatwg.org/#concept-urlencoded-serializer
- pub fn serialize(&self, encoding: Option<EncodingRef>) -> String {
+ pub fn serialize_utf8(&self) -> String {
let list = self.list.borrow();
form_urlencoded::Serializer::new(String::new())
- .encoding_override(encoding)
.extend_pairs(&*list)
.finish()
}
-}
-
-impl URLSearchParams {
// https://url.spec.whatwg.org/#concept-urlsearchparams-update
fn update_steps(&self) {
if let Some(url) = self.url.root() {
@@ -166,7 +197,6 @@ impl URLSearchParams {
}
}
-
impl Iterable for URLSearchParams {
type Key = USVString;
type Value = USVString;