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.rs52
1 files changed, 29 insertions, 23 deletions
diff --git a/components/script/dom/urlsearchparams.rs b/components/script/dom/urlsearchparams.rs
index bb7b1cb910c..800cd10db88 100644
--- a/components/script/dom/urlsearchparams.rs
+++ b/components/script/dom/urlsearchparams.rs
@@ -14,7 +14,7 @@ use dom::bindings::str::USVString;
use dom::bindings::weakref::MutableWeakRef;
use dom::url::URL;
use encoding::types::EncodingRef;
-use url::form_urlencoded::{parse, serialize_with_encoding};
+use url::form_urlencoded;
use util::str::DOMString;
// https://url.spec.whatwg.org/#interface-urlsearchparams
@@ -31,7 +31,7 @@ impl URLSearchParams {
fn new_inherited(url: Option<&URL>) -> URLSearchParams {
URLSearchParams {
reflector_: Reflector::new(),
- list: DOMRefCell::new(vec![]),
+ list: DOMRefCell::new(url.map_or(Vec::new(), |url| url.query_pairs())),
url: MutableWeakRef::new(url),
}
}
@@ -49,7 +49,8 @@ impl URLSearchParams {
match init {
Some(USVStringOrURLSearchParams::USVString(init)) => {
// Step 2.
- *query.list.borrow_mut() = parse(init.0.as_bytes());
+ *query.list.borrow_mut() = form_urlencoded::parse(init.0.as_bytes())
+ .into_owned().collect();
},
Some(USVStringOrURLSearchParams::URLSearchParams(init)) => {
// Step 3.
@@ -110,26 +111,28 @@ impl URLSearchParamsMethods for URLSearchParams {
// https://url.spec.whatwg.org/#dom-urlsearchparams-set
fn Set(&self, name: USVString, value: USVString) {
- // Step 1.
- let mut list = self.list.borrow_mut();
- let mut index = None;
- let mut i = 0;
- list.retain(|&(ref k, _)| {
- if index.is_none() {
- if k == &name.0 {
- index = Some(i);
+ {
+ // Step 1.
+ let mut list = self.list.borrow_mut();
+ let mut index = None;
+ let mut i = 0;
+ list.retain(|&(ref k, _)| {
+ if index.is_none() {
+ if k == &name.0 {
+ index = Some(i);
+ } else {
+ i += 1;
+ }
+ true
} else {
- i += 1;
+ k != &name.0
}
- true
- } else {
- k != &name.0
- }
- });
- match index {
- Some(index) => list[index].1 = value.0,
- None => list.push((name.0, value.0)), // Step 2.
- };
+ });
+ match index {
+ Some(index) => list[index].1 = value.0,
+ None => list.push((name.0, value.0)), // Step 2.
+ };
+ } // Un-borrow self.list
// Step 3.
self.update_steps();
}
@@ -145,7 +148,10 @@ impl URLSearchParams {
// https://url.spec.whatwg.org/#concept-urlencoded-serializer
pub fn serialize(&self, encoding: Option<EncodingRef>) -> String {
let list = self.list.borrow();
- serialize_with_encoding(list.iter(), encoding)
+ form_urlencoded::Serializer::new(String::new())
+ .encoding_override(encoding)
+ .extend_pairs(&*list)
+ .finish()
}
}
@@ -154,7 +160,7 @@ impl URLSearchParams {
// https://url.spec.whatwg.org/#concept-urlsearchparams-update
fn update_steps(&self) {
if let Some(url) = self.url.root() {
- url.set_query(self.serialize(None));
+ url.set_query_pairs(&self.list.borrow())
}
}
}