2014-06-25 05:12:07 +00:00
|
|
|
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
|
2012-08-23 18:00:43 +00:00
|
|
|
/* vim: set ft=javascript ts=2 et sw=2 tw=80: */
|
|
|
|
/* 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/. */
|
|
|
|
|
2013-04-11 20:59:08 +00:00
|
|
|
const {Cc, Cu, Ci} = require("chrome");
|
2012-08-23 18:00:43 +00:00
|
|
|
|
|
|
|
// Page size for pageup/pagedown
|
|
|
|
const PAGE_SIZE = 10;
|
2012-09-25 16:33:46 +00:00
|
|
|
const PREVIEW_AREA = 700;
|
2012-12-20 00:16:45 +00:00
|
|
|
const DEFAULT_MAX_CHILDREN = 100;
|
2013-09-12 22:25:08 +00:00
|
|
|
const COLLAPSE_ATTRIBUTE_LENGTH = 120;
|
|
|
|
const COLLAPSE_DATA_URL_REGEX = /^data.+base64/;
|
|
|
|
const COLLAPSE_DATA_URL_LENGTH = 60;
|
2014-01-09 11:36:01 +00:00
|
|
|
const NEW_SELECTION_HIGHLIGHTER_TIMER = 1000;
|
2012-09-25 16:33:46 +00:00
|
|
|
|
2013-09-12 14:48:13 +00:00
|
|
|
const {UndoStack} = require("devtools/shared/undo");
|
|
|
|
const {editableField, InplaceEditor} = require("devtools/shared/inplace-editor");
|
2013-09-16 10:01:25 +00:00
|
|
|
const {gDevTools} = Cu.import("resource:///modules/devtools/gDevTools.jsm", {});
|
2013-10-24 13:41:03 +00:00
|
|
|
const {HTMLEditor} = require("devtools/markupview/html-editor");
|
2014-04-22 19:55:01 +00:00
|
|
|
const promise = require("devtools/toolkit/deprecated-sync-thenables");
|
2013-10-25 19:21:01 +00:00
|
|
|
const {Tooltip} = require("devtools/shared/widgets/Tooltip");
|
2014-02-26 04:22:05 +00:00
|
|
|
const EventEmitter = require("devtools/toolkit/event-emitter");
|
2014-09-29 07:29:00 +00:00
|
|
|
const Heritage = require("sdk/core/heritage");
|
2012-08-23 18:00:43 +00:00
|
|
|
|
2013-09-07 09:39:50 +00:00
|
|
|
Cu.import("resource://gre/modules/devtools/LayoutHelpers.jsm");
|
2013-05-09 14:15:22 +00:00
|
|
|
Cu.import("resource://gre/modules/devtools/Templater.jsm");
|
2012-09-25 16:33:46 +00:00
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
2014-01-30 16:33:53 +00:00
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
2013-08-08 13:55:39 +00:00
|
|
|
|
|
|
|
loader.lazyGetter(this, "DOMParser", function() {
|
|
|
|
return Cc["@mozilla.org/xmlextras/domparser;1"].createInstance(Ci.nsIDOMParser);
|
|
|
|
});
|
2013-10-02 00:14:00 +00:00
|
|
|
loader.lazyGetter(this, "AutocompletePopup", () => {
|
2014-11-22 07:48:00 +00:00
|
|
|
return require("devtools/shared/autocomplete-popup").AutocompletePopup;
|
2013-10-02 00:14:00 +00:00
|
|
|
});
|
2012-08-23 18:00:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Vocabulary for the purposes of this file:
|
|
|
|
*
|
|
|
|
* MarkupContainer - the structure that holds an editor and its
|
|
|
|
* immediate children in the markup panel.
|
2014-09-29 07:29:00 +00:00
|
|
|
* - MarkupElementContainer: markup container for element nodes
|
|
|
|
* - MarkupTextContainer: markup container for text / comment nodes
|
|
|
|
* - MarkupReadonlyContainer: markup container for other nodes
|
2012-08-23 18:00:43 +00:00
|
|
|
* Node - A content node.
|
|
|
|
* object.elt - A UI element in the markup panel.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The markup tree. Manages the mapping of nodes to MarkupContainers,
|
|
|
|
* updating based on mutations, and the undo/redo bindings.
|
|
|
|
*
|
|
|
|
* @param Inspector aInspector
|
|
|
|
* The inspector we're watching.
|
|
|
|
* @param iframe aFrame
|
|
|
|
* An iframe in which the caller has kindly loaded markup-view.xhtml.
|
|
|
|
*/
|
2013-10-02 00:14:00 +00:00
|
|
|
function MarkupView(aInspector, aFrame, aControllerWindow) {
|
2012-08-23 18:00:43 +00:00
|
|
|
this._inspector = aInspector;
|
2013-06-11 04:18:46 +00:00
|
|
|
this.walker = this._inspector.walker;
|
2012-08-23 18:00:43 +00:00
|
|
|
this._frame = aFrame;
|
|
|
|
this.doc = this._frame.contentDocument;
|
|
|
|
this._elt = this.doc.querySelector("#root");
|
2013-10-24 13:41:03 +00:00
|
|
|
this.htmlEditor = new HTMLEditor(this.doc);
|
2012-08-23 18:00:43 +00:00
|
|
|
|
2013-09-08 09:01:00 +00:00
|
|
|
this.layoutHelpers = new LayoutHelpers(this.doc.defaultView);
|
|
|
|
|
2012-12-20 00:16:45 +00:00
|
|
|
try {
|
|
|
|
this.maxChildren = Services.prefs.getIntPref("devtools.markup.pagesize");
|
|
|
|
} catch(ex) {
|
|
|
|
this.maxChildren = DEFAULT_MAX_CHILDREN;
|
|
|
|
}
|
|
|
|
|
2013-08-02 10:35:50 +00:00
|
|
|
// Creating the popup to be used to show CSS suggestions.
|
|
|
|
let options = {
|
|
|
|
autoSelect: true,
|
|
|
|
theme: "auto"
|
|
|
|
};
|
|
|
|
this.popup = new AutocompletePopup(this.doc.defaultView.parent.document, options);
|
|
|
|
|
2012-08-23 18:00:43 +00:00
|
|
|
this.undo = new UndoStack();
|
2012-11-30 08:07:59 +00:00
|
|
|
this.undo.installController(aControllerWindow);
|
2012-08-23 18:00:43 +00:00
|
|
|
|
2013-10-11 15:50:33 +00:00
|
|
|
this._containers = new Map();
|
2012-08-23 18:00:43 +00:00
|
|
|
|
2013-06-11 04:18:46 +00:00
|
|
|
this._boundMutationObserver = this._mutationObserver.bind(this);
|
2013-09-16 10:01:25 +00:00
|
|
|
this.walker.on("mutations", this._boundMutationObserver);
|
2012-08-23 18:00:43 +00:00
|
|
|
|
2014-06-05 12:50:03 +00:00
|
|
|
this._boundOnDisplayChange = this._onDisplayChange.bind(this);
|
|
|
|
this.walker.on("display-change", this._boundOnDisplayChange);
|
|
|
|
|
2014-07-20 11:03:59 +00:00
|
|
|
this._onMouseClick = this._onMouseClick.bind(this);
|
|
|
|
|
2012-11-30 08:07:59 +00:00
|
|
|
this._boundOnNewSelection = this._onNewSelection.bind(this);
|
2013-06-11 04:18:46 +00:00
|
|
|
this._inspector.selection.on("new-node-front", this._boundOnNewSelection);
|
2012-11-30 08:07:59 +00:00
|
|
|
this._onNewSelection();
|
2012-08-23 18:00:43 +00:00
|
|
|
|
|
|
|
this._boundKeyDown = this._onKeyDown.bind(this);
|
2012-12-12 02:04:00 +00:00
|
|
|
this._frame.contentWindow.addEventListener("keydown", this._boundKeyDown, false);
|
2012-08-23 18:00:43 +00:00
|
|
|
|
|
|
|
this._boundFocus = this._onFocus.bind(this);
|
|
|
|
this._frame.addEventListener("focus", this._boundFocus, false);
|
2012-09-25 16:33:46 +00:00
|
|
|
|
2014-07-20 11:03:59 +00:00
|
|
|
this._makeTooltipPersistent = this._makeTooltipPersistent.bind(this);
|
|
|
|
|
2012-09-25 16:33:46 +00:00
|
|
|
this._initPreview();
|
2014-01-09 11:36:01 +00:00
|
|
|
this._initTooltips();
|
|
|
|
this._initHighlighter();
|
2013-11-05 16:19:29 +00:00
|
|
|
|
2014-01-09 11:36:01 +00:00
|
|
|
EventEmitter.decorate(this);
|
2012-08-23 18:00:43 +00:00
|
|
|
}
|
|
|
|
|
2013-04-11 20:59:08 +00:00
|
|
|
exports.MarkupView = MarkupView;
|
|
|
|
|
2012-08-23 18:00:43 +00:00
|
|
|
MarkupView.prototype = {
|
2014-08-11 08:43:00 +00:00
|
|
|
/**
|
|
|
|
* How long does a node flash when it mutates (in ms).
|
|
|
|
*/
|
|
|
|
CONTAINER_FLASHING_DURATION: 500,
|
|
|
|
|
2012-08-23 18:00:43 +00:00
|
|
|
_selectedContainer: null,
|
|
|
|
|
2014-01-09 11:36:01 +00:00
|
|
|
_initTooltips: function() {
|
|
|
|
this.tooltip = new Tooltip(this._inspector.panelDoc);
|
2014-07-20 11:03:59 +00:00
|
|
|
this._makeTooltipPersistent(false);
|
|
|
|
|
|
|
|
this._elt.addEventListener("click", this._onMouseClick, false);
|
2014-01-09 11:36:01 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
_initHighlighter: function() {
|
|
|
|
// Show the box model on markup-view mousemove
|
|
|
|
this._onMouseMove = this._onMouseMove.bind(this);
|
|
|
|
this._elt.addEventListener("mousemove", this._onMouseMove, false);
|
|
|
|
this._onMouseLeave = this._onMouseLeave.bind(this);
|
|
|
|
this._elt.addEventListener("mouseleave", this._onMouseLeave, false);
|
|
|
|
|
|
|
|
// Show markup-containers as hovered on toolbox "picker-node-hovered" event
|
|
|
|
// which happens when the "pick" button is pressed
|
|
|
|
this._onToolboxPickerHover = (event, nodeFront) => {
|
|
|
|
this.showNode(nodeFront, true).then(() => {
|
|
|
|
this._showContainerAsHovered(nodeFront);
|
|
|
|
});
|
2014-07-20 11:03:59 +00:00
|
|
|
};
|
2014-07-19 22:36:56 +00:00
|
|
|
this._inspector.toolbox.on("picker-node-hovered", this._onToolboxPickerHover);
|
2014-07-18 13:25:03 +00:00
|
|
|
},
|
|
|
|
|
2014-07-20 11:03:59 +00:00
|
|
|
_makeTooltipPersistent: function(state) {
|
|
|
|
if (state) {
|
|
|
|
this.tooltip.stopTogglingOnHover();
|
|
|
|
} else {
|
|
|
|
this.tooltip.startTogglingOnHover(this._elt,
|
|
|
|
this._isImagePreviewTarget.bind(this));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-01-09 11:36:01 +00:00
|
|
|
_onMouseMove: function(event) {
|
|
|
|
let target = event.target;
|
|
|
|
|
|
|
|
// Search target for a markupContainer reference, if not found, walk up
|
|
|
|
while (!target.container) {
|
|
|
|
if (target.tagName.toLowerCase() === "body") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
target = target.parentNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
let container = target.container;
|
|
|
|
if (this._hoveredNode !== container.node) {
|
|
|
|
if (container.node.nodeType !== Ci.nsIDOMNode.TEXT_NODE) {
|
|
|
|
this._showBoxModel(container.node);
|
|
|
|
} else {
|
|
|
|
this._hideBoxModel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this._showContainerAsHovered(container.node);
|
|
|
|
},
|
|
|
|
|
2014-07-20 11:03:59 +00:00
|
|
|
_onMouseClick: function(event) {
|
|
|
|
// From the target passed here, let's find the parent MarkupContainer
|
|
|
|
// and ask it if the tooltip should be shown
|
|
|
|
let parentNode = event.target;
|
|
|
|
let container;
|
|
|
|
while (parentNode !== this.doc.body) {
|
|
|
|
if (parentNode.container) {
|
|
|
|
container = parentNode.container;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
parentNode = parentNode.parentNode;
|
|
|
|
}
|
|
|
|
|
2014-09-29 07:29:00 +00:00
|
|
|
if (container instanceof MarkupElementContainer) {
|
2014-07-20 11:03:59 +00:00
|
|
|
// With the newly found container, delegate the tooltip content creation
|
|
|
|
// and decision to show or not the tooltip
|
|
|
|
container._buildEventTooltipContent(event.target, this.tooltip);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-01-09 11:36:01 +00:00
|
|
|
_hoveredNode: null,
|
2014-06-23 15:20:01 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Show a NodeFront's container as being hovered
|
|
|
|
* @param {NodeFront} nodeFront The node to show as hovered
|
|
|
|
*/
|
2014-01-09 11:36:01 +00:00
|
|
|
_showContainerAsHovered: function(nodeFront) {
|
2014-06-23 15:20:01 +00:00
|
|
|
if (this._hoveredNode === nodeFront) {
|
|
|
|
return;
|
|
|
|
}
|
2014-01-09 11:36:01 +00:00
|
|
|
|
2014-06-23 15:20:01 +00:00
|
|
|
if (this._hoveredNode) {
|
|
|
|
this.getContainer(this._hoveredNode).hovered = false;
|
2014-01-09 11:36:01 +00:00
|
|
|
}
|
2014-06-23 15:20:01 +00:00
|
|
|
|
|
|
|
this.getContainer(nodeFront).hovered = true;
|
|
|
|
this._hoveredNode = nodeFront;
|
2014-01-09 11:36:01 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
_onMouseLeave: function() {
|
2014-03-13 21:36:48 +00:00
|
|
|
this._hideBoxModel(true);
|
2014-02-25 15:33:57 +00:00
|
|
|
if (this._hoveredNode) {
|
2014-06-23 15:20:01 +00:00
|
|
|
this.getContainer(this._hoveredNode).hovered = false;
|
2014-02-25 15:33:57 +00:00
|
|
|
}
|
2014-02-06 18:45:40 +00:00
|
|
|
this._hoveredNode = null;
|
2014-01-09 11:36:01 +00:00
|
|
|
},
|
|
|
|
|
2014-06-13 14:27:10 +00:00
|
|
|
/**
|
|
|
|
* Show the box model highlighter on a given node front
|
|
|
|
* @param {NodeFront} nodeFront The node to show the highlighter for
|
|
|
|
* @return a promise that resolves when the highlighter for this nodeFront is
|
|
|
|
* shown, taking into account that there could already be highlighter requests
|
|
|
|
* queued up
|
|
|
|
*/
|
2014-06-25 14:40:34 +00:00
|
|
|
_showBoxModel: function(nodeFront) {
|
|
|
|
return this._inspector.toolbox.highlighterUtils.highlightNodeFront(nodeFront);
|
2014-01-09 11:36:01 +00:00
|
|
|
},
|
|
|
|
|
2014-06-13 14:27:10 +00:00
|
|
|
/**
|
|
|
|
* Hide the box model highlighter on a given node front
|
|
|
|
* @param {NodeFront} nodeFront The node to hide the highlighter for
|
|
|
|
* @param {Boolean} forceHide See toolbox-highlighter-utils/unhighlight
|
|
|
|
* @return a promise that resolves when the highlighter for this nodeFront is
|
|
|
|
* hidden, taking into account that there could already be highlighter requests
|
|
|
|
* queued up
|
|
|
|
*/
|
2014-03-13 21:36:48 +00:00
|
|
|
_hideBoxModel: function(forceHide) {
|
2014-03-17 18:11:00 +00:00
|
|
|
return this._inspector.toolbox.highlighterUtils.unhighlight(forceHide);
|
2014-01-09 11:36:01 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
_briefBoxModelTimer: null,
|
2014-06-25 14:40:34 +00:00
|
|
|
_brieflyShowBoxModel: function(nodeFront) {
|
2014-01-09 11:36:01 +00:00
|
|
|
let win = this._frame.contentWindow;
|
|
|
|
|
|
|
|
if (this._briefBoxModelTimer) {
|
|
|
|
win.clearTimeout(this._briefBoxModelTimer);
|
|
|
|
this._briefBoxModelTimer = null;
|
|
|
|
}
|
|
|
|
|
2014-06-25 14:40:34 +00:00
|
|
|
this._showBoxModel(nodeFront);
|
2014-01-09 11:36:01 +00:00
|
|
|
|
|
|
|
this._briefBoxModelTimer = this._frame.contentWindow.setTimeout(() => {
|
|
|
|
this._hideBoxModel();
|
|
|
|
}, NEW_SELECTION_HIGHLIGHTER_TIMER);
|
|
|
|
},
|
|
|
|
|
2013-10-02 00:14:00 +00:00
|
|
|
template: function(aName, aDest, aOptions={stack: "markup-view.xhtml"}) {
|
2012-08-23 18:00:43 +00:00
|
|
|
let node = this.doc.getElementById("template-" + aName).cloneNode(true);
|
|
|
|
node.removeAttribute("id");
|
|
|
|
template(node, aDest, aOptions);
|
|
|
|
return node;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the MarkupContainer object for a given node, or undefined if
|
|
|
|
* none exists.
|
|
|
|
*/
|
2013-10-02 00:14:00 +00:00
|
|
|
getContainer: function(aNode) {
|
2012-08-23 18:00:43 +00:00
|
|
|
return this._containers.get(aNode);
|
|
|
|
},
|
|
|
|
|
2013-09-16 10:01:25 +00:00
|
|
|
update: function() {
|
2014-06-14 10:49:00 +00:00
|
|
|
let updateChildren = (node) => {
|
2013-09-16 10:01:25 +00:00
|
|
|
this.getContainer(node).update();
|
|
|
|
for (let child of node.treeChildren()) {
|
|
|
|
updateChildren(child);
|
|
|
|
}
|
2014-06-14 10:49:00 +00:00
|
|
|
};
|
2013-09-16 10:01:25 +00:00
|
|
|
|
|
|
|
// Start with the documentElement
|
|
|
|
let documentElement;
|
|
|
|
for (let node of this._rootNode.treeChildren()) {
|
|
|
|
if (node.isDocumentElement === true) {
|
|
|
|
documentElement = node;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Recursively update each node starting with documentElement.
|
|
|
|
updateChildren(documentElement);
|
|
|
|
},
|
|
|
|
|
2014-03-19 09:11:34 +00:00
|
|
|
/**
|
|
|
|
* Executed when the mouse hovers over a target in the markup-view and is used
|
|
|
|
* to decide whether this target should be used to display an image preview
|
|
|
|
* tooltip.
|
|
|
|
* Delegates the actual decision to the corresponding MarkupContainer instance
|
|
|
|
* if one is found.
|
2014-09-29 07:29:00 +00:00
|
|
|
* @return the promise returned by MarkupElementContainer._isImagePreviewTarget
|
2014-03-19 09:11:34 +00:00
|
|
|
*/
|
|
|
|
_isImagePreviewTarget: function(target) {
|
2013-11-05 16:19:29 +00:00
|
|
|
// From the target passed here, let's find the parent MarkupContainer
|
|
|
|
// and ask it if the tooltip should be shown
|
|
|
|
let parent = target, container;
|
|
|
|
while (parent !== this.doc.body) {
|
|
|
|
if (parent.container) {
|
|
|
|
container = parent.container;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
parent = parent.parentNode;
|
|
|
|
}
|
|
|
|
|
2014-09-29 07:29:00 +00:00
|
|
|
if (container instanceof MarkupElementContainer) {
|
2013-11-05 16:19:29 +00:00
|
|
|
// With the newly found container, delegate the tooltip content creation
|
|
|
|
// and decision to show or not the tooltip
|
2014-09-29 07:29:00 +00:00
|
|
|
return container.isImagePreviewTarget(target, this.tooltip);
|
2013-11-05 16:19:29 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-01-17 22:07:48 +00:00
|
|
|
/**
|
|
|
|
* Given the known reason, should the current selection be briefly highlighted
|
|
|
|
* In a few cases, we don't want to highlight the node:
|
|
|
|
* - If the reason is null (used to reset the selection),
|
|
|
|
* - if it's "inspector-open" (when the inspector opens up, let's not highlight
|
|
|
|
* the default node)
|
|
|
|
* - if it's "navigateaway" (since the page is being navigated away from)
|
|
|
|
* - if it's "test" (this is a special case for mochitest. In tests, we often
|
|
|
|
* need to select elements but don't necessarily want the highlighter to come
|
|
|
|
* and go after a delay as this might break test scenarios)
|
2014-02-06 18:45:40 +00:00
|
|
|
* We also do not want to start a brief highlight timeout if the node is already
|
|
|
|
* being hovered over, since in that case it will already be highlighted.
|
2014-01-17 22:07:48 +00:00
|
|
|
*/
|
|
|
|
_shouldNewSelectionBeHighlighted: function() {
|
|
|
|
let reason = this._inspector.selection.reason;
|
2014-06-17 11:50:41 +00:00
|
|
|
let unwantedReasons = ["inspector-open", "navigateaway", "nodeselected", "test"];
|
2014-02-06 18:45:40 +00:00
|
|
|
let isHighlitNode = this._hoveredNode === this._inspector.selection.nodeFront;
|
|
|
|
return !isHighlitNode && reason && unwantedReasons.indexOf(reason) === -1;
|
2014-01-17 22:07:48 +00:00
|
|
|
},
|
|
|
|
|
2012-08-23 18:00:43 +00:00
|
|
|
/**
|
2012-11-30 08:07:59 +00:00
|
|
|
* Highlight the inspector selected node.
|
2012-08-23 18:00:43 +00:00
|
|
|
*/
|
2013-10-02 00:14:00 +00:00
|
|
|
_onNewSelection: function() {
|
2014-01-09 11:36:01 +00:00
|
|
|
let selection = this._inspector.selection;
|
|
|
|
|
2013-10-24 13:41:03 +00:00
|
|
|
this.htmlEditor.hide();
|
2014-06-23 15:20:01 +00:00
|
|
|
if (this._hoveredNode && this._hoveredNode !== selection.nodeFront) {
|
|
|
|
this.getContainer(this._hoveredNode).hovered = false;
|
|
|
|
this._hoveredNode = null;
|
|
|
|
}
|
|
|
|
|
2013-06-11 04:18:46 +00:00
|
|
|
let done = this._inspector.updating("markup-view");
|
2014-01-09 11:36:01 +00:00
|
|
|
if (selection.isNode()) {
|
2014-01-17 22:07:48 +00:00
|
|
|
if (this._shouldNewSelectionBeHighlighted()) {
|
2014-06-25 14:40:34 +00:00
|
|
|
this._brieflyShowBoxModel(selection.nodeFront);
|
2014-01-09 11:36:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this.showNode(selection.nodeFront, true).then(() => {
|
|
|
|
if (selection.reason !== "treepanel") {
|
|
|
|
this.markNodeAsSelected(selection.nodeFront);
|
|
|
|
}
|
2013-06-11 04:18:46 +00:00
|
|
|
done();
|
2014-03-22 08:02:14 +00:00
|
|
|
}, (e) => {
|
|
|
|
console.error(e);
|
|
|
|
done();
|
2013-06-11 04:18:46 +00:00
|
|
|
});
|
2012-11-30 08:07:59 +00:00
|
|
|
} else {
|
|
|
|
this.unmarkSelectedNode();
|
2013-06-11 04:18:46 +00:00
|
|
|
done();
|
2012-08-23 18:00:43 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a TreeWalker to find the next/previous
|
|
|
|
* node for selection.
|
|
|
|
*/
|
2013-10-02 00:14:00 +00:00
|
|
|
_selectionWalker: function(aStart) {
|
2012-08-23 18:00:43 +00:00
|
|
|
let walker = this.doc.createTreeWalker(
|
|
|
|
aStart || this._elt,
|
|
|
|
Ci.nsIDOMNodeFilter.SHOW_ELEMENT,
|
|
|
|
function(aElement) {
|
2013-06-11 04:18:46 +00:00
|
|
|
if (aElement.container &&
|
|
|
|
aElement.container.elt === aElement &&
|
|
|
|
aElement.container.visible) {
|
2012-08-23 18:00:43 +00:00
|
|
|
return Ci.nsIDOMNodeFilter.FILTER_ACCEPT;
|
|
|
|
}
|
|
|
|
return Ci.nsIDOMNodeFilter.FILTER_SKIP;
|
2013-02-06 14:22:33 +00:00
|
|
|
}
|
2012-08-23 18:00:43 +00:00
|
|
|
);
|
|
|
|
walker.currentNode = this._selectedContainer.elt;
|
|
|
|
return walker;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Key handling.
|
|
|
|
*/
|
2013-10-02 00:14:00 +00:00
|
|
|
_onKeyDown: function(aEvent) {
|
2012-08-23 18:00:43 +00:00
|
|
|
let handled = true;
|
|
|
|
|
|
|
|
// Ignore keystrokes that originated in editors.
|
|
|
|
if (aEvent.target.tagName.toLowerCase() === "input" ||
|
|
|
|
aEvent.target.tagName.toLowerCase() === "textarea") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(aEvent.keyCode) {
|
2013-07-31 18:33:56 +00:00
|
|
|
case Ci.nsIDOMKeyEvent.DOM_VK_H:
|
2014-06-16 11:43:00 +00:00
|
|
|
if (aEvent.metaKey || aEvent.shiftKey) {
|
|
|
|
handled = false;
|
2013-07-31 18:33:56 +00:00
|
|
|
} else {
|
2014-06-16 11:43:00 +00:00
|
|
|
let node = this._selectedContainer.node;
|
|
|
|
if (node.hidden) {
|
|
|
|
this.walker.unhideNode(node).then(() => this.nodeChanged(node));
|
|
|
|
} else {
|
|
|
|
this.walker.hideNode(node).then(() => this.nodeChanged(node));
|
|
|
|
}
|
2013-07-31 18:33:56 +00:00
|
|
|
}
|
|
|
|
break;
|
2012-08-23 18:00:43 +00:00
|
|
|
case Ci.nsIDOMKeyEvent.DOM_VK_DELETE:
|
2012-08-25 00:59:48 +00:00
|
|
|
this.deleteNode(this._selectedContainer.node);
|
2012-08-23 18:00:43 +00:00
|
|
|
break;
|
2014-12-07 19:54:00 +00:00
|
|
|
case Ci.nsIDOMKeyEvent.DOM_VK_BACK_SPACE:
|
|
|
|
this.deleteNode(this._selectedContainer.node, true);
|
|
|
|
break;
|
2012-08-23 18:00:43 +00:00
|
|
|
case Ci.nsIDOMKeyEvent.DOM_VK_HOME:
|
2014-06-23 15:20:01 +00:00
|
|
|
let rootContainer = this.getContainer(this._rootNode);
|
2013-06-11 04:18:46 +00:00
|
|
|
this.navigate(rootContainer.children.firstChild.container);
|
2012-08-23 18:00:43 +00:00
|
|
|
break;
|
|
|
|
case Ci.nsIDOMKeyEvent.DOM_VK_LEFT:
|
2013-03-01 02:50:24 +00:00
|
|
|
if (this._selectedContainer.expanded) {
|
|
|
|
this.collapseNode(this._selectedContainer.node);
|
|
|
|
} else {
|
|
|
|
let parent = this._selectionWalker().parentNode();
|
|
|
|
if (parent) {
|
|
|
|
this.navigate(parent.container);
|
|
|
|
}
|
|
|
|
}
|
2012-08-23 18:00:43 +00:00
|
|
|
break;
|
|
|
|
case Ci.nsIDOMKeyEvent.DOM_VK_RIGHT:
|
2013-09-12 14:48:13 +00:00
|
|
|
if (!this._selectedContainer.expanded &&
|
|
|
|
this._selectedContainer.hasChildren) {
|
2013-06-11 04:18:46 +00:00
|
|
|
this._expandContainer(this._selectedContainer);
|
2013-03-01 02:50:24 +00:00
|
|
|
} else {
|
|
|
|
let next = this._selectionWalker().nextNode();
|
|
|
|
if (next) {
|
|
|
|
this.navigate(next.container);
|
|
|
|
}
|
|
|
|
}
|
2012-08-23 18:00:43 +00:00
|
|
|
break;
|
|
|
|
case Ci.nsIDOMKeyEvent.DOM_VK_UP:
|
|
|
|
let prev = this._selectionWalker().previousNode();
|
|
|
|
if (prev) {
|
|
|
|
this.navigate(prev.container);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Ci.nsIDOMKeyEvent.DOM_VK_DOWN:
|
|
|
|
let next = this._selectionWalker().nextNode();
|
|
|
|
if (next) {
|
|
|
|
this.navigate(next.container);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Ci.nsIDOMKeyEvent.DOM_VK_PAGE_UP: {
|
|
|
|
let walker = this._selectionWalker();
|
|
|
|
let selection = this._selectedContainer;
|
|
|
|
for (let i = 0; i < PAGE_SIZE; i++) {
|
|
|
|
let prev = walker.previousNode();
|
|
|
|
if (!prev) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
selection = prev.container;
|
|
|
|
}
|
|
|
|
this.navigate(selection);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Ci.nsIDOMKeyEvent.DOM_VK_PAGE_DOWN: {
|
|
|
|
let walker = this._selectionWalker();
|
|
|
|
let selection = this._selectedContainer;
|
|
|
|
for (let i = 0; i < PAGE_SIZE; i++) {
|
|
|
|
let next = walker.nextNode();
|
|
|
|
if (!next) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
selection = next.container;
|
|
|
|
}
|
|
|
|
this.navigate(selection);
|
|
|
|
break;
|
|
|
|
}
|
2013-11-07 16:23:25 +00:00
|
|
|
case Ci.nsIDOMKeyEvent.DOM_VK_F2: {
|
|
|
|
this.beginEditingOuterHTML(this._selectedContainer.node);
|
|
|
|
break;
|
|
|
|
}
|
2012-08-23 18:00:43 +00:00
|
|
|
default:
|
|
|
|
handled = false;
|
|
|
|
}
|
|
|
|
if (handled) {
|
|
|
|
aEvent.stopPropagation();
|
|
|
|
aEvent.preventDefault();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a node from the DOM.
|
|
|
|
* This is an undoable action.
|
2014-12-07 19:54:00 +00:00
|
|
|
*
|
|
|
|
* @param {NodeFront} aNode The node to remove.
|
|
|
|
* @param {boolean} moveBackward If set to true, focus the previous sibling,
|
|
|
|
* otherwise the next one.
|
2012-08-23 18:00:43 +00:00
|
|
|
*/
|
2014-12-07 19:54:00 +00:00
|
|
|
deleteNode: function(aNode, moveBackward) {
|
2013-06-17 13:52:55 +00:00
|
|
|
if (aNode.isDocumentElement ||
|
2014-09-29 07:29:00 +00:00
|
|
|
aNode.nodeType == Ci.nsIDOMNode.DOCUMENT_TYPE_NODE ||
|
|
|
|
aNode.isAnonymous) {
|
2012-08-25 00:59:48 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-06-23 15:20:01 +00:00
|
|
|
let container = this.getContainer(aNode);
|
2012-08-23 18:00:43 +00:00
|
|
|
|
2013-06-17 13:52:55 +00:00
|
|
|
// Retain the node so we can undo this...
|
|
|
|
this.walker.retainNode(aNode).then(() => {
|
|
|
|
let parent = aNode.parentNode();
|
2014-11-21 20:48:04 +00:00
|
|
|
let nextSibling = null;
|
2013-06-17 13:52:55 +00:00
|
|
|
this.undo.do(() => {
|
2014-11-21 20:48:04 +00:00
|
|
|
this.walker.removeNode(aNode).then(siblings => {
|
|
|
|
nextSibling = siblings.nextSibling;
|
2014-12-07 19:54:00 +00:00
|
|
|
let focusNode = moveBackward ? siblings.previousSibling : nextSibling;
|
|
|
|
|
|
|
|
// If we can't move as the user wants, we move to the other direction.
|
|
|
|
// If there is no sibling elements anymore, move to the parent node.
|
|
|
|
if (!focusNode) {
|
|
|
|
focusNode = nextSibling || siblings.previousSibling || parent;
|
|
|
|
}
|
|
|
|
|
2014-11-21 20:48:04 +00:00
|
|
|
if (container.selected) {
|
|
|
|
this.navigate(this.getContainer(focusNode));
|
|
|
|
}
|
2013-06-17 13:52:55 +00:00
|
|
|
});
|
|
|
|
}, () => {
|
2014-11-21 20:48:04 +00:00
|
|
|
this.walker.insertBefore(aNode, parent, nextSibling);
|
2013-06-17 13:52:55 +00:00
|
|
|
});
|
|
|
|
}).then(null, console.error);
|
2012-08-23 18:00:43 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If an editable item is focused, select its container.
|
|
|
|
*/
|
2013-10-02 00:14:00 +00:00
|
|
|
_onFocus: function(aEvent) {
|
2012-08-23 18:00:43 +00:00
|
|
|
let parent = aEvent.target;
|
|
|
|
while (!parent.container) {
|
|
|
|
parent = parent.parentNode;
|
|
|
|
}
|
|
|
|
if (parent) {
|
|
|
|
this.navigate(parent.container, true);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle a user-requested navigation to a given MarkupContainer,
|
|
|
|
* updating the inspector's currently-selected node.
|
|
|
|
*
|
|
|
|
* @param MarkupContainer aContainer
|
|
|
|
* The container we're navigating to.
|
|
|
|
* @param aIgnoreFocus aIgnoreFocus
|
|
|
|
* If falsy, keyboard focus will be moved to the container too.
|
|
|
|
*/
|
2013-10-02 00:14:00 +00:00
|
|
|
navigate: function(aContainer, aIgnoreFocus) {
|
2012-08-23 18:00:43 +00:00
|
|
|
if (!aContainer) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let node = aContainer.node;
|
2013-10-24 13:41:03 +00:00
|
|
|
this.markNodeAsSelected(node, "treepanel");
|
|
|
|
|
2012-08-23 18:00:43 +00:00
|
|
|
if (!aIgnoreFocus) {
|
|
|
|
aContainer.focus();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Make sure a node is included in the markup tool.
|
|
|
|
*
|
2014-09-29 07:29:00 +00:00
|
|
|
* @param NodeFront aNode
|
2012-08-23 18:00:43 +00:00
|
|
|
* The node in the content document.
|
2013-09-23 06:46:12 +00:00
|
|
|
* @param boolean aFlashNode
|
|
|
|
* Whether the newly imported node should be flashed
|
2012-08-23 18:00:43 +00:00
|
|
|
* @returns MarkupContainer The MarkupContainer object for this element.
|
|
|
|
*/
|
2013-10-02 00:14:00 +00:00
|
|
|
importNode: function(aNode, aFlashNode) {
|
2012-08-23 18:00:43 +00:00
|
|
|
if (!aNode) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._containers.has(aNode)) {
|
2014-06-23 15:20:01 +00:00
|
|
|
return this.getContainer(aNode);
|
2012-08-23 18:00:43 +00:00
|
|
|
}
|
|
|
|
|
2014-09-29 07:29:00 +00:00
|
|
|
let container;
|
|
|
|
let {nodeType, isPseudoElement} = aNode;
|
2013-06-11 04:18:46 +00:00
|
|
|
if (aNode === this.walker.rootNode) {
|
2014-09-29 07:29:00 +00:00
|
|
|
container = new RootContainer(this, aNode);
|
2012-08-23 18:00:43 +00:00
|
|
|
this._elt.appendChild(container.elt);
|
|
|
|
this._rootNode = aNode;
|
2014-09-29 07:29:00 +00:00
|
|
|
} else if (nodeType == Ci.nsIDOMNode.ELEMENT_NODE && !isPseudoElement) {
|
|
|
|
container = new MarkupElementContainer(this, aNode, this._inspector);
|
|
|
|
} else if (nodeType == Ci.nsIDOMNode.COMMENT_NODE ||
|
|
|
|
nodeType == Ci.nsIDOMNode.TEXT_NODE) {
|
|
|
|
container = new MarkupTextContainer(this, aNode, this._inspector);
|
2013-06-11 04:18:46 +00:00
|
|
|
} else {
|
2014-09-29 07:29:00 +00:00
|
|
|
container = new MarkupReadOnlyContainer(this, aNode, this._inspector);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aFlashNode) {
|
|
|
|
container.flashMutation();
|
2012-08-23 18:00:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this._containers.set(aNode, container);
|
2012-12-20 00:16:45 +00:00
|
|
|
container.childrenDirty = true;
|
2013-06-11 04:18:46 +00:00
|
|
|
|
2012-08-23 18:00:43 +00:00
|
|
|
this._updateChildren(container);
|
|
|
|
|
|
|
|
return container;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mutation observer used for included nodes.
|
|
|
|
*/
|
2013-10-02 00:14:00 +00:00
|
|
|
_mutationObserver: function(aMutations) {
|
2013-10-15 12:09:49 +00:00
|
|
|
let requiresLayoutChange = false;
|
2013-10-24 13:41:03 +00:00
|
|
|
|
2012-08-23 18:00:43 +00:00
|
|
|
for (let mutation of aMutations) {
|
2013-06-11 04:18:46 +00:00
|
|
|
let type = mutation.type;
|
|
|
|
let target = mutation.target;
|
|
|
|
|
|
|
|
if (mutation.type === "documentUnload") {
|
|
|
|
// Treat this as a childList change of the child (maybe the protocol
|
|
|
|
// should do this).
|
2013-09-12 14:48:13 +00:00
|
|
|
type = "childList";
|
2013-06-11 04:18:46 +00:00
|
|
|
target = mutation.targetParent;
|
|
|
|
if (!target) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-23 15:20:01 +00:00
|
|
|
let container = this.getContainer(target);
|
2012-08-25 18:04:46 +00:00
|
|
|
if (!container) {
|
2013-06-11 04:18:46 +00:00
|
|
|
// Container might not exist if this came from a load event for a node
|
2012-08-25 18:04:46 +00:00
|
|
|
// we're not viewing.
|
|
|
|
continue;
|
|
|
|
}
|
2013-06-11 04:18:46 +00:00
|
|
|
if (type === "attributes" || type === "characterData") {
|
2013-10-08 12:53:19 +00:00
|
|
|
container.update();
|
2013-10-15 12:09:49 +00:00
|
|
|
|
|
|
|
// Auto refresh style properties on selected node when they change.
|
|
|
|
if (type === "attributes" && container.selected) {
|
|
|
|
requiresLayoutChange = true;
|
|
|
|
}
|
2013-06-11 04:18:46 +00:00
|
|
|
} else if (type === "childList") {
|
2012-12-20 00:16:45 +00:00
|
|
|
container.childrenDirty = true;
|
2013-10-24 13:41:03 +00:00
|
|
|
// Update the children to take care of changes in the markup view DOM.
|
2014-10-08 22:46:16 +00:00
|
|
|
this._updateChildren(container, {flash: true});
|
2012-08-23 18:00:43 +00:00
|
|
|
}
|
|
|
|
}
|
2013-10-15 12:09:49 +00:00
|
|
|
|
|
|
|
if (requiresLayoutChange) {
|
|
|
|
this._inspector.immediateLayoutChange();
|
|
|
|
}
|
2013-10-24 13:41:03 +00:00
|
|
|
this._waitForChildren().then((nodes) => {
|
2013-09-23 06:46:12 +00:00
|
|
|
this._flashMutatedNodes(aMutations);
|
2013-10-24 13:41:03 +00:00
|
|
|
this._inspector.emit("markupmutation", aMutations);
|
|
|
|
|
|
|
|
// Since the htmlEditor is absolutely positioned, a mutation may change
|
|
|
|
// the location in which it should be shown.
|
|
|
|
this.htmlEditor.refresh();
|
2013-06-11 04:18:46 +00:00
|
|
|
});
|
2012-08-23 18:00:43 +00:00
|
|
|
},
|
|
|
|
|
2014-06-05 12:50:03 +00:00
|
|
|
/**
|
|
|
|
* React to display-change events from the walker
|
|
|
|
* @param {Array} nodes An array of nodeFronts
|
|
|
|
*/
|
|
|
|
_onDisplayChange: function(nodes) {
|
|
|
|
for (let node of nodes) {
|
2014-06-23 15:20:01 +00:00
|
|
|
let container = this.getContainer(node);
|
2014-06-05 12:50:03 +00:00
|
|
|
if (container) {
|
|
|
|
container.isDisplayed = node.isDisplayed;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-09-23 06:46:12 +00:00
|
|
|
/**
|
|
|
|
* Given a list of mutations returned by the mutation observer, flash the
|
|
|
|
* corresponding containers to attract attention.
|
|
|
|
*/
|
2013-10-02 00:14:00 +00:00
|
|
|
_flashMutatedNodes: function(aMutations) {
|
2013-09-23 06:46:12 +00:00
|
|
|
let addedOrEditedContainers = new Set();
|
|
|
|
let removedContainers = new Set();
|
|
|
|
|
|
|
|
for (let {type, target, added, removed} of aMutations) {
|
2014-06-23 15:20:01 +00:00
|
|
|
let container = this.getContainer(target);
|
2013-09-23 06:46:12 +00:00
|
|
|
|
|
|
|
if (container) {
|
|
|
|
if (type === "attributes" || type === "characterData") {
|
|
|
|
addedOrEditedContainers.add(container);
|
|
|
|
} else if (type === "childList") {
|
|
|
|
// If there has been removals, flash the parent
|
|
|
|
if (removed.length) {
|
|
|
|
removedContainers.add(container);
|
|
|
|
}
|
|
|
|
|
2014-11-22 07:48:00 +00:00
|
|
|
// If there has been additions, flash the nodes if their associated
|
|
|
|
// container exist (so if their parent is expanded in the inspector).
|
2013-09-23 06:46:12 +00:00
|
|
|
added.forEach(added => {
|
2014-06-23 15:20:01 +00:00
|
|
|
let addedContainer = this.getContainer(added);
|
2014-11-22 07:48:00 +00:00
|
|
|
if (addedContainer) {
|
|
|
|
addedOrEditedContainers.add(addedContainer);
|
|
|
|
|
|
|
|
// The node may be added as a result of an append, in which case
|
|
|
|
// it will have been removed from another container first, but in
|
|
|
|
// these cases we don't want to flash both the removal and the
|
|
|
|
// addition
|
|
|
|
removedContainers.delete(container);
|
|
|
|
}
|
2013-09-23 06:46:12 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let container of removedContainers) {
|
|
|
|
container.flashMutation();
|
|
|
|
}
|
|
|
|
for (let container of addedOrEditedContainers) {
|
|
|
|
container.flashMutation();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-08-23 18:00:43 +00:00
|
|
|
/**
|
|
|
|
* Make sure the given node's parents are expanded and the
|
|
|
|
* node is scrolled on to screen.
|
|
|
|
*/
|
2013-10-02 00:14:00 +00:00
|
|
|
showNode: function(aNode, centered) {
|
2013-06-11 04:18:46 +00:00
|
|
|
let parent = aNode;
|
2014-01-10 17:55:58 +00:00
|
|
|
|
|
|
|
this.importNode(aNode);
|
|
|
|
|
2013-06-11 04:18:46 +00:00
|
|
|
while ((parent = parent.parentNode())) {
|
|
|
|
this.importNode(parent);
|
2012-08-23 18:00:43 +00:00
|
|
|
this.expandNode(parent);
|
|
|
|
}
|
2013-06-11 04:18:46 +00:00
|
|
|
|
|
|
|
return this._waitForChildren().then(() => {
|
|
|
|
return this._ensureVisible(aNode);
|
|
|
|
}).then(() => {
|
|
|
|
// Why is this not working?
|
2014-06-23 15:20:01 +00:00
|
|
|
this.layoutHelpers.scrollIntoViewIfNeeded(this.getContainer(aNode).editor.elt, centered);
|
2014-12-10 08:09:19 +00:00
|
|
|
}, e => {
|
|
|
|
// Only report this rejection as an error if the panel hasn't been
|
|
|
|
// destroyed in the meantime.
|
|
|
|
if (!this._destroyer) {
|
|
|
|
console.error(e);
|
|
|
|
} else {
|
|
|
|
console.warn("Could not show the node, the markup-view was destroyed " +
|
|
|
|
"while waiting for children");
|
|
|
|
}
|
2013-06-11 04:18:46 +00:00
|
|
|
});
|
2012-08-23 18:00:43 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Expand the container's children.
|
|
|
|
*/
|
2013-10-02 00:14:00 +00:00
|
|
|
_expandContainer: function(aContainer) {
|
2013-09-23 06:46:12 +00:00
|
|
|
return this._updateChildren(aContainer, {expand: true}).then(() => {
|
2012-08-23 18:00:43 +00:00
|
|
|
aContainer.expanded = true;
|
2013-09-12 14:48:13 +00:00
|
|
|
});
|
2012-08-23 18:00:43 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Expand the node's children.
|
|
|
|
*/
|
2013-10-02 00:14:00 +00:00
|
|
|
expandNode: function(aNode) {
|
2014-06-23 15:20:01 +00:00
|
|
|
let container = this.getContainer(aNode);
|
2012-08-23 18:00:43 +00:00
|
|
|
this._expandContainer(container);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Expand the entire tree beneath a container.
|
|
|
|
*
|
|
|
|
* @param aContainer The container to expand.
|
|
|
|
*/
|
2013-10-02 00:14:00 +00:00
|
|
|
_expandAll: function(aContainer) {
|
2013-06-11 04:18:46 +00:00
|
|
|
return this._expandContainer(aContainer).then(() => {
|
|
|
|
let child = aContainer.children.firstChild;
|
|
|
|
let promises = [];
|
|
|
|
while (child) {
|
|
|
|
promises.push(this._expandAll(child.container));
|
|
|
|
child = child.nextSibling;
|
|
|
|
}
|
|
|
|
return promise.all(promises);
|
|
|
|
}).then(null, console.error);
|
2012-08-23 18:00:43 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Expand the entire tree beneath a node.
|
|
|
|
*
|
|
|
|
* @param aContainer The node to expand, or null
|
|
|
|
* to start from the top.
|
|
|
|
*/
|
2013-10-02 00:14:00 +00:00
|
|
|
expandAll: function(aNode) {
|
2012-08-23 18:00:43 +00:00
|
|
|
aNode = aNode || this._rootNode;
|
2014-06-23 15:20:01 +00:00
|
|
|
return this._expandAll(this.getContainer(aNode));
|
2012-08-23 18:00:43 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Collapse the node's children.
|
|
|
|
*/
|
2013-10-02 00:14:00 +00:00
|
|
|
collapseNode: function(aNode) {
|
2014-06-23 15:20:01 +00:00
|
|
|
let container = this.getContainer(aNode);
|
2012-08-23 18:00:43 +00:00
|
|
|
container.expanded = false;
|
|
|
|
},
|
|
|
|
|
2014-11-22 07:48:00 +00:00
|
|
|
/**
|
|
|
|
* Returns either the innerHTML or the outerHTML for a remote node.
|
|
|
|
* @param aNode The NodeFront to get the outerHTML / innerHTML for.
|
|
|
|
* @param isOuter A boolean that, if true, makes the function return the
|
|
|
|
* outerHTML, otherwise the innerHTML.
|
|
|
|
* @returns A promise that will be resolved with the outerHTML / innerHTML.
|
|
|
|
*/
|
|
|
|
_getNodeHTML: function(aNode, isOuter) {
|
|
|
|
let walkerPromise = null;
|
|
|
|
|
|
|
|
if (isOuter) {
|
|
|
|
walkerPromise = this.walker.outerHTML(aNode);
|
|
|
|
} else {
|
|
|
|
walkerPromise = this.walker.innerHTML(aNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
return walkerPromise.then(longstr => {
|
|
|
|
return longstr.string().then(html => {
|
|
|
|
longstr.release().then(null, console.error);
|
|
|
|
return html;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2013-10-24 13:41:03 +00:00
|
|
|
/**
|
|
|
|
* Retrieve the outerHTML for a remote node.
|
|
|
|
* @param aNode The NodeFront to get the outerHTML for.
|
|
|
|
* @returns A promise that will be resolved with the outerHTML.
|
|
|
|
*/
|
|
|
|
getNodeOuterHTML: function(aNode) {
|
2014-11-22 07:48:00 +00:00
|
|
|
return this._getNodeHTML(aNode, true);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve the innerHTML for a remote node.
|
|
|
|
* @param aNode The NodeFront to get the innerHTML for.
|
|
|
|
* @returns A promise that will be resolved with the innerHTML.
|
|
|
|
*/
|
|
|
|
getNodeInnerHTML: function(aNode) {
|
|
|
|
return this._getNodeHTML(aNode);
|
2013-10-24 13:41:03 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2014-10-08 22:46:16 +00:00
|
|
|
* Listen to mutations, expect a given node to be removed and try and select
|
|
|
|
* the node that sits at the same place instead.
|
|
|
|
* This is useful when changing the outerHTML or the tag name so that the
|
|
|
|
* newly inserted node gets selected instead of the one that just got removed.
|
2013-10-24 13:41:03 +00:00
|
|
|
*/
|
2014-10-08 22:46:16 +00:00
|
|
|
reselectOnRemoved: function(removedNode, reason) {
|
|
|
|
// Only allow one removed node reselection at a time, so that when there are
|
|
|
|
// more than 1 request in parallel, the last one wins.
|
|
|
|
this.cancelReselectOnRemoved();
|
|
|
|
|
|
|
|
// Get the removedNode index in its parent node to reselect the right node.
|
|
|
|
let isHTMLTag = removedNode.tagName.toLowerCase() === "html";
|
|
|
|
let oldContainer = this.getContainer(removedNode);
|
|
|
|
let parentContainer = this.getContainer(removedNode.parentNode());
|
|
|
|
let childIndex = parentContainer.getChildContainers().indexOf(oldContainer);
|
|
|
|
|
|
|
|
let onMutations = this._removedNodeObserver = (e, mutations) => {
|
|
|
|
let isNodeRemovalMutation = false;
|
|
|
|
for (let mutation of mutations) {
|
|
|
|
let containsRemovedNode = mutation.removed &&
|
|
|
|
mutation.removed.some(n => n === removedNode);
|
|
|
|
if (mutation.type === "childList" && (containsRemovedNode || isHTMLTag)) {
|
|
|
|
isNodeRemovalMutation = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!isNodeRemovalMutation) {
|
|
|
|
return;
|
|
|
|
}
|
2013-10-24 13:41:03 +00:00
|
|
|
|
2014-10-08 22:46:16 +00:00
|
|
|
this._inspector.off("markupmutation", onMutations);
|
|
|
|
this._removedNodeObserver = null;
|
|
|
|
|
|
|
|
// Don't select the new node if the user has already changed the current
|
|
|
|
// selection.
|
|
|
|
if (this._inspector.selection.nodeFront === parentContainer.node ||
|
|
|
|
(this._inspector.selection.nodeFront === removedNode && isHTMLTag)) {
|
|
|
|
let childContainers = parentContainer.getChildContainers();
|
|
|
|
if (childContainers && childContainers[childIndex]) {
|
|
|
|
this.markNodeAsSelected(childContainers[childIndex].node, reason);
|
|
|
|
if (childContainers[childIndex].hasChildren) {
|
|
|
|
this.expandNode(childContainers[childIndex].node);
|
|
|
|
}
|
|
|
|
this.emit("reselectedonremoved");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2013-10-24 13:41:03 +00:00
|
|
|
|
2014-10-08 22:46:16 +00:00
|
|
|
// Start listening for mutations until we find a childList change that has
|
|
|
|
// removedNode removed.
|
|
|
|
this._inspector.on("markupmutation", onMutations);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Make sure to stop listening for node removal markupmutations and not
|
|
|
|
* reselect the corresponding node when that happens.
|
|
|
|
* Useful when the outerHTML/tagname edition failed.
|
|
|
|
*/
|
|
|
|
cancelReselectOnRemoved: function() {
|
|
|
|
if (this._removedNodeObserver) {
|
|
|
|
this._inspector.off("markupmutation", this._removedNodeObserver);
|
|
|
|
this._removedNodeObserver = null;
|
|
|
|
this.emit("canceledreselectonremoved");
|
|
|
|
}
|
2013-10-24 13:41:03 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2014-08-21 16:59:53 +00:00
|
|
|
* Replace the outerHTML of any node displayed in the inspector with
|
|
|
|
* some other HTML code
|
2014-11-22 07:48:00 +00:00
|
|
|
* @param {NodeFront} node node which outerHTML will be replaced.
|
|
|
|
* @param {string} newValue The new outerHTML to set on the node.
|
|
|
|
* @param {string} oldValue The old outerHTML that will be used if the
|
|
|
|
* user undoes the update.
|
2014-08-21 16:59:53 +00:00
|
|
|
* @returns A promise that will resolve when the outer HTML has been updated.
|
2013-10-24 13:41:03 +00:00
|
|
|
*/
|
2014-11-22 07:48:00 +00:00
|
|
|
updateNodeOuterHTML: function(node, newValue, oldValue) {
|
|
|
|
let container = this.getContainer(node);
|
2013-10-24 13:41:03 +00:00
|
|
|
if (!container) {
|
2014-08-21 16:59:53 +00:00
|
|
|
return promise.reject();
|
2013-10-24 13:41:03 +00:00
|
|
|
}
|
|
|
|
|
2014-10-08 22:46:16 +00:00
|
|
|
// Changing the outerHTML removes the node which outerHTML was changed.
|
|
|
|
// Listen to this removal to reselect the right node afterwards.
|
2014-11-22 07:48:00 +00:00
|
|
|
this.reselectOnRemoved(node, "outerhtml");
|
|
|
|
return this.walker.setOuterHTML(node, newValue).then(null, () => {
|
2014-10-08 22:46:16 +00:00
|
|
|
this.cancelReselectOnRemoved();
|
2013-10-24 13:41:03 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2014-11-22 07:48:00 +00:00
|
|
|
/**
|
|
|
|
* Replace the innerHTML of any node displayed in the inspector with
|
|
|
|
* some other HTML code
|
|
|
|
* @param {Node} node node which innerHTML will be replaced.
|
|
|
|
* @param {string} newValue The new innerHTML to set on the node.
|
|
|
|
* @param {string} oldValue The old innerHTML that will be used if the user
|
|
|
|
* undoes the update.
|
|
|
|
* @returns A promise that will resolve when the inner HTML has been updated.
|
|
|
|
*/
|
|
|
|
updateNodeInnerHTML: function(node, newValue, oldValue) {
|
|
|
|
let container = this.getContainer(node);
|
|
|
|
if (!container) {
|
|
|
|
return promise.reject();
|
|
|
|
}
|
|
|
|
|
|
|
|
let def = promise.defer();
|
|
|
|
|
|
|
|
container.undo.do(() => {
|
|
|
|
this.walker.setInnerHTML(node, newValue).then(def.resolve, def.reject);
|
|
|
|
}, () => {
|
|
|
|
this.walker.setInnerHTML(node, oldValue);
|
|
|
|
});
|
|
|
|
|
|
|
|
return def.promise;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Insert adjacent HTML to any node displayed in the inspector.
|
|
|
|
*
|
|
|
|
* @param {NodeFront} node The reference node.
|
|
|
|
* @param {string} position The position as specified for Element.insertAdjacentHTML
|
|
|
|
* (i.e. "beforeBegin", "afterBegin", "beforeEnd", "afterEnd").
|
|
|
|
* @param {string} newValue The adjacent HTML.
|
|
|
|
* @returns A promise that will resolve when the adjacent HTML has
|
|
|
|
* been inserted.
|
|
|
|
*/
|
|
|
|
insertAdjacentHTMLToNode: function(node, position, value) {
|
|
|
|
let container = this.getContainer(node);
|
|
|
|
if (!container) {
|
|
|
|
return promise.reject();
|
|
|
|
}
|
|
|
|
|
|
|
|
let def = promise.defer();
|
|
|
|
|
|
|
|
let injectedNodes = [];
|
|
|
|
container.undo.do(() => {
|
|
|
|
this.walker.insertAdjacentHTML(node, position, value).then(nodeArray => {
|
|
|
|
injectedNodes = nodeArray.nodes;
|
|
|
|
return nodeArray;
|
|
|
|
}).then(def.resolve, def.reject);
|
|
|
|
}, () => {
|
|
|
|
this.walker.removeNodes(injectedNodes);
|
|
|
|
});
|
|
|
|
|
|
|
|
return def.promise;
|
|
|
|
},
|
|
|
|
|
2013-10-24 13:41:03 +00:00
|
|
|
/**
|
|
|
|
* Open an editor in the UI to allow editing of a node's outerHTML.
|
|
|
|
* @param aNode The NodeFront to edit.
|
|
|
|
*/
|
|
|
|
beginEditingOuterHTML: function(aNode) {
|
2014-11-22 07:48:00 +00:00
|
|
|
this.getNodeOuterHTML(aNode).then(oldValue => {
|
2014-06-23 15:20:01 +00:00
|
|
|
let container = this.getContainer(aNode);
|
2013-10-24 13:41:03 +00:00
|
|
|
if (!container) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.htmlEditor.show(container.tagLine, oldValue);
|
2013-11-07 16:23:25 +00:00
|
|
|
this.htmlEditor.once("popuphidden", (e, aCommit, aValue) => {
|
|
|
|
// Need to focus the <html> element instead of the frame / window
|
|
|
|
// in order to give keyboard focus back to doc (from editor).
|
|
|
|
this._frame.contentDocument.documentElement.focus();
|
|
|
|
|
2013-10-24 13:41:03 +00:00
|
|
|
if (aCommit) {
|
|
|
|
this.updateNodeOuterHTML(aNode, aValue, oldValue);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mark the given node expanded.
|
2014-04-17 21:39:29 +00:00
|
|
|
* @param {NodeFront} aNode The NodeFront to mark as expanded.
|
|
|
|
* @param {Boolean} aExpanded Whether the expand or collapse.
|
|
|
|
* @param {Boolean} aExpandDescendants Whether to expand all descendants too
|
2013-10-24 13:41:03 +00:00
|
|
|
*/
|
2014-04-17 21:39:29 +00:00
|
|
|
setNodeExpanded: function(aNode, aExpanded, aExpandDescendants) {
|
2013-06-11 04:18:46 +00:00
|
|
|
if (aExpanded) {
|
2014-04-17 21:39:29 +00:00
|
|
|
if (aExpandDescendants) {
|
|
|
|
this.expandAll(aNode);
|
|
|
|
} else {
|
|
|
|
this.expandNode(aNode);
|
|
|
|
}
|
2013-06-11 04:18:46 +00:00
|
|
|
} else {
|
|
|
|
this.collapseNode(aNode);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-08-23 18:00:43 +00:00
|
|
|
/**
|
2013-10-24 13:41:03 +00:00
|
|
|
* Mark the given node selected, and update the inspector.selection
|
|
|
|
* object's NodeFront to keep consistent state between UI and selection.
|
|
|
|
* @param aNode The NodeFront to mark as selected.
|
2012-08-23 18:00:43 +00:00
|
|
|
*/
|
2013-10-24 13:41:03 +00:00
|
|
|
markNodeAsSelected: function(aNode, reason) {
|
2014-06-23 15:20:01 +00:00
|
|
|
let container = this.getContainer(aNode);
|
2012-08-23 18:00:43 +00:00
|
|
|
if (this._selectedContainer === container) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (this._selectedContainer) {
|
|
|
|
this._selectedContainer.selected = false;
|
|
|
|
}
|
|
|
|
this._selectedContainer = container;
|
|
|
|
if (aNode) {
|
|
|
|
this._selectedContainer.selected = true;
|
|
|
|
}
|
|
|
|
|
2013-10-24 13:41:03 +00:00
|
|
|
this._inspector.selection.setNodeFront(aNode, reason || "nodeselected");
|
2012-08-23 18:00:43 +00:00
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
2012-12-20 00:16:45 +00:00
|
|
|
/**
|
|
|
|
* Make sure that every ancestor of the selection are updated
|
|
|
|
* and included in the list of visible children.
|
|
|
|
*/
|
2013-10-02 00:14:00 +00:00
|
|
|
_ensureVisible: function(node) {
|
2012-12-20 00:16:45 +00:00
|
|
|
while (node) {
|
2014-06-23 15:20:01 +00:00
|
|
|
let container = this.getContainer(node);
|
2013-06-11 04:18:46 +00:00
|
|
|
let parent = node.parentNode();
|
2012-12-20 00:16:45 +00:00
|
|
|
if (!container.elt.parentNode) {
|
2014-06-23 15:20:01 +00:00
|
|
|
let parentContainer = this.getContainer(parent);
|
2014-03-22 08:02:14 +00:00
|
|
|
if (parentContainer) {
|
|
|
|
parentContainer.childrenDirty = true;
|
2014-09-29 07:29:00 +00:00
|
|
|
this._updateChildren(parentContainer, {expand: true});
|
2014-03-22 08:02:14 +00:00
|
|
|
}
|
2012-12-20 00:16:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
node = parent;
|
|
|
|
}
|
2013-06-11 04:18:46 +00:00
|
|
|
return this._waitForChildren();
|
2012-12-20 00:16:45 +00:00
|
|
|
},
|
|
|
|
|
2012-11-30 08:07:59 +00:00
|
|
|
/**
|
|
|
|
* Unmark selected node (no node selected).
|
|
|
|
*/
|
2013-10-02 00:14:00 +00:00
|
|
|
unmarkSelectedNode: function() {
|
2012-11-30 08:07:59 +00:00
|
|
|
if (this._selectedContainer) {
|
|
|
|
this._selectedContainer.selected = false;
|
|
|
|
this._selectedContainer = null;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-08-25 18:04:47 +00:00
|
|
|
/**
|
|
|
|
* Called when the markup panel initiates a change on a node.
|
|
|
|
*/
|
2013-10-02 00:14:00 +00:00
|
|
|
nodeChanged: function(aNode) {
|
2013-07-31 18:33:56 +00:00
|
|
|
if (aNode === this._inspector.selection.nodeFront) {
|
2012-08-25 18:04:47 +00:00
|
|
|
this._inspector.change("markupview");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-06-11 04:18:46 +00:00
|
|
|
/**
|
|
|
|
* Check if the current selection is a descendent of the container.
|
|
|
|
* if so, make sure it's among the visible set for the container,
|
|
|
|
* and set the dirty flag if needed.
|
|
|
|
* @returns The node that should be made visible, if any.
|
|
|
|
*/
|
|
|
|
_checkSelectionVisible: function(aContainer) {
|
|
|
|
let centered = null;
|
|
|
|
let node = this._inspector.selection.nodeFront;
|
|
|
|
while (node) {
|
|
|
|
if (node.parentNode() === aContainer.node) {
|
|
|
|
centered = node;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
node = node.parentNode();
|
|
|
|
}
|
|
|
|
|
|
|
|
return centered;
|
|
|
|
},
|
|
|
|
|
2012-08-23 18:00:43 +00:00
|
|
|
/**
|
|
|
|
* Make sure all children of the given container's node are
|
|
|
|
* imported and attached to the container in the right order.
|
2013-06-11 04:18:46 +00:00
|
|
|
*
|
|
|
|
* Children need to be updated only in the following circumstances:
|
|
|
|
* a) We just imported this node and have never seen its children.
|
|
|
|
* container.childrenDirty will be set by importNode in this case.
|
|
|
|
* b) We received a childList mutation on the node.
|
|
|
|
* container.childrenDirty will be set in that case too.
|
|
|
|
* c) We have changed the selection, and the path to that selection
|
|
|
|
* wasn't loaded in a previous children request (because we only
|
|
|
|
* grab a subset).
|
|
|
|
* container.childrenDirty should be set in that case too!
|
|
|
|
*
|
2013-09-23 06:46:12 +00:00
|
|
|
* @param MarkupContainer aContainer
|
|
|
|
* The markup container whose children need updating
|
|
|
|
* @param Object options
|
|
|
|
* Options are {expand:boolean,flash:boolean}
|
|
|
|
* @return a promise that will be resolved when the children are ready
|
|
|
|
* (which may be immediately).
|
2012-08-23 18:00:43 +00:00
|
|
|
*/
|
2013-10-02 00:14:00 +00:00
|
|
|
_updateChildren: function(aContainer, options) {
|
2013-09-23 06:46:12 +00:00
|
|
|
let expand = options && options.expand;
|
|
|
|
let flash = options && options.flash;
|
|
|
|
|
2013-06-11 04:18:46 +00:00
|
|
|
aContainer.hasChildren = aContainer.node.hasChildren;
|
|
|
|
|
|
|
|
if (!this._queuedChildUpdates) {
|
|
|
|
this._queuedChildUpdates = new Map();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._queuedChildUpdates.has(aContainer)) {
|
|
|
|
return this._queuedChildUpdates.get(aContainer);
|
|
|
|
}
|
|
|
|
|
2012-12-20 00:16:45 +00:00
|
|
|
if (!aContainer.childrenDirty) {
|
2013-06-11 04:18:46 +00:00
|
|
|
return promise.resolve(aContainer);
|
2012-12-20 00:16:45 +00:00
|
|
|
}
|
|
|
|
|
2013-06-11 04:18:46 +00:00
|
|
|
if (!aContainer.hasChildren) {
|
|
|
|
while (aContainer.children.firstChild) {
|
|
|
|
aContainer.children.removeChild(aContainer.children.firstChild);
|
|
|
|
}
|
|
|
|
aContainer.childrenDirty = false;
|
|
|
|
return promise.resolve(aContainer);
|
|
|
|
}
|
2012-12-20 00:16:45 +00:00
|
|
|
|
2013-06-11 04:18:46 +00:00
|
|
|
// If we're not expanded (or asked to update anyway), we're done for
|
|
|
|
// now. Note that this will leave the childrenDirty flag set, so when
|
|
|
|
// expanded we'll refresh the child list.
|
2013-09-23 06:46:12 +00:00
|
|
|
if (!(aContainer.expanded || expand)) {
|
2013-06-11 04:18:46 +00:00
|
|
|
return promise.resolve(aContainer);
|
2012-12-20 00:16:45 +00:00
|
|
|
}
|
|
|
|
|
2013-06-11 04:18:46 +00:00
|
|
|
// We're going to issue a children request, make sure it includes the
|
|
|
|
// centered node.
|
|
|
|
let centered = this._checkSelectionVisible(aContainer);
|
|
|
|
|
|
|
|
// Children aren't updated yet, but clear the childrenDirty flag anyway.
|
|
|
|
// If the dirty flag is re-set while we're fetching we'll need to fetch
|
|
|
|
// again.
|
2012-12-20 00:16:45 +00:00
|
|
|
aContainer.childrenDirty = false;
|
2013-06-11 04:18:46 +00:00
|
|
|
let updatePromise = this._getVisibleChildren(aContainer, centered).then(children => {
|
2013-06-17 13:52:55 +00:00
|
|
|
if (!this._containers) {
|
|
|
|
return promise.reject("markup view destroyed");
|
|
|
|
}
|
2013-06-11 04:18:46 +00:00
|
|
|
this._queuedChildUpdates.delete(aContainer);
|
2012-12-20 00:16:45 +00:00
|
|
|
|
2013-06-11 04:18:46 +00:00
|
|
|
// If children are dirty, we got a change notification for this node
|
|
|
|
// while the request was in progress, we need to do it again.
|
|
|
|
if (aContainer.childrenDirty) {
|
2013-09-23 06:46:12 +00:00
|
|
|
return this._updateChildren(aContainer, {expand: centered});
|
2013-06-11 04:18:46 +00:00
|
|
|
}
|
2012-12-20 00:16:45 +00:00
|
|
|
|
2013-06-11 04:18:46 +00:00
|
|
|
let fragment = this.doc.createDocumentFragment();
|
2012-12-20 00:16:45 +00:00
|
|
|
|
2013-06-11 04:18:46 +00:00
|
|
|
for (let child of children.nodes) {
|
2013-09-23 06:46:12 +00:00
|
|
|
let container = this.importNode(child, flash);
|
2013-06-11 04:18:46 +00:00
|
|
|
fragment.appendChild(container.elt);
|
|
|
|
}
|
2012-12-20 00:16:45 +00:00
|
|
|
|
2013-06-11 04:18:46 +00:00
|
|
|
while (aContainer.children.firstChild) {
|
|
|
|
aContainer.children.removeChild(aContainer.children.firstChild);
|
2012-12-20 08:37:22 +00:00
|
|
|
}
|
2013-06-11 04:18:46 +00:00
|
|
|
|
|
|
|
if (!(children.hasFirst && children.hasLast)) {
|
|
|
|
let data = {
|
|
|
|
showing: this.strings.GetStringFromName("markupView.more.showing"),
|
|
|
|
showAll: this.strings.formatStringFromName(
|
|
|
|
"markupView.more.showAll",
|
|
|
|
[aContainer.node.numChildren.toString()], 1),
|
|
|
|
allButtonClick: () => {
|
|
|
|
aContainer.maxChildren = -1;
|
|
|
|
aContainer.childrenDirty = true;
|
|
|
|
this._updateChildren(aContainer);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!children.hasFirst) {
|
|
|
|
let span = this.template("more-nodes", data);
|
|
|
|
fragment.insertBefore(span, fragment.firstChild);
|
|
|
|
}
|
|
|
|
if (!children.hasLast) {
|
|
|
|
let span = this.template("more-nodes", data);
|
|
|
|
fragment.appendChild(span);
|
|
|
|
}
|
2012-12-20 00:16:45 +00:00
|
|
|
}
|
|
|
|
|
2013-06-11 04:18:46 +00:00
|
|
|
aContainer.children.appendChild(fragment);
|
|
|
|
return aContainer;
|
|
|
|
}).then(null, console.error);
|
|
|
|
this._queuedChildUpdates.set(aContainer, updatePromise);
|
|
|
|
return updatePromise;
|
|
|
|
},
|
2012-12-20 00:16:45 +00:00
|
|
|
|
2013-06-11 04:18:46 +00:00
|
|
|
_waitForChildren: function() {
|
|
|
|
if (!this._queuedChildUpdates) {
|
|
|
|
return promise.resolve(undefined);
|
|
|
|
}
|
|
|
|
return promise.all([updatePromise for (updatePromise of this._queuedChildUpdates.values())]);
|
2012-12-20 00:16:45 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a list of the children to display for this container.
|
|
|
|
*/
|
2013-10-02 00:14:00 +00:00
|
|
|
_getVisibleChildren: function(aContainer, aCentered) {
|
2012-12-20 00:16:45 +00:00
|
|
|
let maxChildren = aContainer.maxChildren || this.maxChildren;
|
|
|
|
if (maxChildren == -1) {
|
2013-06-11 04:18:46 +00:00
|
|
|
maxChildren = undefined;
|
2012-12-20 00:16:45 +00:00
|
|
|
}
|
2012-12-20 00:16:45 +00:00
|
|
|
|
2013-06-11 04:18:46 +00:00
|
|
|
return this.walker.children(aContainer.node, {
|
|
|
|
maxNodes: maxChildren,
|
|
|
|
center: aCentered
|
|
|
|
});
|
2012-08-23 18:00:43 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tear down the markup panel.
|
|
|
|
*/
|
2013-10-02 00:14:00 +00:00
|
|
|
destroy: function() {
|
2014-03-17 18:11:00 +00:00
|
|
|
if (this._destroyer) {
|
|
|
|
return this._destroyer;
|
|
|
|
}
|
|
|
|
|
2014-01-09 11:36:01 +00:00
|
|
|
// Note that if the toolbox is closed, this will work fine, but will fail
|
|
|
|
// in case the browser is closed and will trigger a noSuchActor message.
|
2014-03-17 18:11:00 +00:00
|
|
|
this._destroyer = this._hideBoxModel();
|
2014-01-09 11:36:01 +00:00
|
|
|
|
2014-07-20 11:03:59 +00:00
|
|
|
this._elt.removeEventListener("click", this._onMouseClick, false);
|
|
|
|
|
2014-01-09 11:36:01 +00:00
|
|
|
this._hoveredNode = null;
|
|
|
|
this._inspector.toolbox.off("picker-node-hovered", this._onToolboxPickerHover);
|
|
|
|
|
2013-10-24 13:41:03 +00:00
|
|
|
this.htmlEditor.destroy();
|
2014-01-09 11:36:01 +00:00
|
|
|
this.htmlEditor = null;
|
2013-10-24 13:41:03 +00:00
|
|
|
|
2012-08-23 18:00:43 +00:00
|
|
|
this.undo.destroy();
|
2014-01-09 11:36:01 +00:00
|
|
|
this.undo = null;
|
2012-08-23 18:00:43 +00:00
|
|
|
|
2013-08-02 10:35:50 +00:00
|
|
|
this.popup.destroy();
|
2014-01-09 11:36:01 +00:00
|
|
|
this.popup = null;
|
2013-08-02 10:35:50 +00:00
|
|
|
|
2012-09-25 16:33:46 +00:00
|
|
|
this._frame.removeEventListener("focus", this._boundFocus, false);
|
2014-01-09 11:36:01 +00:00
|
|
|
this._boundFocus = null;
|
2012-08-23 18:00:43 +00:00
|
|
|
|
2013-06-11 04:18:46 +00:00
|
|
|
if (this._boundUpdatePreview) {
|
2013-10-02 00:14:00 +00:00
|
|
|
this._frame.contentWindow.removeEventListener("scroll",
|
|
|
|
this._boundUpdatePreview, true);
|
2014-01-09 11:36:01 +00:00
|
|
|
this._boundUpdatePreview = null;
|
2013-06-11 04:18:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (this._boundResizePreview) {
|
2013-10-02 00:14:00 +00:00
|
|
|
this._frame.contentWindow.removeEventListener("resize",
|
|
|
|
this._boundResizePreview, true);
|
|
|
|
this._frame.contentWindow.removeEventListener("overflow",
|
|
|
|
this._boundResizePreview, true);
|
|
|
|
this._frame.contentWindow.removeEventListener("underflow",
|
|
|
|
this._boundResizePreview, true);
|
2014-01-09 11:36:01 +00:00
|
|
|
this._boundResizePreview = null;
|
2013-06-11 04:18:46 +00:00
|
|
|
}
|
2012-09-25 16:33:46 +00:00
|
|
|
|
2013-10-02 00:14:00 +00:00
|
|
|
this._frame.contentWindow.removeEventListener("keydown",
|
|
|
|
this._boundKeyDown, false);
|
2014-01-09 11:36:01 +00:00
|
|
|
this._boundKeyDown = null;
|
2012-08-23 18:00:43 +00:00
|
|
|
|
2013-06-11 04:18:46 +00:00
|
|
|
this._inspector.selection.off("new-node-front", this._boundOnNewSelection);
|
2014-01-09 11:36:01 +00:00
|
|
|
this._boundOnNewSelection = null;
|
2012-08-23 18:00:43 +00:00
|
|
|
|
2014-11-22 07:48:00 +00:00
|
|
|
this.walker.off("mutations", this._boundMutationObserver);
|
2014-01-09 11:36:01 +00:00
|
|
|
this._boundMutationObserver = null;
|
2013-06-11 04:18:46 +00:00
|
|
|
|
2014-06-05 12:50:03 +00:00
|
|
|
this.walker.off("display-change", this._boundOnDisplayChange);
|
|
|
|
this._boundOnDisplayChange = null;
|
|
|
|
|
2014-01-09 11:36:01 +00:00
|
|
|
this._elt.removeEventListener("mousemove", this._onMouseMove, false);
|
|
|
|
this._elt.removeEventListener("mouseleave", this._onMouseLeave, false);
|
|
|
|
this._elt = null;
|
2012-08-23 18:00:43 +00:00
|
|
|
|
2013-11-02 00:55:13 +00:00
|
|
|
for (let [key, container] of this._containers) {
|
2013-10-11 15:50:33 +00:00
|
|
|
container.destroy();
|
|
|
|
}
|
2014-01-09 11:36:01 +00:00
|
|
|
this._containers = null;
|
2013-11-05 16:19:29 +00:00
|
|
|
|
|
|
|
this.tooltip.destroy();
|
2014-01-09 11:36:01 +00:00
|
|
|
this.tooltip = null;
|
2014-03-17 18:11:00 +00:00
|
|
|
|
|
|
|
return this._destroyer;
|
2012-09-25 16:33:46 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize the preview panel.
|
|
|
|
*/
|
2013-10-02 00:14:00 +00:00
|
|
|
_initPreview: function() {
|
2013-11-07 19:03:40 +00:00
|
|
|
this._previewEnabled = Services.prefs.getBoolPref("devtools.inspector.markupPreview");
|
|
|
|
if (!this._previewEnabled) {
|
2012-09-25 16:33:46 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._previewBar = this.doc.querySelector("#previewbar");
|
|
|
|
this._preview = this.doc.querySelector("#preview");
|
|
|
|
this._viewbox = this.doc.querySelector("#viewbox");
|
|
|
|
|
|
|
|
this._previewBar.classList.remove("disabled");
|
|
|
|
|
|
|
|
this._previewWidth = this._preview.getBoundingClientRect().width;
|
|
|
|
|
|
|
|
this._boundResizePreview = this._resizePreview.bind(this);
|
2013-10-02 00:14:00 +00:00
|
|
|
this._frame.contentWindow.addEventListener("resize",
|
|
|
|
this._boundResizePreview, true);
|
|
|
|
this._frame.contentWindow.addEventListener("overflow",
|
|
|
|
this._boundResizePreview, true);
|
|
|
|
this._frame.contentWindow.addEventListener("underflow",
|
|
|
|
this._boundResizePreview, true);
|
2012-09-25 16:33:46 +00:00
|
|
|
|
|
|
|
this._boundUpdatePreview = this._updatePreview.bind(this);
|
2013-10-02 00:14:00 +00:00
|
|
|
this._frame.contentWindow.addEventListener("scroll",
|
|
|
|
this._boundUpdatePreview, true);
|
2012-09-25 16:33:46 +00:00
|
|
|
this._updatePreview();
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Move the preview viewbox.
|
|
|
|
*/
|
2013-10-02 00:14:00 +00:00
|
|
|
_updatePreview: function() {
|
2013-11-07 19:03:40 +00:00
|
|
|
if (!this._previewEnabled) {
|
|
|
|
return;
|
|
|
|
}
|
2012-09-25 16:33:46 +00:00
|
|
|
let win = this._frame.contentWindow;
|
|
|
|
|
|
|
|
if (win.scrollMaxY == 0) {
|
|
|
|
this._previewBar.classList.add("disabled");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._previewBar.classList.remove("disabled");
|
|
|
|
|
|
|
|
let ratio = this._previewWidth / PREVIEW_AREA;
|
|
|
|
let width = ratio * win.innerWidth;
|
|
|
|
|
|
|
|
let height = ratio * (win.scrollMaxY + win.innerHeight);
|
|
|
|
let scrollTo
|
|
|
|
if (height >= win.innerHeight) {
|
|
|
|
scrollTo = -(height - win.innerHeight) * (win.scrollY / win.scrollMaxY);
|
2013-10-02 00:14:00 +00:00
|
|
|
this._previewBar.setAttribute("style", "height:" + height +
|
|
|
|
"px;transform:translateY(" + scrollTo + "px)");
|
2012-09-25 16:33:46 +00:00
|
|
|
} else {
|
|
|
|
this._previewBar.setAttribute("style", "height:100%");
|
|
|
|
}
|
|
|
|
|
|
|
|
let bgSize = ~~width + "px " + ~~height + "px";
|
|
|
|
this._preview.setAttribute("style", "background-size:" + bgSize);
|
|
|
|
|
Bug 1001090 - Part 4: Fix errors in chrome code. (r=zombie,gavin,fitzgen,dcamp,bgrins,fabrice,gwagner,margaret,mrbkap,mak,njn,vicamo)
2014-09-15 23:30:46 +00:00
|
|
|
height = ~~(win.innerHeight * ratio) + "px";
|
2012-09-25 16:33:46 +00:00
|
|
|
let top = ~~(win.scrollY * ratio) + "px";
|
2013-10-02 00:14:00 +00:00
|
|
|
this._viewbox.setAttribute("style", "height:" + height +
|
|
|
|
";transform: translateY(" + top + ")");
|
2012-09-25 16:33:46 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hide the preview while resizing, to avoid slowness.
|
|
|
|
*/
|
2013-10-02 00:14:00 +00:00
|
|
|
_resizePreview: function() {
|
2013-11-07 19:03:40 +00:00
|
|
|
if (!this._previewEnabled) {
|
|
|
|
return;
|
|
|
|
}
|
2012-09-25 16:33:46 +00:00
|
|
|
let win = this._frame.contentWindow;
|
|
|
|
this._previewBar.classList.add("hide");
|
|
|
|
win.clearTimeout(this._resizePreviewTimeout);
|
|
|
|
|
2014-06-14 10:49:00 +00:00
|
|
|
win.setTimeout(() => {
|
2012-09-25 16:33:46 +00:00
|
|
|
this._updatePreview();
|
|
|
|
this._previewBar.classList.remove("hide");
|
2014-06-14 10:49:00 +00:00
|
|
|
}, 1000);
|
2013-09-12 14:48:13 +00:00
|
|
|
}
|
2012-08-23 18:00:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The main structure for storing a document node in the markup
|
|
|
|
* tree. Manages creation of the editor for the node and
|
|
|
|
* a <ul> for placing child elements, and expansion/collapsing
|
|
|
|
* of the element.
|
2013-09-23 06:46:12 +00:00
|
|
|
*
|
2014-09-29 07:29:00 +00:00
|
|
|
* This should not be instantiated directly, instead use one of:
|
|
|
|
* MarkupReadOnlyContainer
|
|
|
|
* MarkupTextContainer
|
|
|
|
* MarkupElementContainer
|
2012-08-23 18:00:43 +00:00
|
|
|
*/
|
2014-09-29 07:29:00 +00:00
|
|
|
function MarkupContainer() { }
|
2012-08-23 18:00:43 +00:00
|
|
|
|
2014-09-29 07:29:00 +00:00
|
|
|
MarkupContainer.prototype = {
|
2012-08-23 18:00:43 +00:00
|
|
|
|
2014-09-29 07:29:00 +00:00
|
|
|
/*
|
|
|
|
* Initialize the MarkupContainer. Should be called while one
|
|
|
|
* of the other contain classes is instantiated.
|
|
|
|
*
|
|
|
|
* @param MarkupView markupView
|
|
|
|
* The markup view that owns this container.
|
|
|
|
* @param NodeFront node
|
|
|
|
* The node to display.
|
|
|
|
* @param string templateID
|
|
|
|
* Which template to render for this container
|
|
|
|
*/
|
|
|
|
initialize: function(markupView, node, templateID) {
|
|
|
|
this.markup = markupView;
|
|
|
|
this.node = node;
|
|
|
|
this.undo = this.markup.undo;
|
2012-08-23 18:00:43 +00:00
|
|
|
|
2014-09-29 07:29:00 +00:00
|
|
|
// The template will fill the following properties
|
|
|
|
this.elt = null;
|
|
|
|
this.expander = null;
|
|
|
|
this.tagState = null;
|
|
|
|
this.tagLine = null;
|
|
|
|
this.children = null;
|
|
|
|
this.markup.template(templateID, this);
|
|
|
|
this.elt.container = this;
|
2012-08-23 18:00:43 +00:00
|
|
|
|
2014-09-29 07:29:00 +00:00
|
|
|
// Binding event listeners
|
|
|
|
this._onMouseDown = this._onMouseDown.bind(this);
|
|
|
|
this.elt.addEventListener("mousedown", this._onMouseDown, false);
|
2013-01-23 17:01:55 +00:00
|
|
|
|
2014-09-29 07:29:00 +00:00
|
|
|
this._onToggle = this._onToggle.bind(this);
|
2013-10-25 19:21:01 +00:00
|
|
|
|
2014-09-29 07:29:00 +00:00
|
|
|
// Expanding/collapsing the node on dblclick of the whole tag-line element
|
|
|
|
this.elt.addEventListener("dblclick", this._onToggle, false);
|
2014-06-05 12:50:03 +00:00
|
|
|
|
2014-09-29 07:29:00 +00:00
|
|
|
if (this.expander) {
|
|
|
|
this.expander.addEventListener("click", this._onToggle, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Marking the node as shown or hidden
|
|
|
|
this.isDisplayed = this.node.isDisplayed;
|
|
|
|
},
|
2012-08-23 18:00:43 +00:00
|
|
|
|
2013-09-12 14:48:13 +00:00
|
|
|
toString: function() {
|
|
|
|
return "[MarkupContainer for " + this.node + "]";
|
|
|
|
},
|
2013-06-11 04:18:46 +00:00
|
|
|
|
2014-01-30 16:33:53 +00:00
|
|
|
isPreviewable: function() {
|
2014-09-29 07:29:00 +00:00
|
|
|
if (this.node.tagName && !this.node.isPseudoElement) {
|
2013-10-25 19:21:01 +00:00
|
|
|
let tagName = this.node.tagName.toLowerCase();
|
2013-11-05 16:19:29 +00:00
|
|
|
let srcAttr = this.editor.getAttributeElement("src");
|
|
|
|
let isImage = tagName === "img" && srcAttr;
|
|
|
|
let isCanvas = tagName === "canvas";
|
2013-10-25 19:21:01 +00:00
|
|
|
|
2014-01-30 16:33:53 +00:00
|
|
|
return isImage || isCanvas;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-06-05 12:50:03 +00:00
|
|
|
/**
|
|
|
|
* Show the element has displayed or not
|
|
|
|
*/
|
|
|
|
set isDisplayed(isDisplayed) {
|
|
|
|
this.elt.classList.remove("not-displayed");
|
|
|
|
if (!isDisplayed) {
|
|
|
|
this.elt.classList.add("not-displayed");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-08-23 18:00:43 +00:00
|
|
|
/**
|
|
|
|
* True if the current node has children. The MarkupView
|
|
|
|
* will set this attribute for the MarkupContainer.
|
|
|
|
*/
|
|
|
|
_hasChildren: false,
|
|
|
|
|
|
|
|
get hasChildren() {
|
|
|
|
return this._hasChildren;
|
|
|
|
},
|
|
|
|
|
|
|
|
set hasChildren(aValue) {
|
|
|
|
this._hasChildren = aValue;
|
2014-09-29 07:29:00 +00:00
|
|
|
if (!this.expander) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-08-23 18:00:43 +00:00
|
|
|
if (aValue) {
|
|
|
|
this.expander.style.visibility = "visible";
|
|
|
|
} else {
|
|
|
|
this.expander.style.visibility = "hidden";
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-10-08 22:46:16 +00:00
|
|
|
/**
|
|
|
|
* If the node has children, return the list of containers for all these
|
|
|
|
* children.
|
|
|
|
*/
|
|
|
|
getChildContainers: function() {
|
|
|
|
if (!this.hasChildren) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return [...this.children.children].map(node => node.container);
|
|
|
|
},
|
|
|
|
|
2012-08-23 18:00:43 +00:00
|
|
|
/**
|
|
|
|
* True if the node has been visually expanded in the tree.
|
|
|
|
*/
|
|
|
|
get expanded() {
|
2013-09-12 14:48:13 +00:00
|
|
|
return !this.elt.classList.contains("collapsed");
|
2012-08-23 18:00:43 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
set expanded(aValue) {
|
2014-09-29 07:29:00 +00:00
|
|
|
if (!this.expander) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-09-12 14:48:13 +00:00
|
|
|
if (aValue && this.elt.classList.contains("collapsed")) {
|
|
|
|
// Expanding a node means cloning its "inline" closing tag into a new
|
|
|
|
// tag-line that the user can interact with and showing the children.
|
2014-09-29 07:29:00 +00:00
|
|
|
let closingTag = this.elt.querySelector(".close");
|
|
|
|
if (closingTag) {
|
|
|
|
if (!this.closeTagLine) {
|
|
|
|
let line = this.markup.doc.createElement("div");
|
|
|
|
line.classList.add("tag-line");
|
2013-09-12 14:48:13 +00:00
|
|
|
|
2014-09-29 07:29:00 +00:00
|
|
|
let tagState = this.markup.doc.createElement("div");
|
|
|
|
tagState.classList.add("tag-state");
|
|
|
|
line.appendChild(tagState);
|
2013-09-12 14:48:13 +00:00
|
|
|
|
2014-09-29 07:29:00 +00:00
|
|
|
line.appendChild(closingTag.cloneNode(true));
|
2013-09-12 14:48:13 +00:00
|
|
|
|
2014-09-29 07:29:00 +00:00
|
|
|
this.closeTagLine = line;
|
2013-09-12 14:48:13 +00:00
|
|
|
}
|
2014-09-29 07:29:00 +00:00
|
|
|
this.elt.appendChild(this.closeTagLine);
|
2013-09-12 14:48:13 +00:00
|
|
|
}
|
2014-09-29 07:29:00 +00:00
|
|
|
|
2013-09-12 14:48:13 +00:00
|
|
|
this.elt.classList.remove("collapsed");
|
2013-03-27 22:20:38 +00:00
|
|
|
this.expander.setAttribute("open", "");
|
2014-01-09 11:36:01 +00:00
|
|
|
this.hovered = false;
|
2013-09-12 14:48:13 +00:00
|
|
|
} else if (!aValue) {
|
2014-09-29 07:29:00 +00:00
|
|
|
if (this.closeTagLine) {
|
2013-09-12 14:48:13 +00:00
|
|
|
this.elt.removeChild(this.closeTagLine);
|
2013-01-23 17:01:55 +00:00
|
|
|
}
|
2013-09-12 14:48:13 +00:00
|
|
|
this.elt.classList.add("collapsed");
|
2013-03-27 22:20:38 +00:00
|
|
|
this.expander.removeAttribute("open");
|
2013-09-12 14:48:13 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-09-29 07:29:00 +00:00
|
|
|
parentContainer: function() {
|
|
|
|
return this.elt.parentNode ? this.elt.parentNode.container : null;
|
2013-09-12 14:48:13 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
_onMouseDown: function(event) {
|
2013-11-11 15:17:41 +00:00
|
|
|
let target = event.target;
|
|
|
|
|
2014-01-13 15:32:19 +00:00
|
|
|
// Target may be a resource link (generated by the output-parser)
|
2013-11-11 15:17:41 +00:00
|
|
|
if (target.nodeName === "a") {
|
|
|
|
event.stopPropagation();
|
|
|
|
event.preventDefault();
|
|
|
|
let browserWin = this.markup._inspector.target
|
|
|
|
.tab.ownerDocument.defaultView;
|
|
|
|
browserWin.openUILinkIn(target.href, "tab");
|
|
|
|
}
|
2014-01-13 15:32:19 +00:00
|
|
|
// Or it may be the "show more nodes" button (which already has its onclick)
|
|
|
|
// Else, it's the container itself
|
|
|
|
else if (target.nodeName !== "button") {
|
|
|
|
this.hovered = false;
|
|
|
|
this.markup.navigate(this);
|
|
|
|
event.stopPropagation();
|
|
|
|
}
|
2013-09-12 14:48:13 +00:00
|
|
|
},
|
|
|
|
|
2013-09-23 06:46:12 +00:00
|
|
|
/**
|
|
|
|
* Temporarily flash the container to attract attention.
|
|
|
|
* Used for markup mutations.
|
|
|
|
*/
|
|
|
|
flashMutation: function() {
|
|
|
|
if (!this.selected) {
|
|
|
|
let contentWin = this.markup._frame.contentWindow;
|
|
|
|
this.flashed = true;
|
|
|
|
if (this._flashMutationTimer) {
|
|
|
|
contentWin.clearTimeout(this._flashMutationTimer);
|
|
|
|
this._flashMutationTimer = null;
|
|
|
|
}
|
|
|
|
this._flashMutationTimer = contentWin.setTimeout(() => {
|
|
|
|
this.flashed = false;
|
2014-08-11 08:43:00 +00:00
|
|
|
}, this.markup.CONTAINER_FLASHING_DURATION);
|
2013-09-23 06:46:12 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
set flashed(aValue) {
|
|
|
|
if (aValue) {
|
|
|
|
// Make sure the animation class is not here
|
2014-01-09 11:36:01 +00:00
|
|
|
this.tagState.classList.remove("flash-out");
|
2013-09-23 06:46:12 +00:00
|
|
|
|
|
|
|
// Change the background
|
2014-01-09 11:36:01 +00:00
|
|
|
this.tagState.classList.add("theme-bg-contrast");
|
2013-09-23 06:46:12 +00:00
|
|
|
|
|
|
|
// Change the text color
|
|
|
|
this.editor.elt.classList.add("theme-fg-contrast");
|
|
|
|
[].forEach.call(
|
|
|
|
this.editor.elt.querySelectorAll("[class*=theme-fg-color]"),
|
|
|
|
span => span.classList.add("theme-fg-contrast")
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
// Add the animation class to smoothly remove the background
|
2014-01-09 11:36:01 +00:00
|
|
|
this.tagState.classList.add("flash-out");
|
2013-09-23 06:46:12 +00:00
|
|
|
|
|
|
|
// Remove the background
|
2014-01-09 11:36:01 +00:00
|
|
|
this.tagState.classList.remove("theme-bg-contrast");
|
2013-09-23 06:46:12 +00:00
|
|
|
|
|
|
|
// Remove the text color
|
|
|
|
this.editor.elt.classList.remove("theme-fg-contrast");
|
|
|
|
[].forEach.call(
|
|
|
|
this.editor.elt.querySelectorAll("[class*=theme-fg-color]"),
|
|
|
|
span => span.classList.remove("theme-fg-contrast")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-01-09 11:36:01 +00:00
|
|
|
_hovered: false,
|
2013-09-12 14:48:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Highlight the currently hovered tag + its closing tag if necessary
|
|
|
|
* (that is if the tag is expanded)
|
|
|
|
*/
|
2014-01-09 11:36:01 +00:00
|
|
|
set hovered(aValue) {
|
|
|
|
this.tagState.classList.remove("flash-out");
|
|
|
|
this._hovered = aValue;
|
2013-09-12 14:48:13 +00:00
|
|
|
if (aValue) {
|
|
|
|
if (!this.selected) {
|
2014-01-09 11:36:01 +00:00
|
|
|
this.tagState.classList.add("theme-bg-darker");
|
2013-09-12 14:48:13 +00:00
|
|
|
}
|
|
|
|
if (this.closeTagLine) {
|
2014-01-09 11:36:01 +00:00
|
|
|
this.closeTagLine.querySelector(".tag-state").classList.add(
|
2013-10-02 00:14:00 +00:00
|
|
|
"theme-bg-darker");
|
2013-09-12 14:48:13 +00:00
|
|
|
}
|
|
|
|
} else {
|
2014-01-09 11:36:01 +00:00
|
|
|
this.tagState.classList.remove("theme-bg-darker");
|
2013-09-12 14:48:13 +00:00
|
|
|
if (this.closeTagLine) {
|
2014-01-09 11:36:01 +00:00
|
|
|
this.closeTagLine.querySelector(".tag-state").classList.remove(
|
2013-10-02 00:14:00 +00:00
|
|
|
"theme-bg-darker");
|
2013-01-23 17:01:55 +00:00
|
|
|
}
|
2012-08-23 18:00:43 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* True if the container is visible in the markup tree.
|
|
|
|
*/
|
2013-09-12 14:48:13 +00:00
|
|
|
get visible() {
|
2012-08-23 18:00:43 +00:00
|
|
|
return this.elt.getBoundingClientRect().height > 0;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* True if the container is currently selected.
|
|
|
|
*/
|
|
|
|
_selected: false,
|
|
|
|
|
|
|
|
get selected() {
|
|
|
|
return this._selected;
|
|
|
|
},
|
|
|
|
|
|
|
|
set selected(aValue) {
|
2014-01-09 11:36:01 +00:00
|
|
|
this.tagState.classList.remove("flash-out");
|
2012-08-23 18:00:43 +00:00
|
|
|
this._selected = aValue;
|
2013-06-11 04:18:46 +00:00
|
|
|
this.editor.selected = aValue;
|
2012-08-23 18:00:43 +00:00
|
|
|
if (this._selected) {
|
2013-09-12 14:48:13 +00:00
|
|
|
this.tagLine.setAttribute("selected", "");
|
2014-01-09 11:36:01 +00:00
|
|
|
this.tagState.classList.add("theme-selected");
|
2012-08-23 18:00:43 +00:00
|
|
|
} else {
|
2013-09-12 14:48:13 +00:00
|
|
|
this.tagLine.removeAttribute("selected");
|
2014-01-09 11:36:01 +00:00
|
|
|
this.tagState.classList.remove("theme-selected");
|
2012-08-23 18:00:43 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the container's editor to the current state of the
|
|
|
|
* viewed node.
|
|
|
|
*/
|
2013-10-08 12:53:19 +00:00
|
|
|
update: function() {
|
2012-08-23 18:00:43 +00:00
|
|
|
if (this.editor.update) {
|
2013-10-08 12:53:19 +00:00
|
|
|
this.editor.update();
|
2012-08-23 18:00:43 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Try to put keyboard focus on the current editor.
|
|
|
|
*/
|
2013-09-12 14:48:13 +00:00
|
|
|
focus: function() {
|
2012-08-23 18:00:43 +00:00
|
|
|
let focusable = this.editor.elt.querySelector("[tabindex]");
|
|
|
|
if (focusable) {
|
|
|
|
focusable.focus();
|
|
|
|
}
|
2013-10-11 15:50:33 +00:00
|
|
|
},
|
|
|
|
|
2014-09-29 07:29:00 +00:00
|
|
|
_onToggle: function(event) {
|
|
|
|
this.markup.navigate(this);
|
|
|
|
if (this.hasChildren) {
|
|
|
|
this.markup.setNodeExpanded(this.node, !this.expanded, event.altKey);
|
|
|
|
}
|
|
|
|
event.stopPropagation();
|
|
|
|
},
|
|
|
|
|
2013-10-11 15:50:33 +00:00
|
|
|
/**
|
|
|
|
* Get rid of event listeners and references, when the container is no longer
|
|
|
|
* needed
|
|
|
|
*/
|
|
|
|
destroy: function() {
|
2014-09-29 07:29:00 +00:00
|
|
|
// Remove event listeners
|
|
|
|
this.elt.removeEventListener("mousedown", this._onMouseDown, false);
|
|
|
|
this.elt.removeEventListener("dblclick", this._onToggle, false);
|
|
|
|
|
|
|
|
if (this.expander) {
|
|
|
|
this.expander.removeEventListener("click", this._onToggle, false);
|
|
|
|
}
|
|
|
|
|
2013-10-11 15:50:33 +00:00
|
|
|
// Recursively destroy children containers
|
|
|
|
let firstChild;
|
|
|
|
while (firstChild = this.children.firstChild) {
|
2014-01-13 15:32:19 +00:00
|
|
|
// Not all children of a container are containers themselves
|
|
|
|
// ("show more nodes" button is one example)
|
|
|
|
if (firstChild.container) {
|
|
|
|
firstChild.container.destroy();
|
|
|
|
}
|
2013-10-11 15:50:33 +00:00
|
|
|
this.children.removeChild(firstChild);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.editor.destroy();
|
2013-09-12 14:48:13 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-09-29 07:29:00 +00:00
|
|
|
/**
|
|
|
|
* An implementation of MarkupContainer for Pseudo Elements,
|
|
|
|
* Doctype nodes, or any other type generic node that doesn't
|
|
|
|
* fit for other editors.
|
|
|
|
* Does not allow any editing, just viewing / selecting.
|
|
|
|
*
|
|
|
|
* @param MarkupView markupView
|
|
|
|
* The markup view that owns this container.
|
|
|
|
* @param NodeFront node
|
|
|
|
* The node to display.
|
|
|
|
*/
|
|
|
|
function MarkupReadOnlyContainer(markupView, node) {
|
|
|
|
MarkupContainer.prototype.initialize.call(this, markupView, node, "readonlycontainer");
|
|
|
|
|
|
|
|
this.editor = new GenericEditor(this, node);
|
|
|
|
this.tagLine.appendChild(this.editor.elt);
|
|
|
|
}
|
|
|
|
|
|
|
|
MarkupReadOnlyContainer.prototype = Heritage.extend(MarkupContainer.prototype, {});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An implementation of MarkupContainer for text node and comment nodes.
|
|
|
|
* Allows basic text editing in a textarea.
|
|
|
|
*
|
|
|
|
* @param MarkupView aMarkupView
|
|
|
|
* The markup view that owns this container.
|
|
|
|
* @param NodeFront aNode
|
|
|
|
* The node to display.
|
|
|
|
* @param Inspector aInspector
|
|
|
|
* The inspector tool container the markup-view
|
|
|
|
*/
|
|
|
|
function MarkupTextContainer(markupView, node) {
|
|
|
|
MarkupContainer.prototype.initialize.call(this, markupView, node, "textcontainer");
|
|
|
|
|
|
|
|
if (node.nodeType == Ci.nsIDOMNode.TEXT_NODE) {
|
|
|
|
this.editor = new TextEditor(this, node, "text");
|
|
|
|
} else if (node.nodeType == Ci.nsIDOMNode.COMMENT_NODE) {
|
|
|
|
this.editor = new TextEditor(this, node, "comment");
|
|
|
|
} else {
|
|
|
|
throw "Invalid node for MarkupTextContainer";
|
|
|
|
}
|
|
|
|
|
|
|
|
this.tagLine.appendChild(this.editor.elt);
|
|
|
|
}
|
|
|
|
|
|
|
|
MarkupTextContainer.prototype = Heritage.extend(MarkupContainer.prototype, {});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An implementation of MarkupContainer for Elements that can contain
|
|
|
|
* child nodes.
|
|
|
|
* Allows editing of tag name, attributes, expanding / collapsing.
|
|
|
|
*
|
|
|
|
* @param MarkupView markupView
|
|
|
|
* The markup view that owns this container.
|
|
|
|
* @param NodeFront node
|
|
|
|
* The node to display.
|
|
|
|
*/
|
|
|
|
function MarkupElementContainer(markupView, node) {
|
|
|
|
MarkupContainer.prototype.initialize.call(this, markupView, node, "elementcontainer");
|
|
|
|
|
|
|
|
if (node.nodeType === Ci.nsIDOMNode.ELEMENT_NODE) {
|
|
|
|
this.editor = new ElementEditor(this, node);
|
|
|
|
} else {
|
|
|
|
throw "Invalid node for MarkupElementContainer";
|
|
|
|
}
|
|
|
|
|
|
|
|
this.tagLine.appendChild(this.editor.elt);
|
|
|
|
|
|
|
|
// Prepare the image preview tooltip data if any
|
|
|
|
this._prepareImagePreview();
|
|
|
|
}
|
|
|
|
|
|
|
|
MarkupElementContainer.prototype = Heritage.extend(MarkupContainer.prototype, {
|
|
|
|
|
|
|
|
_buildEventTooltipContent: function(target, tooltip) {
|
|
|
|
if (target.hasAttribute("data-event")) {
|
|
|
|
tooltip.hide(target);
|
|
|
|
|
|
|
|
this.node.getEventListenerInfo().then(listenerInfo => {
|
|
|
|
tooltip.setEventContent({
|
|
|
|
eventListenerInfos: listenerInfo,
|
|
|
|
toolbox: this.markup._inspector.toolbox
|
|
|
|
});
|
|
|
|
|
|
|
|
this.markup._makeTooltipPersistent(true);
|
|
|
|
tooltip.once("hidden", () => {
|
|
|
|
this.markup._makeTooltipPersistent(false);
|
|
|
|
});
|
|
|
|
tooltip.show(target);
|
|
|
|
});
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If the node is an image or canvas (@see isPreviewable), then get the
|
|
|
|
* image data uri from the server so that it can then later be previewed in
|
|
|
|
* a tooltip if needed.
|
|
|
|
* Stores a promise in this.tooltipData.data that resolves when the data has
|
|
|
|
* been retrieved
|
|
|
|
*/
|
|
|
|
_prepareImagePreview: function() {
|
|
|
|
if (this.isPreviewable()) {
|
|
|
|
// Get the image data for later so that when the user actually hovers over
|
|
|
|
// the element, the tooltip does contain the image
|
|
|
|
let def = promise.defer();
|
|
|
|
|
|
|
|
this.tooltipData = {
|
|
|
|
target: this.editor.getAttributeElement("src") || this.editor.tag,
|
|
|
|
data: def.promise
|
|
|
|
};
|
|
|
|
|
|
|
|
let maxDim = Services.prefs.getIntPref("devtools.inspector.imagePreviewTooltipSize");
|
|
|
|
this.node.getImageData(maxDim).then(data => {
|
|
|
|
data.data.string().then(str => {
|
|
|
|
let res = {data: str, size: data.size};
|
|
|
|
// Resolving the data promise and, to always keep tooltipData.data
|
|
|
|
// as a promise, create a new one that resolves immediately
|
|
|
|
def.resolve(res);
|
|
|
|
this.tooltipData.data = promise.resolve(res);
|
|
|
|
});
|
|
|
|
}, () => {
|
|
|
|
this.tooltipData.data = promise.reject();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Executed by MarkupView._isImagePreviewTarget which is itself called when the
|
|
|
|
* mouse hovers over a target in the markup-view.
|
|
|
|
* Checks if the target is indeed something we want to have an image tooltip
|
|
|
|
* preview over and, if so, inserts content into the tooltip.
|
|
|
|
* @return a promise that resolves when the content has been inserted or
|
|
|
|
* rejects if no preview is required. This promise is then used by Tooltip.js
|
|
|
|
* to decide if/when to show the tooltip
|
|
|
|
*/
|
|
|
|
isImagePreviewTarget: function(target, tooltip) {
|
|
|
|
if (!this.tooltipData || this.tooltipData.target !== target) {
|
|
|
|
return promise.reject();
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.tooltipData.data.then(({data, size}) => {
|
|
|
|
tooltip.setImageContent(data, size);
|
|
|
|
}, () => {
|
|
|
|
tooltip.setBrokenImageContent();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
copyImageDataUri: function() {
|
|
|
|
// We need to send again a request to gettooltipData even if one was sent for
|
|
|
|
// the tooltip, because we want the full-size image
|
|
|
|
this.node.getImageData().then(data => {
|
|
|
|
data.data.string().then(str => {
|
|
|
|
clipboardHelper.copyString(str, this.markup.doc);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2012-08-23 18:00:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Dummy container node used for the root document element.
|
|
|
|
*/
|
2013-09-12 14:48:13 +00:00
|
|
|
function RootContainer(aMarkupView, aNode) {
|
2012-08-23 18:00:43 +00:00
|
|
|
this.doc = aMarkupView.doc;
|
|
|
|
this.elt = this.doc.createElement("ul");
|
2013-06-11 04:18:46 +00:00
|
|
|
this.elt.container = this;
|
2012-08-23 18:00:43 +00:00
|
|
|
this.children = this.elt;
|
|
|
|
this.node = aNode;
|
2013-10-25 19:21:01 +00:00
|
|
|
this.toString = () => "[root container]";
|
2012-08-23 18:00:43 +00:00
|
|
|
}
|
|
|
|
|
2013-09-12 14:48:13 +00:00
|
|
|
RootContainer.prototype = {
|
|
|
|
hasChildren: true,
|
|
|
|
expanded: true,
|
2013-10-11 15:50:33 +00:00
|
|
|
update: function() {},
|
2014-10-08 22:46:16 +00:00
|
|
|
destroy: function() {},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If the node has children, return the list of containers for all these
|
|
|
|
* children.
|
|
|
|
*/
|
|
|
|
getChildContainers: function() {
|
|
|
|
return [...this.children.children].map(node => node.container);
|
|
|
|
}
|
2013-09-12 14:48:13 +00:00
|
|
|
};
|
|
|
|
|
2012-08-23 18:00:43 +00:00
|
|
|
/**
|
2014-09-29 07:29:00 +00:00
|
|
|
* Creates an editor for non-editable nodes.
|
2012-08-23 18:00:43 +00:00
|
|
|
*/
|
2013-10-02 00:14:00 +00:00
|
|
|
function GenericEditor(aContainer, aNode) {
|
2014-09-29 07:29:00 +00:00
|
|
|
this.container = aContainer;
|
|
|
|
this.markup = this.container.markup;
|
|
|
|
this.template = this.markup.template.bind(this.markup);
|
|
|
|
this.elt = null;
|
|
|
|
this.template("generic", this);
|
2013-10-11 15:50:33 +00:00
|
|
|
|
2014-09-29 07:29:00 +00:00
|
|
|
if (aNode.isPseudoElement) {
|
|
|
|
this.tag.classList.add("theme-fg-color5");
|
|
|
|
this.tag.textContent = aNode.isBeforePseudoElement ? "::before" : "::after";
|
|
|
|
} else if (aNode.nodeType == Ci.nsIDOMNode.DOCUMENT_TYPE_NODE) {
|
|
|
|
this.elt.classList.add("comment");
|
|
|
|
this.tag.textContent = '<!DOCTYPE ' + aNode.name +
|
|
|
|
(aNode.publicId ? ' PUBLIC "' + aNode.publicId + '"': '') +
|
|
|
|
(aNode.systemId ? ' "' + aNode.systemId + '"' : '') +
|
|
|
|
'>';
|
|
|
|
} else {
|
|
|
|
this.tag.textContent = aNode.nodeName;
|
|
|
|
}
|
2012-08-23 18:00:43 +00:00
|
|
|
}
|
|
|
|
|
2014-09-29 07:29:00 +00:00
|
|
|
GenericEditor.prototype = {
|
|
|
|
destroy: function() {
|
|
|
|
this.elt.remove();
|
|
|
|
}
|
2013-10-11 15:50:33 +00:00
|
|
|
};
|
|
|
|
|
2012-08-23 18:00:43 +00:00
|
|
|
/**
|
|
|
|
* Creates a simple text editor node, used for TEXT and COMMENT
|
|
|
|
* nodes.
|
|
|
|
*
|
|
|
|
* @param MarkupContainer aContainer The container owning this editor.
|
|
|
|
* @param DOMNode aNode The node being edited.
|
|
|
|
* @param string aTemplate The template id to use to build the editor.
|
|
|
|
*/
|
2013-10-02 00:14:00 +00:00
|
|
|
function TextEditor(aContainer, aNode, aTemplate) {
|
2014-09-29 07:29:00 +00:00
|
|
|
this.container = aContainer;
|
|
|
|
this.markup = this.container.markup;
|
2012-08-23 18:00:43 +00:00
|
|
|
this.node = aNode;
|
2014-09-29 07:29:00 +00:00
|
|
|
this.template = this.markup.template.bind(aTemplate);
|
2013-06-11 04:18:46 +00:00
|
|
|
this._selected = false;
|
2012-08-23 18:00:43 +00:00
|
|
|
|
2014-09-29 07:29:00 +00:00
|
|
|
this.markup.template(aTemplate, this);
|
2012-08-23 18:00:43 +00:00
|
|
|
|
2013-06-17 13:52:55 +00:00
|
|
|
editableField({
|
|
|
|
element: this.value,
|
|
|
|
stopOnReturn: true,
|
|
|
|
trigger: "dblclick",
|
|
|
|
multiline: true,
|
|
|
|
done: (aVal, aCommit) => {
|
|
|
|
if (!aCommit) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.node.getNodeValue().then(longstr => {
|
|
|
|
longstr.string().then(oldValue => {
|
|
|
|
longstr.release().then(null, console.error);
|
|
|
|
|
2014-09-29 07:29:00 +00:00
|
|
|
this.container.undo.do(() => {
|
2013-06-17 13:52:55 +00:00
|
|
|
this.node.setNodeValue(aVal).then(() => {
|
2014-09-29 07:29:00 +00:00
|
|
|
this.markup.nodeChanged(this.node);
|
2013-06-17 13:52:55 +00:00
|
|
|
});
|
|
|
|
}, () => {
|
|
|
|
this.node.setNodeValue(oldValue).then(() => {
|
2014-09-29 07:29:00 +00:00
|
|
|
this.markup.nodeChanged(this.node);
|
2014-11-22 07:48:00 +00:00
|
|
|
});
|
2013-06-17 13:52:55 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2012-08-23 18:00:43 +00:00
|
|
|
|
|
|
|
this.update();
|
|
|
|
}
|
|
|
|
|
|
|
|
TextEditor.prototype = {
|
2013-06-11 04:18:46 +00:00
|
|
|
get selected() this._selected,
|
|
|
|
set selected(aValue) {
|
|
|
|
if (aValue === this._selected) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this._selected = aValue;
|
|
|
|
this.update();
|
|
|
|
},
|
|
|
|
|
2013-10-02 00:14:00 +00:00
|
|
|
update: function() {
|
2013-06-11 04:18:46 +00:00
|
|
|
if (!this.selected || !this.node.incompleteValue) {
|
|
|
|
let text = this.node.shortValue;
|
|
|
|
// XXX: internationalize the elliding
|
|
|
|
if (this.node.incompleteValue) {
|
|
|
|
text += "…";
|
|
|
|
}
|
|
|
|
this.value.textContent = text;
|
|
|
|
} else {
|
|
|
|
let longstr = null;
|
|
|
|
this.node.getNodeValue().then(ret => {
|
|
|
|
longstr = ret;
|
|
|
|
return longstr.string();
|
|
|
|
}).then(str => {
|
|
|
|
longstr.release().then(null, console.error);
|
|
|
|
if (this.selected) {
|
|
|
|
this.value.textContent = str;
|
|
|
|
}
|
|
|
|
}).then(null, console.error);
|
|
|
|
}
|
2013-10-11 15:50:33 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
destroy: function() {}
|
2012-08-23 18:00:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates an editor for an Element node.
|
|
|
|
*
|
|
|
|
* @param MarkupContainer aContainer The container owning this editor.
|
|
|
|
* @param Element aNode The node being edited.
|
|
|
|
*/
|
2013-10-02 00:14:00 +00:00
|
|
|
function ElementEditor(aContainer, aNode) {
|
2012-08-25 18:04:46 +00:00
|
|
|
this.container = aContainer;
|
2012-08-23 18:00:43 +00:00
|
|
|
this.node = aNode;
|
2014-09-29 07:29:00 +00:00
|
|
|
this.markup = this.container.markup;
|
|
|
|
this.template = this.markup.template.bind(this.markup);
|
|
|
|
this.doc = this.markup.doc;
|
2012-08-23 18:00:43 +00:00
|
|
|
|
2013-09-12 14:48:13 +00:00
|
|
|
this.attrs = {};
|
2012-08-23 18:00:43 +00:00
|
|
|
|
|
|
|
// The templates will fill the following properties
|
|
|
|
this.elt = null;
|
|
|
|
this.tag = null;
|
2013-09-12 14:48:13 +00:00
|
|
|
this.closeTag = null;
|
2012-08-23 18:00:43 +00:00
|
|
|
this.attrList = null;
|
|
|
|
this.newAttr = null;
|
|
|
|
this.closeElt = null;
|
|
|
|
|
|
|
|
// Create the main editor
|
2012-12-20 00:16:45 +00:00
|
|
|
this.template("element", this);
|
2012-08-23 18:00:43 +00:00
|
|
|
|
2013-06-17 13:52:55 +00:00
|
|
|
// Make the tag name editable (unless this is a remote node or
|
|
|
|
// a document element)
|
2014-10-08 22:46:16 +00:00
|
|
|
if (!aNode.isDocumentElement) {
|
2013-06-17 13:52:55 +00:00
|
|
|
this.tag.setAttribute("tabindex", "0");
|
2013-03-20 12:11:50 +00:00
|
|
|
editableField({
|
2013-06-17 13:52:55 +00:00
|
|
|
element: this.tag,
|
2012-08-23 18:00:43 +00:00
|
|
|
trigger: "dblclick",
|
|
|
|
stopOnReturn: true,
|
2013-06-17 13:52:55 +00:00
|
|
|
done: this.onTagEdit.bind(this),
|
2012-08-23 18:00:43 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-06-17 13:52:55 +00:00
|
|
|
// Make the new attribute space editable.
|
|
|
|
editableField({
|
|
|
|
element: this.newAttr,
|
|
|
|
trigger: "dblclick",
|
|
|
|
stopOnReturn: true,
|
2013-08-02 10:35:50 +00:00
|
|
|
contentType: InplaceEditor.CONTENT_TYPES.CSS_MIXED,
|
|
|
|
popup: this.markup.popup,
|
2013-06-17 13:52:55 +00:00
|
|
|
done: (aVal, aCommit) => {
|
|
|
|
if (!aCommit) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
let doMods = this._startModifyingAttributes();
|
|
|
|
let undoMods = this._startModifyingAttributes();
|
|
|
|
this._applyAttributes(aVal, null, doMods, undoMods);
|
2014-09-29 07:29:00 +00:00
|
|
|
this.container.undo.do(() => {
|
2013-06-17 13:52:55 +00:00
|
|
|
doMods.apply();
|
|
|
|
}, function() {
|
|
|
|
undoMods.apply();
|
|
|
|
});
|
|
|
|
} catch(x) {
|
2013-08-08 13:55:39 +00:00
|
|
|
console.error(x);
|
2013-06-17 13:52:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2012-08-23 18:00:43 +00:00
|
|
|
let tagName = this.node.nodeName.toLowerCase();
|
|
|
|
this.tag.textContent = tagName;
|
|
|
|
this.closeTag.textContent = tagName;
|
2014-07-20 11:03:59 +00:00
|
|
|
this.eventNode.style.display = this.node.hasEventListeners ? "inline-block" : "none";
|
2012-08-23 18:00:43 +00:00
|
|
|
|
|
|
|
this.update();
|
|
|
|
}
|
|
|
|
|
|
|
|
ElementEditor.prototype = {
|
|
|
|
/**
|
|
|
|
* Update the state of the editor from the node.
|
|
|
|
*/
|
2013-10-08 12:53:19 +00:00
|
|
|
update: function() {
|
2012-08-23 18:00:43 +00:00
|
|
|
let attrs = this.node.attributes;
|
|
|
|
if (!attrs) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hide all the attribute editors, they'll be re-shown if they're
|
|
|
|
// still applicable. Don't update attributes that are being
|
|
|
|
// actively edited.
|
|
|
|
let attrEditors = this.attrList.querySelectorAll(".attreditor");
|
|
|
|
for (let i = 0; i < attrEditors.length; i++) {
|
|
|
|
if (!attrEditors[i].inplaceEditor) {
|
|
|
|
attrEditors[i].style.display = "none";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the attribute editor for each attribute that exists on
|
|
|
|
// the node and show it.
|
2013-09-16 10:01:25 +00:00
|
|
|
for (let attr of attrs) {
|
|
|
|
let attribute = this._createAttribute(attr);
|
|
|
|
if (!attribute.inplaceEditor) {
|
|
|
|
attribute.style.removeProperty("display");
|
2012-08-23 18:00:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-06-17 13:52:55 +00:00
|
|
|
_startModifyingAttributes: function() {
|
|
|
|
return this.node.startModifyingAttributes();
|
|
|
|
},
|
|
|
|
|
2013-10-25 19:21:01 +00:00
|
|
|
/**
|
|
|
|
* Get the element used for one of the attributes of this element
|
|
|
|
* @param string attrName The name of the attribute to get the element for
|
|
|
|
* @return DOMElement
|
|
|
|
*/
|
|
|
|
getAttributeElement: function(attrName) {
|
|
|
|
return this.attrList.querySelector(
|
|
|
|
".attreditor[data-attr=" + attrName + "] .attr-value");
|
|
|
|
},
|
|
|
|
|
2013-10-02 00:14:00 +00:00
|
|
|
_createAttribute: function(aAttr, aBefore = null) {
|
2013-08-15 14:40:44 +00:00
|
|
|
// Create the template editor, which will save some variables here.
|
|
|
|
let data = {
|
|
|
|
attrName: aAttr.name,
|
|
|
|
};
|
|
|
|
this.template("attribute", data);
|
|
|
|
var {attr, inner, name, val} = data;
|
|
|
|
|
|
|
|
// Double quotes need to be handled specially to prevent DOMParser failing.
|
|
|
|
// name="v"a"l"u"e" when editing -> name='v"a"l"u"e"'
|
|
|
|
// name="v'a"l'u"e" when editing -> name="v'a"l'u"e"
|
2013-09-08 09:01:00 +00:00
|
|
|
let editValueDisplayed = aAttr.value || "";
|
2013-08-15 14:40:44 +00:00
|
|
|
let hasDoubleQuote = editValueDisplayed.contains('"');
|
|
|
|
let hasSingleQuote = editValueDisplayed.contains("'");
|
|
|
|
let initial = aAttr.name + '="' + editValueDisplayed + '"';
|
|
|
|
|
|
|
|
// Can't just wrap value with ' since the value contains both " and '.
|
|
|
|
if (hasDoubleQuote && hasSingleQuote) {
|
|
|
|
editValueDisplayed = editValueDisplayed.replace(/\"/g, """);
|
|
|
|
initial = aAttr.name + '="' + editValueDisplayed + '"';
|
|
|
|
}
|
2013-06-17 13:52:55 +00:00
|
|
|
|
2013-08-15 14:40:44 +00:00
|
|
|
// Wrap with ' since there are no single quotes in the attribute value.
|
|
|
|
if (hasDoubleQuote && !hasSingleQuote) {
|
|
|
|
initial = aAttr.name + "='" + editValueDisplayed + "'";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make the attribute editable.
|
|
|
|
editableField({
|
|
|
|
element: inner,
|
|
|
|
trigger: "dblclick",
|
|
|
|
stopOnReturn: true,
|
|
|
|
selectAll: false,
|
|
|
|
initial: initial,
|
|
|
|
contentType: InplaceEditor.CONTENT_TYPES.CSS_MIXED,
|
|
|
|
popup: this.markup.popup,
|
|
|
|
start: (aEditor, aEvent) => {
|
|
|
|
// If the editing was started inside the name or value areas,
|
|
|
|
// select accordingly.
|
|
|
|
if (aEvent && aEvent.target === name) {
|
|
|
|
aEditor.input.setSelectionRange(0, name.textContent.length);
|
|
|
|
} else if (aEvent && aEvent.target === val) {
|
|
|
|
let length = editValueDisplayed.length;
|
|
|
|
let editorLength = aEditor.input.value.length;
|
|
|
|
let start = editorLength - (length + 1);
|
|
|
|
aEditor.input.setSelectionRange(start, start + length);
|
|
|
|
} else {
|
|
|
|
aEditor.input.select();
|
2013-06-17 13:52:55 +00:00
|
|
|
}
|
2013-08-15 14:40:44 +00:00
|
|
|
},
|
|
|
|
done: (aVal, aCommit) => {
|
2013-09-23 06:46:12 +00:00
|
|
|
if (!aCommit || aVal === initial) {
|
2013-08-15 14:40:44 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let doMods = this._startModifyingAttributes();
|
|
|
|
let undoMods = this._startModifyingAttributes();
|
2012-08-23 18:00:43 +00:00
|
|
|
|
2013-08-15 14:40:44 +00:00
|
|
|
// Remove the attribute stored in this editor and re-add any attributes
|
|
|
|
// parsed out of the input element. Restore original attribute if
|
|
|
|
// parsing fails.
|
|
|
|
try {
|
|
|
|
this._saveAttribute(aAttr.name, undoMods);
|
|
|
|
doMods.removeAttribute(aAttr.name);
|
|
|
|
this._applyAttributes(aVal, attr, doMods, undoMods);
|
2014-09-29 07:29:00 +00:00
|
|
|
this.container.undo.do(() => {
|
2013-08-15 14:40:44 +00:00
|
|
|
doMods.apply();
|
|
|
|
}, () => {
|
|
|
|
undoMods.apply();
|
2014-11-22 07:48:00 +00:00
|
|
|
});
|
2013-08-15 14:40:44 +00:00
|
|
|
} catch(ex) {
|
|
|
|
console.error(ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Figure out where we should place the attribute.
|
|
|
|
let before = aBefore;
|
|
|
|
if (aAttr.name == "id") {
|
|
|
|
before = this.attrList.firstChild;
|
|
|
|
} else if (aAttr.name == "class") {
|
|
|
|
let idNode = this.attrs["id"];
|
|
|
|
before = idNode ? idNode.nextSibling : this.attrList.firstChild;
|
2012-08-23 18:00:43 +00:00
|
|
|
}
|
2013-08-15 14:40:44 +00:00
|
|
|
this.attrList.insertBefore(attr, before);
|
|
|
|
|
|
|
|
// Remove the old version of this attribute from the DOM.
|
|
|
|
let oldAttr = this.attrs[aAttr.name];
|
|
|
|
if (oldAttr && oldAttr.parentNode) {
|
|
|
|
oldAttr.parentNode.removeChild(oldAttr);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.attrs[aAttr.name] = attr;
|
2012-08-23 18:00:43 +00:00
|
|
|
|
2014-01-10 17:55:58 +00:00
|
|
|
let collapsedValue;
|
|
|
|
if (aAttr.value.match(COLLAPSE_DATA_URL_REGEX)) {
|
|
|
|
collapsedValue = truncateString(aAttr.value, COLLAPSE_DATA_URL_LENGTH);
|
|
|
|
} else {
|
|
|
|
collapsedValue = truncateString(aAttr.value, COLLAPSE_ATTRIBUTE_LENGTH);
|
2013-09-12 22:25:08 +00:00
|
|
|
}
|
|
|
|
|
2014-01-10 17:55:58 +00:00
|
|
|
name.textContent = aAttr.name;
|
|
|
|
val.textContent = collapsedValue;
|
2013-10-18 14:01:20 +00:00
|
|
|
|
2014-01-10 17:55:58 +00:00
|
|
|
return attr;
|
2012-08-23 18:00:43 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse a user-entered attribute string and apply the resulting
|
|
|
|
* attributes to the node. This operation is undoable.
|
|
|
|
*
|
|
|
|
* @param string aValue the user-entered value.
|
|
|
|
* @param Element aAttrNode the attribute editor that created this
|
|
|
|
* set of attributes, used to place new attributes where the
|
|
|
|
* user put them.
|
|
|
|
*/
|
2013-10-02 00:14:00 +00:00
|
|
|
_applyAttributes: function(aValue, aAttrNode, aDoMods, aUndoMods) {
|
2013-08-08 13:55:39 +00:00
|
|
|
let attrs = parseAttributeValues(aValue, this.doc);
|
2013-03-21 11:36:52 +00:00
|
|
|
for (let attr of attrs) {
|
2012-08-23 18:00:43 +00:00
|
|
|
// Create an attribute editor next to the current attribute if needed.
|
2013-06-17 13:52:55 +00:00
|
|
|
this._createAttribute(attr, aAttrNode ? aAttrNode.nextSibling : null);
|
|
|
|
this._saveAttribute(attr.name, aUndoMods);
|
|
|
|
aDoMods.setAttribute(attr.name, attr.value);
|
2012-08-23 18:00:43 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2013-06-17 13:52:55 +00:00
|
|
|
* Saves the current state of the given attribute into an attribute
|
|
|
|
* modification list.
|
2012-08-23 18:00:43 +00:00
|
|
|
*/
|
2013-10-02 00:14:00 +00:00
|
|
|
_saveAttribute: function(aName, aUndoMods) {
|
2013-06-17 13:52:55 +00:00
|
|
|
let node = this.node;
|
|
|
|
if (node.hasAttribute(aName)) {
|
|
|
|
let oldValue = node.getAttribute(aName);
|
|
|
|
aUndoMods.setAttribute(aName, oldValue);
|
|
|
|
} else {
|
|
|
|
aUndoMods.removeAttribute(aName);
|
2012-08-23 18:00:43 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when the tag name editor has is done editing.
|
|
|
|
*/
|
2014-10-08 22:46:16 +00:00
|
|
|
onTagEdit: function(newTagName, isCommit) {
|
2014-12-05 00:34:47 +00:00
|
|
|
if (!isCommit || newTagName.toLowerCase() === this.node.tagName.toLowerCase() ||
|
2014-10-08 22:46:16 +00:00
|
|
|
!("editTagName" in this.markup.walker)) {
|
2012-08-25 18:04:46 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-08 22:46:16 +00:00
|
|
|
// Changing the tagName removes the node. Make sure the replacing node gets
|
|
|
|
// selected afterwards.
|
|
|
|
this.markup.reselectOnRemoved(this.node, "edittagname");
|
|
|
|
this.markup.walker.editTagName(this.node, newTagName).then(null, () => {
|
|
|
|
// Failed to edit the tag name, cancel the reselection.
|
|
|
|
this.markup.cancelReselectOnRemoved();
|
|
|
|
});
|
2013-10-11 15:50:33 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
destroy: function() {}
|
2012-08-23 18:00:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
function nodeDocument(node) {
|
2013-10-02 00:14:00 +00:00
|
|
|
return node.ownerDocument ||
|
|
|
|
(node.nodeType == Ci.nsIDOMNode.DOCUMENT_NODE ? node : null);
|
2012-08-23 18:00:43 +00:00
|
|
|
}
|
|
|
|
|
2013-09-12 22:25:08 +00:00
|
|
|
function truncateString(str, maxLength) {
|
|
|
|
if (str.length <= maxLength) {
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
return str.substring(0, Math.ceil(maxLength / 2)) +
|
|
|
|
"…" +
|
|
|
|
str.substring(str.length - Math.floor(maxLength / 2));
|
|
|
|
}
|
2014-09-08 12:30:00 +00:00
|
|
|
|
2013-03-21 11:36:52 +00:00
|
|
|
/**
|
2013-08-08 13:55:39 +00:00
|
|
|
* Parse attribute names and values from a string.
|
2013-03-21 11:36:52 +00:00
|
|
|
*
|
|
|
|
* @param {String} attr
|
2013-08-08 13:55:39 +00:00
|
|
|
* The input string for which names/values are to be parsed.
|
|
|
|
* @param {HTMLDocument} doc
|
|
|
|
* A document that can be used to test valid attributes.
|
2013-03-21 11:36:52 +00:00
|
|
|
* @return {Array}
|
2013-08-08 13:55:39 +00:00
|
|
|
* An array of attribute names and their values.
|
2013-03-21 11:36:52 +00:00
|
|
|
*/
|
2013-08-08 13:55:39 +00:00
|
|
|
function parseAttributeValues(attr, doc) {
|
|
|
|
attr = attr.trim();
|
2013-03-21 11:36:52 +00:00
|
|
|
|
2014-09-08 12:30:00 +00:00
|
|
|
// Handle bad user inputs by appending a " or ' if it fails to parse without
|
|
|
|
// them. Also note that a SVG tag is used to make sure the HTML parser
|
|
|
|
// preserves mixed-case attributes
|
|
|
|
let el = DOMParser.parseFromString("<svg " + attr + "></svg>", "text/html").body.childNodes[0] ||
|
|
|
|
DOMParser.parseFromString("<svg " + attr + "\"></svg>", "text/html").body.childNodes[0] ||
|
|
|
|
DOMParser.parseFromString("<svg " + attr + "'></svg>", "text/html").body.childNodes[0];
|
2014-09-04 08:28:24 +00:00
|
|
|
|
2013-08-08 13:55:39 +00:00
|
|
|
let div = doc.createElement("div");
|
|
|
|
let attributes = [];
|
2014-09-08 12:30:00 +00:00
|
|
|
for (let {name, value} of el.attributes) {
|
2013-08-08 13:55:39 +00:00
|
|
|
// Try to set on an element in the document, throws exception on bad input.
|
|
|
|
// Prevents InvalidCharacterError - "String contains an invalid character".
|
|
|
|
try {
|
2014-09-04 08:28:24 +00:00
|
|
|
div.setAttribute(name, value);
|
|
|
|
attributes.push({ name, value });
|
2013-03-21 11:36:52 +00:00
|
|
|
}
|
2013-08-08 13:55:39 +00:00
|
|
|
catch(e) { }
|
2013-03-21 11:36:52 +00:00
|
|
|
}
|
|
|
|
|
2013-08-08 13:55:39 +00:00
|
|
|
// Attributes return from DOMParser in reverse order from how they are entered.
|
|
|
|
return attributes.reverse();
|
2012-08-23 18:00:43 +00:00
|
|
|
}
|
|
|
|
|
2013-04-11 20:59:08 +00:00
|
|
|
loader.lazyGetter(MarkupView.prototype, "strings", () => Services.strings.createBundle(
|
|
|
|
"chrome://browser/locale/devtools/inspector.properties"
|
|
|
|
));
|
2014-01-30 16:33:53 +00:00
|
|
|
|
|
|
|
XPCOMUtils.defineLazyGetter(this, "clipboardHelper", function() {
|
|
|
|
return Cc["@mozilla.org/widget/clipboardhelper;1"].
|
|
|
|
getService(Ci.nsIClipboardHelper);
|
|
|
|
});
|