aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2013-04-10 21:07:01 +0100
committerMichael Williamson <mike@zwobble.org>2013-04-10 21:07:01 +0100
commit9a4c6832d7f3cd64744ec525ab763617aa675bad (patch)
tree9a4fa47bbccd6f753f94b7a9ff4ad15bd58ea806 /lib
parente67ed8a6715942beed2c90423d81119c200aa2ec (diff)
Explicitly map XML namespaces for safety
Diffstat (limited to 'lib')
-rw-r--r--lib/docx-reader.js14
-rw-r--r--lib/xmlreader.js21
2 files changed, 31 insertions, 4 deletions
diff --git a/lib/docx-reader.js b/lib/docx-reader.js
index 35f1627..1012068 100644
--- a/lib/docx-reader.js
+++ b/lib/docx-reader.js
@@ -87,9 +87,19 @@ function openZip(zipPath) {
}
function readXmlFile(docxFile, path) {
+ var xmlNamespaceMap = {
+ "http://schemas.openxmlformats.org/wordprocessingml/2006/main": "w",
+ "http://schemas.openxmlformats.org/officeDocument/2006/relationships": "r",
+ "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing": "wp",
+ "http://schemas.openxmlformats.org/drawingml/2006/main": "a",
+ "http://schemas.openxmlformats.org/drawingml/2006/picture": "pic"
+ };
+
if (docxFile.exists(path)) {
return docxFile.read(path, "utf-8")
- .then(xmlreader.read);
+ .then(function(xmlString) {
+ return xmlreader.read(xmlString, xmlNamespaceMap);
+ });
} else {
return null;
}
@@ -98,7 +108,7 @@ function readXmlFile(docxFile, path) {
function readRelationships(relationshipsXml) {
var relationships = {};
relationshipsXml.root.children.forEach(function(child) {
- if (child.name === "Relationship") {
+ if (child.name === "{http://schemas.openxmlformats.org/package/2006/relationships}:Relationship") {
relationships[child.attributes.Id] = {
target: child.attributes.Target
};
diff --git a/lib/xmlreader.js b/lib/xmlreader.js
index 7c15019..f5a43ae 100644
--- a/lib/xmlreader.js
+++ b/lib/xmlreader.js
@@ -5,7 +5,9 @@ var _ = require("underscore");
exports.read = read;
exports.Element = Element;
-function read(xmlString) {
+function read(xmlString, namespaceMap) {
+ namespaceMap = namespaceMap || {};
+
var parser = sax.parser(true, {xmlns: true, position: false});
//~ parser.onerror = function(error) {
@@ -25,12 +27,27 @@ function read(xmlString) {
return attribute.value;
});
- var element = new Element(node.name, attributes);
+ var element = new Element(generateTagName(node), attributes);
currentElement.children.push(element);
stack.push(currentElement);
currentElement = element
};
+ function generateTagName(node) {
+ if (node.uri) {
+ var mappedPrefix = namespaceMap[node.uri];
+ var prefix;
+ if (mappedPrefix) {
+ prefix = mappedPrefix;
+ } else {
+ prefix = "{" + node.uri + "}";
+ }
+ return prefix + ":" + node.local;
+ } else {
+ return node.local;
+ }
+ }
+
parser.onclosetag = function(node) {
currentElement = stack.pop();
};