servo: Merge #13637 - Make URLSearchParams iterable (from servo:URLSearchParams); r=emilio
authoryoyo930021 <yoyo930021@gmail.com>
Fri, 07 Oct 2016 11:10:01 -0500
changeset 339853 8ca9059c239001a27f8fbce456f4e3162b5c019e
parent 339852 26159920024c30ba18cd7877f9064d52a75d98b5
child 339854 a235117279ca1918a965b8930553a1e6dc0d2084
push id31307
push usergszorc@mozilla.com
push dateSat, 04 Feb 2017 00:59:06 +0000
treeherdermozilla-central@94079d43835f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersemilio
servo: Merge #13637 - Make URLSearchParams iterable (from servo:URLSearchParams); r=emilio Fixes #13022. Fixes #13077. Source-Repo: https://github.com/servo/servo Source-Revision: b1c5b9182003cf56b0953a6a1aa01f47b49d3f14
servo/components/script/dom/urlsearchparams.rs
servo/components/script/dom/webidls/URLSearchParams.webidl
--- a/servo/components/script/dom/urlsearchparams.rs
+++ b/servo/components/script/dom/urlsearchparams.rs
@@ -1,17 +1,18 @@
 /* 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;
 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;
 use encoding::types::EncodingRef;
 use url::form_urlencoded;
@@ -32,17 +33,17 @@ impl URLSearchParams {
             reflector_: Reflector::new(),
             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,
-                           URLSearchParamsBinding::Wrap)
+                           URLSearchParamsWrap)
     }
 
     // https://url.spec.whatwg.org/#dom-urlsearchparams-urlsearchparams
     pub fn Constructor(global: &GlobalScope, init: Option<USVStringOrURLSearchParams>) ->
                        Fallible<Root<URLSearchParams>> {
         // Step 1.
         let query = URLSearchParams::new(global, None);
         match init {
@@ -158,8 +159,28 @@ impl URLSearchParams {
 impl URLSearchParams {
     // https://url.spec.whatwg.org/#concept-urlsearchparams-update
     fn update_steps(&self) {
         if let Some(url) = self.url.root() {
             url.set_query_pairs(&self.list.borrow())
         }
     }
 }
+
+
+impl Iterable for URLSearchParams {
+    type Key = USVString;
+    type Value = USVString;
+
+    fn get_iterable_length(&self) -> u32 {
+        self.list.borrow().len() as u32
+    }
+
+    fn get_value_at_index(&self, n: u32) -> USVString {
+        let value = self.list.borrow()[n as usize].1.clone();
+        USVString(value)
+    }
+
+    fn get_key_at_index(&self, n: u32) -> USVString {
+        let key = self.list.borrow()[n as usize].0.clone();
+        USVString(key)
+    }
+}
--- a/servo/components/script/dom/webidls/URLSearchParams.webidl
+++ b/servo/components/script/dom/webidls/URLSearchParams.webidl
@@ -11,12 +11,11 @@ interface URLSearchParams {
   void append(USVString name, USVString value);
   void delete(USVString name);
   USVString? get(USVString name);
   sequence<USVString> getAll(USVString name);
   boolean has(USVString name);
   void set(USVString name, USVString value);
   // Be careful with implementing iterable interface.
   // Search params might be mutated by URL::SetSearch while iterating (discussed in PR #10351).
-  // iterable<USVString, USVString>;
+  iterable<USVString, USVString>;
   stringifier;
 };
-