Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(628)

Unified Diff: Source/core/dom/DOMURLSearchParams.cpp

Issue 143313002: Implement URLSearchParams. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: More tests + ref count unattached URLSearchParams objects Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: Source/core/dom/DOMURLSearchParams.cpp
diff --git a/Source/core/dom/DOMURLSearchParams.cpp b/Source/core/dom/DOMURLSearchParams.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..1afd14a8d4e817a110b6d5d51079c98cbe9c1e05
--- /dev/null
+++ b/Source/core/dom/DOMURLSearchParams.cpp
@@ -0,0 +1,221 @@
+/*
+ * Copyright (c) 2014, Opera Software ASA. 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.
+ * 3. Neither the name of Opera Software ASA nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 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 THE
+ * COPYRIGHT HOLDER OR 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/DOMURLSearchParams.h"
+
+#include "bindings/v8/ExceptionState.h"
+#include "core/dom/DOMURLUtils.h"
+#include "platform/weborigin/KURL.h"
+#include "wtf/text/StringBuilder.h"
+#include "wtf/text/TextEncoding.h"
+
+namespace WebCore {
+
+DOMURLSearchParams::DOMURLSearchParams(const String& queryString, DOMURLUtils* urlObject)
+ : m_urlObject(0)
+ , m_isUpdating(false)
+ , m_refCount(1)
+{
+ ScriptWrappable::init(this);
+ if (!queryString.isEmpty())
+ setInput(queryString);
+ setURLObject(urlObject);
+}
+
+DOMURLSearchParams::DOMURLSearchParams(DOMURLSearchParams* searchParams, DOMURLUtils* urlObject)
+ : m_urlObject(0)
+ , m_isUpdating(false)
+ , m_refCount(1)
+{
+ ScriptWrappable::init(this);
+ ASSERT(searchParams);
+ m_params = searchParams->m_params;
+ setURLObject(urlObject);
+}
+
+void DOMURLSearchParams::setURLObject(DOMURLUtils* urlObject)
+{
+ m_urlObject = urlObject;
+ if (m_urlObject) {
+ // Transfer references to new owner.
+ while (m_refCount-- > 0)
+ ref();
+ m_refCount = 1;
+ }
+}
+
+void DOMURLSearchParams::ref()
+{
+ if (m_urlObject)
+ m_urlObject->refURLUtils();
+ else
+ m_refCount++;
+}
+
+void DOMURLSearchParams::deref()
+{
+ if (m_urlObject)
+ m_urlObject->derefURLUtils();
+ else if (!--m_refCount)
+ delete this;
+}
+
+void DOMURLSearchParams::runUpdateSteps()
+{
+ if (m_urlObject) {
+ m_isUpdating = true;
+ DOMURLUtils::setSearch(m_urlObject, toString());
+ m_isUpdating = false;
+ }
+}
+
+static String decodeString(String input)
+{
+ return decodeURLEscapeSequences(input.replace('+', ' '));
+}
+
+void DOMURLSearchParams::setInput(const String& queryString)
+{
+ if (m_isUpdating)
+ return;
+
+ 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 DOMURLSearchParams::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 DOMURLSearchParams::appendNameValue(const String& name, const String& value)
+{
+ m_params.append(std::make_pair(name, value));
+ runUpdateSteps();
+}
+
+void DOMURLSearchParams::deleteAllWithName(const String& name)
+{
+ for (unsigned i = 0; i < m_params.size();) {
+ if (m_params[i].first == name)
+ m_params.remove(i);
+ else
+ i++;
+ }
+ runUpdateSteps();
+}
+
+String DOMURLSearchParams::getFirstValue(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 it->second;
+ }
+ return String();
+}
+
+Vector<String> DOMURLSearchParams::getAllValues(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 DOMURLSearchParams::hasName(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 DOMURLSearchParams::setNameValue(const String& name, const String& value)
+{
+ bool foundMatch = false;
+ for (unsigned i = 0; i < m_params.size();) {
+ if (m_params[i].first == name) {
+ if (!foundMatch) {
+ m_params[i++].second = value;
+ foundMatch = true;
+ } else {
+ m_params.remove(i);
+ }
+ } else {
+ i++;
+ }
+ }
+ if (!foundMatch)
+ appendNameValue(name, value);
+ else
+ runUpdateSteps();
+}
+
+} // namespace WebCore

Powered by Google App Engine
This is Rietveld 408576698