1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
var assert = require("assert");
var docxReader = require("../../lib/docx/docx-reader");
var documents = require("../../lib/documents");
var xml = require("../../lib/xml");
var testing = require("../testing");
var test = require("../test")(module);
var testData = testing.testData;
var createFakeDocxFile = testing.createFakeDocxFile;
test("can read document with single paragraph with single run of text", function() {
var expectedDocument = documents.Document([
documents.Paragraph([
documents.Run([
documents.Text("Hello.")
])
])
]);
var docxFile = createFakeDocxFile({
"word/document.xml": testData("simple/word/document.xml")
});
return docxReader.read(docxFile).then(function(result) {
assert.deepEqual(expectedDocument, result.value);
});
});
test("hyperlink hrefs are read from relationships file", function() {
var docxFile = createFakeDocxFile({
"word/document.xml": testData("hyperlinks/word/document.xml"),
"word/_rels/document.xml.rels": testData("hyperlinks/word/_rels/document.xml.rels")
});
return docxReader.read(docxFile).then(function(result) {
var paragraph = result.value.children[0];
assert.equal(1, paragraph.children.length);
var hyperlink = paragraph.children[0];
assert.equal(hyperlink.href, "http://www.example.com");
assert.equal(hyperlink.children.length, 1);
});
});
var relationshipNamespaces = {
"r": "http://schemas.openxmlformats.org/package/2006/relationships"
};
test("main document is found using _rels/.rels", function() {
var relationships = xml.element("r:Relationships", {}, [
xml.element("r:Relationship", {
"Type": "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument",
"Target": "/word/document2.xml"
})
]);
var docxFile = createFakeDocxFile({
"word/document2.xml": testData("simple/word/document.xml"),
"_rels/.rels": xml.writeString(relationships, relationshipNamespaces)
});
var expectedDocument = documents.Document([
documents.Paragraph([
documents.Run([
documents.Text("Hello.")
])
])
]);
return docxReader.read(docxFile).then(function(result) {
assert.deepEqual(expectedDocument, result.value);
});
});
test("error is thrown when main document part does not exist", function() {
var relationships = xml.element("r:Relationships", {}, [
xml.element("r:Relationship", {
"Type": "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument",
"Target": "/word/document2.xml"
})
]);
var docxFile = createFakeDocxFile({
"_rels/.rels": xml.writeString(relationships, relationshipNamespaces)
});
return docxReader.read(docxFile).then(function(result) {
assert.ok(false, "Expected error");
}, function(error) {
assert.equal(error.message, "Could not find main document part. Are you sure this is a valid .docx file?");
});
});
test("part paths", {
"main document part is found using package relationships": function() {
var relationships = xml.element("r:Relationships", {}, [
xml.element("r:Relationship", {
"Type": "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument",
"Target": "/word/document2.xml"
})
]);
var docxFile = createFakeDocxFile({
"word/document2.xml": " ",
"_rels/.rels": xml.writeString(relationships, relationshipNamespaces)
});
return docxReader._findPartPaths(docxFile).then(function(partPaths) {
assert.equal(partPaths.mainDocument, "word/document2.xml");
});
},
"word/document.xml is used as fallback location for main document part": function() {
var docxFile = createFakeDocxFile({
"word/document.xml": " "
});
return docxReader._findPartPaths(docxFile).then(function(partPaths) {
assert.equal(partPaths.mainDocument, "word/document.xml");
});
}
});
[
{
name: "comments",
type: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments",
},
{
name: "endnotes",
type: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/endnotes",
},
{
name: "footnotes",
type: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/footnotes",
},
{
name: "numbering",
type: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering",
},
{
name: "styles",
type: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles",
}
].forEach(function(options) {
test(options.name + " part is found using main document relationships", function() {
var docxFile = createFakeDocxFile({
"_rels/.rels": createPackageRelationships("word/document.xml"),
"word/document.xml": " ",
"word/_rels/document.xml.rels": xml.writeString(xml.element("r:Relationships", {}, [
xml.element("r:Relationship", {
"Type": options.type,
"Target": "target-path.xml"
})
]), relationshipNamespaces),
"word/target-path.xml": " "
});
return docxReader._findPartPaths(docxFile).then(function(partPaths) {
assert.equal(partPaths[options.name], "word/target-path.xml");
});
});
test("word/" + options.name + ".xml is used as fallback location for " + options.name + " part", function() {
var zipContents = {
"_rels/.rels": createPackageRelationships("word/document.xml"),
"word/document.xml": " "
};
zipContents["word/" + options.name + ".xml"] = " ";
var docxFile = createFakeDocxFile(zipContents);
return docxReader._findPartPaths(docxFile).then(function(partPaths) {
assert.equal(partPaths[options.name], "word/" + options.name + ".xml");
});
});
});
function createPackageRelationships(mainDocumentPath) {
return xml.writeString(xml.element("r:Relationships", {}, [
xml.element("r:Relationship", {
"Type": "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument",
"Target": mainDocumentPath
})
]), relationshipNamespaces);
}
|