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

Commit 8ecb5ed

Browse files
saurabh95swmitra
authored andcommitted
Now BOM is preserved for UTF-8 files (#13477)
* Now BOM is preserved for UTF-8 files * Added error strings for failure in encode/decode and utf-16 * Removed utf-16 from encodings list * Addressed review comments
1 parent 3aa8ef6 commit 8ecb5ed

File tree

7 files changed

+39
-11
lines changed

7 files changed

+39
-11
lines changed

src/file/FileUtils.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,12 @@ define(function (require, exports, module) {
188188
result = Strings.UNSUPPORTED_ENCODING_ERR;
189189
} else if (name === FileSystemError.EXCEEDS_MAX_FILE_SIZE) {
190190
result = StringUtils.format(Strings.EXCEEDS_MAX_FILE_SIZE, MAX_FILE_SIZE_MB);
191+
} else if (name === FileSystemError.ENCODE_FILE_FAILED) {
192+
result = Strings.ENCODE_FILE_FAILED_ERR;
193+
} else if (name === FileSystemError.DECODE_FILE_FAILED) {
194+
result = Strings.DECODE_FILE_FAILED_ERR;
195+
} else if (name === FileSystemError.UNSUPPORTED_UTF16_ENCODING) {
196+
result = Strings.UNSUPPORTED_UTF16_ENCODING_ERR;
191197
} else {
192198
result = StringUtils.format(Strings.GENERIC_ERROR, name);
193199
}

src/filesystem/File.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@ define(function (require, exports, module) {
6363
*/
6464
File.prototype._encoding = null;
6565

66+
/**
67+
* BOM detected by brackets-shell
68+
* @private
69+
* @type {?bool}
70+
*/
71+
File.prototype._preserveBOM = false;
72+
6673
/**
6774
* Consistency hash for this file. Reads and writes update this value, and
6875
* writes confirm the hash before overwriting existing files. The type of
@@ -112,7 +119,7 @@ define(function (require, exports, module) {
112119
options.stat = this._stat;
113120
}
114121

115-
this._impl.readFile(this._path, options, function (err, data, encoding, stat) {
122+
this._impl.readFile(this._path, options, function (err, data, encoding, preserveBOM, stat) {
116123
if (err) {
117124
this._clearCachedData();
118125
callback(err);
@@ -122,6 +129,7 @@ define(function (require, exports, module) {
122129
// Always store the hash
123130
this._hash = stat._hash;
124131
this._encoding = encoding;
132+
this._preserveBOM = preserveBOM;
125133

126134
// Only cache data for watched files
127135
if (watched) {
@@ -158,7 +166,10 @@ define(function (require, exports, module) {
158166
options.expectedHash = this._hash;
159167
options.expectedContents = this._contents;
160168
}
161-
options.encoding = this._encoding || "utf8";
169+
if (!options.encoding) {
170+
options.encoding = this._encoding || "utf8";
171+
}
172+
options.preserveBOM = this._preserveBOM;
162173

163174
// Block external change events until after the write has finished
164175
this._fileSystem._beginChange();

src/filesystem/FileSystemError.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,10 @@ define(function (require, exports, module) {
4848
CONTENTS_MODIFIED : "ContentsModified",
4949
ROOT_NOT_WATCHED : "RootNotBeingWatched",
5050
EXCEEDS_MAX_FILE_SIZE : "ExceedsMaxFileSize",
51-
NETWORK_DRIVE_NOT_SUPPORTED : "NetworkDriveNotSupported"
51+
NETWORK_DRIVE_NOT_SUPPORTED : "NetworkDriveNotSupported",
52+
ENCODE_FILE_FAILED : "EncodeFileFailed",
53+
DECODE_FILE_FAILED : "DecodeFileFailed",
54+
UNSUPPORTED_UTF16_ENCODING : "UnsupportedUTF16Encoding"
5255

5356
// FUTURE: Add remote connection errors: timeout, not logged in, connection err, etc.
5457
};

src/filesystem/impls/appshell/AppshellFileSystem.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,12 @@ define(function (require, exports, module) {
160160
return FileSystemError.OUT_OF_SPACE;
161161
case appshell.fs.ERR_FILE_EXISTS:
162162
return FileSystemError.ALREADY_EXISTS;
163+
case appshell.fs.ERR_ENCODE_FILE_FAILED:
164+
return FileSystemError.ENCODE_FILE_FAILED;
165+
case appshell.fs.ERR_ENCODE_FILE_FAILED:
166+
return FileSystemError.DECODE_FILE_FAILED;
167+
case appshell.fs.ERR_UNSUPPORTED_UTF16_ENCODING:
168+
return FileSystemError.UNSUPPORTED_UTF16_ENCODING;
163169
}
164170
return FileSystemError.UNKNOWN;
165171
}
@@ -363,11 +369,11 @@ define(function (require, exports, module) {
363369
if (stat.size > (FileUtils.MAX_FILE_SIZE)) {
364370
callback(FileSystemError.EXCEEDS_MAX_FILE_SIZE);
365371
} else {
366-
appshell.fs.readFile(path, encoding, function (_err, _data, encoding) {
372+
appshell.fs.readFile(path, encoding, function (_err, _data, encoding, preserveBOM) {
367373
if (_err) {
368374
callback(_mapError(_err));
369375
} else {
370-
callback(null, _data, encoding, stat);
376+
callback(null, _data, encoding, preserveBOM, stat);
371377
}
372378
});
373379
}
@@ -403,10 +409,11 @@ define(function (require, exports, module) {
403409
* @param {function(?string, FileSystemStats=, boolean)} callback
404410
*/
405411
function writeFile(path, data, options, callback) {
406-
var encoding = options.encoding || "utf8";
412+
var encoding = options.encoding || "utf8",
413+
preserveBOM = options.preserveBOM;
407414

408415
function _finishWrite(created) {
409-
appshell.fs.writeFile(path, data, encoding, function (err) {
416+
appshell.fs.writeFile(path, data, encoding, preserveBOM, function (err) {
410417
if (err) {
411418
callback(_mapError(err));
412419
} else {

src/nls/root/strings.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ define({
3535
"NO_MODIFICATION_ALLOWED_ERR" : "The target directory cannot be modified.",
3636
"NO_MODIFICATION_ALLOWED_ERR_FILE" : "The permissions do not allow you to make modifications.",
3737
"CONTENTS_MODIFIED_ERR" : "The file has been modified outside of {APP_NAME}.",
38-
"UNSUPPORTED_ENCODING_ERR" : "{APP_NAME} currently only supports UTF-8 encoded text files.",
38+
"UNSUPPORTED_ENCODING_ERR" : "Unknown encoding format",
39+
"ENCODE_FILE_FAILED_ERR" : "{APP_NAME} was not able to encode the contents of file.",
40+
"DECODE_FILE_FAILED_ERR" : "{APP_NAME} was not able to decode the contents of file.",
41+
"UNSUPPORTED_UTF16_ENCODING_ERR" : "{APP_NAME} currently doesn't support UTF-16 encoded text files.",
3942
"FILE_EXISTS_ERR" : "The file or directory already exists.",
4043
"FILE" : "file",
4144
"FILE_TITLE" : "File",

src/supported-encodings.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@
3131
"KOI8-R",
3232
"KOI8-RU",
3333
"SHIFT_JIS",
34-
"UTF-16BE",
35-
"UTF-16LE",
3634
"UTF-8",
3735
"WINDOWS-1250",
3836
"WINDOWS-1251",

test/spec/MockFileSystemImpl.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ define(function (require, exports, module) {
141141
if (!_model.exists(path)) {
142142
cb(FileSystemError.NOT_FOUND);
143143
} else {
144-
cb(null, _model.readFile(path), "UTF-8", _model.stat(path));
144+
cb(null, _model.readFile(path), "UTF-8", false, _model.stat(path));
145145
}
146146
}
147147

0 commit comments

Comments
 (0)