summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2015-09-06 17:48:55 +0100
committerMichael Williamson <mike@zwobble.org>2015-09-06 17:48:55 +0100
commitced42637087b386df99cfd017262afad03b809ce (patch)
tree17d63ea592c875464e8001076b38e9ff66d2657c
parentf16aeec2396f78cc3cedcaafa5d58899408ffb99 (diff)
Parse file URIs on Windows
-rw-r--r--lib/docx/files.js14
-rw-r--r--tests/docx/files.tests.js12
2 files changed, 23 insertions, 3 deletions
diff --git a/lib/docx/files.js b/lib/docx/files.js
index 4e3a820..e17bd45 100644
--- a/lib/docx/files.js
+++ b/lib/docx/files.js
@@ -1,5 +1,6 @@
var fs = require("fs");
var url = require("url");
+var os = require("os");
var promises = require("../promises");
@@ -21,10 +22,19 @@ function Files() {
var readFile = promises.promisify(fs.readFile.bind(fs));
-function uriToPath(uriString) {
+function uriToPath(uriString, platform) {
+ if (!platform) {
+ platform = os.platform();
+ }
+
var uri = url.parse(uriString);
if (uri.protocol === "file:" && (!uri.host || uri.host === "localhost")) {
- return decodeURIComponent(uri.path);
+ var path = decodeURIComponent(uri.path);
+ if (platform === "win32" && /^\/[a-z]:/i.test(path)) {
+ return path.slice(1);
+ } else {
+ return path;
+ }
} else {
throw new Error("Could not convert URI to path: " + uriString);
}
diff --git a/tests/docx/files.tests.js b/tests/docx/files.tests.js
index 0ec64e1..0221834 100644
--- a/tests/docx/files.tests.js
+++ b/tests/docx/files.tests.js
@@ -27,7 +27,8 @@ describe("Files", function() {
describe("uriToPath", function() {
test("leading slash is retained on non-Windows file URIs", function() {
- assert.equal(uriToPath("file:///a/b/c"), "/a/b/c");
+ assert.equal(uriToPath("file:///a/b/c", "linux"), "/a/b/c");
+ assert.equal(uriToPath("file:///a/b/c", "win32"), "/a/b/c");
});
test("URI is unquoted", function() {
@@ -41,4 +42,13 @@ describe("uriToPath", function() {
test("when host is set but not localhost then path cannot be found", function() {
assert.throws(function() { uriToPath("file://example/a/b/c"); }, /Could not convert URI to path: file:\/\/example\/a\/b\/c/);
});
+
+ test("leading slash is not dropped on Windows file URIs when platform is not Windows", function() {
+ assert.equal(uriToPath("file:///c:/a", "linux"), "/c:/a");
+ });
+
+ test("leading slash is dropped on Windows file URIs when platform is Windows", function() {
+ assert.equal(uriToPath("file:///c:/a", "win32"), "c:/a");
+ assert.equal(uriToPath("file:///C:/a", "win32"), "C:/a");
+ });
});