Bug 575789 - JSTerm: helper functions, r=sdwilsh, a=blocking2.0

This commit is contained in:
Julian Viereck 2010-08-31 14:31:48 -03:00
Родитель 13e27f720f
Коммит 82da26d250
5 изменённых файлов: 221 добавлений и 3 удалений

Просмотреть файл

@ -79,6 +79,11 @@ XPCOMUtils.defineLazyGetter(this, "PropertyPanel", function () {
return obj.PropertyPanel;
});
XPCOMUtils.defineLazyGetter(this, "namesAndValuesOf", function () {
var obj = {};
Cu.import("resource://gre/modules/PropertyPanel.jsm", obj);
return obj.namesAndValuesOf;
});
function LogFactory(aMessagePrefix)
{
@ -3474,6 +3479,171 @@ function JSPropertyProvider(aScope, aInputValue)
// JSTerm
//////////////////////////////////////////////////////////////////////////
/**
* JSTermHelper
*
* Defines a set of functions ("helper functions") that are available from the
* WebConsole but not from the webpage.
* A list of helper functions used by Firebug can be found here:
* http://getfirebug.com/wiki/index.php/Command_Line_API
*/
function JSTermHelper(aJSTerm)
{
return {
/**
* Returns the result of document.getElementById(aId).
*
* @param string aId
* A string that is passed to window.document.getElementById.
* @returns nsIDOMNode or null
*/
$: function JSTH_$(aId)
{
try {
return aJSTerm._window.document.getElementById(aId);
}
catch (ex) {
aJSTerm.console.error(ex.message);
}
},
/**
* Returns the result of document.querySelectorAll(aSelector).
*
* @param string aSelector
* A string that is passed to window.document.querySelectorAll.
* @returns array of nsIDOMNode
*/
$$: function JSTH_$$(aSelector)
{
try {
return aJSTerm._window.document.querySelectorAll(aSelector);
}
catch (ex) {
aJSTerm.console.error(ex.message);
}
},
/**
* Runs a xPath query and returns all matched nodes.
*
* @param string aXPath
* xPath search query to execute.
* @param [optional] nsIDOMNode aContext
* Context to run the xPath query on. Uses window.document if not set.
* @returns array of nsIDOMNode
*/
$x: function JSTH_$x(aXPath, aContext)
{
let nodes = [];
let doc = aJSTerm._window.wrappedJSObject.document;
let aContext = aContext || doc;
try {
let results = doc.evaluate(aXPath, aContext, null,
Ci.nsIDOMXPathResult.ANY_TYPE, null);
let node;
while (node = results.iterateNext()) {
nodes.push(node);
}
}
catch (ex) {
aJSTerm.console.error(ex.message);
}
return nodes;
},
/**
* Clears the output of the JSTerm.
*/
clear: function JSTH_clear()
{
aJSTerm.clearOutput();
},
/**
* Returns the result of Object.keys(aObject).
*
* @param object aObject
* Object to return the property names from.
* @returns array of string
*/
keys: function JSTH_keys(aObject)
{
try {
return Object.keys(XPCNativeWrapper.unwrap(aObject));
}
catch (ex) {
aJSTerm.console.error(ex.message);
}
},
/**
* Returns the values of all properties on aObject.
*
* @param object aObject
* Object to display the values from.
* @returns array of string
*/
values: function JSTH_values(aObject)
{
let arrValues = [];
let obj = XPCNativeWrapper.unwrap(aObject);
try {
for (let prop in obj) {
arrValues.push(obj[prop]);
}
}
catch (ex) {
aJSTerm.console.error(ex.message);
}
return arrValues;
},
/**
* Inspects the passed aObject. This is done by opening the PropertyPanel.
*
* @param object aObject
* Object to inspect.
* @returns void
*/
inspect: function JSTH_inspect(aObject)
{
let obj = XPCNativeWrapper.unwrap(aObject);
aJSTerm.openPropertyPanel(null, obj);
},
/**
* Prints aObject to the output.
*
* @param object aObject
* Object to print to the output.
* @returns void
*/
pprint: function JSTH_pprint(aObject)
{
if (aObject === null || aObject === undefined || aObject === true || aObject === false) {
aJSTerm.console.error(HUDService.getStr("helperFuncUnsupportedTypeError"));
return;
}
let output = [];
if (typeof aObject != "string") {
aObject = XPCNativeWrapper.unwrap(aObject);
}
let pairs = namesAndValuesOf(aObject);
pairs.forEach(function(pair) {
output.push(" " + pair.display);
});
aJSTerm.writeOutput(output.join("\n"));
}
}
}
/**
* JSTerm
*
@ -3562,6 +3732,7 @@ JSTerm.prototype = {
this.sandbox = new Cu.Sandbox(this._window);
this.sandbox.window = this._window;
this.sandbox.console = this.console;
this.sandbox.__helperFunctions__ = JSTermHelper(this);
this.sandbox.__proto__ = this._window.wrappedJSObject;
},
@ -3580,7 +3751,7 @@ JSTerm.prototype = {
*/
evalInSandbox: function JST_evalInSandbox(aString)
{
let execStr = "with(window) {" + aString + "}";
let execStr = "with(__helperFunctions__) { with(window) {" + aString + "} }";
return Cu.evalInSandbox(execStr, this.sandbox, "default", "HUD Console", 1);
},

Просмотреть файл

@ -44,7 +44,7 @@ const Cu = Components.utils;
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/Services.jsm");
var EXPORTED_SYMBOLS = ["PropertyPanel", "PropertyTreeView"];
var EXPORTED_SYMBOLS = ["PropertyPanel", "PropertyTreeView", "namesAndValuesOf"];
///////////////////////////////////////////////////////////////////////////
//// Helper for PropertyTreeView

Просмотреть файл

@ -1049,6 +1049,51 @@ function testExecutionScope()
"command was executed in the window scope");
}
function testJSTermHelper()
{
content.location.href = TEST_URI;
let HUD = HUDService.hudWeakReferences[hudId].get();
let jsterm = HUD.jsterm;
jsterm.clearOutput();
jsterm.execute("'id=' + $('header').getAttribute('id')");
let group = jsterm.outputNode.querySelector(".hud-group");
is(group.childNodes[2].textContent, "id=header", "$() worked");
jsterm.clearOutput();
jsterm.execute("headerQuery = $$('h1')");
jsterm.execute("'length=' + headerQuery.length");
let group = jsterm.outputNode.querySelector(".hud-group");
is(group.childNodes[4].textContent, "length=1", "$$() worked");
jsterm.clearOutput();
jsterm.execute("xpathQuery = $x('.//*', document.body);");
jsterm.execute("'headerFound=' + (xpathQuery[0] == headerQuery[0])");
let group = jsterm.outputNode.querySelector(".hud-group");
is(group.childNodes[4].textContent, "headerFound=true", "$x() worked");
// no jsterm.clearOutput() here as we clear the output using the clear() fn.
jsterm.execute("clear()");
let group = jsterm.outputNode.querySelector(".hud-group");
is(group.childNodes[1].textContent, "undefined", "clear() worked");
jsterm.clearOutput();
jsterm.execute("'keysResult=' + (keys({b:1})[0] == 'b')");
let group = jsterm.outputNode.querySelector(".hud-group");
is(group.childNodes[2].textContent, "keysResult=true", "keys() worked");
jsterm.clearOutput();
jsterm.execute("'valuesResult=' + (values({b:1})[0] == 1)");
let group = jsterm.outputNode.querySelector(".hud-group");
is(group.childNodes[2].textContent, "valuesResult=true", "values() worked");
jsterm.clearOutput();
jsterm.execute("pprint({b:2, a:1})");
let group = jsterm.outputNode.querySelector(".hud-group");
is(group.childNodes[2].textContent, " a: 1\n b: 2", "pprint() worked");
}
function testPropertyPanel()
{
var HUD = HUDService.hudWeakReferences[hudId].get();
@ -1377,6 +1422,7 @@ function test() {
testPropertyProvider();
testJSInputExpand();
testPropertyPanel();
testJSTermHelper();
// NOTE: Put any sync test above this comment.
//

Просмотреть файл

@ -16,7 +16,7 @@
</script>
</head>
<body>
<h1>Heads Up Display Demo</h1>
<h1 id="header">Heads Up Display Demo</h1>
<button onclick="test();">Log stuff about Dolske</button>
</body>
</html>

Просмотреть файл

@ -63,6 +63,7 @@ jsPropertyTitle=Object Inspector
jsPropertyInspectTitle=Inspect: %S
copyCmd.label=Copy
copyCmd.accesskey=C
helperFuncUnsupportedTypeError=Can't call pprint on this type of object.
# LOCALIZATION NOTE (networkUrlWithStatus):
#
# When the HTTP request is started only the URL of the request is printed to the