Skip to content
This repository was archived by the owner on Sep 6, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all 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/editor/ImageViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ define(function (require, exports, module) {
*/
function ImageView(file, $container) {
this.file = file;
this.$el = $(Mustache.render(ImageViewTemplate, {fullPath: file.fullPath,
this.$el = $(Mustache.render(ImageViewTemplate, {fullPath: FileUtils.encodeFilePath(file.fullPath),
now: new Date().valueOf()}));

$container.append(this.$el);
Expand Down
14 changes: 14 additions & 0 deletions src/file/FileUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,19 @@ define(function (require, exports, module) {
return 0;
}

/**
* @param {string} path Native path in the format used by FileSystemEntry.fullPath
* @return {string} URI-encoded version suitable for appending to 'file:///`. It's not safe to use encodeURI()
* directly since it doesn't escape chars like "#".
*/
function encodeFilePath(path) {
Copy link
Member

Choose a reason for hiding this comment

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

Would be good to add a short comment here, something like:

/**
 * @param {string} path Native path in the format used by FileSystemEntry.fullPath
 * @return {string} URI-encoded version suitable for appending to 'file:///`. It's not safe to use encodeURI()
 *     directly since it doesn't escape chars like "#".
 */

var pathArray = path.split("/");
pathArray = pathArray.map(function (subPath) {
return encodeURIComponent(subPath);
});
return pathArray.join("/");
}

// Asynchronously loading Dialogs to avoid the circular dependency
require(["widgets/Dialogs"], function (dialogsModule) {
Dialogs = dialogsModule;
Expand Down Expand Up @@ -554,4 +567,5 @@ define(function (require, exports, module) {
exports.compareFilenames = compareFilenames;
exports.comparePaths = comparePaths;
exports.MAX_FILE_SIZE = MAX_FILE_SIZE;
exports.encodeFilePath = encodeFilePath;
});
15 changes: 15 additions & 0 deletions test/spec/FileUtils-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,21 @@ define(function (require, exports, module) {
});
});

describe("encodeFilePath", function () {

it("should encode symbols in path", function () {
expect(FileUtils.encodeFilePath("#?@test&\".js")).toBe("%23%3F%40test%26%22.js");
});

it("should work with a common path", function () {
expect(FileUtils.encodeFilePath("C:/test/$data.txt")).toBe("C%3A/test/%24data.txt");
});

it("should work with a path with no special symbols", function () {
expect(FileUtils.encodeFilePath("/Applications/Test/test.html")).toBe("/Applications/Test/test.html");
});
});

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think we need a test for every single char.
I'd suggest one test with a totally fake path like #?@test&".js, one with a more common like C:/test/$data.txt, and one totally valid like /Applications/Test/test.html.

Copy link
Contributor

Choose a reason for hiding this comment

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

And btw, we usually use something like expect(...).toEqual(xy); or .toBe(xy).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I noticed that the tests for FileUtils are all listed as Integration, even though some are unit tests. Is this a bug?

Copy link
Contributor

Choose a reason for hiding this comment

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

Ehm, they are listed under Unit for me...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm, perhaps that was a different unit test. Will report if I see again.

describe("compareFilenames", function () {

it("should compare filenames using German rules", function () {
Expand Down