Bug 1074886 - URLSearchParams.get() should return null (not empty string) when not find pair. r=bzbarsky
authorDonato Sciarra <sciarp@gmail.com>
Tue, 07 Oct 2014 10:06:00 +0200
changeset 209277 8f67c2d3a7489b5ef9ad02acadddc9988a88ca7e
parent 209276 7ba191b165e420add8d0c79f78e0a5b26d7e90b4
child 209278 0ea4be6186b5778ef64180763abc1eb243798950
push id1
push userroot
push dateMon, 20 Oct 2014 17:29:22 +0000
reviewersbzbarsky
bugs1074886
milestone35.0a1
Bug 1074886 - URLSearchParams.get() should return null (not empty string) when not find pair. r=bzbarsky
dom/base/URLSearchParams.cpp
dom/base/test/test_urlSearchParams.html
dom/workers/test/urlSearchParams_worker.js
--- a/dom/base/URLSearchParams.cpp
+++ b/dom/base/URLSearchParams.cpp
@@ -1,16 +1,17 @@
 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "URLSearchParams.h"
 #include "mozilla/dom/URLSearchParamsBinding.h"
 #include "mozilla/dom/EncodingUtils.h"
+#include "nsDOMString.h"
 
 namespace mozilla {
 namespace dom {
 
 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(URLSearchParams, mObservers)
 NS_IMPL_CYCLE_COLLECTING_ADDREF(URLSearchParams)
 NS_IMPL_CYCLE_COLLECTING_RELEASE(URLSearchParams)
 
@@ -225,17 +226,17 @@ void
 URLSearchParams::RemoveObservers()
 {
   mObservers.Clear();
 }
 
 void
 URLSearchParams::Get(const nsAString& aName, nsString& aRetval)
 {
-  aRetval.Truncate();
+  SetDOMStringToNull(aRetval);
 
   for (uint32_t i = 0, len = mSearchParams.Length(); i < len; ++i) {
     if (mSearchParams[i].mKey.Equals(aName)) {
       aRetval.Assign(mSearchParams[i].mValue);
       break;
     }
   }
 }
--- a/dom/base/test/test_urlSearchParams.html
+++ b/dom/base/test/test_urlSearchParams.html
@@ -1,17 +1,17 @@
 
 <!DOCTYPE HTML>
 <html>
 <!--
 https://bugzilla.mozilla.org/show_bug.cgi?id=887836
 -->
 <head>
   <meta charset="utf-8">
-  <title>Test for Bug 887836</title>
+  <title>Test for URLSearchParams</title>
   <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
   <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
 </head>
 <body>
 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=887836">Mozilla Bug 887836</a>
 <p id="display"></p>
 <div id="content" style="display: none">
   <iframe name="x" id="x"></iframe>
@@ -25,17 +25,17 @@ https://bugzilla.mozilla.org/show_bug.cg
 
   /** Test for Bug 887836 **/
   ok("URLSearchParams" in window, "window.URLSearchParams exists");
 
   function testSimpleURLSearchParams() {
     var u = new URLSearchParams();
     ok(u, "URLSearchParams created");
     is(u.has('foo'), false, 'URLSearchParams.has(foo)');
-    is(u.get('foo'), '', 'URLSearchParams.get(foo)');
+    is(u.get('foo'), null, 'URLSearchParams.get(foo)');
     is(u.getAll('foo').length, 0, 'URLSearchParams.getAll(foo)');
 
     u.append('foo', 'bar');
     is(u.has('foo'), true, 'URLSearchParams.has(foo)');
     is(u.get('foo'), 'bar', 'URLSearchParams.get(foo)');
     is(u.getAll('foo').length, 1, 'URLSearchParams.getAll(foo)');
 
     u.set('foo', 'bar2');
@@ -270,27 +270,41 @@ https://bugzilla.mozilla.org/show_bug.cg
 
     a.delete('a');
     is(a.getAll('a').length, 0, "Correct length of getAll()");
     is(a.toString(), "b=3&c=4&c=5", "Order is correct");
 
     runTest();
   }
 
+  function testGetNULL() {
+      
+    var u = new URLSearchParams();
+    is(typeof u.get(''), "object", "typeof URL.searchParams.get('')");
+    is(u.get(''), null, "URL.searchParams.get('') should be null");
+
+    var url = new URL('http://www.example.net?a=b');
+    is(url.searchParams.get('b'), null, "URL.searchParams.get('b') should be null");
+    is(url.searchParams.get('a'), 'b', "URL.searchParams.get('a')");
+
+    runTest();
+  }
+
   var tests = [
     testSimpleURLSearchParams,
     testCopyURLSearchParams,
     testParserURLSearchParams,
     testURL,
     function() { testElement(document.getElementById('anchor')) },
     function() { testElement(document.getElementById('area')) },
     testEncoding,
     testMultiURL,
     testOrdering,
-    testDelete
+    testDelete,
+    testGetNULL
   ];
 
   function runTest() {
     if (!tests.length) {
       SimpleTest.finish();
       return;
     }
 
--- a/dom/workers/test/urlSearchParams_worker.js
+++ b/dom/workers/test/urlSearchParams_worker.js
@@ -17,17 +17,17 @@ onmessage = function() {
   } catch(e) {
   }
   ok(status, "URLSearchParams in workers \\o/");
 
   function testSimpleURLSearchParams() {
     var u = new URLSearchParams();
     ok(u, "URLSearchParams created");
     is(u.has('foo'), false, 'URLSearchParams.has(foo)');
-    is(u.get('foo'), '', 'URLSearchParams.get(foo)');
+    is(u.get('foo'), null, 'URLSearchParams.get(foo)');
     is(u.getAll('foo').length, 0, 'URLSearchParams.getAll(foo)');
 
     u.append('foo', 'bar');
     is(u.has('foo'), true, 'URLSearchParams.has(foo)');
     is(u.get('foo'), 'bar', 'URLSearchParams.get(foo)');
     is(u.getAll('foo').length, 1, 'URLSearchParams.getAll(foo)');
 
     u.set('foo', 'bar2');