1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
/* 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::UnionTypes::StringOrURLSearchParams;
use dom::bindings::codegen::UnionTypes::StringOrURLSearchParams::{eURLSearchParams, eString};
use dom::bindings::error::Fallible;
use dom::bindings::global::GlobalRef;
use dom::bindings::js::{JSRef, Rootable, Temporary};
use dom::bindings::utils::{Reflector, reflect_dom_object};
use encoding::types::EncodingRef;
use url::form_urlencoded::{parse, serialize_with_encoding};
use util::str::DOMString;
// https://url.spec.whatwg.org/#interface-urlsearchparams
#[dom_struct]
pub struct URLSearchParams {
reflector_: Reflector,
// https://url.spec.whatwg.org/#concept-urlsearchparams-list
list: DOMRefCell<Vec<(DOMString, DOMString)>>,
}
impl URLSearchParams {
fn new_inherited() -> URLSearchParams {
URLSearchParams {
reflector_: Reflector::new(),
list: DOMRefCell::new(vec![]),
}
}
pub fn new(global: GlobalRef) -> Temporary<URLSearchParams> {
reflect_dom_object(box URLSearchParams::new_inherited(), global,
URLSearchParamsBinding::Wrap)
}
// https://url.spec.whatwg.org/#dom-urlsearchparams-urlsearchparams
pub fn Constructor(global: GlobalRef, init: Option<StringOrURLSearchParams>) ->
Fallible<Temporary<URLSearchParams>> {
// Step 1.
let query = URLSearchParams::new(global).root();
match init {
Some(eString(init)) => {
// Step 2.
let query = query.r();
*query.list.borrow_mut() = parse(init.as_bytes());
},
Some(eURLSearchParams(init)) => {
// Step 3.
// FIXME(https://github.com/rust-lang/rust/issues/23338)
let query = query.r();
let init = init.root();
let init = init.r();
*query.list.borrow_mut() = init.list.borrow().clone();
},
None => {}
}
// Step 4.
Ok(Temporary::from_rooted(query.r()))
}
}
impl<'a> URLSearchParamsMethods for JSRef<'a, URLSearchParams> {
// https://url.spec.whatwg.org/#dom-urlsearchparams-append
fn Append(self, name: DOMString, value: DOMString) {
// Step 1.
self.list.borrow_mut().push((name, value));
// Step 2.
self.update_steps();
}
// https://url.spec.whatwg.org/#dom-urlsearchparams-delete
fn Delete(self, name: DOMString) {
// Step 1.
self.list.borrow_mut().retain(|&(ref k, _)| k != &name);
// Step 2.
self.update_steps();
}
// https://url.spec.whatwg.org/#dom-urlsearchparams-get
fn Get(self, name: DOMString) -> Option<DOMString> {
let list = self.list.borrow();
list.iter().filter_map(|&(ref k, ref v)| {
if k == &name {
Some(v.clone())
} else {
None
}
}).next()
}
// https://url.spec.whatwg.org/#dom-urlsearchparams-has
fn Has(self, name: DOMString) -> bool {
let list = self.list.borrow();
list.iter().find(|&&(ref k, _)| k == &name).is_some()
}
// https://url.spec.whatwg.org/#dom-urlsearchparams-set
fn Set(self, name: DOMString, value: DOMString) {
let mut list = self.list.borrow_mut();
let mut index = None;
let mut i = 0;
list.retain(|&(ref k, _)| {
if index.is_none() {
if k == &name {
index = Some(i);
} else {
i += 1;
}
true
} else {
k != &name
}
});
match index {
Some(index) => list[index].1 = value,
None => list.push((name, value)),
};
self.update_steps();
}
// https://url.spec.whatwg.org/#stringification-behavior
fn Stringifier(self) -> DOMString {
self.serialize(None)
}
}
pub trait URLSearchParamsHelpers {
fn serialize(self, encoding: Option<EncodingRef>) -> DOMString;
}
impl<'a> URLSearchParamsHelpers for JSRef<'a, URLSearchParams> {
// https://url.spec.whatwg.org/#concept-urlencoded-serializer
fn serialize(self, encoding: Option<EncodingRef>) -> DOMString {
let list = self.list.borrow();
serialize_with_encoding(list.iter(), encoding)
}
}
trait PrivateURLSearchParamsHelpers {
fn update_steps(self);
}
impl<'a> PrivateURLSearchParamsHelpers for JSRef<'a, URLSearchParams> {
// https://url.spec.whatwg.org/#concept-uq-update
fn update_steps(self) {
// XXXManishearth Implement this when the URL interface is implemented
}
}
|