OLD | NEW |
---|---|
(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 "core/dom/DOMURLUtils.h" | |
34 #include "platform/weborigin/KURL.h" | |
35 #include "wtf/text/StringBuilder.h" | |
36 #include "wtf/text/TextEncoding.h" | |
37 | |
38 namespace WebCore { | |
39 | |
40 DOMURLSearchParams::DOMURLSearchParams() | |
41 : m_urlObject(0) | |
42 , m_isUpdating(false) | |
43 { | |
44 ScriptWrappable::init(this); | |
45 } | |
46 | |
47 void DOMURLSearchParams::runUpdateSteps() | |
48 { | |
49 if (m_urlObject) { | |
50 m_isUpdating = true; | |
51 DOMURLUtils::setSearch(m_urlObject, toString()); | |
52 m_isUpdating = false; | |
53 } | |
54 } | |
55 | |
56 static String decodeString(String input) | |
57 { | |
58 return decodeURLEscapeSequences(input.replace('+', ' ')); | |
59 } | |
60 | |
61 void DOMURLSearchParams::setInput(const String& queryString) | |
62 { | |
63 if (m_isUpdating) | |
64 return; | |
65 | |
66 m_params.clear(); | |
67 unsigned start = 0; | |
68 unsigned queryStringLength = queryString.length(); | |
69 while (start < queryStringLength) { | |
70 size_t nameStart = start; | |
71 size_t nameValueEnd = queryString.find('&', start); | |
72 if (nameValueEnd == kNotFound) | |
73 nameValueEnd = queryStringLength; | |
74 | |
75 if (nameValueEnd > start) { | |
76 size_t endOfName = queryString.find('=', start); | |
77 | |
78 if (endOfName == kNotFound || endOfName > nameValueEnd) | |
79 endOfName = nameValueEnd; | |
80 | |
81 String name = decodeString(queryString.substring(nameStart, endOfNam e - nameStart)); | |
82 | |
83 bool validUTF8 = true; | |
84 if (!name.isEmpty()) { | |
85 name = name.utf8(StrictUTF8Conversion).data(); | |
86 validUTF8 = !name.isEmpty(); | |
87 } | |
88 | |
89 String value; | |
90 if (validUTF8 && endOfName != nameValueEnd) { | |
91 value = decodeString(queryString.substring(endOfName + 1, nameVa lueEnd - endOfName - 1)); | |
92 if (!value.isEmpty()) { | |
93 value = value.utf8(StrictUTF8Conversion).data(); | |
94 validUTF8 = !value.isEmpty(); | |
95 } | |
96 } | |
97 if (validUTF8 && value.isNull()) | |
98 value = ""; | |
99 | |
100 if (validUTF8) | |
101 m_params.append(std::make_pair(name, value)); | |
102 } | |
103 start = nameValueEnd + 1; | |
104 } | |
105 } | |
106 | |
107 PassRefPtr<DOMURLSearchParams> DOMURLSearchParams::create(const String& queryStr ing) | |
108 { | |
109 RefPtr<DOMURLSearchParams> searchParams = adoptRef(new DOMURLSearchParams()) ; | |
110 if (queryString.isEmpty()) | |
111 return searchParams; | |
112 | |
113 searchParams->setInput(queryString); | |
114 return searchParams; | |
Inactive
2014/01/20 22:06:12
.release()
sof
2014/01/21 13:20:11
Done.
| |
115 } | |
116 | |
117 PassRefPtr<DOMURLSearchParams> DOMURLSearchParams::create(DOMURLSearchParams* in it) | |
118 { | |
119 RefPtr<DOMURLSearchParams> searchParams = adoptRef(new DOMURLSearchParams()) ; | |
120 if (!init) | |
121 return searchParams; | |
Inactive
2014/01/20 22:06:12
.release()
sof
2014/01/21 13:20:11
Done.
| |
122 | |
123 searchParams->m_params = init->m_params; | |
124 return searchParams; | |
Inactive
2014/01/20 22:06:12
.release()
sof
2014/01/21 13:20:11
Done.
| |
125 } | |
126 | |
127 static String encodeString(const String& input) | |
128 { | |
129 String escaped = encodeWithURLEscapeSequences(input); | |
130 // Encode "%20" as "*"; as required and much prettier. | |
131 escaped.replace("%20", "+"); | |
Inactive
2014/01/20 22:06:12
nit: Can return here directly.
sof
2014/01/21 13:20:11
Done.
| |
132 return escaped; | |
133 } | |
134 | |
135 String DOMURLSearchParams::toString() const | |
136 { | |
137 StringBuilder result; | |
138 bool isFirst = true; | |
139 for (Vector<std::pair<String, String> >::const_iterator it = m_params.begin( ); it != m_params.end(); ++it) { | |
140 String name = encodeString(it->first); | |
141 if (!name.isEmpty()) { | |
142 if (!isFirst) | |
143 result.append('&'); | |
144 else | |
145 isFirst = false; | |
146 | |
147 result.append(name); | |
148 String value = encodeString(it->second); | |
149 if (!value.isEmpty()) { | |
150 result.append('='); | |
151 result.append(value); | |
152 } | |
153 } | |
154 } | |
155 return result.toString(); | |
156 } | |
157 | |
158 void DOMURLSearchParams::appendNameValue(const String& name, const String& value ) | |
159 { | |
160 m_params.append(std::make_pair(name, value)); | |
161 runUpdateSteps(); | |
162 } | |
163 | |
164 void DOMURLSearchParams::deleteAllWithName(const String& name) | |
165 { | |
166 for (unsigned i = 0; i < m_params.size();) { | |
167 if (m_params[i].first == name) | |
168 m_params.remove(i); | |
169 else | |
170 i++; | |
171 } | |
172 runUpdateSteps(); | |
173 } | |
174 | |
175 String DOMURLSearchParams::getFirstValue(const String& name) const | |
176 { | |
177 for (Vector<std::pair<String, String> >::const_iterator it = m_params.begin( ); it != m_params.end(); ++it) { | |
178 if (it->first == name) | |
179 return it->second; | |
180 } | |
181 return String(); | |
182 } | |
183 | |
184 Vector<String> DOMURLSearchParams::getAllValues(const String& name) const | |
185 { | |
186 Vector<String> result; | |
187 for (Vector<std::pair<String, String> >::const_iterator it = m_params.begin( ); it != m_params.end(); ++it) { | |
188 if (it->first == name) | |
189 result.append(it->second); | |
190 } | |
191 return result; | |
192 } | |
193 | |
194 bool DOMURLSearchParams::hasName(const String& name) const | |
195 { | |
196 for (Vector<std::pair<String, String> >::const_iterator it = m_params.begin( ); it != m_params.end(); ++it) { | |
197 if (it->first == name) | |
198 return true; | |
199 } | |
200 return false; | |
201 } | |
202 | |
203 void DOMURLSearchParams::setNameValue(const String& name, const String& value) | |
204 { | |
205 bool foundMatch = false; | |
206 for (unsigned i = 0; i < m_params.size();) { | |
207 if (m_params[i].first == name) { | |
208 if (!foundMatch) { | |
209 m_params[i++].second = value; | |
210 foundMatch = true; | |
211 } else { | |
212 m_params.remove(i); | |
213 } | |
214 } else { | |
215 i++; | |
216 } | |
217 } | |
218 if (!foundMatch) | |
219 appendNameValue(name, value); | |
220 else | |
221 runUpdateSteps(); | |
222 } | |
223 | |
224 } // namespace WebCore | |
OLD | NEW |