mirror of
https://github.com/servo/servo.git
synced 2025-06-30 12:03:38 +01:00
Auto merge of #13637 - servo:URLSearchParams, r=emilio
Make URLSearchParams iterable. Fixes #13022. Fixes #13077. <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/13637) <!-- Reviewable:end -->
This commit is contained in:
commit
b1c5b91820
4 changed files with 67 additions and 4 deletions
|
@ -3,10 +3,11 @@
|
||||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||||
|
|
||||||
use dom::bindings::cell::DOMRefCell;
|
use dom::bindings::cell::DOMRefCell;
|
||||||
use dom::bindings::codegen::Bindings::URLSearchParamsBinding;
|
|
||||||
use dom::bindings::codegen::Bindings::URLSearchParamsBinding::URLSearchParamsMethods;
|
use dom::bindings::codegen::Bindings::URLSearchParamsBinding::URLSearchParamsMethods;
|
||||||
|
use dom::bindings::codegen::Bindings::URLSearchParamsBinding::URLSearchParamsWrap;
|
||||||
use dom::bindings::codegen::UnionTypes::USVStringOrURLSearchParams;
|
use dom::bindings::codegen::UnionTypes::USVStringOrURLSearchParams;
|
||||||
use dom::bindings::error::Fallible;
|
use dom::bindings::error::Fallible;
|
||||||
|
use dom::bindings::iterable::Iterable;
|
||||||
use dom::bindings::js::Root;
|
use dom::bindings::js::Root;
|
||||||
use dom::bindings::reflector::{Reflector, reflect_dom_object};
|
use dom::bindings::reflector::{Reflector, reflect_dom_object};
|
||||||
use dom::bindings::str::{DOMString, USVString};
|
use dom::bindings::str::{DOMString, USVString};
|
||||||
|
@ -37,7 +38,7 @@ impl URLSearchParams {
|
||||||
|
|
||||||
pub fn new(global: &GlobalScope, url: Option<&URL>) -> Root<URLSearchParams> {
|
pub fn new(global: &GlobalScope, url: Option<&URL>) -> Root<URLSearchParams> {
|
||||||
reflect_dom_object(box URLSearchParams::new_inherited(url), global,
|
reflect_dom_object(box URLSearchParams::new_inherited(url), global,
|
||||||
URLSearchParamsBinding::Wrap)
|
URLSearchParamsWrap)
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://url.spec.whatwg.org/#dom-urlsearchparams-urlsearchparams
|
// 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);
|
void set(USVString name, USVString value);
|
||||||
// Be careful with implementing iterable interface.
|
// Be careful with implementing iterable interface.
|
||||||
// Search params might be mutated by URL::SetSearch while iterating (discussed in PR #10351).
|
// Search params might be mutated by URL::SetSearch while iterating (discussed in PR #10351).
|
||||||
// iterable<USVString, USVString>;
|
iterable<USVString, USVString>;
|
||||||
stringifier;
|
stringifier;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -30387,6 +30387,10 @@
|
||||||
"path": "url/urlsearchparams-delete.html",
|
"path": "url/urlsearchparams-delete.html",
|
||||||
"url": "/url/urlsearchparams-delete.html"
|
"url": "/url/urlsearchparams-delete.html"
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"path": "url/urlsearchparams-foreach.html",
|
||||||
|
"url": "/url/urlsearchparams-foreach.html"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"path": "url/urlsearchparams-get.html",
|
"path": "url/urlsearchparams-get.html",
|
||||||
"url": "/url/urlsearchparams-get.html"
|
"url": "/url/urlsearchparams-get.html"
|
||||||
|
|
|
@ -0,0 +1,39 @@
|
||||||
|
<!doctype html>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<link rel="help" href="https://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');
|
||||||
|
var keys = [];
|
||||||
|
var values = [];
|
||||||
|
params.forEach(function(value, key) {
|
||||||
|
keys.push(key);
|
||||||
|
values.push(value);
|
||||||
|
});
|
||||||
|
assert_array_equals(keys, ['a', 'b', 'c']);
|
||||||
|
assert_array_equals(values, ['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";
|
||||||
|
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");
|
||||||
|
let b = a.searchParams;
|
||||||
|
for (i of b) {
|
||||||
|
assert_unreached(i);
|
||||||
|
}
|
||||||
|
}, "empty");
|
||||||
|
</script>
|
Loading…
Add table
Add a link
Reference in a new issue