diff options
author | Stjepan Glavina <stjepang@gmail.com> | 2016-04-01 22:25:08 +0200 |
---|---|---|
committer | Stjepan Glavina <stjepang@gmail.com> | 2016-04-05 16:10:05 +0200 |
commit | 7b38f289b0751a6e14ea3b5524bcadcf7f8c34dc (patch) | |
tree | 3b87c195d3343a2aa753eccf59ffe888f1580dd6 /components/script/dom/urlsearchparams.rs | |
parent | b38fafcf11d2588fecc565c581260106411ad484 (diff) | |
download | servo-7b38f289b0751a6e14ea3b5524bcadcf7f8c34dc.tar.gz servo-7b38f289b0751a6e14ea3b5524bcadcf7f8c34dc.zip |
Implement URL.searchParams
Spec: https://url.spec.whatwg.org/#dom-url-searchparams
Diffstat (limited to 'components/script/dom/urlsearchparams.rs')
-rw-r--r-- | components/script/dom/urlsearchparams.rs | 27 |
1 files changed, 20 insertions, 7 deletions
diff --git a/components/script/dom/urlsearchparams.rs b/components/script/dom/urlsearchparams.rs index 27df100aac1..bb7b1cb910c 100644 --- a/components/script/dom/urlsearchparams.rs +++ b/components/script/dom/urlsearchparams.rs @@ -11,6 +11,8 @@ use dom::bindings::global::GlobalRef; use dom::bindings::js::Root; use dom::bindings::reflector::{Reflector, reflect_dom_object}; 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 util::str::DOMString; @@ -21,18 +23,21 @@ pub struct URLSearchParams { reflector_: Reflector, // https://url.spec.whatwg.org/#concept-urlsearchparams-list list: DOMRefCell<Vec<(String, String)>>, + // https://url.spec.whatwg.org/#concept-urlsearchparams-url-object + url: MutableWeakRef<URL>, } impl URLSearchParams { - fn new_inherited() -> URLSearchParams { + fn new_inherited(url: Option<&URL>) -> URLSearchParams { URLSearchParams { reflector_: Reflector::new(), list: DOMRefCell::new(vec![]), + url: MutableWeakRef::new(url), } } - pub fn new(global: GlobalRef) -> Root<URLSearchParams> { - reflect_dom_object(box URLSearchParams::new_inherited(), global, + pub fn new(global: GlobalRef, url: Option<&URL>) -> Root<URLSearchParams> { + reflect_dom_object(box URLSearchParams::new_inherited(url), global, URLSearchParamsBinding::Wrap) } @@ -40,7 +45,7 @@ impl URLSearchParams { pub fn Constructor(global: GlobalRef, init: Option<USVStringOrURLSearchParams>) -> Fallible<Root<URLSearchParams>> { // Step 1. - let query = URLSearchParams::new(global); + let query = URLSearchParams::new(global, None); match init { Some(USVStringOrURLSearchParams::USVString(init)) => { // Step 2. @@ -55,6 +60,10 @@ impl URLSearchParams { // Step 4. Ok(query) } + + pub fn set_list(&self, list: Vec<(String, String)>) { + *self.list.borrow_mut() = list; + } } impl URLSearchParamsMethods for URLSearchParams { @@ -101,6 +110,7 @@ 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; @@ -118,8 +128,9 @@ impl URLSearchParamsMethods for URLSearchParams { }); match index { Some(index) => list[index].1 = value.0, - None => list.push((name.0, value.0)), + None => list.push((name.0, value.0)), // Step 2. }; + // Step 3. self.update_steps(); } @@ -140,8 +151,10 @@ impl URLSearchParams { impl URLSearchParams { - // https://url.spec.whatwg.org/#concept-uq-update + // https://url.spec.whatwg.org/#concept-urlsearchparams-update fn update_steps(&self) { - // XXXManishearth Implement this when the URL interface is implemented + if let Some(url) = self.url.root() { + url.set_query(self.serialize(None)); + } } } |