Index: Source/core/dom/URLQuery.cpp |
diff --git a/Source/core/dom/URLQuery.cpp b/Source/core/dom/URLQuery.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..e33bb25db380cc9bdc8579817ac93bd324cc5839 |
--- /dev/null |
+++ b/Source/core/dom/URLQuery.cpp |
@@ -0,0 +1,150 @@ |
+/* |
+ * Copyright (C) 2013 Google Inc. All rights reserved. |
+ * |
+ * Redistribution and use in source and binary forms, with or without |
+ * modification, are permitted provided that the following conditions |
+ * are met: |
+ * |
+ * 1. Redistributions of source code must retain the above copyright |
+ * notice, this list of conditions and the following disclaimer. |
+ * 2. Redistributions in binary form must reproduce the above copyright |
+ * notice, this list of conditions and the following disclaimer in the |
+ * documentation and/or other materials provided with the distribution. |
+ * |
+ * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY |
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
+ * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY |
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
+ */ |
+ |
+ |
+#include "config.h" |
+#include "core/dom/URLQuery.h" |
+ |
+#include "weborigin/KURL.h" |
+#include "wtf/text/StringBuilder.h" |
+ |
+namespace WebCore { |
+ |
+static String decode(String& input) |
+{ |
+ return decodeURLEscapeSequences(input.replace('+', ' ')); |
+} |
+ |
+static Vector<pair<String, String> > parseQuery(const String& input) |
+{ |
+ Vector<pair<String, String> > result; |
+ unsigned length = input.length(); |
+ unsigned lastEqual = 0; |
+ unsigned lastAmpersand = 0; |
+ |
+ for (unsigned i = 0; i <= length; ++i) { |
+ if (i == length || input[i] == '&') { |
+ if (lastAmpersand == i) { |
+ // do nothing |
+ } else if (lastAmpersand == lastEqual) { |
+ String name = input.substring(lastAmpersand, i - lastAmpersand); |
+ result.append(std::make_pair(decode(name), String())); |
+ } else { |
+ String name = input.substring(lastAmpersand, lastEqual - lastAmpersand - 1); |
+ String value = input.substring(lastEqual, i - lastEqual); |
+ result.append(std::make_pair(decode(name), decode(value))); |
+ } |
+ lastAmpersand = i + 1; |
+ lastEqual = i + 1; |
+ } else if (input[i] == '=' && lastEqual == lastAmpersand) { |
+ lastEqual = i + 1; |
+ } |
+ } |
+ |
+ return result; |
+} |
+ |
+URLQuery::URLQuery(const String& init) : m_entries(parseQuery(init)) { } |
+ |
+URLQuery::URLQuery(PassRefPtr<URLQuery> init) : m_entries(init->m_entries) { } |
+ |
+void URLQuery::append(const String& name, const String& value) |
+{ |
+ m_entries.append(std::make_pair(name, value)); |
+} |
+ |
+void URLQuery::deleteFunction(const String& name) |
+{ |
+ for (int i = m_entries.size() - 1; i >= 0; --i) { |
+ if (m_entries[i].first == name) |
+ m_entries.remove(i); |
+ } |
+} |
+ |
+String URLQuery::get(const String& name) |
+{ |
+ for (int i = 0; i < m_entries.size(); ++i) { |
+ if (m_entries[i].first == name) |
+ return m_entries[i].second; |
+ } |
+ return String(); |
+} |
+ |
+const Vector<String> URLQuery::getAll(const String& name) |
+{ |
+ Vector<String> result; |
+ for (int i = 0; i < m_entries.size(); ++i) { |
+ if (m_entries[i].first == name) |
+ result.append(m_entries[i].second); |
+ } |
+ return result; |
+} |
+ |
+bool URLQuery::has(const String& name) |
+{ |
+ for (int i = 0; i < m_entries.size(); ++i) { |
+ if (m_entries[i].first == name) |
+ return true; |
+ } |
+ return false; |
+} |
+ |
+void URLQuery::set(const String& name, const String& value) |
+{ |
+ bool found = false; |
+ for (int i = 0; i < m_entries.size(); ++i) { |
+ if (m_entries[i].first == name) { |
+ if (found) { |
+ m_entries.remove(i); |
+ } else { |
+ m_entries[i].second = value; |
+ found = true; |
+ } |
+ } |
+ } |
+ |
+ if (!found) |
+ append(name, value); |
+} |
+ |
+static String encode(String input) |
+{ |
+ return encodeWithURLEscapeSequences(input).replace("%20", "+"); |
+} |
+ |
+String URLQuery::toString() const |
+{ |
+ StringBuilder builder; |
+ for (int i = 0; i < m_entries.size(); ++i) { |
+ if (i) |
+ builder.append('&'); |
+ builder.append(encode(m_entries[i].first)); |
+ builder.append('='); |
+ builder.append(encode(m_entries[i].second)); |
+ } |
+ return builder.toString(); |
+} |
+ |
+} // namespace WebCore |