Auto merge of #22555 - CYBAI:new-urlsearchparams, r=jdm

Construct URLSearchParams from array or object

---
- [x] `./mach build -d` does not report any errors
- [x] `./mach test-tidy` does not report any errors
- [x] These changes fix #22554, fix #22556 and also fix #22557
- [x] There are tests in `url/urlsearchparams-constructor.any.js` for these changes

<!-- 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/22555)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2018-12-26 08:36:00 -05:00 committed by GitHub
commit 7bc6c8d18f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 53 additions and 67 deletions

View file

@ -7,6 +7,7 @@
use crate::dom::bindings::conversions::jsid_to_string;
use crate::dom::bindings::error::report_pending_exception;
use crate::dom::bindings::str::DOMString;
use indexmap::IndexMap;
use js::conversions::{ConversionResult, FromJSValConvertible, ToJSValConvertible};
use js::jsapi::JSContext;
use js::jsapi::JS_NewPlainObject;
@ -22,28 +23,27 @@ use js::rust::wrappers::JS_GetPropertyById;
use js::rust::HandleValue;
use js::rust::IdVector;
use js::rust::MutableHandleValue;
use std::collections::HashMap;
use std::ops::Deref;
/// The `MozMap` (open-ended dictionary) type.
#[derive(Clone, JSTraceable)]
pub struct MozMap<T> {
map: HashMap<DOMString, T>,
map: IndexMap<DOMString, T>,
}
impl<T> MozMap<T> {
/// Create an empty `MozMap`.
pub fn new() -> Self {
MozMap {
map: HashMap::new(),
map: IndexMap::new(),
}
}
}
impl<T> Deref for MozMap<T> {
type Target = HashMap<DOMString, T>;
type Target = IndexMap<DOMString, T>;
fn deref(&self) -> &HashMap<DOMString, T> {
fn deref(&self) -> &IndexMap<DOMString, T> {
&self.map
}
}
@ -82,7 +82,7 @@ where
));
}
let mut map = HashMap::new();
let mut map = IndexMap::new();
for id in &*ids {
rooted!(in(cx) let id = *id);

View file

@ -61,6 +61,7 @@ use html5ever::{LocalName, Namespace, Prefix, QualName};
use http::header::HeaderMap;
use hyper::Method;
use hyper::StatusCode;
use indexmap::IndexMap;
use ipc_channel::ipc::{IpcReceiver, IpcSender};
use js::glue::{CallObjectTracer, CallValueTracer};
use js::jsapi::{GCTraceKindToAscii, Heap, JSObject, JSTracer, TraceKind};
@ -356,6 +357,21 @@ unsafe impl<K: Ord + JSTraceable, V: JSTraceable> JSTraceable for BTreeMap<K, V>
}
}
unsafe impl<K, V, S> JSTraceable for IndexMap<K, V, S>
where
K: Hash + Eq + JSTraceable,
V: JSTraceable,
S: BuildHasher,
{
#[inline]
unsafe fn trace(&self, trc: *mut JSTracer) {
for (k, v) in &*self {
k.trace(trc);
v.trace(trc);
}
}
}
unsafe impl<A: JSTraceable, B: JSTraceable> JSTraceable for (A, B) {
#[inline]
unsafe fn trace(&self, trc: *mut JSTracer) {

View file

@ -5,8 +5,8 @@
use crate::dom::bindings::cell::DomRefCell;
use crate::dom::bindings::codegen::Bindings::URLSearchParamsBinding::URLSearchParamsMethods;
use crate::dom::bindings::codegen::Bindings::URLSearchParamsBinding::URLSearchParamsWrap;
use crate::dom::bindings::codegen::UnionTypes::USVStringOrURLSearchParams;
use crate::dom::bindings::error::Fallible;
use crate::dom::bindings::codegen::UnionTypes::USVStringSequenceSequenceOrUSVStringUSVStringRecordOrUSVString;
use crate::dom::bindings::error::{Error, Fallible};
use crate::dom::bindings::iterable::Iterable;
use crate::dom::bindings::reflector::{reflect_dom_object, Reflector};
use crate::dom::bindings::root::DomRoot;
@ -47,13 +47,30 @@ impl URLSearchParams {
// https://url.spec.whatwg.org/#dom-urlsearchparams-urlsearchparams
pub fn Constructor(
global: &GlobalScope,
init: Option<USVStringOrURLSearchParams>,
init: Option<USVStringSequenceSequenceOrUSVStringUSVStringRecordOrUSVString>,
) -> Fallible<DomRoot<URLSearchParams>> {
// Step 1.
let query = URLSearchParams::new(global, None);
match init {
Some(USVStringOrURLSearchParams::USVString(init)) => {
Some(USVStringSequenceSequenceOrUSVStringUSVStringRecordOrUSVString::USVStringSequenceSequence(init)) => {
// Step 2.
// Step 2-1.
if init.iter().any(|pair| pair.len() != 2) {
return Err(Error::Type("Sequence initializer must only contain pair elements.".to_string()));
}
// Step 2-2.
*query.list.borrow_mut() =
init.iter().map(|pair| (pair[0].to_string(), pair[1].to_string())).collect::<Vec<_>>();
},
Some(USVStringSequenceSequenceOrUSVStringUSVStringRecordOrUSVString::USVStringUSVStringRecord(init)) => {
// Step 3.
*query.list.borrow_mut() =
(*init).iter().map(|(name, value)| (name.to_string(), value.to_string())).collect::<Vec<_>>();
},
Some(USVStringSequenceSequenceOrUSVStringUSVStringRecordOrUSVString::USVString(init)) => {
// Step 4.
let init_bytes = match init.0.chars().next() {
Some(first_char) if first_char == '?' => {
let (_, other_bytes) = init.0.as_bytes().split_at(1);
@ -66,13 +83,10 @@ impl URLSearchParams {
*query.list.borrow_mut() =
form_urlencoded::parse(init_bytes).into_owned().collect();
},
Some(USVStringOrURLSearchParams::URLSearchParams(init)) => {
// Step 3.
*query.list.borrow_mut() = init.list.borrow().clone();
},
None => {},
}
// Step 4.
// Step 5.
Ok(query)
}

View file

@ -6,7 +6,9 @@
* https://url.spec.whatwg.org/#interface-urlsearchparams
*/
[Constructor(optional (USVString or URLSearchParams) init/* = ""*/), Exposed=(Window,Worker)]
[Constructor(
optional (sequence<sequence<USVString>> or record<USVString, USVString> or USVString) init/* = ""*/
), Exposed=(Window,Worker)]
interface URLSearchParams {
void append(USVString name, USVString value);
void delete(USVString name);