Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(435)

Side by Side Diff: Source/core/dom/DOMURLSearchParams.cpp

Issue 143313002: Implement URLSearchParams. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Rebased Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Source/core/dom/DOMURLSearchParams.h ('k') | Source/core/dom/DOMURLUtils.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2014 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/DOMURLSearchParams.h"
7
8 #include "core/dom/DOMURLUtils.h"
9 #include "platform/weborigin/KURL.h"
10 #include "wtf/text/StringBuilder.h"
11 #include "wtf/text/TextEncoding.h"
12
13 namespace blink {
14
15 DOMURLSearchParams::DOMURLSearchParams(const String& queryString, DOMURLUtils* u rlObject)
16 : m_isUpdating(false)
17 {
18 ScriptWrappable::init(this);
19 if (!queryString.isEmpty())
20 setInput(queryString);
21 if (urlObject)
22 addURLObject(urlObject);
23 }
24
25 DOMURLSearchParams::DOMURLSearchParams(DOMURLSearchParams* searchParams, DOMURLU tils* urlObject)
26 : m_isUpdating(false)
27 {
28 ScriptWrappable::init(this);
29 ASSERT(searchParams);
30 m_params = searchParams->m_params;
31 if (urlObject)
32 addURLObject(urlObject);
33 }
34
35 DOMURLSearchParams::~DOMURLSearchParams()
36 {
37 }
38
39 void DOMURLSearchParams::dropURLObject(DOMURLUtils* urlObject)
40 {
41 for (unsigned i = 0; i < m_urlObjects.size(); i++) {
42 if (m_urlObjects[i] == urlObject) {
43 m_urlObjects.remove(i);
44 return;
45 }
46 }
47 }
48
49 void DOMURLSearchParams::addURLObject(DOMURLUtils* urlObject)
50 {
51 #if URLUTILS_SUPPORTS_SEARCHPARAMS
52 for (unsigned i = 0; i < m_urlObjects.size(); i++) {
53 if (m_urlObjects[i] == urlObject)
54 return;
55 }
56 m_urlObjects.append(urlObject);
57 #endif
58 }
59
60 #if ENABLE(ASSERT)
61 bool DOMURLSearchParams::hasURLObject(DOMURLUtils* urlObject) const
62 {
63 return m_urlObjects.contains(urlObject);
64 }
65 #endif
66
67 void DOMURLSearchParams::runUpdateSteps()
68 {
69 if (m_urlObjects.size()) {
70 m_isUpdating = true;
71 const String& query = toString();
72 for (unsigned i = 0; i < m_urlObjects.size(); i++)
73 m_urlObjects[i]->setQuery(query);
74 m_isUpdating = false;
75 }
76 }
77
78 static String decodeString(String input)
79 {
80 return decodeURLEscapeSequences(input.replace('+', ' '));
81 }
82
83 void DOMURLSearchParams::setInput(const String& queryString)
84 {
85 if (m_isUpdating)
86 return;
87
88 m_params.clear();
89 unsigned start = 0;
90 unsigned queryStringLength = queryString.length();
91 while (start < queryStringLength) {
92 size_t nameStart = start;
93 size_t nameValueEnd = queryString.find('&', start);
94 if (nameValueEnd == kNotFound)
95 nameValueEnd = queryStringLength;
96
97 if (nameValueEnd > start) {
98 size_t endOfName = queryString.find('=', start);
99
100 if (endOfName == kNotFound || endOfName > nameValueEnd)
101 endOfName = nameValueEnd;
102
103 String name = decodeString(queryString.substring(nameStart, endOfNam e - nameStart));
104 String value;
105 if (endOfName != nameValueEnd)
106 value = decodeString(queryString.substring(endOfName + 1, nameVa lueEnd - endOfName - 1));
107 if (value.isNull())
108 value = "";
109
110 m_params.append(std::make_pair(name, value));
111 }
112 start = nameValueEnd + 1;
113 }
114 runUpdateSteps();
115 }
116
117 static String encodeString(const String& input)
118 {
119 return encodeWithURLEscapeSequences(input).replace("%20", "+");
120 }
121
122 String DOMURLSearchParams::toString() const
123 {
124 StringBuilder result;
125 for (unsigned i = 0; i < m_params.size(); ++i) {
126 if (i)
127 result.append('&');
128 result.append(encodeString(m_params[i].first));
129 result.append('=');
130 result.append(encodeString(m_params[i].second));
131 }
132 return result.toString();
133 }
134
135 void DOMURLSearchParams::append(const String& name, const String& value)
136 {
137 m_params.append(std::make_pair(name, value));
138 runUpdateSteps();
139 }
140
141 void DOMURLSearchParams::deleteAllWithName(const String& name)
142 {
143 for (unsigned i = 0; i < m_params.size();) {
144 if (m_params[i].first == name)
145 m_params.remove(i);
146 else
147 i++;
148 }
149 runUpdateSteps();
150 }
151
152 String DOMURLSearchParams::get(const String& name) const
153 {
154 for (Vector<std::pair<String, String> >::const_iterator it = m_params.begin( ); it != m_params.end(); ++it) {
155 if (it->first == name)
156 return it->second;
157 }
158 return String();
159 }
160
161 Vector<String> DOMURLSearchParams::getAll(const String& name) const
162 {
163 Vector<String> result;
164 for (Vector<std::pair<String, String> >::const_iterator it = m_params.begin( ); it != m_params.end(); ++it) {
165 if (it->first == name)
166 result.append(it->second);
167 }
168 return result;
169 }
170
171 bool DOMURLSearchParams::has(const String& name) const
172 {
173 for (Vector<std::pair<String, String> >::const_iterator it = m_params.begin( ); it != m_params.end(); ++it) {
174 if (it->first == name)
175 return true;
176 }
177 return false;
178 }
179
180 void DOMURLSearchParams::set(const String& name, const String& value)
181 {
182 bool foundMatch = false;
183 for (unsigned i = 0; i < m_params.size();) {
184 // If there are any name-value whose name is 'name', set
185 // the value of the first such name-value pair to 'value'
186 // and remove the others.
187 if (m_params[i].first == name) {
188 if (!foundMatch) {
189 m_params[i++].second = value;
190 foundMatch = true;
191 } else {
192 m_params.remove(i);
193 }
194 } else {
195 i++;
196 }
197 }
198 // Otherwise, append a new name-value pair to the list.
199 if (!foundMatch)
200 append(name, value);
201 else
202 runUpdateSteps();
203 }
204
205 void DOMURLSearchParams::trace(Visitor* visitor)
206 {
207 #if URLUTILS_SUPPORTS_SEARCHPARAMS
208 visitor->trace(m_urlObjects);
209 #endif
210 }
211
212 } // namespace blink
OLDNEW
« no previous file with comments | « Source/core/dom/DOMURLSearchParams.h ('k') | Source/core/dom/DOMURLUtils.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698