diff options
author | bors-servo <lbergstrom+bors@mozilla.com> | 2016-04-06 14:27:30 +0530 |
---|---|---|
committer | bors-servo <lbergstrom+bors@mozilla.com> | 2016-04-06 14:27:30 +0530 |
commit | e36e3be855549ec86790c072808563538a950a41 (patch) | |
tree | 8b37806a056797209d71c5c0e8ed893f33c63dca /components/script/dom/url.rs | |
parent | 883cde424b61f4a06d52da5448da0095503b29b8 (diff) | |
parent | 7b38f289b0751a6e14ea3b5524bcadcf7f8c34dc (diff) | |
download | servo-e36e3be855549ec86790c072808563538a950a41.tar.gz servo-e36e3be855549ec86790c072808563538a950a41.zip |
Auto merge of #10351 - stjepang:impl-url-searchparams, r=Manishearth
Implement URL.searchParams
Spec: https://url.spec.whatwg.org/#dom-url-searchparams
Closes #10335.
<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="35" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/10351)
<!-- Reviewable:end -->
Diffstat (limited to 'components/script/dom/url.rs')
-rw-r--r-- | components/script/dom/url.rs | 31 |
1 files changed, 27 insertions, 4 deletions
diff --git a/components/script/dom/url.rs b/components/script/dom/url.rs index adc758a8f18..06e1af5b133 100644 --- a/components/script/dom/url.rs +++ b/components/script/dom/url.rs @@ -6,11 +6,13 @@ use dom::bindings::cell::DOMRefCell; use dom::bindings::codegen::Bindings::URLBinding::{self, URLMethods}; use dom::bindings::error::{Error, ErrorResult, Fallible}; use dom::bindings::global::GlobalRef; -use dom::bindings::js::Root; -use dom::bindings::reflector::{Reflector, reflect_dom_object}; +use dom::bindings::js::{JS, MutNullableHeap, Root}; +use dom::bindings::reflector::{Reflectable, Reflector, reflect_dom_object}; use dom::bindings::str::USVString; use dom::urlhelper::UrlHelper; +use dom::urlsearchparams::URLSearchParams; use std::borrow::ToOwned; +use std::default::Default; use url::{Host, ParseResult, Url, UrlParser}; use util::str::DOMString; @@ -24,6 +26,9 @@ pub struct URL { // https://url.spec.whatwg.org/#concept-urlutils-get-the-base base: Option<Url>, + + // https://url.spec.whatwg.org/#dom-url-searchparams + search_params: MutNullableHeap<JS<URLSearchParams>>, } impl URL { @@ -32,6 +37,7 @@ impl URL { reflector_: Reflector::new(), url: DOMRefCell::new(url), base: base, + search_params: Default::default(), } } @@ -39,6 +45,10 @@ impl URL { reflect_dom_object(box URL::new_inherited(url, base), global, URLBinding::Wrap) } + + pub fn set_query(&self, query: String) { + self.url.borrow_mut().query = Some(query); + } } impl URL { @@ -69,8 +79,13 @@ impl URL { return Err(Error::Type(format!("could not parse URL: {}", error))); } }; - // Steps 5-8. - Ok(URL::new(global, parsed_url, parsed_base)) + // Step 5: Skip (see step 8 below). + // Steps 6-7. + let result = URL::new(global, parsed_url, parsed_base); + // Step 8: Instead of construcing a new `URLSearchParams` object here, construct it + // on-demand inside `URL::SearchParams`. + // Step 9. + Ok(result) } // https://url.spec.whatwg.org/#dom-url-domaintoasciidomain @@ -189,6 +204,14 @@ impl URLMethods for URL { // https://url.spec.whatwg.org/#dom-url-search fn SetSearch(&self, value: USVString) { UrlHelper::SetSearch(&mut self.url.borrow_mut(), value); + if let Some(search_params) = self.search_params.get() { + search_params.set_list(self.url.borrow().query_pairs().unwrap_or_else(|| vec![])); + } + } + + // https://url.spec.whatwg.org/#dom-url-searchparams + fn SearchParams(&self) -> Root<URLSearchParams> { + self.search_params.or_init(|| URLSearchParams::new(self.global().r(), Some(self))) } // https://url.spec.whatwg.org/#dom-url-href |