summaryrefslogtreecommitdiffstats
path: root/dom/url/URLSearchParams.cpp
diff options
context:
space:
mode:
authorMatt A. Tobin <email@mattatobin.com>2020-05-02 16:43:30 -0400
committerMatt A. Tobin <email@mattatobin.com>2020-05-02 16:43:30 -0400
commitea491d0383e89839b831fabc11e7e0d6732659dd (patch)
treea9dd7e6833599bf0078a7f7935fc619606c50d7d /dom/url/URLSearchParams.cpp
parent4ecfad8e5340787fedf231e390e181c19b391393 (diff)
downloadUXP-ea491d0383e89839b831fabc11e7e0d6732659dd.tar
UXP-ea491d0383e89839b831fabc11e7e0d6732659dd.tar.gz
UXP-ea491d0383e89839b831fabc11e7e0d6732659dd.tar.lz
UXP-ea491d0383e89839b831fabc11e7e0d6732659dd.tar.xz
UXP-ea491d0383e89839b831fabc11e7e0d6732659dd.zip
Issue #1449 - Implement URLSearchParams's sort()
Diffstat (limited to 'dom/url/URLSearchParams.cpp')
-rw-r--r--dom/url/URLSearchParams.cpp42
1 files changed, 42 insertions, 0 deletions
diff --git a/dom/url/URLSearchParams.cpp b/dom/url/URLSearchParams.cpp
index f762299f8..e2172ea0e 100644
--- a/dom/url/URLSearchParams.cpp
+++ b/dom/url/URLSearchParams.cpp
@@ -448,6 +448,15 @@ URLSearchParams::GetValueAtIndex(uint32_t aIndex) const
return mParams->GetValueAtIndex(aIndex);
}
+void
+URLSearchParams::Sort(ErrorResult& aRv)
+{
+ aRv = mParams->Sort();
+ if (!aRv.Failed()) {
+ NotifyObserver();
+ }
+}
+
// Helper functions for structured cloning
inline bool
ReadString(JSStructuredCloneReader* aReader, nsString& aString)
@@ -472,6 +481,39 @@ ReadString(JSStructuredCloneReader* aReader, nsString& aString)
return true;
}
+nsresult
+URLParams::Sort()
+{
+ // Unfortunately we cannot use nsTArray<>.Sort() because it doesn't keep the
+ // correct order of the values for equal keys.
+
+ // Let's sort the keys, without duplicates.
+ FallibleTArray<nsString> keys;
+ for (const Param& param : mParams) {
+ if (!keys.Contains(param.mKey) &&
+ !keys.InsertElementSorted(param.mKey, fallible)) {
+ return NS_ERROR_OUT_OF_MEMORY;
+ }
+ }
+
+ FallibleTArray<Param> params;
+
+ // Here we recreate the array starting from the sorted keys.
+ for (uint32_t keyId = 0, keysLength = keys.Length(); keyId < keysLength;
+ ++keyId) {
+ const nsString& key = keys[keyId];
+ for (const Param& param : mParams) {
+ if (param.mKey.Equals(key) &&
+ !params.AppendElement(param, fallible)) {
+ return NS_ERROR_OUT_OF_MEMORY;
+ }
+ }
+ }
+
+ mParams.SwapElements(params);
+ return NS_OK;
+}
+
inline bool
WriteString(JSStructuredCloneWriter* aWriter, const nsString& aString)
{