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 Oct 20, 2014
1 parent 6ea75ad commit 73ff6c8
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 @@ -533,6 +533,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("/");
}

// Define public API
exports.LINE_ENDINGS_CRLF = LINE_ENDINGS_CRLF;
exports.LINE_ENDINGS_LF = LINE_ENDINGS_LF;
Expand Down Expand Up @@ -562,4 +575,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 73ff6c8

Please sign in to comment.