Bug 1379674 - Port Bug 1372520 [remove dependency between nsContextMenu and devtools] to SeaMonkey. r=IanN
authorIsaac Schemm <isaacschemm@gmail.com>
Sun, 16 Jul 2017 13:41:46 +0200
changeset 28450 b201b01c6fc1bf5ea4f9b4b2525419de5c6971a7
parent 28449 34a2b1f74bf9a8d1812d40fdd1521981decb4357
child 28451 4c51474a7081d04ec257a06c8e956c75a9e1ecb2
push id1986
push userclokep@gmail.com
push dateWed, 02 Aug 2017 14:43:31 +0000
treeherdercomm-beta@b51c9adf2c9e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersIanN
bugs1379674, 1372520, 1356415
Bug 1379674 - Port Bug 1372520 [remove dependency between nsContextMenu and devtools] to SeaMonkey. r=IanN See Bug 1356415 [get CSS selector in content for contextmenu to fix devtools CPOW] See Bug 1356415 [move devtools helper findCssSelector to shared module in toolkit]
suite/common/nsContextMenu.js
--- a/suite/common/nsContextMenu.js
+++ b/suite/common/nsContextMenu.js
@@ -24,16 +24,21 @@ XPCOMUtils.defineLazyGetter(this, "Inlin
 });
 
 XPCOMUtils.defineLazyGetter(this, "PageMenuParent", function() {
   let tmp = {};
   Components.utils.import("resource://gre/modules/PageMenu.jsm", tmp);
   return new tmp.PageMenuParent();
 });
 
+XPCOMUtils.defineLazyModuleGetter(this, "DevToolsShim",
+  "chrome://devtools-shim/content/DevToolsShim.jsm");
+XPCOMUtils.defineLazyModuleGetter(this, "findCssSelector",
+  "resource://gre/modules/css-selector.js");
+
 function nsContextMenu(aXulMenu, aIsShift, aEvent) {
   this.shouldDisplay = true;
   this.initMenu(aXulMenu, aIsShift, aEvent);
 }
 
 // Prototype for nsContextMenu "class."
 nsContextMenu.prototype = {
   initMenu: function(aXulMenu, aIsShift, aEvent) {
@@ -215,18 +220,19 @@ nsContextMenu.prototype = {
 
     var showView = !(this.inDirList || this.onImage || this.isContentSelected ||
                      this.onCanvas || this.onVideo || this.onAudio ||
                      this.onLink || this.onTextInput);
 
     this.showItem("context-viewsource", showView);
     this.showItem("context-viewinfo", showView);
 
-    var showInspect = "gDevTools" in window &&
-                      Services.prefs.getBoolPref("devtools.inspector.enabled");
+    var showInspect = DevToolsShim.isInstalled() &&
+                      "gDevTools" in window &&
+                      Services.prefs.getBoolPref("devtools.inspector.enabled", false);
     this.showItem("inspect-separator", showInspect);
     this.showItem("context-inspect", showInspect);
 
     this.showItem("context-sep-properties",
                   !(this.inDirList || this.isContentSelected || this.onTextInput ||
                     this.onCanvas || this.onVideo || this.onAudio));
     // Set Desktop Background depends on whether an image was clicked on,
     // and requires the shell service.
@@ -484,22 +490,43 @@ nsContextMenu.prototype = {
         this.setItemAttr("context-video-fullscreen", "disabled", hasError);
         this.setItemAttr("context-video-showstats", "disabled", hasError);
         this.setItemAttr("context-video-hidestats", "disabled", hasError);
       }
     }
     this.showItem("context-media-sep-commands", onMedia);
   },
 
+  /**
+   * Retrieve the array of CSS selectors corresponding to the provided node. The first item
+   * of the array is the selector of the node in its owner document. Additional items are
+   * used if the node is inside a frame, each representing the CSS selector for finding the
+   * frame element in its parent document.
+   *
+   * This format is expected by DevTools in order to handle the Inspect Node context menu
+   * item.
+   *
+   * @param  {Node}
+   *         The node for which the CSS selectors should be computed
+   * @return {Array} array of css selectors (strings).
+   */
+  getNodeSelectors: function(node) {
+    let selectors = [];
+    while (node) {
+      selectors.push(findCssSelector(node));
+      node = node.ownerGlobal.frameElement;
+    }
+
+    return selectors;
+  },
+
   inspectNode: function() {
-    let tmp = {};
-    Components.utils.import("resource://devtools/shared/Loader.jsm", tmp);
     let gBrowser = this.browser.ownerDocument.defaultView.gBrowser;
-    let gDevToolsBrowser = tmp.require("devtools/client/framework/devtools-browser").gDevToolsBrowser;
-    return gDevToolsBrowser.inspectNode(gBrowser.selectedTab, this.target);
+    return DevToolsShim.inspectNode(gBrowser.selectedTab,
+                                    this.getNodeSelectors(this.target));
   },
 
   // Set various context menu attributes based on the state of the world.
   setTarget: function(aNode, aRangeParent, aRangeOffset) {
     // Currently "isRemote" is always false.
     //this.isRemote = gContextMenuContentData && gContextMenuContentData.isRemote;
 
     const xulNS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";