aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/docx/body-reader.js28
1 files changed, 21 insertions, 7 deletions
diff --git a/lib/docx/body-reader.js b/lib/docx/body-reader.js
index e671f53..11aeec5 100644
--- a/lib/docx/body-reader.js
+++ b/lib/docx/body-reader.js
@@ -20,7 +20,7 @@ function createBodyReader(options) {
}
function BodyReader(options) {
- var fieldHyperlinks = [];
+ var complexFields = [];
var currentInstrText = [];
var relationships = options.relationships;
var contentTypes = options.contentTypes;
@@ -148,9 +148,9 @@ function BodyReader(options) {
var properties = _.find(children, isRunProperties);
children = children.filter(negate(isRunProperties));
- var validFieldHyperlinks = _.compact(fieldHyperlinks);
- if (validFieldHyperlinks.length !== 0) {
- children = [new documents.Hyperlink(children, {href: _.last(validFieldHyperlinks)})];
+ var scopedHyperlink = scopedComplexFieldOfType(complexFields, "hyperlink");
+ if (scopedHyperlink !== undefined) {
+ children = [new documents.Hyperlink(children, {href: scopedHyperlink.href})];
}
return new documents.Run(children, properties);
@@ -160,10 +160,14 @@ function BodyReader(options) {
"w:fldChar": function(element) {
var type = element.attributes["w:fldCharType"];
if (type === "end") {
- fieldHyperlinks.pop();
+ complexFields.pop();
} else if (type === "separate") {
- var hyperlink = parseHyperlinkFieldCode(currentInstrText.join(''));
- fieldHyperlinks.push(hyperlink);
+ var href = parseHyperlinkFieldCode(currentInstrText.join(''));
+ if (href !== null) {
+ complexFields.push({type: "hyperlink", href: href});
+ } else {
+ complexFields.push(null);
+ }
currentInstrText = [];
}
return emptyResult();
@@ -533,3 +537,13 @@ function parseHyperlinkFieldCode(code) {
return null;
}
}
+
+function complexFieldsOfType(complexFields, type) {
+ return complexFields.filter(function(complexField) {
+ return complexField.type === type;
+ });
+}
+
+function scopedComplexFieldOfType(complexFields, type) {
+ return _.last(complexFieldsOfType(complexFields, type));
+}