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

Commit

Permalink
Add function to FileUtils that encodes paths.
Browse files Browse the repository at this point in the history
Use this function to properly set the source of images in ImageViewer.
  • Loading branch information
Mark-Simulacrum committed Nov 19, 2014
1 parent e273f83 commit afb2309
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
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) {
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");
});
});

describe("compareFilenames", function () {

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

0 comments on commit afb2309

Please sign in to comment.