aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorJonathan Lehman <jonathan.lehman91@gmail.com>2017-02-18 11:50:51 -0500
committerGitHub <noreply@github.com>2017-02-18 11:50:51 -0500
commit140a1f71532d0a34df2cff7cff688e97dca7c164 (patch)
treed8aec937f9ae9e7a835c43cf6ffb777f2d40a898 /lib
parentc626ef8e1047fb9f27fc4db8c17783a711bd566a (diff)
parent35df4eed2249ee7e582cca4d371a78ce92c44d2a (diff)
Merge pull request #1 from jdlehman/hyperlink-edge-cases
Hyperlink edge cases
Diffstat (limited to 'lib')
-rw-r--r--lib/docx/body-reader.js33
1 files changed, 25 insertions, 8 deletions
diff --git a/lib/docx/body-reader.js b/lib/docx/body-reader.js
index e92f8b4..11aeec5 100644
--- a/lib/docx/body-reader.js
+++ b/lib/docx/body-reader.js
@@ -20,7 +20,8 @@ function createBodyReader(options) {
}
function BodyReader(options) {
- var fieldHyperlink = null;
+ var complexFields = [];
+ var currentInstrText = [];
var relationships = options.relationships;
var contentTypes = options.contentTypes;
var docxFile = options.docxFile;
@@ -147,8 +148,9 @@ function BodyReader(options) {
var properties = _.find(children, isRunProperties);
children = children.filter(negate(isRunProperties));
- if (fieldHyperlink !== null) {
- children = [new documents.Hyperlink(children, {href: fieldHyperlink})];
+ var scopedHyperlink = scopedComplexFieldOfType(complexFields, "hyperlink");
+ if (scopedHyperlink !== undefined) {
+ children = [new documents.Hyperlink(children, {href: scopedHyperlink.href})];
}
return new documents.Run(children, properties);
@@ -158,15 +160,20 @@ function BodyReader(options) {
"w:fldChar": function(element) {
var type = element.attributes["w:fldCharType"];
if (type === "end") {
- fieldHyperlink = null;
+ complexFields.pop();
+ } else if (type === "separate") {
+ var href = parseHyperlinkFieldCode(currentInstrText.join(''));
+ if (href !== null) {
+ complexFields.push({type: "hyperlink", href: href});
+ } else {
+ complexFields.push(null);
+ }
+ currentInstrText = [];
}
return emptyResult();
},
"w:instrText": function(element) {
- var hyperlink = parseHyperlinkFieldCode(element.text());
- if (hyperlink !== null) {
- fieldHyperlink = hyperlink;
- }
+ currentInstrText.push(element.text());
return emptyResult();
},
"w:t": function(element) {
@@ -530,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));
+}