2013-08-01 08:53:31 +00:00
|
|
|
/* -*- Mode: Javascript; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 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;
|
2012-09-25 16:33:46 +00:00
|
|
|
|
2013-04-11 20:59:08 +00:00
|
|
|
let {UndoStack} = require("devtools/shared/undo");
|
|
|
|
let EventEmitter = require("devtools/shared/event-emitter");
|
|
|
|
let {editableField, InplaceEditor} = require("devtools/shared/inplace-editor");
|
2013-06-11 04:18:46 +00:00
|
|
|
let promise = require("sdk/core/promise");
|
2012-08-23 18:00:43 +00:00
|
|
|
|
|
|
|
Cu.import("resource:///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");
|
2013-08-02 10:35:50 +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-08-03 10:29:48 +00:00
|
|
|
loader.lazyGetter(this, "AutocompletePopup", () => require("devtools/shared/autocomplete-popup").AutocompletePopup);
|
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.
|
|
|
|
* 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-04-11 20:59:08 +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");
|
|
|
|
|
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 = {
|
|
|
|
fixedWidth: true,
|
|
|
|
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
|
|
|
|
|
|
|
this._containers = new WeakMap();
|
|
|
|
|
2013-06-11 04:18:46 +00:00
|
|
|
this._boundMutationObserver = this._mutationObserver.bind(this);
|
|
|
|
this.walker.on("mutations", this._boundMutationObserver)
|
2012-08-23 18:00:43 +00:00
|
|
|
|
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
|
|
|
|
|
|
|
this._initPreview();
|
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 = {
|
|
|
|
_selectedContainer: null,
|
|
|
|
|
2012-12-20 00:16:45 +00:00
|
|
|
template: function MT_template(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.
|
|
|
|
*/
|
|
|
|
getContainer: function MT_getContainer(aNode)
|
|
|
|
{
|
|
|
|
return this._containers.get(aNode);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2012-11-30 08:07:59 +00:00
|
|
|
* Highlight the inspector selected node.
|
2012-08-23 18:00:43 +00:00
|
|
|
*/
|
2012-11-30 08:07:59 +00:00
|
|
|
_onNewSelection: function MT__onNewSelection()
|
2012-08-23 18:00:43 +00:00
|
|
|
{
|
2013-06-11 04:18:46 +00:00
|
|
|
let done = this._inspector.updating("markup-view");
|
2012-11-30 08:07:59 +00:00
|
|
|
if (this._inspector.selection.isNode()) {
|
2013-06-11 04:18:46 +00:00
|
|
|
this.showNode(this._inspector.selection.nodeFront, true).then(() => {
|
|
|
|
this.markNodeAsSelected(this._inspector.selection.nodeFront);
|
|
|
|
done();
|
|
|
|
});
|
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.
|
|
|
|
*/
|
|
|
|
_selectionWalker: function MT__seletionWalker(aStart)
|
|
|
|
{
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
_onKeyDown: function MT__KeyDown(aEvent)
|
|
|
|
{
|
|
|
|
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:
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
break;
|
2012-08-23 18:00:43 +00:00
|
|
|
case Ci.nsIDOMKeyEvent.DOM_VK_DELETE:
|
|
|
|
case Ci.nsIDOMKeyEvent.DOM_VK_BACK_SPACE:
|
2012-08-25 00:59:48 +00:00
|
|
|
this.deleteNode(this._selectedContainer.node);
|
2012-08-23 18:00:43 +00:00
|
|
|
break;
|
|
|
|
case Ci.nsIDOMKeyEvent.DOM_VK_HOME:
|
2013-06-11 04:18:46 +00:00
|
|
|
let rootContainer = this._containers.get(this._rootNode);
|
|
|
|
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-03-01 02:50:24 +00:00
|
|
|
if (!this._selectedContainer.expanded) {
|
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;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
handled = false;
|
|
|
|
}
|
|
|
|
if (handled) {
|
|
|
|
aEvent.stopPropagation();
|
|
|
|
aEvent.preventDefault();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a node from the DOM.
|
|
|
|
* This is an undoable action.
|
|
|
|
*/
|
|
|
|
deleteNode: function MC__deleteNode(aNode)
|
|
|
|
{
|
2013-06-17 13:52:55 +00:00
|
|
|
if (aNode.isDocumentElement ||
|
2012-08-25 03:37:05 +00:00
|
|
|
aNode.nodeType == Ci.nsIDOMNode.DOCUMENT_TYPE_NODE) {
|
2012-08-25 00:59:48 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-17 13:52:55 +00:00
|
|
|
let container = this._containers.get(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();
|
|
|
|
let sibling = null;
|
|
|
|
this.undo.do(() => {
|
|
|
|
if (container.selected) {
|
|
|
|
this.navigate(this._containers.get(parent));
|
|
|
|
}
|
|
|
|
this.walker.removeNode(aNode).then(nextSibling => {
|
|
|
|
sibling = nextSibling;
|
|
|
|
});
|
|
|
|
}, () => {
|
|
|
|
this.walker.insertBefore(aNode, parent, sibling);
|
|
|
|
});
|
|
|
|
}).then(null, console.error);
|
2012-08-23 18:00:43 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If an editable item is focused, select its container.
|
|
|
|
*/
|
|
|
|
_onFocus: function MC__onFocus(aEvent) {
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
navigate: function MT__navigate(aContainer, aIgnoreFocus)
|
|
|
|
{
|
|
|
|
if (!aContainer) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let node = aContainer.node;
|
2013-06-11 04:18:46 +00:00
|
|
|
this.markNodeAsSelected(node);
|
|
|
|
this._inspector.selection.setNodeFront(node, "treepanel");
|
2012-11-30 08:07:59 +00:00
|
|
|
// This event won't be fired if the node is the same. But the highlighter
|
|
|
|
// need to lock the node if it wasn't.
|
|
|
|
this._inspector.selection.emit("new-node");
|
2013-06-11 04:18:46 +00:00
|
|
|
this._inspector.selection.emit("new-node-front");
|
2012-08-23 18:00:43 +00:00
|
|
|
|
|
|
|
if (!aIgnoreFocus) {
|
|
|
|
aContainer.focus();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Make sure a node is included in the markup tool.
|
|
|
|
*
|
|
|
|
* @param DOMNode aNode
|
|
|
|
* The node in the content document.
|
|
|
|
* @returns MarkupContainer The MarkupContainer object for this element.
|
|
|
|
*/
|
2013-06-11 04:18:46 +00:00
|
|
|
importNode: function MT_importNode(aNode)
|
2012-08-23 18:00:43 +00:00
|
|
|
{
|
|
|
|
if (!aNode) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._containers.has(aNode)) {
|
|
|
|
return this._containers.get(aNode);
|
|
|
|
}
|
|
|
|
|
2013-06-11 04:18:46 +00:00
|
|
|
if (aNode === this.walker.rootNode) {
|
2012-08-23 18:00:43 +00:00
|
|
|
var container = new RootContainer(this, aNode);
|
|
|
|
this._elt.appendChild(container.elt);
|
|
|
|
this._rootNode = aNode;
|
2013-06-11 04:18:46 +00:00
|
|
|
} else {
|
|
|
|
var container = new MarkupContainer(this, aNode);
|
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;
|
|
|
|
},
|
|
|
|
|
2013-07-01 10:59:28 +00:00
|
|
|
|
2012-08-23 18:00:43 +00:00
|
|
|
/**
|
|
|
|
* Mutation observer used for included nodes.
|
|
|
|
*/
|
|
|
|
_mutationObserver: function MT__mutationObserver(aMutations)
|
|
|
|
{
|
|
|
|
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).
|
|
|
|
type = "childList"
|
|
|
|
target = mutation.targetParent;
|
|
|
|
if (!target) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let container = this._containers.get(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") {
|
2012-08-23 18:00:43 +00:00
|
|
|
container.update();
|
2013-06-11 04:18:46 +00:00
|
|
|
} else if (type === "childList") {
|
2012-12-20 00:16:45 +00:00
|
|
|
container.childrenDirty = true;
|
2012-08-23 18:00:43 +00:00
|
|
|
this._updateChildren(container);
|
|
|
|
}
|
|
|
|
}
|
2013-06-11 04:18:46 +00:00
|
|
|
this._waitForChildren().then(() => {
|
|
|
|
this._inspector.emit("markupmutation");
|
|
|
|
});
|
2012-08-23 18:00:43 +00:00
|
|
|
},
|
|
|
|
|
2013-06-11 04:18:46 +00:00
|
|
|
|
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.
|
|
|
|
*/
|
2012-10-01 15:15:23 +00:00
|
|
|
showNode: function MT_showNode(aNode, centered)
|
2012-08-23 18:00:43 +00:00
|
|
|
{
|
2012-12-20 00:16:45 +00:00
|
|
|
let container = this.importNode(aNode);
|
2013-06-11 04:18:46 +00:00
|
|
|
let parent = aNode;
|
|
|
|
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?
|
|
|
|
LayoutHelpers.scrollIntoViewIfNeeded(this._containers.get(aNode).editor.elt, centered);
|
|
|
|
});
|
2012-08-23 18:00:43 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Expand the container's children.
|
|
|
|
*/
|
|
|
|
_expandContainer: function MT__expandContainer(aContainer)
|
|
|
|
{
|
2013-06-11 04:18:46 +00:00
|
|
|
return this._updateChildren(aContainer, true).then(() => {
|
2012-08-23 18:00:43 +00:00
|
|
|
aContainer.expanded = true;
|
2013-06-11 04:18:46 +00:00
|
|
|
})
|
2012-08-23 18:00:43 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Expand the node's children.
|
|
|
|
*/
|
|
|
|
expandNode: function MT_expandNode(aNode)
|
|
|
|
{
|
|
|
|
let container = this._containers.get(aNode);
|
|
|
|
this._expandContainer(container);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Expand the entire tree beneath a container.
|
|
|
|
*
|
|
|
|
* @param aContainer The container to expand.
|
|
|
|
*/
|
|
|
|
_expandAll: function MT_expandAll(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.
|
|
|
|
*/
|
|
|
|
expandAll: function MT_expandAll(aNode)
|
|
|
|
{
|
|
|
|
aNode = aNode || this._rootNode;
|
2013-06-11 04:18:46 +00:00
|
|
|
return this._expandAll(this._containers.get(aNode));
|
2012-08-23 18:00:43 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Collapse the node's children.
|
|
|
|
*/
|
|
|
|
collapseNode: function MT_collapseNode(aNode)
|
|
|
|
{
|
|
|
|
let container = this._containers.get(aNode);
|
|
|
|
container.expanded = false;
|
|
|
|
},
|
|
|
|
|
2013-06-11 04:18:46 +00:00
|
|
|
setNodeExpanded: function(aNode, aExpanded)
|
|
|
|
{
|
|
|
|
if (aExpanded) {
|
|
|
|
this.expandNode(aNode);
|
|
|
|
} else {
|
|
|
|
this.collapseNode(aNode);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-08-23 18:00:43 +00:00
|
|
|
/**
|
|
|
|
* Mark the given node selected.
|
|
|
|
*/
|
2012-11-30 08:07:59 +00:00
|
|
|
markNodeAsSelected: function MT_markNodeAsSelected(aNode)
|
2012-08-23 18:00:43 +00:00
|
|
|
{
|
|
|
|
let container = this._containers.get(aNode);
|
|
|
|
if (this._selectedContainer === container) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (this._selectedContainer) {
|
|
|
|
this._selectedContainer.selected = false;
|
|
|
|
}
|
|
|
|
this._selectedContainer = container;
|
|
|
|
if (aNode) {
|
|
|
|
this._selectedContainer.selected = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-06-11 04:18:46 +00:00
|
|
|
_ensureVisible: function(node)
|
2012-12-20 00:16:45 +00:00
|
|
|
{
|
|
|
|
while (node) {
|
|
|
|
let container = this._containers.get(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) {
|
|
|
|
let parentContainer = this._containers.get(parent);
|
|
|
|
parentContainer.childrenDirty = true;
|
|
|
|
this._updateChildren(parentContainer, node);
|
|
|
|
}
|
|
|
|
|
|
|
|
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).
|
|
|
|
*/
|
|
|
|
unmarkSelectedNode: function MT_unmarkSelectedNode()
|
|
|
|
{
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
nodeChanged: function MT_nodeChanged(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!
|
|
|
|
*
|
|
|
|
* This method returns a promise that will be resolved when the children
|
|
|
|
* are ready (which may be immediately).
|
2012-08-23 18:00:43 +00:00
|
|
|
*/
|
2013-06-11 04:18:46 +00:00
|
|
|
_updateChildren: function(aContainer, aExpand)
|
2012-08-23 18:00:43 +00:00
|
|
|
{
|
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.
|
|
|
|
if (!(aContainer.expanded || aExpand)) {
|
|
|
|
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) {
|
|
|
|
return this._updateChildren(aContainer, centered);
|
|
|
|
}
|
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) {
|
|
|
|
let container = this.importNode(child);
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
_getVisibleChildren: function MV__getVisibleChildren(aContainer, aCentered)
|
|
|
|
{
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
destroy: function MT_destroy()
|
|
|
|
{
|
|
|
|
this.undo.destroy();
|
|
|
|
delete this.undo;
|
|
|
|
|
2013-08-02 10:35:50 +00:00
|
|
|
this.popup.destroy();
|
|
|
|
delete this.popup;
|
|
|
|
|
2012-09-25 16:33:46 +00:00
|
|
|
this._frame.removeEventListener("focus", this._boundFocus, false);
|
2012-08-23 18:00:43 +00:00
|
|
|
delete this._boundFocus;
|
|
|
|
|
2013-06-11 04:18:46 +00:00
|
|
|
if (this._boundUpdatePreview) {
|
|
|
|
this._frame.contentWindow.removeEventListener("scroll", this._boundUpdatePreview, true);
|
|
|
|
delete this._boundUpdatePreview;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._boundResizePreview) {
|
|
|
|
this._frame.contentWindow.removeEventListener("resize", this._boundResizePreview, true);
|
|
|
|
this._frame.contentWindow.removeEventListener("overflow", this._boundResizePreview, true);
|
|
|
|
this._frame.contentWindow.removeEventListener("underflow", this._boundResizePreview, true);
|
|
|
|
delete this._boundResizePreview;
|
|
|
|
}
|
2012-09-25 16:33:46 +00:00
|
|
|
|
2013-07-01 10:59:28 +00:00
|
|
|
this._frame.contentWindow.removeEventListener("keydown", this._boundKeyDown, false);
|
2012-08-23 18:00:43 +00:00
|
|
|
delete this._boundKeyDown;
|
|
|
|
|
2013-06-11 04:18:46 +00:00
|
|
|
this._inspector.selection.off("new-node-front", this._boundOnNewSelection);
|
2012-11-30 08:07:59 +00:00
|
|
|
delete this._boundOnNewSelection;
|
2012-08-23 18:00:43 +00:00
|
|
|
|
2013-06-11 04:18:46 +00:00
|
|
|
this.walker.off("mutations", this._boundMutationObserver)
|
|
|
|
delete this._boundMutationObserver;
|
|
|
|
|
2012-08-23 18:00:43 +00:00
|
|
|
delete this._elt;
|
|
|
|
|
|
|
|
delete this._containers;
|
2012-09-25 16:33:46 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize the preview panel.
|
|
|
|
*/
|
|
|
|
_initPreview: function MT_initPreview()
|
|
|
|
{
|
|
|
|
if (!Services.prefs.getBoolPref("devtools.inspector.markupPreview")) {
|
|
|
|
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);
|
|
|
|
this._frame.contentWindow.addEventListener("resize", this._boundResizePreview, true);
|
|
|
|
this._frame.contentWindow.addEventListener("overflow", this._boundResizePreview, true);
|
|
|
|
this._frame.contentWindow.addEventListener("underflow", this._boundResizePreview, true);
|
|
|
|
|
|
|
|
this._boundUpdatePreview = this._updatePreview.bind(this);
|
|
|
|
this._frame.contentWindow.addEventListener("scroll", this._boundUpdatePreview, true);
|
|
|
|
this._updatePreview();
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Move the preview viewbox.
|
|
|
|
*/
|
|
|
|
_updatePreview: function MT_updatePreview()
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
this._previewBar.setAttribute("style", "height:" + height + "px;transform:translateY(" + scrollTo + "px)");
|
|
|
|
} else {
|
|
|
|
this._previewBar.setAttribute("style", "height:100%");
|
|
|
|
}
|
|
|
|
|
|
|
|
let bgSize = ~~width + "px " + ~~height + "px";
|
|
|
|
this._preview.setAttribute("style", "background-size:" + bgSize);
|
|
|
|
|
|
|
|
let height = ~~(win.innerHeight * ratio) + "px";
|
|
|
|
let top = ~~(win.scrollY * ratio) + "px";
|
|
|
|
this._viewbox.setAttribute("style", "height:" + height + ";transform: translateY(" + top + ")");
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hide the preview while resizing, to avoid slowness.
|
|
|
|
*/
|
|
|
|
_resizePreview: function MT_resizePreview()
|
|
|
|
{
|
|
|
|
let win = this._frame.contentWindow;
|
|
|
|
this._previewBar.classList.add("hide");
|
|
|
|
win.clearTimeout(this._resizePreviewTimeout);
|
|
|
|
|
|
|
|
win.setTimeout(function() {
|
|
|
|
this._updatePreview();
|
|
|
|
this._previewBar.classList.remove("hide");
|
|
|
|
}.bind(this), 1000);
|
|
|
|
},
|
|
|
|
|
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.
|
|
|
|
*
|
|
|
|
* @param MarkupView aMarkupView
|
|
|
|
* The markup view that owns this container.
|
|
|
|
* @param DOMNode aNode
|
|
|
|
* The node to display.
|
|
|
|
*/
|
|
|
|
function MarkupContainer(aMarkupView, aNode)
|
|
|
|
{
|
|
|
|
this.markup = aMarkupView;
|
|
|
|
this.doc = this.markup.doc;
|
|
|
|
this.undo = this.markup.undo;
|
|
|
|
this.node = aNode;
|
|
|
|
|
|
|
|
if (aNode.nodeType == Ci.nsIDOMNode.TEXT_NODE) {
|
|
|
|
this.editor = new TextEditor(this, aNode, "text");
|
|
|
|
} else if (aNode.nodeType == Ci.nsIDOMNode.COMMENT_NODE) {
|
|
|
|
this.editor = new TextEditor(this, aNode, "comment");
|
|
|
|
} else if (aNode.nodeType == Ci.nsIDOMNode.ELEMENT_NODE) {
|
|
|
|
this.editor = new ElementEditor(this, aNode);
|
|
|
|
} else if (aNode.nodeType == Ci.nsIDOMNode.DOCUMENT_TYPE_NODE) {
|
|
|
|
this.editor = new DoctypeEditor(this, aNode);
|
|
|
|
} else {
|
|
|
|
this.editor = new GenericEditor(this.markup, aNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
// The template will fill the following properties
|
|
|
|
this.elt = null;
|
|
|
|
this.expander = null;
|
|
|
|
this.codeBox = null;
|
|
|
|
this.children = null;
|
2012-12-20 00:16:45 +00:00
|
|
|
this.markup.template("container", this);
|
2012-08-23 18:00:43 +00:00
|
|
|
this.elt.container = this;
|
2013-06-11 04:18:46 +00:00
|
|
|
this.children.container = this;
|
2012-08-23 18:00:43 +00:00
|
|
|
|
|
|
|
this.expander.addEventListener("click", function() {
|
|
|
|
this.markup.navigate(this);
|
|
|
|
|
2013-06-11 04:18:46 +00:00
|
|
|
this.markup.setNodeExpanded(this.node, !this.expanded);
|
2012-08-23 18:00:43 +00:00
|
|
|
}.bind(this));
|
|
|
|
|
|
|
|
this.codeBox.insertBefore(this.editor.elt, this.children);
|
|
|
|
|
|
|
|
this.editor.elt.addEventListener("mousedown", function(evt) {
|
|
|
|
this.markup.navigate(this);
|
|
|
|
}.bind(this), false);
|
|
|
|
|
2013-01-23 17:01:55 +00:00
|
|
|
if (this.editor.summaryElt) {
|
|
|
|
this.editor.summaryElt.addEventListener("click", function(evt) {
|
|
|
|
this.markup.navigate(this);
|
|
|
|
this.markup.expandNode(this.node);
|
|
|
|
}.bind(this), false);
|
|
|
|
this.codeBox.appendChild(this.editor.summaryElt);
|
|
|
|
}
|
|
|
|
|
2012-08-23 18:00:43 +00:00
|
|
|
if (this.editor.closeElt) {
|
2013-01-17 15:21:24 +00:00
|
|
|
this.editor.closeElt.addEventListener("mousedown", function(evt) {
|
|
|
|
this.markup.navigate(this);
|
|
|
|
}.bind(this), false);
|
2012-08-23 18:00:43 +00:00
|
|
|
this.codeBox.appendChild(this.editor.closeElt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MarkupContainer.prototype = {
|
2013-06-11 04:18:46 +00:00
|
|
|
toString: function() { return "[MarkupContainer for " + this.node + "]" },
|
|
|
|
|
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;
|
|
|
|
if (aValue) {
|
|
|
|
this.expander.style.visibility = "visible";
|
|
|
|
} else {
|
|
|
|
this.expander.style.visibility = "hidden";
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-06-11 04:18:46 +00:00
|
|
|
parentContainer: function() {
|
|
|
|
return this.elt.parentNode ? this.elt.parentNode.container : null;
|
|
|
|
},
|
|
|
|
|
2012-08-23 18:00:43 +00:00
|
|
|
/**
|
|
|
|
* True if the node has been visually expanded in the tree.
|
|
|
|
*/
|
|
|
|
get expanded() {
|
|
|
|
return this.children.hasAttribute("expanded");
|
|
|
|
},
|
|
|
|
|
|
|
|
set expanded(aValue) {
|
|
|
|
if (aValue) {
|
2013-03-27 22:20:38 +00:00
|
|
|
this.expander.setAttribute("open", "");
|
2012-08-23 18:00:43 +00:00
|
|
|
this.children.setAttribute("expanded", "");
|
2013-01-23 17:01:55 +00:00
|
|
|
if (this.editor.summaryElt) {
|
|
|
|
this.editor.summaryElt.setAttribute("expanded", "");
|
|
|
|
}
|
2012-08-23 18:00:43 +00:00
|
|
|
} else {
|
2013-03-27 22:20:38 +00:00
|
|
|
this.expander.removeAttribute("open");
|
2012-08-23 18:00:43 +00:00
|
|
|
this.children.removeAttribute("expanded");
|
2013-01-23 17:01:55 +00:00
|
|
|
if (this.editor.summaryElt) {
|
|
|
|
this.editor.summaryElt.removeAttribute("expanded");
|
|
|
|
}
|
2012-08-23 18:00:43 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* True if the container is visible in the markup tree.
|
|
|
|
*/
|
|
|
|
get visible()
|
|
|
|
{
|
|
|
|
return this.elt.getBoundingClientRect().height > 0;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* True if the container is currently selected.
|
|
|
|
*/
|
|
|
|
_selected: false,
|
|
|
|
|
|
|
|
get selected() {
|
|
|
|
return this._selected;
|
|
|
|
},
|
|
|
|
|
|
|
|
set selected(aValue) {
|
|
|
|
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-03-27 22:20:38 +00:00
|
|
|
this.editor.elt.classList.add("theme-selected");
|
2012-08-23 18:00:43 +00:00
|
|
|
if (this.editor.closeElt) {
|
2013-03-27 22:20:38 +00:00
|
|
|
this.editor.closeElt.classList.add("theme-selected");
|
2012-08-23 18:00:43 +00:00
|
|
|
}
|
|
|
|
} else {
|
2013-03-27 22:20:38 +00:00
|
|
|
this.editor.elt.classList.remove("theme-selected");
|
2012-08-23 18:00:43 +00:00
|
|
|
if (this.editor.closeElt) {
|
2013-03-27 22:20:38 +00:00
|
|
|
this.editor.closeElt.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.
|
|
|
|
*/
|
|
|
|
update: function MC_update()
|
|
|
|
{
|
|
|
|
if (this.editor.update) {
|
|
|
|
this.editor.update();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Try to put keyboard focus on the current editor.
|
|
|
|
*/
|
|
|
|
focus: function MC_focus()
|
|
|
|
{
|
|
|
|
let focusable = this.editor.elt.querySelector("[tabindex]");
|
|
|
|
if (focusable) {
|
|
|
|
focusable.focus();
|
|
|
|
}
|
2012-12-20 00:16:45 +00:00
|
|
|
},
|
2012-08-23 18:00:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dummy container node used for the root document element.
|
|
|
|
*/
|
|
|
|
function RootContainer(aMarkupView, aNode)
|
|
|
|
{
|
|
|
|
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-06-11 04:18:46 +00:00
|
|
|
this.toString = function() { return "[root container]"}
|
2012-08-23 18:00:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates an editor for simple nodes.
|
|
|
|
*/
|
|
|
|
function GenericEditor(aContainer, aNode)
|
|
|
|
{
|
|
|
|
this.elt = aContainer.doc.createElement("span");
|
|
|
|
this.elt.className = "editor";
|
|
|
|
this.elt.textContent = aNode.nodeName;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates an editor for a DOCTYPE node.
|
|
|
|
*
|
|
|
|
* @param MarkupContainer aContainer The container owning this editor.
|
|
|
|
* @param DOMNode aNode The node being edited.
|
|
|
|
*/
|
|
|
|
function DoctypeEditor(aContainer, aNode)
|
|
|
|
{
|
|
|
|
this.elt = aContainer.doc.createElement("span");
|
|
|
|
this.elt.className = "editor comment";
|
|
|
|
this.elt.textContent = '<!DOCTYPE ' + aNode.name +
|
|
|
|
(aNode.publicId ? ' PUBLIC "' + aNode.publicId + '"': '') +
|
|
|
|
(aNode.systemId ? ' "' + aNode.systemId + '"' : '') +
|
|
|
|
'>';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
function TextEditor(aContainer, aNode, aTemplate)
|
|
|
|
{
|
|
|
|
this.node = aNode;
|
2013-06-11 04:18:46 +00:00
|
|
|
this._selected = false;
|
2012-08-23 18:00:43 +00:00
|
|
|
|
|
|
|
aContainer.markup.template(aTemplate, this);
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
aContainer.undo.do(() => {
|
|
|
|
this.node.setNodeValue(aVal).then(() => {
|
|
|
|
aContainer.markup.nodeChanged(this.node);
|
|
|
|
});
|
|
|
|
}, () => {
|
|
|
|
this.node.setNodeValue(oldValue).then(() => {
|
|
|
|
aContainer.markup.nodeChanged(this.node);
|
|
|
|
})
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
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();
|
|
|
|
},
|
|
|
|
|
2012-08-23 18:00:43 +00:00
|
|
|
update: function TE_update()
|
|
|
|
{
|
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);
|
|
|
|
}
|
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.
|
|
|
|
*/
|
|
|
|
function ElementEditor(aContainer, aNode)
|
|
|
|
{
|
|
|
|
this.doc = aContainer.doc;
|
|
|
|
this.undo = aContainer.undo;
|
|
|
|
this.template = aContainer.markup.template.bind(aContainer.markup);
|
2012-08-25 18:04:46 +00:00
|
|
|
this.container = aContainer;
|
2012-08-25 18:04:47 +00:00
|
|
|
this.markup = this.container.markup;
|
2012-08-23 18:00:43 +00:00
|
|
|
this.node = aNode;
|
|
|
|
|
2013-08-08 13:55:39 +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;
|
|
|
|
this.attrList = null;
|
|
|
|
this.newAttr = null;
|
2013-01-23 17:01:55 +00:00
|
|
|
this.summaryElt = null;
|
2012-08-23 18:00:43 +00:00
|
|
|
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-11 04:18:46 +00:00
|
|
|
if (this.node.hasChildren) {
|
2013-01-23 17:01:55 +00:00
|
|
|
// Create the summary placeholder
|
|
|
|
this.template("elementContentSummary", this);
|
|
|
|
}
|
|
|
|
|
2012-08-23 18:00:43 +00:00
|
|
|
// Create the closing tag
|
2012-12-20 00:16:45 +00:00
|
|
|
this.template("elementClose", this);
|
2012-08-23 18:00:43 +00:00
|
|
|
|
2013-06-11 04:18:46 +00:00
|
|
|
this.rawNode = aNode.rawNode();
|
|
|
|
|
2013-06-17 13:52:55 +00:00
|
|
|
// Make the tag name editable (unless this is a remote node or
|
|
|
|
// a document element)
|
|
|
|
if (this.rawNode && !aNode.isDocumentElement) {
|
|
|
|
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);
|
|
|
|
this.undo.do(() => {
|
|
|
|
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;
|
|
|
|
|
|
|
|
this.update();
|
|
|
|
}
|
|
|
|
|
|
|
|
ElementEditor.prototype = {
|
|
|
|
/**
|
|
|
|
* Update the state of the editor from the node.
|
|
|
|
*/
|
|
|
|
update: function EE_update()
|
|
|
|
{
|
|
|
|
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.
|
|
|
|
for (let i = 0; i < attrs.length; i++) {
|
|
|
|
let attr = this._createAttribute(attrs[i]);
|
|
|
|
if (!attr.inplaceEditor) {
|
|
|
|
attr.style.removeProperty("display");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-06-17 13:52:55 +00:00
|
|
|
_startModifyingAttributes: function() {
|
|
|
|
return this.node.startModifyingAttributes();
|
|
|
|
},
|
|
|
|
|
2013-08-15 14:40:44 +00:00
|
|
|
_createAttribute: function EE_createAttribute(aAttr, aBefore = null)
|
2012-08-23 18:00:43 +00:00
|
|
|
{
|
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"
|
|
|
|
let editValueDisplayed = aAttr.value;
|
|
|
|
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) => {
|
|
|
|
if (!aCommit) {
|
|
|
|
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);
|
|
|
|
this.undo.do(() => {
|
|
|
|
doMods.apply();
|
|
|
|
}, () => {
|
|
|
|
undoMods.apply();
|
|
|
|
})
|
|
|
|
} 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
|
|
|
|
|
|
|
name.textContent = aAttr.name;
|
|
|
|
val.textContent = aAttr.value;
|
|
|
|
|
|
|
|
return attr;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-06-17 13:52:55 +00:00
|
|
|
_applyAttributes: function EE__applyAttributes(aValue, aAttrNode, aDoMods, aUndoMods)
|
2012-08-23 18:00:43 +00:00
|
|
|
{
|
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);
|
2012-08-23 18:00:43 +00:00
|
|
|
|
2013-06-17 13:52:55 +00:00
|
|
|
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-06-17 13:52:55 +00:00
|
|
|
_saveAttribute: function(aName, aUndoMods)
|
2012-08-23 18:00:43 +00:00
|
|
|
{
|
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.
|
|
|
|
*/
|
|
|
|
onTagEdit: function EE_onTagEdit(aVal, aCommit) {
|
2013-06-11 04:18:46 +00:00
|
|
|
if (!aCommit || aVal == this.rawNode.tagName) {
|
2012-08-23 18:00:43 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new element with the same attributes as the
|
|
|
|
// current element and prepare to replace the current node
|
|
|
|
// with it.
|
2012-08-25 18:04:46 +00:00
|
|
|
try {
|
2013-06-11 04:18:46 +00:00
|
|
|
var newElt = nodeDocument(this.rawNode).createElement(aVal);
|
2012-08-25 18:04:46 +00:00
|
|
|
} catch(x) {
|
|
|
|
// Failed to create a new element with that tag name, ignore
|
|
|
|
// the change.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-11 04:18:46 +00:00
|
|
|
let attrs = this.rawNode.attributes;
|
2012-08-23 18:00:43 +00:00
|
|
|
|
|
|
|
for (let i = 0 ; i < attrs.length; i++) {
|
|
|
|
newElt.setAttribute(attrs[i].name, attrs[i].value);
|
|
|
|
}
|
2013-06-11 04:18:46 +00:00
|
|
|
let newFront = this.markup.walker.frontForRawNode(newElt);
|
|
|
|
let newContainer = this.markup.importNode(newFront);
|
|
|
|
|
|
|
|
// Retain the two nodes we care about here so we can undo.
|
|
|
|
let walker = this.markup.walker;
|
|
|
|
promise.all([
|
|
|
|
walker.retainNode(newFront), walker.retainNode(this.node)
|
|
|
|
]).then(() => {
|
|
|
|
function swapNodes(aOld, aNew) {
|
|
|
|
aOld.parentNode.insertBefore(aNew, aOld);
|
|
|
|
while (aOld.firstChild) {
|
|
|
|
aNew.appendChild(aOld.firstChild);
|
|
|
|
}
|
|
|
|
aOld.parentNode.removeChild(aOld);
|
2012-08-25 18:04:46 +00:00
|
|
|
}
|
|
|
|
|
2013-06-11 04:18:46 +00:00
|
|
|
this.undo.do(() => {
|
|
|
|
swapNodes(this.rawNode, newElt);
|
|
|
|
this.markup.setNodeExpanded(newFront, this.container.expanded);
|
|
|
|
if (this.container.selected) {
|
|
|
|
this.markup.navigate(newContainer);
|
|
|
|
}
|
|
|
|
}, () => {
|
|
|
|
swapNodes(newElt, this.rawNode);
|
|
|
|
this.markup.setNodeExpanded(this.node, newContainer.expanded);
|
|
|
|
if (newContainer.selected) {
|
|
|
|
this.markup.navigate(this.container);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}).then(null, console.error);
|
|
|
|
}
|
2012-08-23 18:00:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
RootContainer.prototype = {
|
|
|
|
hasChildren: true,
|
|
|
|
expanded: true,
|
|
|
|
update: function RC_update() {}
|
|
|
|
};
|
|
|
|
|
|
|
|
function nodeDocument(node) {
|
|
|
|
return node.ownerDocument || (node.nodeType == Ci.nsIDOMNode.DOCUMENT_NODE ? node : null);
|
|
|
|
}
|
|
|
|
|
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) {
|
2013-03-21 11:36:52 +00:00
|
|
|
|
2013-08-08 13:55:39 +00:00
|
|
|
attr = attr.trim();
|
2013-03-21 11:36:52 +00:00
|
|
|
|
2013-08-08 13:55:39 +00:00
|
|
|
// Handle bad user inputs by appending a " or ' if it fails to parse without them.
|
|
|
|
let el = DOMParser.parseFromString("<div " + attr + "></div>", "text/html").body.childNodes[0] ||
|
|
|
|
DOMParser.parseFromString("<div " + attr + "\"></div>", "text/html").body.childNodes[0] ||
|
|
|
|
DOMParser.parseFromString("<div " + attr + "'></div>", "text/html").body.childNodes[0];
|
|
|
|
let div = doc.createElement("div");
|
2013-03-21 11:36:52 +00:00
|
|
|
|
2013-08-08 13:55:39 +00:00
|
|
|
let attributes = [];
|
|
|
|
for (let attribute of el.attributes) {
|
|
|
|
// Try to set on an element in the document, throws exception on bad input.
|
|
|
|
// Prevents InvalidCharacterError - "String contains an invalid character".
|
|
|
|
try {
|
|
|
|
div.setAttribute(attribute.name, attribute.value);
|
|
|
|
attributes.push({
|
|
|
|
name: attribute.name,
|
|
|
|
value: attribute.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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A tree walker filter for avoiding empty whitespace text nodes.
|
|
|
|
*/
|
|
|
|
function whitespaceTextFilter(aNode)
|
|
|
|
{
|
|
|
|
if (aNode.nodeType == Ci.nsIDOMNode.TEXT_NODE &&
|
|
|
|
!/[^\s]/.exec(aNode.nodeValue)) {
|
|
|
|
return Ci.nsIDOMNodeFilter.FILTER_SKIP;
|
|
|
|
} else {
|
|
|
|
return Ci.nsIDOMNodeFilter.FILTER_ACCEPT;
|
|
|
|
}
|
|
|
|
}
|
2012-12-20 00:16:45 +00:00
|
|
|
|
2013-04-11 20:59:08 +00:00
|
|
|
loader.lazyGetter(MarkupView.prototype, "strings", () => Services.strings.createBundle(
|
|
|
|
"chrome://browser/locale/devtools/inspector.properties"
|
|
|
|
));
|