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 1 commit
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
2 changes: 1 addition & 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
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
9 changes: 5 additions & 4 deletions src/filesystem/impls/appshell/AppshellFileSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -363,11 +363,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 +403,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
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