Bug 699121 - Style Editor should save file:// URLs immediately; r=cedricv,paul

This commit is contained in:
Victor Porof 2012-02-02 13:27:43 +02:00
Родитель f8b3ca7191
Коммит 7563f8ac78
3 изменённых файлов: 115 добавлений и 1 удалений

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

@ -111,6 +111,7 @@ function StyleEditor(aDocument, aStyleSheet)
this._styleSheet = aStyleSheet;
this._styleSheetIndex = -1; // unknown for now, will be set after load
this._styleSheetFilePath = null; // original file path for the style sheet
this._loaded = false;
@ -571,6 +572,7 @@ StyleEditor.prototype = {
* @param mixed aFile
* Optional nsIFile or string representing the filename to save in the
* background, no UI will be displayed.
* If not specified, the original style sheet URI is used.
* To implement 'Save' instead of 'Save as', you can pass savedFile here.
* @param function(nsIFile aFile) aCallback
* Optional callback called when the operation has finished.
@ -580,7 +582,8 @@ StyleEditor.prototype = {
*/
saveToFile: function SE_saveToFile(aFile, aCallback)
{
aFile = this._showFilePicker(aFile, true);
aFile = this._showFilePicker(aFile || this._styleSheetFilePath, true);
if (!aFile) {
if (aCallback) {
aCallback(null);
@ -729,6 +732,14 @@ StyleEditor.prototype = {
_showFilePicker: function SE__showFilePicker(aFile, aSave, aParentWindow)
{
if (typeof(aFile) == "string") {
try {
if (Services.io.extractScheme(aFile) == "file") {
let uri = Services.io.newURI(aFile, null, null);
let file = uri.QueryInterface(Ci.nsIFileURL).file;
return file;
}
} catch (ex) {
}
try {
let file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
file.initWithPath(aFile);
@ -772,6 +783,7 @@ StyleEditor.prototype = {
let scheme = Services.io.extractScheme(this.styleSheet.href);
switch (scheme) {
case "file":
this._styleSheetFilePath = this.styleSheet.href;
case "chrome":
case "resource":
this._loadSourceFromFile(this.styleSheet.href);

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

@ -46,6 +46,7 @@ include $(topsrcdir)/config/rules.mk
_BROWSER_TEST_FILES = \
browser_styleeditor_enabled.js \
browser_styleeditor_filesave.js \
browser_styleeditor_import.js \
browser_styleeditor_init.js \
browser_styleeditor_loading.js \

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

@ -0,0 +1,101 @@
/* vim: set ts=2 et sw=2 tw=80: */
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
const TESTCASE_URI_HTML = TEST_BASE + "simple.html";
const TESTCASE_URI_CSS = TEST_BASE + "simple.css";
const Cc = Components.classes;
const Ci = Components.interfaces;
let tempScope = {};
Components.utils.import("resource://gre/modules/FileUtils.jsm", tempScope);
Components.utils.import("resource://gre/modules/NetUtil.jsm", tempScope);
let FileUtils = tempScope.FileUtils;
let NetUtil = tempScope.NetUtil;
function test()
{
waitForExplicitFinish();
copy(TESTCASE_URI_HTML, "simple.html", function(htmlFile) {
copy(TESTCASE_URI_CSS, "simple.css", function(cssFile) {
addTabAndLaunchStyleEditorChromeWhenLoaded(function (aChrome) {
aChrome.addChromeListener({
onEditorAdded: function (aChrome, aEditor) {
if (aEditor.styleSheetIndex != 0) {
return; // we want to test against the first stylesheet
}
if (aEditor.sourceEditor) {
run(aEditor); // already attached to input element
} else {
aEditor.addActionListener({
onAttach: run
});
}
}
});
});
let uri = Services.io.newFileURI(htmlFile);
let filePath = uri.resolve("");
content.location = filePath;
});
});
}
function run(aEditor)
{
aEditor.saveToFile(null, function (aFile) {
ok(aFile, "file should get saved directly when using a file:// URI");
gChromeWindow.close();
finish();
});
}
function copy(aSrcChromeURL, aDestFileName, aCallback)
{
let destFile = FileUtils.getFile("ProfD", [aDestFileName]);
write(read(aSrcChromeURL), destFile, aCallback);
}
function read(aSrcChromeURL)
{
let scriptableStream = Cc["@mozilla.org/scriptableinputstream;1"]
.getService(Ci.nsIScriptableInputStream);
let channel = Services.io.newChannel(aSrcChromeURL, null, null);
let input = channel.open();
scriptableStream.init(input);
let data = scriptableStream.read(input.available());
scriptableStream.close();
input.close();
return data;
}
function write(aData, aFile, aCallback)
{
let converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"]
.createInstance(Ci.nsIScriptableUnicodeConverter);
converter.charset = "UTF-8";
let istream = converter.convertToInputStream(aData);
let ostream = FileUtils.openSafeFileOutputStream(aFile);
NetUtil.asyncCopy(istream, ostream, function(status) {
if (!Components.isSuccessCode(status)) {
info("Coudln't write to " + aFile.path);
return;
}
aCallback(aFile);
});
}