зеркало из https://github.com/mozilla/gecko-dev.git
Bug 644413 - Add support for -sp-context mode-line in Scratchpad. r=harth
This commit is contained in:
Родитель
4321cc6a9f
Коммит
1d1c919b9c
|
@ -46,6 +46,41 @@ var Scratchpad = {
|
|||
_instanceId: null,
|
||||
_initialWindowTitle: document.title,
|
||||
|
||||
/**
|
||||
* Check if provided string is a mode-line and, if it is, return an
|
||||
* object with its values.
|
||||
*
|
||||
* @param string aLine
|
||||
* @return string
|
||||
*/
|
||||
_scanModeLine: function SP__scanModeLine(aLine="")
|
||||
{
|
||||
aLine = aLine.trim();
|
||||
|
||||
let obj = {};
|
||||
let ch1 = aLine.charAt(0);
|
||||
let ch2 = aLine.charAt(1);
|
||||
|
||||
if (ch1 !== "/" || (ch2 !== "*" && ch2 !== "/")) {
|
||||
return obj;
|
||||
}
|
||||
|
||||
aLine = aLine
|
||||
.replace(/^\/\//, "")
|
||||
.replace(/^\/\*/, "")
|
||||
.replace(/\*\/$/, "");
|
||||
|
||||
aLine.split(",").forEach(function (pair) {
|
||||
let [key, val] = pair.split(":");
|
||||
|
||||
if (key && val) {
|
||||
obj[key.trim()] = val.trim();
|
||||
}
|
||||
});
|
||||
|
||||
return obj;
|
||||
},
|
||||
|
||||
/**
|
||||
* The script execution context. This tells Scratchpad in which context the
|
||||
* script shall execute.
|
||||
|
@ -660,6 +695,15 @@ var Scratchpad = {
|
|||
content = NetUtil.readInputStreamToString(aInputStream,
|
||||
aInputStream.available());
|
||||
content = converter.ConvertToUnicode(content);
|
||||
|
||||
// Check to see if the first line is a mode-line comment.
|
||||
let line = content.split("\n")[0];
|
||||
let modeline = self._scanModeLine(line);
|
||||
|
||||
if (modeline["-sp-context"] === "browser") {
|
||||
self.setBrowserContext();
|
||||
}
|
||||
|
||||
self.setText(content);
|
||||
self.editor.resetUndo();
|
||||
}
|
||||
|
|
|
@ -36,6 +36,7 @@ MOCHITEST_BROWSER_FILES = \
|
|||
browser_scratchpad_bug_751744_revert_to_saved.js \
|
||||
browser_scratchpad_bug740948_reload_and_run.js \
|
||||
browser_scratchpad_bug_661762_wrong_window_focus.js \
|
||||
browser_scratchpad_bug_644413_modeline.js \
|
||||
head.js \
|
||||
|
||||
include $(topsrcdir)/config/rules.mk
|
||||
|
|
|
@ -0,0 +1,82 @@
|
|||
/* vim: set ts=2 et sw=2 tw=80: */
|
||||
/* Any copyright is dedicated to the Public Domain.
|
||||
http://creativecommons.org/publicdomain/zero/1.0/ */
|
||||
|
||||
let tempScope = {};
|
||||
Cu.import("resource://gre/modules/NetUtil.jsm", tempScope);
|
||||
Cu.import("resource://gre/modules/FileUtils.jsm", tempScope);
|
||||
let NetUtil = tempScope.NetUtil;
|
||||
let FileUtils = tempScope.FileUtils;
|
||||
|
||||
// Reference to the Scratchpad object.
|
||||
let gScratchpad;
|
||||
|
||||
// Reference to the temporary nsIFile we will work with.
|
||||
let gFile;
|
||||
|
||||
// The temporary file content.
|
||||
let gFileContent = "function main() { return 0; }";
|
||||
|
||||
const SCRATCHPAD_CONTEXT_CONTENT = 1;
|
||||
const SCRATCHPAD_CONTEXT_BROWSER = 2;
|
||||
|
||||
function test() {
|
||||
waitForExplicitFinish();
|
||||
|
||||
gBrowser.selectedTab = gBrowser.addTab();
|
||||
gBrowser.selectedBrowser.addEventListener("load", function onLoad() {
|
||||
gBrowser.selectedBrowser.removeEventListener("load", onLoad, true);
|
||||
openScratchpad(runTests);
|
||||
}, true);
|
||||
|
||||
content.location = "data:text/html,<p>test file open and save in Scratchpad";
|
||||
}
|
||||
|
||||
function runTests() {
|
||||
gScratchpad = gScratchpadWindow.Scratchpad;
|
||||
function size(obj) { return Object.keys(obj).length; }
|
||||
|
||||
// Test Scratchpad._scanModeLine method.
|
||||
let obj = gScratchpad._scanModeLine();
|
||||
is(size(obj), 0, "Mode-line object has no properties");
|
||||
|
||||
obj = gScratchpad._scanModeLine("/* This is not a mode-line comment */");
|
||||
is(size(obj), 0, "Mode-line object has no properties");
|
||||
|
||||
obj = gScratchpad._scanModeLine("/* -sp-context:browser */");
|
||||
is(size(obj), 1, "Mode-line object has one property");
|
||||
is(obj["-sp-context"], "browser");
|
||||
|
||||
obj = gScratchpad._scanModeLine("/* -sp-context: browser */");
|
||||
is(size(obj), 1, "Mode-line object has one property");
|
||||
is(obj["-sp-context"], "browser");
|
||||
|
||||
obj = gScratchpad._scanModeLine("// -sp-context: browser");
|
||||
is(size(obj), 1, "Mode-line object has one property");
|
||||
is(obj["-sp-context"], "browser");
|
||||
|
||||
obj = gScratchpad._scanModeLine("/* -sp-context:browser, other:true */");
|
||||
is(size(obj), 2, "Mode-line object has two properties");
|
||||
is(obj["-sp-context"], "browser");
|
||||
is(obj["other"], "true");
|
||||
|
||||
// Test importing files with a mode-line in them.
|
||||
let content = "/* -sp-context:browser */\n" + gFileContent;
|
||||
createTempFile("fileForBug644413.tmp", content, function(aStatus, aFile) {
|
||||
ok(Components.isSuccessCode(aStatus), "File was saved successfully");
|
||||
|
||||
gFile = aFile;
|
||||
gScratchpad.importFromFile(gFile.QueryInterface(Ci.nsILocalFile), true, fileImported);
|
||||
});
|
||||
}
|
||||
|
||||
function fileImported(status, content) {
|
||||
ok(Components.isSuccessCode(status), "File was imported successfully");
|
||||
is(gScratchpad.executionContext, SCRATCHPAD_CONTEXT_BROWSER);
|
||||
|
||||
gFile.remove(false);
|
||||
gFile = null;
|
||||
gScratchpad = null;
|
||||
finish();
|
||||
}
|
||||
|
|
@ -4,9 +4,7 @@
|
|||
|
||||
let tempScope = {};
|
||||
Cu.import("resource://gre/modules/NetUtil.jsm", tempScope);
|
||||
Cu.import("resource://gre/modules/FileUtils.jsm", tempScope);
|
||||
let NetUtil = tempScope.NetUtil;
|
||||
let FileUtils = tempScope.FileUtils;
|
||||
|
||||
// Reference to the Scratchpad object.
|
||||
let gScratchpad;
|
||||
|
@ -34,32 +32,14 @@ function runTests()
|
|||
{
|
||||
gScratchpad = gScratchpadWindow.Scratchpad;
|
||||
|
||||
// Create a temporary file.
|
||||
gFile = FileUtils.getFile("TmpD", ["fileForBug636725.tmp"]);
|
||||
gFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666);
|
||||
createTempFile("fileForBug636725.tmp", gFileContent, function(aStatus, aFile) {
|
||||
ok(Components.isSuccessCode(aStatus),
|
||||
"The temporary file was saved successfully");
|
||||
|
||||
// Write the temporary file.
|
||||
let fout = Cc["@mozilla.org/network/file-output-stream;1"].
|
||||
createInstance(Ci.nsIFileOutputStream);
|
||||
fout.init(gFile.QueryInterface(Ci.nsILocalFile), 0x02 | 0x08 | 0x20,
|
||||
0644, fout.DEFER_OPEN);
|
||||
|
||||
let converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"].
|
||||
createInstance(Ci.nsIScriptableUnicodeConverter);
|
||||
converter.charset = "UTF-8";
|
||||
let fileContentStream = converter.convertToInputStream(gFileContent);
|
||||
|
||||
NetUtil.asyncCopy(fileContentStream, fout, tempFileSaved);
|
||||
}
|
||||
|
||||
function tempFileSaved(aStatus)
|
||||
{
|
||||
ok(Components.isSuccessCode(aStatus),
|
||||
"the temporary file was saved successfully");
|
||||
|
||||
// Import the file into Scratchpad.
|
||||
gScratchpad.importFromFile(gFile.QueryInterface(Ci.nsILocalFile), true,
|
||||
fileImported);
|
||||
gFile = aFile;
|
||||
gScratchpad.importFromFile(gFile.QueryInterface(Ci.nsILocalFile), true,
|
||||
fileImported);
|
||||
});
|
||||
}
|
||||
|
||||
function fileImported(aStatus, aFileContent)
|
||||
|
|
|
@ -4,6 +4,14 @@
|
|||
|
||||
"use strict";
|
||||
|
||||
let tempScope = {};
|
||||
|
||||
Cu.import("resource://gre/modules/NetUtil.jsm", tempScope);
|
||||
Cu.import("resource://gre/modules/FileUtils.jsm", tempScope);
|
||||
|
||||
let NetUtil = tempScope.NetUtil;
|
||||
let FileUtils = tempScope.FileUtils;
|
||||
|
||||
let gScratchpadWindow; // Reference to the Scratchpad chrome window object
|
||||
|
||||
/**
|
||||
|
@ -61,6 +69,41 @@ function openScratchpad(aReadyCallback, aOptions)
|
|||
return gScratchpadWindow;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a temporary file, write to it and call a callback
|
||||
* when done.
|
||||
*
|
||||
* @param string aName
|
||||
* Name of your temporary file.
|
||||
* @param string aContent
|
||||
* Temporary file's contents.
|
||||
* @param function aCallback
|
||||
* Optional callback to be called when we're done writing
|
||||
* to the file. It will receive two parameters: status code
|
||||
* and a file object.
|
||||
*/
|
||||
function createTempFile(aName, aContent, aCallback=function(){})
|
||||
{
|
||||
// Create a temporary file.
|
||||
let file = FileUtils.getFile("TmpD", [aName]);
|
||||
file.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, parseInt("666", 8));
|
||||
|
||||
// Write the temporary file.
|
||||
let fout = Cc["@mozilla.org/network/file-output-stream;1"].
|
||||
createInstance(Ci.nsIFileOutputStream);
|
||||
fout.init(file.QueryInterface(Ci.nsILocalFile), 0x02 | 0x08 | 0x20,
|
||||
parseInt("644", 8), fout.DEFER_OPEN);
|
||||
|
||||
let converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"].
|
||||
createInstance(Ci.nsIScriptableUnicodeConverter);
|
||||
converter.charset = "UTF-8";
|
||||
let fileContentStream = converter.convertToInputStream(aContent);
|
||||
|
||||
NetUtil.asyncCopy(fileContentStream, fout, function (aStatus) {
|
||||
aCallback(aStatus, file);
|
||||
});
|
||||
}
|
||||
|
||||
function cleanup()
|
||||
{
|
||||
if (gScratchpadWindow) {
|
||||
|
|
Загрузка…
Ссылка в новой задаче