Chromium Code Reviews| Index: third_party/WebKit/Source/core/dom/URLSearchParams.cpp |
| diff --git a/third_party/WebKit/Source/core/dom/URLSearchParams.cpp b/third_party/WebKit/Source/core/dom/URLSearchParams.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..dc9c3c496712873c3dde300178793ea9a1e9e0e9 |
| --- /dev/null |
| +++ b/third_party/WebKit/Source/core/dom/URLSearchParams.cpp |
| @@ -0,0 +1,148 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "config.h" |
| +#include "core/dom/URLSearchParams.h" |
| + |
| +#include "platform/weborigin/KURL.h" |
| +#include "wtf/text/StringBuilder.h" |
| +#include "wtf/text/TextEncoding.h" |
| + |
| +namespace blink { |
| + |
| +URLSearchParams::URLSearchParams(const String& queryString) |
| +{ |
| + if (!queryString.isEmpty()) |
| + setInput(queryString); |
| +} |
| + |
| +URLSearchParams::URLSearchParams(URLSearchParams* searchParams) |
| +{ |
| + ASSERT(searchParams); |
| + m_params = searchParams->m_params; |
| +} |
| + |
| +URLSearchParams::~URLSearchParams() |
| +{ |
| +} |
| + |
| +static String decodeString(String input) |
| +{ |
| + return decodeURLEscapeSequences(input.replace('+', ' ')); |
| +} |
| + |
| +void URLSearchParams::setInput(const String& queryString) |
| +{ |
| + m_params.clear(); |
| + unsigned start = 0; |
| + unsigned queryStringLength = queryString.length(); |
| + while (start < queryStringLength) { |
| + size_t nameStart = start; |
| + size_t nameValueEnd = queryString.find('&', start); |
| + if (nameValueEnd == kNotFound) |
| + nameValueEnd = queryStringLength; |
| + if (nameValueEnd > start) { |
| + size_t endOfName = queryString.find('=', start); |
| + if (endOfName == kNotFound || endOfName > nameValueEnd) |
| + endOfName = nameValueEnd; |
| + String name = decodeString(queryString.substring(nameStart, endOfName - nameStart)); |
| + String value; |
| + if (endOfName != nameValueEnd) |
| + value = decodeString(queryString.substring(endOfName + 1, nameValueEnd - endOfName - 1)); |
| + if (value.isNull()) |
| + value = ""; |
| + m_params.append(std::make_pair(name, value)); |
| + } |
| + start = nameValueEnd + 1; |
| + } |
| +} |
| +static String encodeString(const String& input) |
| +{ |
| + return encodeWithURLEscapeSequences(input).replace("%20", "+"); |
| +} |
| + |
| +String URLSearchParams::toString() const |
| +{ |
| + StringBuilder result; |
| + for (unsigned i = 0; i < m_params.size(); ++i) { |
| + if (i) |
| + result.append('&'); |
| + result.append(encodeString(m_params[i].first)); |
| + result.append('='); |
| + result.append(encodeString(m_params[i].second)); |
| + } |
| + return result.toString(); |
| +} |
| + |
| +void URLSearchParams::append(const String& name, const String& value) |
| +{ |
| + m_params.append(std::make_pair(name, value)); |
| +} |
| + |
| +void URLSearchParams::deleteAllWithName(const String& name) |
| +{ |
| + for (unsigned i = 0; i < m_params.size();) { |
| + if (m_params[i].first == name) |
| + m_params.remove(i); |
| + else |
| + i++; |
| + } |
| +} |
| + |
| +String URLSearchParams::get(const String& name) const |
| +{ |
| + for (Vector<std::pair<String, String>>::const_iterator it = m_params.begin(); it != m_params.end(); ++it) { |
|
sof
2015/11/15 09:04:11
should we upgrade this one (and the ones below) to
Mike West
2015/11/16 08:57:57
An excellent idea!
|
| + if (it->first == name) |
| + return it->second; |
| + } |
| + return String(); |
| +} |
| + |
| +Vector<String> URLSearchParams::getAll(const String& name) const |
| +{ |
| + Vector<String> result; |
| + for (Vector<std::pair<String, String>>::const_iterator it = m_params.begin(); it != m_params.end(); ++it) { |
| + if (it->first == name) |
| + result.append(it->second); |
| + } |
| + return result; |
| +} |
| + |
| +bool URLSearchParams::has(const String& name) const |
| +{ |
| + for (Vector<std::pair<String, String>>::const_iterator it = m_params.begin(); it != m_params.end(); ++it) { |
| + if (it->first == name) |
| + return true; |
| + } |
| + return false; |
| +} |
| + |
| +void URLSearchParams::set(const String& name, const String& value) |
| +{ |
| + bool foundMatch = false; |
| + for (unsigned i = 0; i < m_params.size();) { |
| + // If there are any name-value whose name is 'name', set |
| + // the value of the first such name-value pair to 'value' |
| + // and remove the others. |
| + if (m_params[i].first == name) { |
| + if (!foundMatch) { |
| + m_params[i++].second = value; |
| + foundMatch = true; |
| + } else { |
| + m_params.remove(i); |
| + } |
| + } else { |
| + i++; |
| + } |
| + } |
| + // Otherwise, append a new name-value pair to the list. |
| + if (!foundMatch) |
| + append(name, value); |
| +} |
| + |
| +DEFINE_TRACE(URLSearchParams) |
| +{ |
| +} |
| + |
| +} // namespace blink |