OLD | NEW |
(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 WebCore { |
| 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 for (unsigned i = 0; i < m_urlObjects.size(); i++) { |
| 52 if (m_urlObjects[i] == urlObject) |
| 53 return; |
| 54 } |
| 55 m_urlObjects.append(urlObject); |
| 56 } |
| 57 |
| 58 #if ASSERT_ENABLED |
| 59 bool DOMURLSearchParams::hasURLObject(DOMURLUtils* urlObject) const |
| 60 { |
| 61 return m_urlObjects.contains(urlObject); |
| 62 } |
| 63 #endif |
| 64 |
| 65 void DOMURLSearchParams::runUpdateSteps() |
| 66 { |
| 67 if (m_urlObjects.size()) { |
| 68 m_isUpdating = true; |
| 69 const String& query = toString(); |
| 70 for (unsigned i = 0; i < m_urlObjects.size(); i++) |
| 71 m_urlObjects[i]->setQuery(query); |
| 72 m_isUpdating = false; |
| 73 } |
| 74 } |
| 75 |
| 76 static String decodeString(String input) |
| 77 { |
| 78 return decodeURLEscapeSequences(input.replace('+', ' ')); |
| 79 } |
| 80 |
| 81 void DOMURLSearchParams::setInput(const String& queryString) |
| 82 { |
| 83 if (m_isUpdating) |
| 84 return; |
| 85 |
| 86 m_params.clear(); |
| 87 unsigned start = 0; |
| 88 unsigned queryStringLength = queryString.length(); |
| 89 while (start < queryStringLength) { |
| 90 size_t nameStart = start; |
| 91 size_t nameValueEnd = queryString.find('&', start); |
| 92 if (nameValueEnd == kNotFound) |
| 93 nameValueEnd = queryStringLength; |
| 94 |
| 95 if (nameValueEnd > start) { |
| 96 size_t endOfName = queryString.find('=', start); |
| 97 |
| 98 if (endOfName == kNotFound || endOfName > nameValueEnd) |
| 99 endOfName = nameValueEnd; |
| 100 |
| 101 String name = decodeString(queryString.substring(nameStart, endOfNam
e - nameStart)); |
| 102 String value; |
| 103 if (endOfName != nameValueEnd) |
| 104 value = decodeString(queryString.substring(endOfName + 1, nameVa
lueEnd - endOfName - 1)); |
| 105 if (value.isNull()) |
| 106 value = ""; |
| 107 |
| 108 m_params.append(std::make_pair(name, value)); |
| 109 } |
| 110 start = nameValueEnd + 1; |
| 111 } |
| 112 runUpdateSteps(); |
| 113 } |
| 114 |
| 115 static String encodeString(const String& input) |
| 116 { |
| 117 return encodeWithURLEscapeSequences(input).replace("%20", "+"); |
| 118 } |
| 119 |
| 120 String DOMURLSearchParams::toString() const |
| 121 { |
| 122 StringBuilder result; |
| 123 for (unsigned i = 0; i < m_params.size(); ++i) { |
| 124 if (i) |
| 125 result.append('&'); |
| 126 result.append(encodeString(m_params[i].first)); |
| 127 result.append('='); |
| 128 result.append(encodeString(m_params[i].second)); |
| 129 } |
| 130 return result.toString(); |
| 131 } |
| 132 |
| 133 void DOMURLSearchParams::appendNameValue(const String& name, const String& value
) |
| 134 { |
| 135 m_params.append(std::make_pair(name, value)); |
| 136 runUpdateSteps(); |
| 137 } |
| 138 |
| 139 void DOMURLSearchParams::deleteAllWithName(const String& name) |
| 140 { |
| 141 for (unsigned i = 0; i < m_params.size();) { |
| 142 if (m_params[i].first == name) |
| 143 m_params.remove(i); |
| 144 else |
| 145 i++; |
| 146 } |
| 147 runUpdateSteps(); |
| 148 } |
| 149 |
| 150 String DOMURLSearchParams::getFirstValue(const String& name) const |
| 151 { |
| 152 for (Vector<std::pair<String, String> >::const_iterator it = m_params.begin(
); it != m_params.end(); ++it) { |
| 153 if (it->first == name) |
| 154 return it->second; |
| 155 } |
| 156 return String(); |
| 157 } |
| 158 |
| 159 Vector<String> DOMURLSearchParams::getAllValues(const String& name) const |
| 160 { |
| 161 Vector<String> result; |
| 162 for (Vector<std::pair<String, String> >::const_iterator it = m_params.begin(
); it != m_params.end(); ++it) { |
| 163 if (it->first == name) |
| 164 result.append(it->second); |
| 165 } |
| 166 return result; |
| 167 } |
| 168 |
| 169 bool DOMURLSearchParams::hasName(const String& name) const |
| 170 { |
| 171 for (Vector<std::pair<String, String> >::const_iterator it = m_params.begin(
); it != m_params.end(); ++it) { |
| 172 if (it->first == name) |
| 173 return true; |
| 174 } |
| 175 return false; |
| 176 } |
| 177 |
| 178 void DOMURLSearchParams::setNameValue(const String& name, const String& value) |
| 179 { |
| 180 bool foundMatch = false; |
| 181 for (unsigned i = 0; i < m_params.size();) { |
| 182 // If there are any name-value whose name is 'name', set |
| 183 // the value of the first such name-value pair to 'value' |
| 184 // and remove the others. |
| 185 if (m_params[i].first == name) { |
| 186 if (!foundMatch) { |
| 187 m_params[i++].second = value; |
| 188 foundMatch = true; |
| 189 } else { |
| 190 m_params.remove(i); |
| 191 } |
| 192 } else { |
| 193 i++; |
| 194 } |
| 195 } |
| 196 // Otherwise, append a new name-value pair to the list. |
| 197 if (!foundMatch) |
| 198 appendNameValue(name, value); |
| 199 else |
| 200 runUpdateSteps(); |
| 201 } |
| 202 |
| 203 void DOMURLSearchParams::trace(Visitor* visitor) |
| 204 { |
| 205 visitor->trace(m_urlObjects); |
| 206 } |
| 207 |
| 208 } // namespace WebCore |
OLD | NEW |