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
|
var assert = require("assert");
var DocumentXmlReader = require("../lib/document-xml-reader").DocumentXmlReader;
var documents = require("../lib/documents");
var XmlElement = require("../lib/xmlreader").Element;
var testing = require("./testing");
var test = testing.test;
var createFakeDocxFile = testing.createFakeDocxFile;
function readXmlElement(element) {
return new DocumentXmlReader({}).readXmlElement(element);
}
function readXmlElementValue(element) {
var result = readXmlElement(element);
assert.deepEqual(result.messages, []);
return result.value;
}
describe("readXmlElement: ", function() {
test("paragraph has no style if it has no properties", function() {
var paragraphXml = new XmlElement("w:p", {}, []);
var paragraph = readXmlElementValue(paragraphXml);
assert.deepEqual(paragraph.styleName, null);
});
test("paragraph has style name read from paragraph properties if present", function() {
var styleXml = new XmlElement("w:pStyle", {"w:val": "Heading1"}, []);
var propertiesXml = new XmlElement("w:pPr", {}, [styleXml]);
var paragraphXml = new XmlElement("w:p", {}, [propertiesXml]);
var paragraph = readXmlElementValue(paragraphXml);
assert.deepEqual(paragraph.styleName, "Heading1");
});
test("run has no style if it has no properties", function() {
var runXml = runWithProperties([]);
var run = readXmlElementValue(runXml);
assert.deepEqual(run.styleName, null);
});
test("run has style name read from run properties if present", function() {
var runStyleXml = new XmlElement("w:rStyle", {"w:val": "Emphasis"});
var runXml = runWithProperties([runStyleXml]);
var run = readXmlElementValue(runXml);
assert.deepEqual(run.styleName, "Emphasis");
});
test("isBold is false if bold element is not present", function() {
var runXml = runWithProperties([]);
var run = readXmlElementValue(runXml);
assert.deepEqual(run.isBold, false);
});
test("isBold is true if bold element is present", function() {
var boldXml = new XmlElement("w:b");
var runXml = runWithProperties([boldXml]);
var run = readXmlElementValue(runXml);
assert.equal(run.isBold, true);
});
test("isItalic is false if bold element is not present", function() {
var runXml = runWithProperties([]);
var run = readXmlElementValue(runXml);
assert.deepEqual(run.isItalic, false);
});
test("isItalic is true if bold element is present", function() {
var italicXml = new XmlElement("w:i");
var runXml = runWithProperties([italicXml]);
var run = readXmlElementValue(runXml);
assert.equal(run.isItalic, true);
});
test("run properties not included as child of run", function() {
var runStyleXml = new XmlElement("w:rStyle", {"w:val": "Emphasis"});
var runPropertiesXml = new XmlElement("w:rPr", {}, [runStyleXml]);
var runXml = new XmlElement("w:r", {}, [runPropertiesXml]);
var result = readXmlElement(runXml);
assert.deepEqual(result.value.children, []);
});
test("emits warning on unrecognised element", function() {
var unrecognisedElement = new XmlElement("w:not-an-element");
var result = readXmlElement(unrecognisedElement);
assert.deepEqual(
result.messages,
[{
type: "warning",
message: "An unrecognised element was ignored: w:not-an-element"
}]
);
});
test("w:bookmarkStart is ignored without warning", function() {
var ignoredElement = new XmlElement("w:bookmarkStart");
var result = readXmlElement(ignoredElement);
assert.deepEqual(result.messages, []);
assert.equal(null, result.value);
});
test("can read inline pictures", function() {
var drawing = new XmlElement("w:drawing", {}, [
new XmlElement("wp:inline", {}, [
new XmlElement("wp:docPr", {descr: "It's a hat"}),
new XmlElement("a:graphic", {}, [
new XmlElement("a:graphicData", {}, [
new XmlElement("pic:pic", {}, [
new XmlElement("pic:blipFill", {}, [
new XmlElement("a:blip", {"r:embed": "rId5"})
])
])
])
])
])
]);
var imageBuffer = new Buffer("Not an image at all!");
var reader = new DocumentXmlReader(
{
"rId5": {target: "media/hat.png"}
},
createFakeDocxFile({
"word/media/hat.png": imageBuffer
})
);
var result = reader.readXmlElement(drawing);
assert.deepEqual(result.messages, []);
assert.equal("image", result.value.type);
assert.equal(result.value.altText, "It's a hat");
return result.value.read()
.then(function(readValue) {
assert.equal(readValue, imageBuffer)
});
});
});
function runWithProperties(children) {
return new XmlElement("w:r", {}, [createRunPropertiesXml(children)]);
}
function createRunPropertiesXml(children) {
return new XmlElement("w:rPr", {}, children);
}
|