diff options
| author | Jonathan Lehman <jonathan.lehman91@gmail.com> | 2017-02-18 11:50:51 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2017-02-18 11:50:51 -0500 |
| commit | 140a1f71532d0a34df2cff7cff688e97dca7c164 (patch) | |
| tree | d8aec937f9ae9e7a835c43cf6ffb777f2d40a898 /lib | |
| parent | c626ef8e1047fb9f27fc4db8c17783a711bd566a (diff) | |
| parent | 35df4eed2249ee7e582cca4d371a78ce92c44d2a (diff) | |
Merge pull request #1 from jdlehman/hyperlink-edge-cases
Hyperlink edge cases
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)); +} |
