aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorJonathan Lehman <jonathan.lehman91@gmail.com>2017-02-18 11:40:43 -0500
committerJonathan Lehman <jonathan.lehman91@gmail.com>2017-02-18 11:49:48 -0500
commit2555036e27441428666e08408a082346fbef3b62 (patch)
treed75460f52224c86f72d42ba841b3e6d507853163 /lib
parentc044f44f404bb4e38d38082d2989c5686604bb3e (diff)
Store all complex fields with types
- Currently only stores hyperlink complex fields and all other fields are null
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));
+}