gecko-dev/dom/html/htmlMenuBuilder.js
Kris Maglione e930b89c34 Bug 1514594: Part 3 - Change ChromeUtils.import API.
***
Bug 1514594: Part 3a - Change ChromeUtils.import to return an exports object; not pollute global. r=mccr8

This changes the behavior of ChromeUtils.import() to return an exports object,
rather than a module global, in all cases except when `null` is passed as a
second argument, and changes the default behavior not to pollute the global
scope with the module's exports. Thus, the following code written for the old
model:

  ChromeUtils.import("resource://gre/modules/Services.jsm");

is approximately the same as the following, in the new model:

  var {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");

Since the two behaviors are mutually incompatible, this patch will land with a
scripted rewrite to update all existing callers to use the new model rather
than the old.
***
Bug 1514594: Part 3b - Mass rewrite all JS code to use the new ChromeUtils.import API. rs=Gijs

This was done using the followng script:

https://bitbucket.org/kmaglione/m-c-rewrites/src/tip/processors/cu-import-exports.jsm
***
Bug 1514594: Part 3c - Update ESLint plugin for ChromeUtils.import API changes. r=Standard8

Differential Revision: https://phabricator.services.mozilla.com/D16747
***
Bug 1514594: Part 3d - Remove/fix hundreds of duplicate imports from sync tests. r=Gijs

Differential Revision: https://phabricator.services.mozilla.com/D16748
***
Bug 1514594: Part 3e - Remove no-op ChromeUtils.import() calls. r=Gijs

Differential Revision: https://phabricator.services.mozilla.com/D16749
***
Bug 1514594: Part 3f.1 - Cleanup various test corner cases after mass rewrite. r=Gijs
***
Bug 1514594: Part 3f.2 - Cleanup various non-test corner cases after mass rewrite. r=Gijs

Differential Revision: https://phabricator.services.mozilla.com/D16750

--HG--
extra : rebase_source : 359574ee3064c90f33bf36c2ebe3159a24cc8895
extra : histedit_source : b93c8f42808b1599f9122d7842d2c0b3e656a594%2C64a3a4e3359dc889e2ab2b49461bab9e27fc10a7
2019-01-17 10:18:31 -08:00

135 lines
3.3 KiB
JavaScript

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// This component is used to build the menus for the HTML contextmenu attribute.
const {XPCOMUtils} = ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
// A global value that is used to identify each menu item. It is
// incremented with each one that is found.
var gGeneratedId = 1;
function HTMLMenuBuilder() {
this.currentNode = null;
this.root = null;
this.items = {};
this.nestedStack = [];
};
// Building is done in two steps:
// The first generates a hierarchical JS object that contains the menu structure.
// This object is returned by toJSONString.
//
// The second step can take this structure and generate a XUL menu hierarchy or
// other UI from this object. The default UI is done in PageMenu.jsm.
//
// When a multi-process browser is used, the first step is performed by the child
// process and the second step is performed by the parent process.
HTMLMenuBuilder.prototype =
{
classID: Components.ID("{51c65f5d-0de5-4edc-9058-60e50cef77f8}"),
QueryInterface: ChromeUtils.generateQI([Ci.nsIMenuBuilder]),
currentNode: null,
root: null,
items: {},
nestedStack: [],
toJSONString: function() {
return JSON.stringify(this.root);
},
openContainer: function(aLabel) {
if (!this.currentNode) {
this.root = {
type: "menu",
children: []
};
this.currentNode = this.root;
}
else {
let parent = this.currentNode;
this.currentNode = {
type: "menu",
label: aLabel,
children: []
};
parent.children.push(this.currentNode);
this.nestedStack.push(parent);
}
},
addItemFor: function(aElement, aCanLoadIcon) {
// Since we no longer type check this at the IDL level, make sure we've got
// the right element type here.
if (ChromeUtils.getClassName(aElement) !== "HTMLMenuItemElement") {
return;
}
if (!("children" in this.currentNode)) {
return;
}
let item = {
type: "menuitem",
label: aElement.label
};
let elementType = aElement.type;
if (elementType == "checkbox" || elementType == "radio") {
item.checkbox = true;
if (aElement.checked) {
item.checked = true;
}
}
let icon = aElement.icon;
if (icon.length > 0 && aCanLoadIcon) {
item.icon = icon;
}
if (aElement.disabled) {
item.disabled = true;
}
item.id = gGeneratedId++;
this.currentNode.children.push(item);
this.items[item.id] = aElement;
},
addSeparator: function() {
if (!("children" in this.currentNode)) {
return;
}
this.currentNode.children.push({ type: "separator"});
},
undoAddSeparator: function() {
if (!("children" in this.currentNode)) {
return;
}
let children = this.currentNode.children;
if (children.length && children[children.length - 1].type == "separator") {
children.pop();
}
},
closeContainer: function() {
this.currentNode = this.nestedStack.length ? this.nestedStack.pop() : this.root;
},
click: function(id) {
let item = this.items[id];
if (item) {
item.click();
}
}
};
this.NSGetFactory = XPCOMUtils.generateNSGetFactory([HTMLMenuBuilder]);