aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2017-01-23 20:23:03 +0000
committerMichael Williamson <mike@zwobble.org>2017-01-23 20:23:03 +0000
commite81f83906d26ace7c38b0b7b24e0d5f98da35172 (patch)
tree260965d0157d61bd34310181739154e7862f16d7
parent1e85fcd0539d71484a4b5f44e1f0ccd0f48a90ec (diff)
Handle base-relative URIs entirely in entry name generation
-rw-r--r--lib/docx/body-reader.js4
-rw-r--r--lib/zipfile.js24
-rw-r--r--test/zipfile.tests.js36
3 files changed, 16 insertions, 48 deletions
diff --git a/lib/docx/body-reader.js b/lib/docx/body-reader.js
index 175b724..e683168 100644
--- a/lib/docx/body-reader.js
+++ b/lib/docx/body-reader.js
@@ -5,7 +5,7 @@ var _ = require("underscore");
var documents = require("../documents");
var Result = require("../results").Result;
var warning = require("../results").warning;
-var joinZipPath = require("../zipfile").joinZipPath;
+var uriToZipEntryName = require("../zipfile").uriToZipEntryName;
function BodyReader(options) {
@@ -354,7 +354,7 @@ function BodyReader(options) {
}
function findEmbeddedImageFile(relationshipId) {
- var path = joinZipPath("word", relationships[relationshipId].target);
+ var path = uriToZipEntryName("word", relationships[relationshipId].target);
return {
path: path,
read: docxFile.read.bind(docxFile, path)
diff --git a/lib/zipfile.js b/lib/zipfile.js
index efe519c..9542ae1 100644
--- a/lib/zipfile.js
+++ b/lib/zipfile.js
@@ -3,16 +3,16 @@ var JSZip = require("jszip");
var promises = require("./promises");
exports.openArrayBuffer = openArrayBuffer;
-exports.joinZipPath = joinZipPath;
+exports.uriToZipEntryName = uriToZipEntryName;
function openArrayBuffer(arrayBuffer) {
var zipFile = new JSZip(arrayBuffer);
function exists(name) {
- return zipFile.file(normaliseName(name)) !== null;
+ return zipFile.file(name) !== null;
}
function read(name, encoding) {
- var array = zipFile.file(normaliseName(name)).asUint8Array();
+ var array = zipFile.file(name).asUint8Array();
var buffer = new Buffer(array);
if (encoding) {
return promises.when(buffer.toString(encoding));
@@ -22,7 +22,7 @@ function openArrayBuffer(arrayBuffer) {
}
function write(name, contents) {
- zipFile.file(normaliseName(name), contents);
+ zipFile.file(name, contents);
}
function toBuffer() {
@@ -37,20 +37,12 @@ function openArrayBuffer(arrayBuffer) {
};
}
-function normaliseName(name) {
- if (name.charAt(0) === "/") {
- return name.substr(1);
- } else {
- return name;
- }
-}
-
-function joinZipPath(first, second) {
- if (second.charAt(0) === "/") {
- return second;
+function uriToZipEntryName(base, uri) {
+ if (uri.charAt(0) === "/") {
+ return uri.substr(1);
} else {
// In general, we should check first and second for trailing and leading slashes,
// but in our specific case this seems to be sufficient
- return first + "/" + second;
+ return base + "/" + uri;
}
}
diff --git a/test/zipfile.tests.js b/test/zipfile.tests.js
index f6c38d4..8c2f6c1 100644
--- a/test/zipfile.tests.js
+++ b/test/zipfile.tests.js
@@ -17,48 +17,24 @@ test('file in zip can be read after being written', function() {
});
});
-test('leading slash in path is stripped if set when reading', function() {
- var zip = emptyZipFile();
- assert(!zip.exists("/song/title"));
-
- zip.write("song/title", "Dark Blue");
-
- assert(zip.exists("/song/title"));
- return zip.read("/song/title", "utf8").then(function(contents) {
- assert.equal(contents, "Dark Blue");
- });
-});
-
-test('leading slash in path is stripped if set when writing', function() {
- var zip = emptyZipFile();
- assert(!zip.exists("song/title"));
-
- zip.write("/song/title", "Dark Blue");
-
- assert(zip.exists("song/title"));
- return zip.read("song/title", "utf8").then(function(contents) {
- assert.equal(contents, "Dark Blue");
- });
-});
-
function emptyZipFile() {
var zip = new JSZip();
var buffer = zip.generate({type: "arraybuffer"});
return zipfile.openArrayBuffer(buffer);
}
-test("joinZipPath", {
- "joins relative path to base using forward slash": function() {
+test("uriToZipEntryName", {
+ "when path does not have leading slash then path is resolved relative to base": function() {
assert.equal(
- zipfile.joinZipPath("one/two", "three/four"),
+ zipfile.uriToZipEntryName("one/two", "three/four"),
"one/two/three/four"
)
},
- "joining base to absolute path ignores base": function() {
+ "when path has leading slash then base is ignored": function() {
assert.equal(
- zipfile.joinZipPath("one/two", "/three/four"),
- "/three/four"
+ zipfile.uriToZipEntryName("one/two", "/three/four"),
+ "three/four"
)
}
});