Bug 769560 - GCLI should include jsbeautifier in the set of tools it provides to Firefox; r=jwalker

This commit is contained in:
Michael Ratcliffe 2012-07-13 16:28:12 +01:00
parent b7caa04990
commit e51cd331ec
6 changed files with 1593 additions and 0 deletions

View File

@ -7,6 +7,9 @@ let EXPORTED_SYMBOLS = [ "GcliCommands" ];
const { classes: Cc, interfaces: Ci, utils: Cu } = Components;
const XMLHttpRequest =
Components.Constructor("@mozilla.org/xmlextras/xmlhttprequest;1");
Cu.import("resource:///modules/devtools/gcli.jsm");
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/Services.jsm");
@ -26,6 +29,9 @@ XPCOMUtils.defineLazyModuleGetter(this, "console",
XPCOMUtils.defineLazyModuleGetter(this, "AddonManager",
"resource://gre/modules/AddonManager.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "js_beautify",
"resource:///modules/devtools/Jsbeautify.jsm");
XPCOMUtils.defineLazyGetter(this, "Debugger", function() {
let JsDebugger = {};
Components.utils.import("resource://gre/modules/jsdebugger.jsm", JsDebugger);
@ -1415,3 +1421,125 @@ AddonManager.getAllAddons(function addonAsync(aAddons) {
exec: gcli_cmd_resize
});
})();
/**
* jsb command.
*/
gcli.addCommand({
name: 'jsb',
description: gcli.lookup('jsbDesc'),
returnValue:'string',
params: [
{
name: 'url',
type: 'string',
description: gcli.lookup('jsbUrlDesc'),
manual: 'The URL of the JS to prettify'
},
{
name: 'indentSize',
type: 'number',
description: gcli.lookup('jsbIndentSizeDesc'),
manual: gcli.lookup('jsbIndentSizeManual'),
defaultValue: 2
},
{
name: 'indentChar',
type: {
name: 'selection',
lookup: [{name: "space", value: " "}, {name: "tab", value: "\t"}]
},
description: gcli.lookup('jsbIndentCharDesc'),
manual: gcli.lookup('jsbIndentCharManual'),
defaultValue: ' ',
},
{
name: 'preserveNewlines',
type: 'boolean',
description: gcli.lookup('jsbPreserveNewlinesDesc'),
manual: gcli.lookup('jsbPreserveNewlinesManual'),
defaultValue: true
},
{
name: 'preserveMaxNewlines',
type: 'number',
description: gcli.lookup('jsbPreserveMaxNewlinesDesc'),
manual: gcli.lookup('jsbPreserveMaxNewlinesManual'),
defaultValue: -1
},
{
name: 'jslintHappy',
type: 'boolean',
description: gcli.lookup('jsbJslintHappyDesc'),
manual: gcli.lookup('jsbJslintHappyManual'),
defaultValue: false
},
{
name: 'braceStyle',
type: {
name: 'selection',
data: ['collapse', 'expand', 'end-expand', 'expand-strict']
},
description: gcli.lookup('jsbBraceStyleDesc'),
manual: gcli.lookup('jsbBraceStyleManual'),
defaultValue: "collapse"
},
{
name: 'spaceBeforeConditional',
type: 'boolean',
description: gcli.lookup('jsbSpaceBeforeConditionalDesc'),
manual: gcli.lookup('jsbSpaceBeforeConditionalManual'),
defaultValue: true
},
{
name: 'unescapeStrings',
type: 'boolean',
description: gcli.lookup('jsbUnescapeStringsDesc'),
manual: gcli.lookup('jsbUnescapeStringsManual'),
defaultValue: false
}
],
exec: function(args, context) {
let opts = {
indent_size: args.indentSize,
indent_char: args.indentChar,
preserve_newlines: args.preserveNewlines,
max_preserve_newlines: args.preserveMaxNewlines == -1 ?
undefined : args.preserveMaxNewlines,
jslint_happy: args.jslintHappy,
brace_style: args.braceStyle,
space_before_conditional: args.spaceBeforeConditional,
unescape_strings: args.unescapeStrings
}
let xhr = new XMLHttpRequest();
try {
xhr.open("GET", args.url, true);
} catch(e) {
return gcli.lookup('jsbInvalidURL');
}
let promise = context.createPromise();
xhr.onreadystatechange = function(aEvt) {
if (xhr.readyState == 4) {
if (xhr.status == 200 || xhr.status == 0) {
let browserDoc = context.environment.chromeDocument;
let browserWindow = browserDoc.defaultView;
let browser = browserWindow.gBrowser;
browser.selectedTab = browser.addTab("data:text/plain;base64," +
browserWindow.btoa(js_beautify(xhr.responseText, opts)));
promise.resolve();
}
else {
promise.resolve("Unable to load page to beautify: " + args.url + " " +
xhr.status + " " + xhr.statusText);
}
};
}
xhr.send(null);
return promise;
}
});

View File

@ -21,6 +21,7 @@ MOCHITEST_BROWSER_FILES = \
browser_gcli_edit.js \
browser_gcli_inspect.js \
browser_gcli_integrate.js \
browser_gcli_jsb.js \
browser_gcli_pagemod_export.js \
browser_gcli_pref.js \
browser_gcli_responsivemode.js \
@ -37,6 +38,7 @@ MOCHITEST_BROWSER_FILES += \
resources_inpage.js \
resources_inpage1.css \
resources_inpage2.css \
resources_jsb_script.js \
resources.html \
$(NULL)

View File

@ -0,0 +1,45 @@
function test() {
const TEST_URI = "http://example.com/browser/browser/devtools/commandline/" +
"test/resources_jsb_script.js";
DeveloperToolbarTest.test("about:blank", function GJT_test() {
DeveloperToolbarTest.exec({
typed: "jsb AAA",
outputMatch: /valid/
});
gBrowser.addTabsProgressListener({
onProgressChange: function GJT_onProgressChange(aBrowser) {
gBrowser.removeTabsProgressListener(this);
let win = aBrowser._contentWindow;
let uri = win.document.location.href;
let result = win.atob(uri.replace(/.*,/, ""));
result = result.replace(/[\r\n]]/g, "\n");
checkResult(result);
finish();
}
});
info("Checking beautification");
DeveloperToolbarTest.checkInputStatus({
typed: "jsb " + TEST_URI + " 4 space true -1 false collapse true false",
status: "VALID"
});
DeveloperToolbarTest.exec({ completed: false });
function checkResult(aResult) {
let correct = "function somefunc() {\n" +
" for (let n = 0; n < 500; n++) {\n" +
" if (n % 2 == 1) {\n" +
" console.log(n);\n" +
" console.log(n + 1);\n" +
" }\n" +
" }\n" +
"}";
is(aResult, correct, "JS has been correctly prettified");
}
});
}

View File

@ -0,0 +1 @@
function somefunc(){for(let n=0;n<500;n++){if(n%2==1){console.log(n);console.log(n+1);}}}

File diff suppressed because it is too large Load Diff

View File

@ -728,6 +728,106 @@ cookieSetDomainDesc=The domain of the cookie to set
# when the user is using this command.
cookieSetSecureDesc=Only transmitted over https
# LOCALIZATION NOTE (jsbDesc) A very short description of the
# 'jsb' command. This string is designed to be shown in a menu
# alongside the command name, which is why it should be as short as possible.
jsbDesc=Javascript beautifier
# LOCALIZATION NOTE (jsbUrlDesc) A very short description of the
# 'jsb <url>' parameter. This string is designed to be shown in a menu
# alongside the command name, which is why it should be as short as possible.
jsbUrlDesc=The URL of the JS file to prettify
# LOCALIZATION NOTE (jsbIndentSizeDesc) A very short description of the
# 'jsb <indentSize>' parameter. This string is designed to be shown in a menu
# alongside the command name, which is why it should be as short as possible.
jsbIndentSizeDesc=Indentation size in chars
# LOCALIZATION NOTE (jsbIndentSizeManual) A fuller description of the
# 'jsb <indentChar>' parameter, displayed when the user asks for help on what it
# does.
jsbIndentSizeManual=The number of chars with which to indent each line
# LOCALIZATION NOTE (jsbIndentCharDesc) A very short description of the
# 'jsb <indentChar>' parameter. This string is designed to be shown in a menu
# alongside the command name, which is why it should be as short as possible.
jsbIndentCharDesc=The chars used to indent each line
# LOCALIZATION NOTE (jsbIndentCharManual) A fuller description of the
# 'jsb <indentChar>' parameter, displayed when the user asks for help on what it
# does.
jsbIndentCharManual=The chars used to indent each line, the possible choices are space or tab.
# LOCALIZATION NOTE (jsbPreserveNewlinesDesc) A very short description of the
# 'jsb <jsbPreserveNewlines>' parameter. This string is designed to be shown
# in a menu alongside the command name, which is why it should be as short as
# possible.
jsbPreserveNewlinesDesc=Keep existing line breaks?
# LOCALIZATION NOTE (jsbPreserveNewlinesManual) A fuller description of the
# 'jsb <jsbPreserveNewlines>' parameter, displayed when the user asks for help
# on what it does.
jsbPreserveNewlinesManual=Should existing line breaks be preserved
# LOCALIZATION NOTE (jsbPreserveMaxNewlinesDesc) A very short description of the
# 'jsb <preserveMaxNewlines>' parameter. This string is designed to be shown
# in a menu alongside the command name, which is why it should be as short as
# possible.
jsbPreserveMaxNewlinesDesc=Max consecutive line breaks
# LOCALIZATION NOTE (jsbPreserveMaxNewlinesManual) A fuller description of the
# 'jsb <preserveMaxNewlines>' parameter, displayed when the user asks for help
# on what it does.
jsbPreserveMaxNewlinesManual=The maximum number of consecutive line breaks to preserve
# LOCALIZATION NOTE (jsbJslintHappyDesc) A very short description of the
# 'jsb <jslintHappy>' parameter. This string is designed to be shown
# in a menu alongside the command name, which is why it should be as short as
# possible.
jsbJslintHappyDesc=Enforce jslint-stricter mode?
# LOCALIZATION NOTE (jsbJslintHappyManual) A fuller description of the
# 'jsb <jslintHappy>' parameter, displayed when the user asks for help
# on what it does.
jsbJslintHappyManual=When set to true, jslint-stricter mode is enforced
# LOCALIZATION NOTE (jsbBraceStyleDesc) A very short description of the
# 'jsb <braceStyle>' parameter. This string is designed to be shown
# in a menu alongside the command name, which is why it should be as short as
# possible.
jsbBraceStyleDesc=Collapse, expand, end-expand, expand-strict
# LOCALIZATION NOTE (jsbBraceStyleManual) A fuller description of the
# 'jsb <braceStyle>' parameter, displayed when the user asks for help
# on what it does.
jsbBraceStyleManual=The coding style of braces. Either collapse, expand, end-expand or expand-strict
# LOCALIZATION NOTE (jsbSpaceBeforeConditionalDesc) A very short description of
# the 'jsb <spaceBeforeConditional>' parameter. This string is designed to be
# shown in a menu alongside the command name, which is why it should be as short
# as possible.
jsbSpaceBeforeConditionalDesc=Space before if statements?
# LOCALIZATION NOTE (jsbSpaceBeforeConditionalManual) A fuller description of
# the 'jsb <spaceBeforeConditional>' parameter, displayed when the user asks for
# help on what it does.
jsbSpaceBeforeConditionalManual=Should a space be added before conditional statements?
# LOCALIZATION NOTE (jsbUnescapeStringsDesc) A very short description of the
# 'jsb <unescapeStrings>' parameter. This string is designed to be shown
# in a menu alongside the command name, which is why it should be as short as
# possible.
jsbUnescapeStringsDesc=Unescape \\xNN characters?
# LOCALIZATION NOTE (jsbUnescapeStringsManual) A fuller description of the
# 'jsb <unescapeStrings>' parameter, displayed when the user asks for help
# on what it does.
jsbUnescapeStringsManual=Should printable characters in strings encoded in \\xNN notation be unescaped?
# LOCALIZATION NOTE (jsbInvalidURL) Displayed when an invalid URL is passed to
# the jsb command.
jsbInvalidURL=Please enter a valid URL
# LOCALIZATION NOTE (calllogDesc) A very short description of the
# 'calllog' command. This string is designed to be shown in a menu
# alongside the command name, which is why it should be as short as possible.