Skip to content
This repository has been archived by the owner on Sep 6, 2021. It is now read-only.

Now BOM is preserved for UTF-8 files #13477

Merged
merged 4 commits into from
Jun 27, 2017
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/document/DocumentManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ define(function (require, exports, module) {
if (doc) {
result.resolve(doc.getText(), doc.diskTimestamp, checkLineEndings ? doc._lineEndings : null);
} else {
file.read(function (err, contents, encoding, stat) {
file.read(function (err, contents, encoding, preserveBOM, stat) {
if (err) {
result.reject(err);
} else {
Expand Down
8 changes: 7 additions & 1 deletion src/file/FileUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ define(function (require, exports, module) {
});

// Read file
file.read(function (err, data, encoding, stat) {
file.read(function (err, data, encoding, preserveBOM, stat) {
if (!err) {
result.resolve(data, stat.mtime);
} else {
Expand Down Expand Up @@ -188,6 +188,12 @@ define(function (require, exports, module) {
result = Strings.UNSUPPORTED_ENCODING_ERR;
} else if (name === FileSystemError.EXCEEDS_MAX_FILE_SIZE) {
result = StringUtils.format(Strings.EXCEEDS_MAX_FILE_SIZE, MAX_FILE_SIZE_MB);
} else if (name === FileSystemError.ENCODE_FILE_FAILED) {
result = Strings.ENCODE_FILE_FAILED_ERR;
} else if (name === FileSystemError.DECODE_FILE_FAILED) {
result = Strings.DECODE_FILE_FAILED_ERR;
} else if (name === FileSystemError.UNSUPPORTED_UTF16_ENCODING) {
result = Strings.UNSUPPORTED_UTF16_ENCODING_ERR;
} else {
result = StringUtils.format(Strings.GENERIC_ERROR, name);
}
Expand Down
15 changes: 12 additions & 3 deletions src/filesystem/File.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ define(function (require, exports, module) {
*/
File.prototype._encoding = null;

/**
* BOM detected by brackets-shell
* @private
* @type {?bool}
*/
File.prototype._preserveBOM = false;

/**
* Consistency hash for this file. Reads and writes update this value, and
* writes confirm the hash before overwriting existing files. The type of
Expand Down Expand Up @@ -103,7 +110,7 @@ define(function (require, exports, module) {
// for a default value; otherwise it could be the empty string, which is
// falsey.
if (this._contents !== null && this._stat) {
callback(null, this._contents, this._encoding, this._stat);
callback(null, this._contents, this._encoding, this._preserveBOM, this._stat);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the preserveBOM marker from callback. It should be consumed internally and kept in File prototype only.

return;
}

Expand All @@ -112,7 +119,7 @@ define(function (require, exports, module) {
options.stat = this._stat;
}

this._impl.readFile(this._path, options, function (err, data, encoding, stat) {
this._impl.readFile(this._path, options, function (err, data, encoding, preserveBOM, stat) {
if (err) {
this._clearCachedData();
callback(err);
Expand All @@ -122,14 +129,15 @@ define(function (require, exports, module) {
// Always store the hash
this._hash = stat._hash;
this._encoding = encoding;
this._preserveBOM = preserveBOM;

// Only cache data for watched files
if (watched) {
this._stat = stat;
this._contents = data;
}

callback(err, data, encoding, stat);
callback(err, data, encoding, this._preserveBOM, stat);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the preserveBOM marker from callback. It should be consumed internally and kept in File prototype only.

}.bind(this));
};

Expand Down Expand Up @@ -159,6 +167,7 @@ define(function (require, exports, module) {
options.expectedContents = this._contents;
}
options.encoding = this._encoding || "utf8";
options.preserveBOM = this._preserveBOM;

// Block external change events until after the write has finished
this._fileSystem._beginChange();
Expand Down
5 changes: 4 additions & 1 deletion src/filesystem/FileSystemError.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ define(function (require, exports, module) {
CONTENTS_MODIFIED : "ContentsModified",
ROOT_NOT_WATCHED : "RootNotBeingWatched",
EXCEEDS_MAX_FILE_SIZE : "ExceedsMaxFileSize",
NETWORK_DRIVE_NOT_SUPPORTED : "NetworkDriveNotSupported"
NETWORK_DRIVE_NOT_SUPPORTED : "NetworkDriveNotSupported",
ENCODE_FILE_FAILED : "EncodeFileFailed",
DECODE_FILE_FAILED : "DecodeFileFailed",
UNSUPPORTED_UTF16_ENCODING : "UnsupportedUTF16Encoding"

// FUTURE: Add remote connection errors: timeout, not logged in, connection err, etc.
};
Expand Down
15 changes: 11 additions & 4 deletions src/filesystem/impls/appshell/AppshellFileSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,12 @@ define(function (require, exports, module) {
return FileSystemError.OUT_OF_SPACE;
case appshell.fs.ERR_FILE_EXISTS:
return FileSystemError.ALREADY_EXISTS;
case appshell.fs.ERR_ENCODE_FILE_FAILED:
return FileSystemError.ENCODE_FILE_FAILED;
case appshell.fs.ERR_ENCODE_FILE_FAILED:
return FileSystemError.DECODE_FILE_FAILED;
case appshell.fs.ERR_UNSUPPORTED_UTF16_ENCODING:
return FileSystemError.UNSUPPORTED_UTF16_ENCODING;
}
return FileSystemError.UNKNOWN;
}
Expand Down Expand Up @@ -363,11 +369,11 @@ define(function (require, exports, module) {
if (stat.size > (FileUtils.MAX_FILE_SIZE)) {
callback(FileSystemError.EXCEEDS_MAX_FILE_SIZE);
} else {
appshell.fs.readFile(path, encoding, function (_err, _data, encoding) {
appshell.fs.readFile(path, encoding, function (_err, _data, encoding, preserveBOM) {
if (_err) {
callback(_mapError(_err));
} else {
callback(null, _data, encoding, stat);
callback(null, _data, encoding, preserveBOM, stat);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the preserveBOM marker from callback. It should be consumed internally and kept in File prototype only.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad. You do need it .

}
});
}
Expand Down Expand Up @@ -403,10 +409,11 @@ define(function (require, exports, module) {
* @param {function(?string, FileSystemStats=, boolean)} callback
*/
function writeFile(path, data, options, callback) {
var encoding = options.encoding || "utf8";
var encoding = options.encoding || "utf8",
preserveBOM = options.preserveBOM;

function _finishWrite(created) {
appshell.fs.writeFile(path, data, encoding, function (err) {
appshell.fs.writeFile(path, data, encoding, preserveBOM, function (err) {
if (err) {
callback(_mapError(err));
} else {
Expand Down
5 changes: 4 additions & 1 deletion src/nls/root/strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ define({
"NO_MODIFICATION_ALLOWED_ERR" : "The target directory cannot be modified.",
"NO_MODIFICATION_ALLOWED_ERR_FILE" : "The permissions do not allow you to make modifications.",
"CONTENTS_MODIFIED_ERR" : "The file has been modified outside of {APP_NAME}.",
"UNSUPPORTED_ENCODING_ERR" : "{APP_NAME} currently only supports UTF-8 encoded text files.",
"UNSUPPORTED_ENCODING_ERR" : "Unknown encoding format",
"ENCODE_FILE_FAILED_ERR" : "{APP_NAME} was not able to encode the contents of file.",
"DECODE_FILE_FAILED_ERR" : "{APP_NAME} was not able to decode the contents of file.",
"UNSUPPORTED_UTF16_ENCODING_ERR" : "{APP_NAME} currently doesn't support UTF-16 encoded text files.",
"FILE_EXISTS_ERR" : "The file or directory already exists.",
"FILE" : "file",
"FILE_TITLE" : "File",
Expand Down
2 changes: 0 additions & 2 deletions src/supported-encodings.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@
"KOI8-R",
"KOI8-RU",
"SHIFT_JIS",
"UTF-16BE",
"UTF-16LE",
"UTF-8",
"WINDOWS-1250",
"WINDOWS-1251",
Expand Down
3 changes: 2 additions & 1 deletion test/spec/FileSystem-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,11 @@ define(function (require, exports, module) {
}

function readCallback() {
var callback = function (err, data, encoding, stat) {
var callback = function (err, data, encoding, preserveBOM, stat) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test shouldn't be modified. The BOM flag is purely an internal config option and shouldn't be exposed. In case we want to expose this in future, it has to be a preference and that too only for write part.

callback.error = err;
callback.data = data;
callback.encoding = "utf8";
callback.preserveBOM = false;
callback.stat = stat;
callback.wasCalled = true;
};
Expand Down
2 changes: 1 addition & 1 deletion test/spec/MockFileSystemImpl.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ define(function (require, exports, module) {
if (!_model.exists(path)) {
cb(FileSystemError.NOT_FOUND);
} else {
cb(null, _model.readFile(path), "UTF-8", _model.stat(path));
cb(null, _model.readFile(path), "UTF-8", false, _model.stat(path));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as before.

}
}

Expand Down