From afb230946b4c40600b7f532b0d8b0a24de02a2bc Mon Sep 17 00:00:00 2001 From: Mark-Simulacrum Date: Sun, 21 Sep 2014 11:39:03 -0600 Subject: [PATCH] Add function to FileUtils that encodes paths. Use this function to properly set the source of images in ImageViewer. --- src/editor/ImageViewer.js | 2 +- src/file/FileUtils.js | 14 ++++++++++++++ test/spec/FileUtils-test.js | 15 +++++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/editor/ImageViewer.js b/src/editor/ImageViewer.js index 499067b3398..159247dfea3 100644 --- a/src/editor/ImageViewer.js +++ b/src/editor/ImageViewer.js @@ -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); diff --git a/src/file/FileUtils.js b/src/file/FileUtils.js index a3e50107178..2bcf3727f63 100644 --- a/src/file/FileUtils.js +++ b/src/file/FileUtils.js @@ -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; @@ -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; }); diff --git a/test/spec/FileUtils-test.js b/test/spec/FileUtils-test.js index 9e262adfbda..30930c1687c 100644 --- a/test/spec/FileUtils-test.js +++ b/test/spec/FileUtils-test.js @@ -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 () {