diff options
| author | Michael Williamson <mike@zwobble.org> | 2017-02-16 11:01:49 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2017-02-16 11:01:49 +0000 |
| commit | 0087555748ea139703ebc905e3ea7591615235f2 (patch) | |
| tree | 8f0165f468826278ddf4174762f19533214778cb /lib | |
| parent | 66e054e0f686dad8cd282dd01660e17b156b58e3 (diff) | |
Don't rely on styles to parse hyperlinks as complex fields
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/docx/body-reader.js | 31 |
1 files changed, 18 insertions, 13 deletions
diff --git a/lib/docx/body-reader.js b/lib/docx/body-reader.js index a247e93..08e1861 100644 --- a/lib/docx/body-reader.js +++ b/lib/docx/body-reader.js @@ -9,7 +9,7 @@ var uriToZipEntryName = require("./uris").uriToZipEntryName; function BodyReader(options) { - var complexFieldData = []; + var fieldHyperlink = null; var relationships = options.relationships; var contentTypes = options.contentTypes; var docxFile = options.docxFile; @@ -134,28 +134,28 @@ function BodyReader(options) { return readXmlElements(element.children) .map(function(children) { var properties = _.find(children, isRunProperties); - var filteredChildren = children.filter(negate(isRunProperties)); + children = children.filter(negate(isRunProperties)); - if (properties && properties.styleId === 'Hyperlink' && complexFieldData.length === 1) { - return new documents.Run( - [new documents.Hyperlink(filteredChildren, {href: parseHyperlinkFieldCode(complexFieldData)})], - properties - ); + if (fieldHyperlink !== null) { + children = [new documents.Hyperlink(children, {href: fieldHyperlink})]; } - return new documents.Run(filteredChildren, properties); + return new documents.Run(children, properties); }); }, "w:rPr": readRunProperties, "w:fldChar": function(element) { var type = element.attributes["w:fldCharType"]; - if (type === 'end') { - complexFieldData = []; + if (type === "end") { + fieldHyperlink = null; } return emptyResult(); }, "w:instrText": function(element) { - complexFieldData.push(element.children[0].value); + var hyperlink = parseHyperlinkFieldCode(element.text()); + if (hyperlink !== null) { + fieldHyperlink = hyperlink; + } return emptyResult(); }, "w:t": function(element) { @@ -509,6 +509,11 @@ function identity(value) { return value; } -function parseHyperlinkFieldCode(complexFieldData) { - return (complexFieldData[0].match(/HYPERLINK "(.*)"/) || [])[1]; +function parseHyperlinkFieldCode(code) { + var result = /HYPERLINK "(.*)"/.exec(code); + if (result) { + return result[1]; + } else { + return null; + } } |
