2011-08-30 05:12:02 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=2 et sw=2 tw=80: */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is the Mozilla Inspector Module.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* The Mozilla Foundation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2011
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
2011-09-26 09:46:44 -07:00
|
|
|
* Mike Ratcliffe <mratcliffe@mozilla.com> (Original Author)
|
|
|
|
* Rob Campbell <rcampbell@mozilla.com>
|
2012-04-19 11:04:46 -07:00
|
|
|
* Dave Camp <dcamp@mozilla.com>
|
2011-08-30 05:12:02 -07:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
const Cu = Components.utils;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
2012-04-19 11:04:46 -07:00
|
|
|
Cu.import("resource:///modules/devtools/CssRuleView.jsm");
|
|
|
|
Cu.import("resource:///modules/inspector.jsm");
|
2011-08-30 05:12:02 -07:00
|
|
|
|
2012-04-19 11:04:46 -07:00
|
|
|
// This module doesn't currently export any symbols directly, it only
|
|
|
|
// registers inspector tools.
|
|
|
|
var EXPORTED_SYMBOLS = [];
|
2011-08-30 05:12:02 -07:00
|
|
|
|
2011-10-21 10:40:22 -07:00
|
|
|
/**
|
2012-04-19 11:04:46 -07:00
|
|
|
* Lookup l10n string from a string bundle.
|
|
|
|
* @param {string} aName The key to lookup.
|
|
|
|
* @returns A localized version of the given key.
|
2011-10-21 10:40:22 -07:00
|
|
|
*/
|
2012-04-19 11:04:46 -07:00
|
|
|
function l10n(aName)
|
2011-10-21 10:40:22 -07:00
|
|
|
{
|
2012-04-19 11:04:46 -07:00
|
|
|
try {
|
|
|
|
return _strings.GetStringFromName(aName);
|
|
|
|
} catch (ex) {
|
|
|
|
Services.console.logStringMessage("Error reading '" + aName + "'");
|
|
|
|
throw new Error("l10n error with " + aName);
|
|
|
|
}
|
2012-03-11 07:01:38 -07:00
|
|
|
}
|
2011-10-21 10:40:22 -07:00
|
|
|
|
2012-04-19 11:04:46 -07:00
|
|
|
function RegisterStyleTools()
|
|
|
|
{
|
|
|
|
// Register the rules view
|
|
|
|
if (Services.prefs.getBoolPref("devtools.ruleview.enabled")) {
|
|
|
|
InspectorUI.registerSidebar({
|
|
|
|
id: "ruleview",
|
|
|
|
label: l10n("ruleView.label"),
|
|
|
|
tooltiptext: l10n("ruleView.tooltiptext"),
|
|
|
|
accesskey: l10n("ruleView.accesskey"),
|
|
|
|
contentURL: "chrome://browser/content/devtools/cssruleview.xul",
|
|
|
|
load: function(aInspector, aFrame) new RuleViewTool(aInspector, aFrame),
|
|
|
|
destroy: function(aContext) aContext.destroy()
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register the computed styles view
|
|
|
|
if (Services.prefs.getBoolPref("devtools.styleinspector.enabled")) {
|
|
|
|
InspectorUI.registerSidebar({
|
|
|
|
id: "computedview",
|
|
|
|
label: this.l10n("style.highlighter.button.label2"),
|
|
|
|
tooltiptext: this.l10n("style.highlighter.button.tooltip2"),
|
|
|
|
accesskey: this.l10n("style.highlighter.accesskey2"),
|
|
|
|
contentURL: "chrome://browser/content/devtools/csshtmltree.xul",
|
|
|
|
load: function(aInspector, aFrame) new ComputedViewTool(aInspector, aFrame),
|
|
|
|
destroy: function(aContext) aContext.destroy()
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2011-10-21 10:40:22 -07:00
|
|
|
|
2012-04-19 11:04:46 -07:00
|
|
|
function RuleViewTool(aInspector, aFrame)
|
|
|
|
{
|
|
|
|
this.inspector = aInspector;
|
|
|
|
this.chromeWindow = this.inspector.chromeWindow;
|
|
|
|
this.doc = aFrame.contentDocument;
|
2012-04-19 11:13:42 -07:00
|
|
|
|
|
|
|
if (!this.inspector._ruleViewStore) {
|
|
|
|
this.inspector._ruleViewStore = {};
|
|
|
|
}
|
|
|
|
this.view = new CssRuleView(this.doc, this.inspector._ruleViewStore);
|
2012-04-19 11:04:46 -07:00
|
|
|
this.doc.documentElement.appendChild(this.view.element);
|
|
|
|
|
|
|
|
this._changeHandler = function() {
|
|
|
|
this.inspector.markDirty();
|
|
|
|
this.inspector.change("ruleview");
|
|
|
|
}.bind(this);
|
|
|
|
|
|
|
|
this.view.element.addEventListener("CssRuleViewChanged", this._changeHandler)
|
|
|
|
|
|
|
|
this._cssLinkHandler = function(aEvent) {
|
|
|
|
let rule = aEvent.detail.rule;
|
|
|
|
let styleSheet = rule.sheet;
|
|
|
|
let doc = this.chromeWindow.content.document;
|
|
|
|
let styleSheets = doc.styleSheets;
|
|
|
|
let contentSheet = false;
|
|
|
|
let line = rule.ruleLine || 0;
|
|
|
|
|
|
|
|
// Array.prototype.indexOf always returns -1 here so we loop through
|
|
|
|
// the styleSheets object instead.
|
|
|
|
for each (let sheet in styleSheets) {
|
|
|
|
if (sheet == styleSheet) {
|
|
|
|
contentSheet = true;
|
|
|
|
break;
|
|
|
|
}
|
2011-10-21 10:40:22 -07:00
|
|
|
}
|
2011-08-30 05:12:02 -07:00
|
|
|
|
2012-04-19 11:04:46 -07:00
|
|
|
if (contentSheet) {
|
|
|
|
this.chromeWindow.StyleEditor.openChrome(styleSheet, line);
|
|
|
|
} else {
|
|
|
|
let href = styleSheet ? styleSheet.href : "";
|
|
|
|
if (rule.elementStyle.element) {
|
|
|
|
href = rule.elementStyle.element.ownerDocument.location.href;
|
2011-11-03 06:37:14 -07:00
|
|
|
}
|
2012-04-19 11:04:46 -07:00
|
|
|
let viewSourceUtils = this.chromeWindow.gViewSourceUtils;
|
|
|
|
viewSourceUtils.viewSource(href, null, doc, line);
|
|
|
|
}
|
|
|
|
}.bind(this);
|
2011-11-03 06:37:14 -07:00
|
|
|
|
2012-04-19 11:04:46 -07:00
|
|
|
this.view.element.addEventListener("CssRuleViewCSSLinkClicked",
|
|
|
|
this._cssLinkHandler);
|
2011-11-03 06:37:14 -07:00
|
|
|
|
2012-04-19 11:04:46 -07:00
|
|
|
this._onSelect = this.onSelect.bind(this);
|
|
|
|
this.inspector.on("select", this._onSelect);
|
2011-11-03 06:37:14 -07:00
|
|
|
|
2012-04-19 11:04:46 -07:00
|
|
|
this._onChange = this.onChange.bind(this);
|
|
|
|
this.inspector.on("change", this._onChange);
|
2011-10-21 10:40:22 -07:00
|
|
|
|
2012-04-19 11:04:46 -07:00
|
|
|
this.onSelect();
|
|
|
|
}
|
2011-09-01 05:17:05 -07:00
|
|
|
|
2012-04-19 11:04:46 -07:00
|
|
|
RuleViewTool.prototype = {
|
|
|
|
onSelect: function RVT_onSelect(aEvent, aFrom) {
|
|
|
|
let node = this.inspector.selection;
|
|
|
|
if (!node) {
|
|
|
|
this.view.highlight(null);
|
|
|
|
return;
|
2011-09-01 05:17:05 -07:00
|
|
|
}
|
2011-08-30 05:12:02 -07:00
|
|
|
|
2012-04-19 11:04:46 -07:00
|
|
|
if (this.inspector.locked) {
|
|
|
|
this.view.highlight(node);
|
2011-10-21 10:40:22 -07:00
|
|
|
}
|
|
|
|
},
|
2011-08-30 05:12:02 -07:00
|
|
|
|
2012-04-19 11:04:46 -07:00
|
|
|
onChange: function RVT_onChange(aEvent, aFrom) {
|
|
|
|
// We're not that good yet at refreshing, only
|
|
|
|
// refresh when we really need to.
|
|
|
|
if (aFrom != "pseudoclass") {
|
|
|
|
return;
|
2011-10-21 10:40:22 -07:00
|
|
|
}
|
2011-10-07 08:38:35 -07:00
|
|
|
|
2012-04-19 11:04:46 -07:00
|
|
|
if (this.inspector.locked) {
|
|
|
|
this.view.nodeChanged();
|
2011-11-06 19:02:08 -08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-04-19 11:04:46 -07:00
|
|
|
destroy: function RVT_destroy() {
|
|
|
|
this.inspector.removeListener("select", this._onSelect);
|
|
|
|
this.inspector.removeListener("change", this._onChange);
|
|
|
|
this.view.element.removeEventListener("CssRuleViewChanged",
|
|
|
|
this._changeHandler);
|
|
|
|
this.view.element.removeEventListener("CssRuleViewCSSLinkClicked",
|
|
|
|
this._cssLinkHandler);
|
|
|
|
this.doc.documentElement.removeChild(this.view.element);
|
2011-08-30 05:12:02 -07:00
|
|
|
|
2012-04-19 11:04:46 -07:00
|
|
|
this.view.destroy();
|
|
|
|
|
|
|
|
delete this._changeHandler;
|
|
|
|
delete this.view;
|
|
|
|
delete this.doc;
|
|
|
|
delete this.inspector;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function ComputedViewTool(aInspector, aFrame)
|
|
|
|
{
|
|
|
|
this.inspector = aInspector;
|
|
|
|
this.iframe = aFrame;
|
|
|
|
this.window = aInspector.chromeWindow;
|
|
|
|
this.document = this.window.document;
|
|
|
|
this.cssLogic = new CssLogic();
|
|
|
|
this.view = new CssHtmlTree(this);
|
2011-10-21 10:40:22 -07:00
|
|
|
|
2012-04-19 11:04:46 -07:00
|
|
|
this._onSelect = this.onSelect.bind(this);
|
|
|
|
this.inspector.on("select", this._onSelect);
|
|
|
|
|
|
|
|
this._onChange = this.onChange.bind(this);
|
|
|
|
this.inspector.on("change", this._onChange);
|
|
|
|
|
|
|
|
// Since refreshes of the computed view are non-destructive,
|
|
|
|
// refresh when the tab is changed so we can notice script-driven
|
|
|
|
// changes.
|
|
|
|
this.inspector.on("sidebaractivated", this._onChange);
|
|
|
|
|
|
|
|
this.cssLogic.highlight(null);
|
|
|
|
this.view.highlight(null);
|
|
|
|
|
|
|
|
this.onSelect();
|
|
|
|
}
|
|
|
|
|
|
|
|
ComputedViewTool.prototype = {
|
|
|
|
onSelect: function CVT_onSelect(aEvent)
|
2011-10-21 10:40:22 -07:00
|
|
|
{
|
2012-04-19 11:04:46 -07:00
|
|
|
if (this.inspector.locked) {
|
|
|
|
this.cssLogic.highlight(this.inspector.selection);
|
|
|
|
this.view.highlight(this.inspector.selection);
|
2011-11-03 06:37:14 -07:00
|
|
|
}
|
2011-08-30 05:12:02 -07:00
|
|
|
},
|
|
|
|
|
2012-04-19 11:04:46 -07:00
|
|
|
onChange: function CVT_change(aEvent, aFrom)
|
2011-08-30 05:12:02 -07:00
|
|
|
{
|
2012-04-19 11:04:46 -07:00
|
|
|
if (aFrom == "computedview" ||
|
|
|
|
this.inspector.selection != this.cssLogic.viewedElement) {
|
|
|
|
return;
|
2011-08-30 05:12:02 -07:00
|
|
|
}
|
2012-04-19 11:04:46 -07:00
|
|
|
|
|
|
|
this.cssLogic.highlight(this.inspector.selection);
|
|
|
|
this.view.refreshPanel();
|
2011-08-30 05:12:02 -07:00
|
|
|
},
|
2011-11-03 06:37:14 -07:00
|
|
|
|
2012-04-19 11:04:46 -07:00
|
|
|
destroy: function CVT_destroy(aContext)
|
2011-11-03 06:37:14 -07:00
|
|
|
{
|
2012-04-19 11:04:46 -07:00
|
|
|
this.inspector.removeListener("select", this._onSelect);
|
|
|
|
this.inspector.removeListener("change", this._onChange);
|
|
|
|
this.inspector.removeListener("sidebaractivated", this._onChange);
|
|
|
|
this.view.destroy();
|
|
|
|
delete this.view;
|
2011-11-03 06:37:14 -07:00
|
|
|
|
|
|
|
delete this.cssLogic;
|
|
|
|
delete this.cssHtmlTree;
|
2012-04-19 11:04:46 -07:00
|
|
|
delete this.iframe;
|
|
|
|
delete this.window;
|
|
|
|
delete this.document;
|
|
|
|
}
|
|
|
|
}
|
2011-08-30 05:12:02 -07:00
|
|
|
|
|
|
|
XPCOMUtils.defineLazyGetter(this, "_strings", function() Services.strings
|
2011-11-03 06:37:14 -07:00
|
|
|
.createBundle("chrome://browser/locale/devtools/styleinspector.properties"));
|
2011-08-30 05:12:02 -07:00
|
|
|
|
|
|
|
XPCOMUtils.defineLazyGetter(this, "CssLogic", function() {
|
|
|
|
let tmp = {};
|
|
|
|
Cu.import("resource:///modules/devtools/CssLogic.jsm", tmp);
|
|
|
|
return tmp.CssLogic;
|
|
|
|
});
|
|
|
|
|
|
|
|
XPCOMUtils.defineLazyGetter(this, "CssHtmlTree", function() {
|
|
|
|
let tmp = {};
|
|
|
|
Cu.import("resource:///modules/devtools/CssHtmlTree.jsm", tmp);
|
|
|
|
return tmp.CssHtmlTree;
|
|
|
|
});
|
2012-04-19 11:04:46 -07:00
|
|
|
|
|
|
|
RegisterStyleTools();
|