Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make URLSearchParams iterable #13022 #13077

Closed
wants to merge 8 commits into from
@@ -3,11 +3,12 @@
* 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::global::GlobalRef;
use dom::bindings::iterable::Iterable;
use dom::bindings::js::Root;
use dom::bindings::reflector::{Reflector, reflect_dom_object};
use dom::bindings::str::{DOMString, USVString};
@@ -37,7 +38,7 @@ impl URLSearchParams {

pub fn new(global: GlobalRef, 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
@@ -163,3 +164,23 @@ impl URLSearchParams {
}
}
}


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)
}
}
@@ -16,7 +16,6 @@ interface URLSearchParams {
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;
};

"path": "url/urlsearchparams-delete.html",
"url": "/url/urlsearchparams-delete.html"
},
{
"path": "url/urlsearchparams-foreach.html",
"url": "/url/urlsearchparams-foreach.html"
},
{
"path": "url/urlsearchparams-get.html",
"url": "/url/urlsearchparams-get.html"
@@ -0,0 +1,56 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<link rel="help" href="http://url.spec.whatwg.org/#dom-urlsearchparams-has">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>

test(function() {
var params = new URLSearchParams('a=1&b=2&c=3');
params.forEach(function(value,index){

This comment has been minimized.

@Ms2ger

Ms2ger Aug 31, 2016

Contributor

I think the suggestion was to make this test more like

var found = [];
params.forEach(function(value, index) {
  found.push(value);
});
assert_array_equals(['1', '2', '3']);

This comment has been minimized.

@emilio

emilio Sep 21, 2016

Member

nit: space after comma here.

switch (index) {
case 'a':
assert_equals('1', value);
break;
case 'b':
assert_equals('2', value);
break;
case 'c':
assert_equals('3', value);
break;
}
});
var found = [];
params.forEach(function(value, index) {
found.push(value);
});
assert_array_equals(found,['1', '2', '3']);
}, "ForEach Check");

test(function(){
let a=new URL("http://a.b/c?a=1&b=2&c=3&d=4");
let b = a.searchParams;
var c = [];
for (i of b) {
a.search="x=1&y=2&z=3";

This comment has been minimized.

@emilio

emilio Sep 21, 2016

Member

nit: Space before and after equals here.

c.push(i);
}
assert_array_equals(c[0],["a","1"]);
assert_array_equals(c[1],["y","2"]);
assert_array_equals(c[2],["z","3"]);
}, "For-of Check");

test(function(){
let a=new URL("http://a.b/c");

This comment has been minimized.

@emilio

emilio Sep 21, 2016

Member

nit: Space before and after equals here.

let b = a.searchParams;
var c = [];
for (i of b) {
c.push(i);
}
assert_equals(c.length,0);

This comment has been minimized.

@emilio

emilio Sep 21, 2016

Member

nit: Space after comma here.

},"empty");
</script>
</head>
</html>
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.