OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "config.h" | |
6 #include "core/dom/URLSearchParams.h" | |
7 | |
8 #include "platform/weborigin/KURL.h" | |
9 #include "wtf/text/StringBuilder.h" | |
10 #include "wtf/text/TextEncoding.h" | |
11 | |
12 namespace blink { | |
13 | |
14 namespace { | |
15 | |
16 class URLSearchParamsIterationSource final : public PairIterable<String, String> ::IterationSource { | |
17 public: | |
18 URLSearchParamsIterationSource(Vector<std::pair<String, String>> params) : m _params(params), m_current(0) { } | |
19 | |
20 bool next(ScriptState* scriptState, String& key, String& value, ExceptionSta te& exception) override | |
philipj_slow
2015/11/17 11:00:17
Maybe drop the scriptState and exception names to
Mike West
2015/11/17 12:39:36
Done.
| |
21 { | |
22 if (m_current >= m_params.size()) | |
23 return false; | |
24 | |
25 key = m_params[m_current].first; | |
26 value = m_params[m_current].second; | |
27 m_current++; | |
28 return true; | |
29 } | |
30 | |
31 private: | |
32 Vector<std::pair<String, String>> m_params; | |
philipj_slow
2015/11/17 11:00:17
If URLSearchParamsIterationSource were put on the
Mike West
2015/11/17 12:39:36
I was surprised I had to write any of this, actual
| |
33 size_t m_current; | |
34 }; | |
35 | |
36 } // namespace | |
37 | |
38 URLSearchParams* URLSearchParams::create(const URLSearchParamsInit& init) | |
39 { | |
40 if (init.isUSVString()) | |
41 return new URLSearchParams(init.getAsUSVString()); | |
42 if (init.isURLSearchParams()) | |
43 return new URLSearchParams(init.getAsURLSearchParams()); | |
44 | |
45 ASSERT(init.isNull()); | |
46 return new URLSearchParams(String()); | |
47 } | |
48 | |
49 URLSearchParams::URLSearchParams(const String& queryString) | |
50 { | |
51 if (!queryString.isEmpty()) | |
52 setInput(queryString); | |
53 } | |
54 | |
55 URLSearchParams::URLSearchParams(URLSearchParams* searchParams) | |
56 { | |
57 ASSERT(searchParams); | |
58 m_params = searchParams->m_params; | |
59 } | |
60 | |
61 URLSearchParams::~URLSearchParams() | |
62 { | |
63 } | |
64 | |
65 static String decodeString(String input) | |
66 { | |
67 return decodeURLEscapeSequences(input.replace('+', ' ')); | |
68 } | |
69 | |
70 void URLSearchParams::setInput(const String& queryString) | |
71 { | |
72 ASSERT(m_params.isEmpty()); | |
73 size_t start = 0; | |
74 size_t queryStringLength = queryString.length(); | |
75 while (start < queryStringLength) { | |
76 size_t nameStart = start; | |
77 size_t nameValueEnd = queryString.find('&', start); | |
78 if (nameValueEnd == kNotFound) | |
79 nameValueEnd = queryStringLength; | |
80 if (nameValueEnd > start) { | |
81 size_t endOfName = queryString.find('=', start); | |
82 if (endOfName == kNotFound || endOfName > nameValueEnd) | |
83 endOfName = nameValueEnd; | |
84 String name = decodeString(queryString.substring(nameStart, endOfNam e - nameStart)); | |
85 String value; | |
86 if (endOfName != nameValueEnd) | |
87 value = decodeString(queryString.substring(endOfName + 1, nameVa lueEnd - endOfName - 1)); | |
88 if (value.isNull()) | |
89 value = ""; | |
90 m_params.append(std::make_pair(name, value)); | |
91 } | |
92 start = nameValueEnd + 1; | |
93 } | |
94 } | |
95 static String encodeString(const String& input) | |
96 { | |
97 return encodeWithURLEscapeSequences(input).replace("%20", "+"); | |
98 } | |
99 | |
100 String URLSearchParams::toString() const | |
101 { | |
102 StringBuilder result; | |
103 for (size_t i = 0; i < m_params.size(); ++i) { | |
104 if (i) | |
105 result.append('&'); | |
106 result.append(encodeString(m_params[i].first)); | |
107 result.append('='); | |
108 result.append(encodeString(m_params[i].second)); | |
109 } | |
110 return result.toString(); | |
111 } | |
112 | |
113 void URLSearchParams::append(const String& name, const String& value) | |
114 { | |
115 m_params.append(std::make_pair(name, value)); | |
116 } | |
117 | |
118 void URLSearchParams::deleteAllWithName(const String& name) | |
119 { | |
120 for (size_t i = 0; i < m_params.size();) { | |
121 if (m_params[i].first == name) | |
122 m_params.remove(i); | |
123 else | |
124 i++; | |
125 } | |
126 } | |
127 | |
128 String URLSearchParams::get(const String& name) const | |
129 { | |
130 for (const auto& param : m_params) { | |
131 if (param.first == name) | |
132 return param.second; | |
133 } | |
134 return String(); | |
135 } | |
136 | |
137 Vector<String> URLSearchParams::getAll(const String& name) const | |
138 { | |
139 Vector<String> result; | |
140 for (const auto& param : m_params) { | |
141 if (param.first == name) | |
142 result.append(param.second); | |
143 } | |
144 return result; | |
145 } | |
146 | |
147 bool URLSearchParams::has(const String& name) const | |
148 { | |
149 for (const auto& param : m_params) { | |
150 if (param.first == name) | |
151 return true; | |
152 } | |
153 return false; | |
154 } | |
155 | |
156 void URLSearchParams::set(const String& name, const String& value) | |
157 { | |
158 bool foundMatch = false; | |
159 for (size_t i = 0; i < m_params.size();) { | |
160 // If there are any name-value whose name is 'name', set | |
161 // the value of the first such name-value pair to 'value' | |
162 // and remove the others. | |
163 if (m_params[i].first == name) { | |
164 if (!foundMatch) { | |
165 m_params[i++].second = value; | |
166 foundMatch = true; | |
167 } else { | |
168 m_params.remove(i); | |
169 } | |
170 } else { | |
171 i++; | |
172 } | |
173 } | |
174 // Otherwise, append a new name-value pair to the list. | |
175 if (!foundMatch) | |
176 append(name, value); | |
177 } | |
178 | |
179 DEFINE_TRACE(URLSearchParams) | |
180 { | |
181 } | |
182 | |
183 PairIterable<String, String>::IterationSource* URLSearchParams::startIteration(S criptState*, ExceptionState&) | |
184 { | |
185 return new URLSearchParamsIterationSource(m_params); | |
186 } | |
187 | |
188 } // namespace blink | |
OLD | NEW |