Bug 1314013 - Allow the bookmarking ui to add a new keyword when the provided postData differs from the existing one. r=adw, a=gchang
authorMarco Bonardo <mbonardo@mozilla.com>
Wed, 16 Nov 2016 15:31:46 +0100
changeset 444301 063eae32d7016f0513bbe6de5c77e956dc712949
parent 444300 f6527a5a10d09c0e068253fcefe0e429f7c81f53
child 444302 5e14c40ee3b9d104b1cbe46183663fc31ec7d390
push id37233
push usermartin.thomson@gmail.com
push dateMon, 28 Nov 2016 00:01:40 +0000
reviewersadw, gchang
bugs1314013
milestone52.0a2
Bug 1314013 - Allow the bookmarking ui to add a new keyword when the provided postData differs from the existing one. r=adw, a=gchang MozReview-Commit-ID: FRqwTxNpej
browser/components/places/content/editBookmarkOverlay.js
browser/components/places/tests/browser/browser_bookmarkProperties_addKeywordForThisSearch.js
browser/components/places/tests/browser/keyword_form.html
--- a/browser/components/places/content/editBookmarkOverlay.js
+++ b/browser/components/places/content/editBookmarkOverlay.js
@@ -136,17 +136,27 @@ var gEditItemOverlay = {
       throw new Error("_initKeywordField called unexpectedly");
     }
 
     if (!newKeyword) {
       let entries = [];
       yield PlacesUtils.keywords.fetch({ url: this._paneInfo.uri.spec },
                                        e => entries.push(e));
       if (entries.length > 0) {
-        this._keyword = newKeyword = entries[0].keyword;
+        // We show an existing keyword if either POST data was not provided, or
+        // if the POST data is the same.
+        let existingKeyword = entries[0].keyword;
+        let postData = this._paneInfo.postData;
+        if (postData) {
+          let sameEntry = entries.find(e => e.postData === postData);
+          existingKeyword = sameEntry ? sameEntry.keyword : "";
+        }
+        if (existingKeyword) {
+          this._keyword = newKeyword = existingKeyword;
+        }
       }
     }
     this._initTextField(this._keywordField, newKeyword);
   }),
 
   _initLoadInSidebar: Task.async(function* () {
     if (!this._paneInfo.isBookmark)
       throw new Error("_initLoadInSidebar called unexpectedly");
--- a/browser/components/places/tests/browser/browser_bookmarkProperties_addKeywordForThisSearch.js
+++ b/browser/components/places/tests/browser/browser_bookmarkProperties_addKeywordForThisSearch.js
@@ -3,17 +3,17 @@
 const TEST_URL = "http://mochi.test:8888/browser/browser/components/places/tests/browser/keyword_form.html";
 
 add_task(function* () {
   yield BrowserTestUtils.withNewTab({
     gBrowser,
     url: TEST_URL,
   }, function* (browser) {
     // We must wait for the context menu code to build metadata.
-    yield openContextMenuForContentSelector(browser, 'form > input[name="search"]');
+    yield openContextMenuForContentSelector(browser, '#form1 > input[name="search"]');
 
     yield withBookmarksDialog(true, AddKeywordForSearchField, function* (dialogWin) {
       let acceptBtn = dialogWin.document.documentElement.getButton("accept");
       ok(acceptBtn.disabled, "Accept button is disabled");
 
       let promiseKeywordNotification = promiseBookmarksNotification(
         "onItemChanged", (itemId, prop, isAnno, val) => prop == "keyword" && val =="kw");
 
@@ -42,14 +42,69 @@ add_task(function* () {
       // Now check getShortcutOrURI.
       let data = yield getShortcutOrURIAndPostData("kw test");
       is(getPostDataString(data.postData), "accenti=\u00E0\u00E8\u00EC\u00F2\u00F9&search=test", "getShortcutOrURI POST data is correct");
       is(data.url, TEST_URL, "getShortcutOrURI URL is correct");
     });
   });
 });
 
+add_task(function* reopen_same_field() {
+  yield PlacesUtils.keywords.insert({
+    url: TEST_URL,
+    keyword: "kw",
+    postData: "accenti%3D%E0%E8%EC%F2%F9&search%3D%25s"
+  });
+  registerCleanupFunction(function* () {
+    yield PlacesUtils.keywords.remove("kw");
+  });
+  // Reopening on the same input field should show the existing keyword.
+  yield BrowserTestUtils.withNewTab({
+    gBrowser,
+    url: TEST_URL,
+  }, function* (browser) {
+    // We must wait for the context menu code to build metadata.
+    yield openContextMenuForContentSelector(browser, '#form1 > input[name="search"]');
+
+    yield withBookmarksDialog(true, AddKeywordForSearchField, function* (dialogWin) {
+      let acceptBtn = dialogWin.document.documentElement.getButton("accept");
+      ok(acceptBtn.disabled, "Accept button is disabled");
+
+      let elt = dialogWin.document.getElementById("editBMPanel_keywordField");
+      is(elt.value, "kw");
+    });
+  });
+});
+
+add_task(function* open_other_field() {
+  yield PlacesUtils.keywords.insert({
+    url: TEST_URL,
+    keyword: "kw2",
+    postData: "search%3D%25s"
+  });
+  registerCleanupFunction(function* () {
+    yield PlacesUtils.keywords.remove("kw2");
+  });
+  // Reopening on another field of the same page that has different postData
+  // should not show the existing keyword.
+  yield BrowserTestUtils.withNewTab({
+    gBrowser,
+    url: TEST_URL,
+  }, function* (browser) {
+    // We must wait for the context menu code to build metadata.
+    yield openContextMenuForContentSelector(browser, '#form2 > input[name="search"]');
+
+    yield withBookmarksDialog(true, AddKeywordForSearchField, function* (dialogWin) {
+      let acceptBtn = dialogWin.document.documentElement.getButton("accept");
+      ok(acceptBtn.disabled, "Accept button is disabled");
+
+      let elt = dialogWin.document.getElementById("editBMPanel_keywordField");
+      is(elt.value, "");
+    });
+  });
+});
+
 function getPostDataString(stream) {
   let sis = Cc["@mozilla.org/scriptableinputstream;1"]
               .createInstance(Ci.nsIScriptableInputStream);
   sis.init(stream);
   return sis.read(stream.available()).split("\n").pop();
 }
--- a/browser/components/places/tests/browser/keyword_form.html
+++ b/browser/components/places/tests/browser/keyword_form.html
@@ -1,13 +1,17 @@
 <!DOCTYPE HTML>
 
 <html lang="en">
 <head>
   <meta http-equiv="Content-Type" content="text/html;charset=windows-1252">
 </head>
 <body>
-  <form method="POST" action="keyword_form.html">
+  <form id="form1" method="POST" action="keyword_form.html">
     <input type="hidden" name="accenti" value="אטלעש">
     <input type="text" name="search">
   </form>
+  <form id="form2" method="POST" action="keyword_form.html">
+    <input type="hidden" name="accenti" value="שעלטא">
+    <input type="text" name="search">
+  </form>
 </body>
 </html>