From 097bae44d17424890ea11b41ade8e15c6874dbab Mon Sep 17 00:00:00 2001 From: Andrea Marchesini Date: Mon, 19 Oct 2015 16:07:40 +0100 Subject: [PATCH] Bug 1085284 - Implement URLSearchParams iterable<>, r=bz --- dom/base/URLSearchParams.cpp | 18 ++++++++ dom/base/URLSearchParams.h | 21 ++++++++++ dom/base/test/test_urlSearchParams.html | 55 ++++++++++++++++++++++++- dom/webidl/URLSearchParams.webidl | 2 +- 4 files changed, 94 insertions(+), 2 deletions(-) diff --git a/dom/base/URLSearchParams.cpp b/dom/base/URLSearchParams.cpp index 9780a54a92c2..f20d2de57363 100644 --- a/dom/base/URLSearchParams.cpp +++ b/dom/base/URLSearchParams.cpp @@ -418,5 +418,23 @@ URLSearchParams::NotifyObserver() } } +uint32_t +URLSearchParams::GetIterableLength() const +{ + return mParams->Length(); +} + +const nsAString& +URLSearchParams::GetKeyAtIndex(uint32_t aIndex) const +{ + return mParams->GetKeyAtIndex(aIndex); +} + +const nsAString& +URLSearchParams::GetValueAtIndex(uint32_t aIndex) const +{ + return mParams->GetValueAtIndex(aIndex); +} + } // namespace dom } // namespace mozilla diff --git a/dom/base/URLSearchParams.h b/dom/base/URLSearchParams.h index 3c36524d852d..84485b8d875a 100644 --- a/dom/base/URLSearchParams.h +++ b/dom/base/URLSearchParams.h @@ -91,6 +91,23 @@ public: mParams.Clear(); } + uint32_t Length() const + { + return mParams.Length(); + } + + const nsAString& GetKeyAtIndex(uint32_t aIndex) const + { + MOZ_ASSERT(aIndex < mParams.Length()); + return mParams[aIndex].mKey; + } + + const nsAString& GetValueAtIndex(uint32_t aIndex) const + { + MOZ_ASSERT(aIndex < mParams.Length()); + return mParams[aIndex].mValue; + } + private: void DecodeString(const nsACString& aInput, nsAString& aOutput); void ConvertString(const nsACString& aInput, nsAString& aOutput); @@ -153,6 +170,10 @@ public: void Delete(const nsAString& aName); + uint32_t GetIterableLength() const; + const nsAString& GetKeyAtIndex(uint32_t aIndex) const; + const nsAString& GetValueAtIndex(uint32_t aIndex) const; + void Stringify(nsString& aRetval) const { Serialize(aRetval); diff --git a/dom/base/test/test_urlSearchParams.html b/dom/base/test/test_urlSearchParams.html index b43c790b707d..c42a00ad9fea 100644 --- a/dom/base/test/test_urlSearchParams.html +++ b/dom/base/test/test_urlSearchParams.html @@ -237,6 +237,58 @@ https://bugzilla.mozilla.org/show_bug.cgi?id=887836 runTest(); } + function testIterable() { + var u = new URLSearchParams(); + u.set('1','2'); + u.set('2','4'); + u.set('3','6'); + u.set('4','8'); + u.set('5','10'); + + var key_iter = u.keys(); + var value_iter = u.values(); + var entries_iter = u.entries(); + for (var i = 0; i < 5; ++i) { + var v = i + 1; + var key = key_iter.next(); + var value = value_iter.next(); + var entry = entries_iter.next(); + is(key.value, v.toString(), "Correct Key iterator: " + v.toString()); + ok(!key.done, "Key.done is false"); + is(value.value, (v * 2).toString(), "Correct Value iterator: " + (v * 2).toString()); + ok(!value.done, "Value.done is false"); + is(entry.value[0], v.toString(), "Correct Entry 0 iterator: " + v.toString()); + is(entry.value[1], (v * 2).toString(), "Correct Entry 1 iterator: " + (v * 2).toString()); + ok(!entry.done, "Entry.done is false"); + } + + var last = key_iter.next(); + ok(last.done, "Nothing more to read."); + is(last.value, undefined, "Undefined is the last key"); + + last = value_iter.next(); + ok(last.done, "Nothing more to read."); + is(last.value, undefined, "Undefined is the last value"); + + last = entries_iter.next(); + ok(last.done, "Nothing more to read."); + + key_iter = u.keys(); + key_iter.next(); + key_iter.next(); + u.delete('1'); + u.delete('2'); + u.delete('3'); + u.delete('4'); + u.delete('5'); + + last = key_iter.next(); + ok(last.done, "Nothing more to read."); + is(last.value, undefined, "Undefined is the last key"); + + runTest(); + } + var tests = [ testSimpleURLSearchParams, testCopyURLSearchParams, @@ -248,7 +300,8 @@ https://bugzilla.mozilla.org/show_bug.cgi?id=887836 testOrdering, testDelete, testGetNULL, - testSet + testSet, + testIterable ]; function runTest() { diff --git a/dom/webidl/URLSearchParams.webidl b/dom/webidl/URLSearchParams.webidl index 39aa200aadd7..a5ef263895b2 100644 --- a/dom/webidl/URLSearchParams.webidl +++ b/dom/webidl/URLSearchParams.webidl @@ -23,6 +23,6 @@ interface URLSearchParams { sequence getAll(USVString name); boolean has(USVString name); void set(USVString name, USVString value); - // iterable; - Bug 1085284 + iterable; stringifier; };