summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2017-02-16 11:01:49 +0000
committerMichael Williamson <mike@zwobble.org>2017-02-16 11:01:49 +0000
commit0087555748ea139703ebc905e3ea7591615235f2 (patch)
tree8f0165f468826278ddf4174762f19533214778cb
parent66e054e0f686dad8cd282dd01660e17b156b58e3 (diff)
Don't rely on styles to parse hyperlinks as complex fields
-rw-r--r--lib/docx/body-reader.js31
-rw-r--r--test/docx/body-reader.tests.js14
2 files changed, 24 insertions, 21 deletions
diff --git a/lib/docx/body-reader.js b/lib/docx/body-reader.js
index a247e93..08e1861 100644
--- a/lib/docx/body-reader.js
+++ b/lib/docx/body-reader.js
@@ -9,7 +9,7 @@ var uriToZipEntryName = require("./uris").uriToZipEntryName;
function BodyReader(options) {
- var complexFieldData = [];
+ var fieldHyperlink = null;
var relationships = options.relationships;
var contentTypes = options.contentTypes;
var docxFile = options.docxFile;
@@ -134,28 +134,28 @@ function BodyReader(options) {
return readXmlElements(element.children)
.map(function(children) {
var properties = _.find(children, isRunProperties);
- var filteredChildren = children.filter(negate(isRunProperties));
+ children = children.filter(negate(isRunProperties));
- if (properties && properties.styleId === 'Hyperlink' && complexFieldData.length === 1) {
- return new documents.Run(
- [new documents.Hyperlink(filteredChildren, {href: parseHyperlinkFieldCode(complexFieldData)})],
- properties
- );
+ if (fieldHyperlink !== null) {
+ children = [new documents.Hyperlink(children, {href: fieldHyperlink})];
}
- return new documents.Run(filteredChildren, properties);
+ return new documents.Run(children, properties);
});
},
"w:rPr": readRunProperties,
"w:fldChar": function(element) {
var type = element.attributes["w:fldCharType"];
- if (type === 'end') {
- complexFieldData = [];
+ if (type === "end") {
+ fieldHyperlink = null;
}
return emptyResult();
},
"w:instrText": function(element) {
- complexFieldData.push(element.children[0].value);
+ var hyperlink = parseHyperlinkFieldCode(element.text());
+ if (hyperlink !== null) {
+ fieldHyperlink = hyperlink;
+ }
return emptyResult();
},
"w:t": function(element) {
@@ -509,6 +509,11 @@ function identity(value) {
return value;
}
-function parseHyperlinkFieldCode(complexFieldData) {
- return (complexFieldData[0].match(/HYPERLINK "(.*)"/) || [])[1];
+function parseHyperlinkFieldCode(code) {
+ var result = /HYPERLINK "(.*)"/.exec(code);
+ if (result) {
+ return result[1];
+ } else {
+ return null;
+ }
}
diff --git a/test/docx/body-reader.tests.js b/test/docx/body-reader.tests.js
index e0878a6..147684f 100644
--- a/test/docx/body-reader.tests.js
+++ b/test/docx/body-reader.tests.js
@@ -147,7 +147,7 @@ test("stores instrText returns empty result", function() {
assert.deepEqual(instrText, []);
});
-test("run is wrapped in a hyperlink if the runStyle is Hyperlink", function() {
+test("runs in a complex field for hyperlinks are read as hyperlinks", function() {
var uri = "http://example.com";
var beginXml = new XmlElement("w:r", {}, [
new XmlElement("w:fldChar", {"w:fldCharType": "begin"})
@@ -156,8 +156,8 @@ test("run is wrapped in a hyperlink if the runStyle is Hyperlink", function() {
new XmlElement("w:fldChar", {"w:fldCharType": "end"})
]);
var instrTextXml = new XmlElement("w:instrText", {}, [xml.text(' HYPERLINK "' + uri + '"')]);
- var hyperlinkRunXml = createHyperlinkRunXml('this is a hyperlink');
- var afterEndXml = createHyperlinkRunXml('this will not be a hyperlink');
+ var hyperlinkRunXml = runOfText("this is a hyperlink");
+ var afterEndXml = runOfText("this will not be a hyperlink");
var parXml = new XmlElement("w:p", {}, [
beginXml,
instrTextXml,
@@ -165,8 +165,7 @@ test("run is wrapped in a hyperlink if the runStyle is Hyperlink", function() {
endXml,
afterEndXml
]);
- var styles = new Styles({}, {"Hyperlink": {name: "Hyperlink"}});
- var paragraph = readXmlElementValue(parXml, {styles: styles});
+ var paragraph = readXmlElementValue(parXml);
var hyperlinkRun = paragraph.children[1];
var hyperlink = hyperlinkRun.children[0];
@@ -903,10 +902,9 @@ function createLinkedBlip(relationshipId) {
return new XmlElement("a:blip", {"r:link": relationshipId});
}
-function createHyperlinkRunXml(text) {
- var styleXml = new XmlElement("w:rStyle", {"w:val": "Hyperlink"});
+function runOfText(text) {
var textXml = new XmlElement("w:t", {}, [xml.text(text)]);
- return new XmlElement("w:r", {}, [createRunPropertiesXml([styleXml]), textXml]);
+ return new XmlElement("w:r", {}, [textXml]);
}
function assertImageBuffer(element, expectedImageBuffer) {