Bug 1314013 - Allow the bookmarking ui to add a new keyword when the provided postData differs from the existing one. r=adw draft
authorMarco Bonardo <mbonardo@mozilla.com>
Wed, 16 Nov 2016 15:31:46 +0100
changeset 441087 cba686d1d80a1a3c8dc7bb397fceb57d1a801a05
parent 441054 28e2a6dde76ab6ad4464a3662df1bd57af04398a
child 537489 d396b435dd9da0a55ef7dc45a1b05a9b2a88b1be
push id36353
push usermak77@bonardo.net
push dateFri, 18 Nov 2016 11:01:42 +0000
reviewersadw
bugs1314013
milestone53.0a1
Bug 1314013 - Allow the bookmarking ui to add a new keyword when the provided postData differs from the existing one. r=adw 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>