Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 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/URLSearchParams.h" | |
| 7 | |
| 8 #include "platform/weborigin/KURL.h" | |
| 9 #include "wtf/text/StringBuilder.h" | |
| 10 #include "wtf/text/TextEncoding.h" | |
| 11 | |
| 12 namespace blink { | |
| 13 | |
| 14 URLSearchParams::URLSearchParams(const String& queryString) | |
| 15 { | |
| 16 if (!queryString.isEmpty()) | |
| 17 setInput(queryString); | |
| 18 } | |
| 19 | |
| 20 URLSearchParams::URLSearchParams(URLSearchParams* searchParams) | |
| 21 { | |
| 22 ASSERT(searchParams); | |
| 23 m_params = searchParams->m_params; | |
| 24 } | |
| 25 | |
| 26 URLSearchParams::~URLSearchParams() | |
| 27 { | |
| 28 } | |
| 29 | |
| 30 static String decodeString(String input) | |
| 31 { | |
| 32 return decodeURLEscapeSequences(input.replace('+', ' ')); | |
| 33 } | |
| 34 | |
| 35 void URLSearchParams::setInput(const String& queryString) | |
| 36 { | |
| 37 m_params.clear(); | |
| 38 unsigned start = 0; | |
| 39 unsigned queryStringLength = queryString.length(); | |
| 40 while (start < queryStringLength) { | |
| 41 size_t nameStart = start; | |
| 42 size_t nameValueEnd = queryString.find('&', start); | |
| 43 if (nameValueEnd == kNotFound) | |
| 44 nameValueEnd = queryStringLength; | |
| 45 if (nameValueEnd > start) { | |
| 46 size_t endOfName = queryString.find('=', start); | |
| 47 if (endOfName == kNotFound || endOfName > nameValueEnd) | |
| 48 endOfName = nameValueEnd; | |
| 49 String name = decodeString(queryString.substring(nameStart, endOfNam e - nameStart)); | |
| 50 String value; | |
| 51 if (endOfName != nameValueEnd) | |
| 52 value = decodeString(queryString.substring(endOfName + 1, nameVa lueEnd - endOfName - 1)); | |
| 53 if (value.isNull()) | |
| 54 value = ""; | |
| 55 m_params.append(std::make_pair(name, value)); | |
| 56 } | |
| 57 start = nameValueEnd + 1; | |
| 58 } | |
| 59 } | |
| 60 static String encodeString(const String& input) | |
| 61 { | |
| 62 return encodeWithURLEscapeSequences(input).replace("%20", "+"); | |
| 63 } | |
| 64 | |
| 65 String URLSearchParams::toString() const | |
| 66 { | |
| 67 StringBuilder result; | |
| 68 for (unsigned i = 0; i < m_params.size(); ++i) { | |
| 69 if (i) | |
| 70 result.append('&'); | |
| 71 result.append(encodeString(m_params[i].first)); | |
| 72 result.append('='); | |
| 73 result.append(encodeString(m_params[i].second)); | |
| 74 } | |
| 75 return result.toString(); | |
| 76 } | |
| 77 | |
| 78 void URLSearchParams::append(const String& name, const String& value) | |
| 79 { | |
| 80 m_params.append(std::make_pair(name, value)); | |
| 81 } | |
| 82 | |
| 83 void URLSearchParams::deleteAllWithName(const String& name) | |
| 84 { | |
| 85 for (unsigned i = 0; i < m_params.size();) { | |
| 86 if (m_params[i].first == name) | |
| 87 m_params.remove(i); | |
| 88 else | |
| 89 i++; | |
| 90 } | |
| 91 } | |
| 92 | |
| 93 String URLSearchParams::get(const String& name) const | |
| 94 { | |
| 95 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!
| |
| 96 if (it->first == name) | |
| 97 return it->second; | |
| 98 } | |
| 99 return String(); | |
| 100 } | |
| 101 | |
| 102 Vector<String> URLSearchParams::getAll(const String& name) const | |
| 103 { | |
| 104 Vector<String> result; | |
| 105 for (Vector<std::pair<String, String>>::const_iterator it = m_params.begin() ; it != m_params.end(); ++it) { | |
| 106 if (it->first == name) | |
| 107 result.append(it->second); | |
| 108 } | |
| 109 return result; | |
| 110 } | |
| 111 | |
| 112 bool URLSearchParams::has(const String& name) const | |
| 113 { | |
| 114 for (Vector<std::pair<String, String>>::const_iterator it = m_params.begin() ; it != m_params.end(); ++it) { | |
| 115 if (it->first == name) | |
| 116 return true; | |
| 117 } | |
| 118 return false; | |
| 119 } | |
| 120 | |
| 121 void URLSearchParams::set(const String& name, const String& value) | |
| 122 { | |
| 123 bool foundMatch = false; | |
| 124 for (unsigned i = 0; i < m_params.size();) { | |
| 125 // If there are any name-value whose name is 'name', set | |
| 126 // the value of the first such name-value pair to 'value' | |
| 127 // and remove the others. | |
| 128 if (m_params[i].first == name) { | |
| 129 if (!foundMatch) { | |
| 130 m_params[i++].second = value; | |
| 131 foundMatch = true; | |
| 132 } else { | |
| 133 m_params.remove(i); | |
| 134 } | |
| 135 } else { | |
| 136 i++; | |
| 137 } | |
| 138 } | |
| 139 // Otherwise, append a new name-value pair to the list. | |
| 140 if (!foundMatch) | |
| 141 append(name, value); | |
| 142 } | |
| 143 | |
| 144 DEFINE_TRACE(URLSearchParams) | |
| 145 { | |
| 146 } | |
| 147 | |
| 148 } // namespace blink | |
| OLD | NEW |