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

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: More tests + ref count unattached URLSearchParams objects Created 6 years, 11 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
OLDNEW
(Empty)
1 /*
2 * Copyright (c) 2014, Opera Software ASA. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. Neither the name of Opera Software ASA nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
19 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
20 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
21 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
27 * OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #include "config.h"
31 #include "core/dom/DOMURLSearchParams.h"
32
33 #include "bindings/v8/ExceptionState.h"
34 #include "core/dom/DOMURLUtils.h"
35 #include "platform/weborigin/KURL.h"
36 #include "wtf/text/StringBuilder.h"
37 #include "wtf/text/TextEncoding.h"
38
39 namespace WebCore {
40
41 DOMURLSearchParams::DOMURLSearchParams(const String& queryString, DOMURLUtils* u rlObject)
42 : m_urlObject(0)
43 , m_isUpdating(false)
44 , m_refCount(1)
45 {
46 ScriptWrappable::init(this);
47 if (!queryString.isEmpty())
48 setInput(queryString);
49 setURLObject(urlObject);
50 }
51
52 DOMURLSearchParams::DOMURLSearchParams(DOMURLSearchParams* searchParams, DOMURLU tils* urlObject)
53 : m_urlObject(0)
54 , m_isUpdating(false)
55 , m_refCount(1)
56 {
57 ScriptWrappable::init(this);
58 ASSERT(searchParams);
59 m_params = searchParams->m_params;
60 setURLObject(urlObject);
61 }
62
63 void DOMURLSearchParams::setURLObject(DOMURLUtils* urlObject)
64 {
65 m_urlObject = urlObject;
66 if (m_urlObject) {
67 // Transfer references to new owner.
68 while (m_refCount-- > 0)
69 ref();
70 m_refCount = 1;
71 }
72 }
73
74 void DOMURLSearchParams::ref()
75 {
76 if (m_urlObject)
77 m_urlObject->refURLUtils();
78 else
79 m_refCount++;
80 }
81
82 void DOMURLSearchParams::deref()
83 {
84 if (m_urlObject)
85 m_urlObject->derefURLUtils();
86 else if (!--m_refCount)
87 delete this;
88 }
89
90 void DOMURLSearchParams::runUpdateSteps()
91 {
92 if (m_urlObject) {
93 m_isUpdating = true;
94 DOMURLUtils::setSearch(m_urlObject, toString());
95 m_isUpdating = false;
96 }
97 }
98
99 static String decodeString(String input)
100 {
101 return decodeURLEscapeSequences(input.replace('+', ' '));
102 }
103
104 void DOMURLSearchParams::setInput(const String& queryString)
105 {
106 if (m_isUpdating)
107 return;
108
109 m_params.clear();
110 unsigned start = 0;
111 unsigned queryStringLength = queryString.length();
112 while (start < queryStringLength) {
113 size_t nameStart = start;
114 size_t nameValueEnd = queryString.find('&', start);
115 if (nameValueEnd == kNotFound)
116 nameValueEnd = queryStringLength;
117
118 if (nameValueEnd > start) {
119 size_t endOfName = queryString.find('=', start);
120
121 if (endOfName == kNotFound || endOfName > nameValueEnd)
122 endOfName = nameValueEnd;
123
124 String name = decodeString(queryString.substring(nameStart, endOfNam e - nameStart));
125 String value;
126 if (endOfName != nameValueEnd)
127 value = decodeString(queryString.substring(endOfName + 1, nameVa lueEnd - endOfName - 1));
128 if (value.isNull())
129 value = "";
130
131 m_params.append(std::make_pair(name, value));
132 }
133 start = nameValueEnd + 1;
134 }
135 }
136
137 static String encodeString(const String& input)
138 {
139 return encodeWithURLEscapeSequences(input).replace("%20", "+");
140 }
141
142 String DOMURLSearchParams::toString() const
143 {
144 StringBuilder result;
145 for (unsigned i = 0; i < m_params.size(); ++i) {
146 if (i)
147 result.append('&');
148 result.append(encodeString(m_params[i].first));
149 result.append('=');
150 result.append(encodeString(m_params[i].second));
151 }
152 return result.toString();
153 }
154
155 void DOMURLSearchParams::appendNameValue(const String& name, const String& value )
156 {
157 m_params.append(std::make_pair(name, value));
158 runUpdateSteps();
159 }
160
161 void DOMURLSearchParams::deleteAllWithName(const String& name)
162 {
163 for (unsigned i = 0; i < m_params.size();) {
164 if (m_params[i].first == name)
165 m_params.remove(i);
166 else
167 i++;
168 }
169 runUpdateSteps();
170 }
171
172 String DOMURLSearchParams::getFirstValue(const String& name) const
173 {
174 for (Vector<std::pair<String, String> >::const_iterator it = m_params.begin( ); it != m_params.end(); ++it) {
175 if (it->first == name)
176 return it->second;
177 }
178 return String();
179 }
180
181 Vector<String> DOMURLSearchParams::getAllValues(const String& name) const
182 {
183 Vector<String> result;
184 for (Vector<std::pair<String, String> >::const_iterator it = m_params.begin( ); it != m_params.end(); ++it) {
185 if (it->first == name)
186 result.append(it->second);
187 }
188 return result;
189 }
190
191 bool DOMURLSearchParams::hasName(const String& name) const
192 {
193 for (Vector<std::pair<String, String> >::const_iterator it = m_params.begin( ); it != m_params.end(); ++it) {
194 if (it->first == name)
195 return true;
196 }
197 return false;
198 }
199
200 void DOMURLSearchParams::setNameValue(const String& name, const String& value)
201 {
202 bool foundMatch = false;
203 for (unsigned i = 0; i < m_params.size();) {
204 if (m_params[i].first == name) {
205 if (!foundMatch) {
206 m_params[i++].second = value;
207 foundMatch = true;
208 } else {
209 m_params.remove(i);
210 }
211 } else {
212 i++;
213 }
214 }
215 if (!foundMatch)
216 appendNameValue(name, value);
217 else
218 runUpdateSteps();
219 }
220
221 } // namespace WebCore
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698