Bug 1616192 - Fix main context menu to work on shadow DOM elements, not their containers. r=mkmelin a=jorgk
authorGeoff Lankow <geoff@darktrojan.net>
Wed, 12 Feb 2020 22:32:22 +1300
changeset 38187 a3c12a0d6d10cd3e7b74ae982cb3048d3448eec1
parent 38186 053fffd2df97af695a4850db1b7b8901cabbef1a
child 38188 db119ad339d16caedefb445ae11a5ffb5714de24
push id398
push userclokep@gmail.com
push dateMon, 09 Mar 2020 19:10:28 +0000
reviewersmkmelin, jorgk
bugs1616192
Bug 1616192 - Fix main context menu to work on shadow DOM elements, not their containers. r=mkmelin a=jorgk
mail/base/content/mailContextMenus.js
mail/base/content/messageWindow.xhtml
mail/base/content/messenger.xhtml
mail/base/content/nsContextMenu.js
--- a/mail/base/content/mailContextMenus.js
+++ b/mail/base/content/mailContextMenus.js
@@ -41,16 +41,28 @@ function RestoreSelectionWithoutContentL
       tree.invalidate();
     }
 
     UpdateMailToolbar("RestoreSelectionWithoutContentLoad");
   }
 }
 
 /**
+ * Used when mailContext opens for elements in a <browser> to keep a reference
+ * to the event's target. This target is preferred over document.popupNode
+ * because it could be an element in a shadow DOM, whereas document.popupNode
+ * would not be.
+ * @param event the oncontextmenu event
+ */
+function mailContextOnContextMenu(event) {
+  document.getElementById("mailContext").target =
+    event.composedTarget || event.originalTarget;
+}
+
+/**
  * Function to clear out the global nsContextMenu, and in the case when we
  * were a threadpane context menu, restore the selection so that a right-click
  * on a non-selected row doesn't move the selection.
  * @param event the onpopuphiding event
  */
 function mailContextOnPopupHiding(aEvent) {
   // Don't do anything if it's a submenu's onpopuphiding that's just bubbling
   // up to the top.
--- a/mail/base/content/messageWindow.xhtml
+++ b/mail/base/content/messageWindow.xhtml
@@ -243,21 +243,31 @@
       </hbox>
       <!-- The msgNotificationBar appears on top of the message and displays
            information like: junk, mdn, remote content and phishing warnings -->
       <hbox id="mail-notification-top">
         <!-- notificationbox will be added here lazily. -->
       </hbox>
 
       <!-- message view -->
-      <browser id="messagepane" context="mailContext" tooltip="aHTMLTooltip"
-               style="height: 0px; min-height: 1px" flex="1" name="messagepane"
-               disablesecurity="true" disablehistory="true" type="content" primary="true"
-               onresize="return messagePaneOnResize(event);" autofind="false"
-               src="about:blank" onclick="return contentAreaClick(event);" />
+      <browser id="messagepane"
+               context="mailContext"
+               tooltip="aHTMLTooltip"
+               style="height: 0px; min-height: 1px"
+               flex="1"
+               name="messagepane"
+               disablesecurity="true"
+               disablehistory="true"
+               type="content"
+               primary="true"
+               autofind="false"
+               src="about:blank"
+               onclick="return contentAreaClick(event);"
+               oncontextmenu="return mailContextOnContextMenu(event);"
+               onresize="return messagePaneOnResize(event);"/>
       <splitter id="attachment-splitter" collapse="after" resizebefore="closest" resizeafter="closest" collapsed="true"/>
       <vbox id="attachmentView" collapsed="true">
 #include msgAttachmentView.inc.xhtml
       </vbox>
       <findbar id="FindToolbar" browserid="messagepane"/>
     </vbox>
   </hbox>
   <panel id="customizeToolbarSheetPopup" noautohide="true">
--- a/mail/base/content/messenger.xhtml
+++ b/mail/base/content/messenger.xhtml
@@ -786,25 +786,32 @@
                            images, or is a suspected phishing URL. -->
                       <hbox id="mail-notification-top">
                         <!-- notificationbox will be added here lazily. -->
                       </hbox>
                       <!-- The messagepanewrapper hbox exists to allow
                            extensions to add sidebars to the message. -->
                       <hbox id="messagepanewrapper" flex="1">
                         <stack flex="1">
-                          <browser id="messagepane" context="mailContext"
+                          <browser id="messagepane"
+                                   context="mailContext"
                                    datetimepicker="DateTimePickerPanel"
-                                   autofind="false" minheight="1" flex="1"
-                                   name="messagepane" tooltip="aHTMLTooltip"
-                                   disablehistory="true" type="content"
-                                   primary="true" src="about:blank"
-                                   onresize="return messagePaneOnResize(event);"
+                                   autofind="false"
+                                   minheight="1"
+                                   flex="1"
+                                   name="messagepane"
+                                   tooltip="aHTMLTooltip"
+                                   disablehistory="true"
+                                   type="content"
+                                   primary="true"
+                                   src="about:blank"
                                    disablesecurity="true"
-                                   onclick="return contentAreaClick(event);"/>
+                                   onclick="return contentAreaClick(event);"
+                                   oncontextmenu="return mailContextOnContextMenu(event);"
+                                   onresize="return messagePaneOnResize(event);"/>
                         </stack>
                       </hbox>
                       <splitter id="attachment-splitter" collapse="after"
                                 resizebefore="closest" resizeafter="closest"
                                 collapsed="true"/>
                       <vbox id="attachmentView" collapsed="true">
 #include msgAttachmentView.inc.xhtml
                       </vbox>
@@ -839,19 +846,24 @@
               <hbox align="center" flex="1">
                 <image class="contentTabSecurity"/>
                 <label class="contentTabUrlbar" flex="1"/>
               </hbox>
             </toolbaritem>
           </hbox>
         </vbox>
         <stack flex="1">
-          <browser id="dummycontentbrowser" type="content" flex="1"
-                   disablehistory="true" autocompletepopup="PopupAutoComplete"
-                   datetimepicker="DateTimePickerPanel" context="mailContext"/>
+          <browser id="dummycontentbrowser"
+                   type="content"
+                   flex="1"
+                   disablehistory="true"
+                   autocompletepopup="PopupAutoComplete"
+                   datetimepicker="DateTimePickerPanel"
+                   context="mailContext"
+                   oncontextmenu="return mailContextOnContextMenu(event);"/>
         </stack>
       </vbox>
     </vbox>
 
     <vbox id="chromeTab" collapsed="true">
       <vbox flex="1" class="chromeTabInstance">
         <vbox id="dummychrometoolbox" class="contentTabToolbox">
           <hbox id="dummychrometoolbar" class="contentTabToolbar"></hbox>
--- a/mail/base/content/nsContextMenu.js
+++ b/mail/base/content/nsContextMenu.js
@@ -73,19 +73,22 @@ nsContextMenu.prototype = {
   /**
    * Init: set properties based on the clicked-on element and the state of
    * the world, then determine which context menu items to show based on
    * those properties.
    */
   initMenu(aPopup, aIsShift) {
     this.menu = aPopup;
 
+    let target = this.menu.target || document.popupNode;
+    delete this.menu.target;
+
     // Get contextual info.
-    this.setTarget(document.popupNode);
-    this.setMessageTargets(document.popupNode);
+    this.setTarget(target);
+    this.setMessageTargets(target);
 
     if (!this.inThreadPane && this.messagepaneIsBlank) {
       this.shouldDisplay = false;
       return;
     }
 
     this.selectionInfo = BrowserUtils.getSelectionDetails(window);
     this.isContentSelected = !this.selectionInfo.docSelectionIsCollapsed;
@@ -115,17 +118,17 @@ nsContextMenu.prototype = {
         srcUrl: this.mediaURL,
         pageUrl: this.browser ? this.browser.currentURI.spec : undefined,
         linkText: this.onLink ? this.linkText() : undefined,
         linkUrl: this.linkURL,
         selectionText: this.isTextSelected
           ? this.selectionInfo.fullText
           : undefined,
       };
-      if (document.popupNode.closest("tree") == gFolderDisplay.tree) {
+      if (target.closest("tree") == gFolderDisplay.tree) {
         subject.displayedFolder = gFolderDisplay.view.displayedFolder;
         subject.selectedMessages = gFolderDisplay.selectedMessages;
       }
       subject.wrappedJSObject = subject;
 
       Services.obs.notifyObservers(subject, "on-build-contextmenu");
     }