diff options
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/docx/body-reader.js | 33 |
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)); +} |
