Bug 1593629 - Restore adding named anchors to context menu for links. r=mkmelin a=jorgk
authorKhushil Mistry <khushil324@gmail.com>
Tue, 19 Nov 2019 04:03:00 +0100
changeset 37296 100d596fc5d4984704d40ba194190bc817065907
parent 37295 c5e0a5542802fa67c10f1bf93b340027ad46630b
child 37297 8a0a5f59230cc0bbc22735a789e16f358a386309
push id395
push userclokep@gmail.com
push dateMon, 02 Dec 2019 19:38:57 +0000
reviewersmkmelin, jorgk
bugs1593629
Bug 1593629 - Restore adding named anchors to context menu for links. r=mkmelin a=jorgk
mail/components/compose/content/dialogs/EdDialogCommon.js
--- a/mail/components/compose/content/dialogs/EdDialogCommon.js
+++ b/mail/components/compose/content/dialogs/EdDialogCommon.js
@@ -567,17 +567,16 @@ var NotAnInlineParent = [
   "tbody",
   "tfoot",
   "thead",
   "tr",
   "ul",
 ];
 
 function FillLinkMenulist(linkMenulist, headingsArray) {
-  var menupopup = linkMenulist.firstElementChild;
   var editor = GetCurrentEditor();
   try {
     var treeWalker = editor.document.createTreeWalker(
       editor.document,
       1,
       null,
       true
     );
@@ -641,51 +640,62 @@ function FillLinkMenulist(linkMenulist, 
         anchor += "_";
       }
       anchorList.push({ anchor, sortkey: anchor.toLowerCase() });
       anchorMap[anchor] = true;
 
       // Save nodes in an array so we can create anchor node under it later
       headingsArray[anchor] = heading;
     }
+    let menuItems = [];
     if (anchorList.length) {
       // case insensitive sort
       anchorList.sort((a, b) => {
         if (a.sortkey < b.sortkey) {
           return -1;
         }
         if (a.sortkey > b.sortkey) {
           return 1;
         }
         return 0;
       });
-
       for (i = 0; i < anchorList.length; i++) {
-        createMenuItem(menupopup, anchorList[i].anchor);
+        menuItems.push(createMenuItem(anchorList[i].anchor));
       }
     } else {
       // Don't bother with named anchors in Mail.
       if (editor && editor.flags & Ci.nsIPlaintextEditor.eEditorMailMask) {
-        menupopup.remove();
         linkMenulist.removeAttribute("enablehistory");
         return;
       }
-      var item = createMenuItem(
-        menupopup,
-        GetString("NoNamedAnchorsOrHeadings")
-      );
+      let item = createMenuItem(GetString("NoNamedAnchorsOrHeadings"));
       item.setAttribute("disabled", "true");
+      menuItems.push(item);
     }
+    window.addEventListener("contextmenu", event => {
+      if (document.getElementById("datalist-menuseparator")) {
+        return;
+      }
+      let menuseparator = document.createXULElement("menuseparator");
+      menuseparator.setAttribute("id", "datalist-menuseparator");
+      document.getElementById("textbox-contextmenu").appendChild(menuseparator);
+      for (let menuitem of menuItems) {
+        document.getElementById("textbox-contextmenu").appendChild(menuitem);
+      }
+    });
   } catch (e) {}
 }
 
-function createMenuItem(aMenuPopup, aLabel) {
+function createMenuItem(label) {
   var menuitem = document.createXULElement("menuitem");
-  menuitem.setAttribute("label", aLabel);
-  aMenuPopup.appendChild(menuitem);
+  menuitem.setAttribute("label", label);
+  menuitem.addEventListener("click", event => {
+    gDialog.hrefInput.value = label;
+    ChangeLinkLocation();
+  });
   return menuitem;
 }
 
 // Shared by Image and Link dialogs for the "Choose" button for links
 function chooseLinkFile() {
   GetLocalFileURL("html, img").then(fileURL => {
     // Always try to relativize local file URLs
     if (gHaveDocumentUrl) {