summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2015-09-06 17:57:18 +0100
committerMichael Williamson <mike@zwobble.org>2015-09-06 17:57:18 +0100
commit97144ee9f557ef4886456972d36583b6b62685d2 (patch)
treece4265dc24f418db6d1738bd5043067d56ba7856
parentced42637087b386df99cfd017262afad03b809ce (diff)
Support relative URIs
-rw-r--r--lib/docx/files.js15
-rw-r--r--tests/docx/files.tests.js14
2 files changed, 26 insertions, 3 deletions
diff --git a/lib/docx/files.js b/lib/docx/files.js
index e17bd45..2d0963b 100644
--- a/lib/docx/files.js
+++ b/lib/docx/files.js
@@ -1,6 +1,7 @@
var fs = require("fs");
var url = require("url");
var os = require("os");
+var resolvePath = require("path").resolve;
var promises = require("../promises");
@@ -9,9 +10,9 @@ exports.Files = Files;
exports.uriToPath = uriToPath;
-function Files() {
+function Files(base) {
function read(uri, encoding) {
- return readFile(uriToPath(uri), encoding);
+ return readFile(resolvePath(base, uriToPath(uri)), encoding);
}
return {
@@ -28,7 +29,7 @@ function uriToPath(uriString, platform) {
}
var uri = url.parse(uriString);
- if (uri.protocol === "file:" && (!uri.host || uri.host === "localhost")) {
+ if (isLocalFileUri(uri) || isRelativeUri(uri)) {
var path = decodeURIComponent(uri.path);
if (platform === "win32" && /^\/[a-z]:/i.test(path)) {
return path.slice(1);
@@ -39,3 +40,11 @@ function uriToPath(uriString, platform) {
throw new Error("Could not convert URI to path: " + uriString);
}
}
+
+function isLocalFileUri(uri) {
+ return uri.protocol === "file:" && (!uri.host || uri.host === "localhost");
+}
+
+function isRelativeUri(uri) {
+ return !uri.protocol && !uri.host;
+}
diff --git a/tests/docx/files.tests.js b/tests/docx/files.tests.js
index 0221834..e37df6b 100644
--- a/tests/docx/files.tests.js
+++ b/tests/docx/files.tests.js
@@ -22,6 +22,16 @@ describe("Files", function() {
});
});
});
+
+ test("can open files with relative URI", function() {
+ var filePath = path.resolve(testing.testPath("tiny-picture.png"));
+ var files = new Files(testing.testPath("."));
+ return files.read("tiny-picture.png", "base64").then(function(contents) {
+ return readFile(filePath, "base64").then(function(expectedContents) {
+ assert.deepEqual(contents, expectedContents);
+ });
+ });
+ });
});
@@ -51,4 +61,8 @@ describe("uriToPath", function() {
assert.equal(uriToPath("file:///c:/a", "win32"), "c:/a");
assert.equal(uriToPath("file:///C:/a", "win32"), "C:/a");
});
+
+ test("relative URI is unquoted", function() {
+ assert.equal(uriToPath("a%20b/c"), "a b/c");
+ });
});