From 66e054e0f686dad8cd282dd01660e17b156b58e3 Mon Sep 17 00:00:00 2001 From: Jonathan Lehman Date: Mon, 6 Feb 2017 15:35:05 -0500 Subject: Refactor creating hyperlinks from complex fields --- lib/docx/body-reader.js | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) (limited to 'lib') diff --git a/lib/docx/body-reader.js b/lib/docx/body-reader.js index 8e71aa6..a247e93 100644 --- a/lib/docx/body-reader.js +++ b/lib/docx/body-reader.js @@ -6,10 +6,10 @@ var documents = require("../documents"); var Result = require("../results").Result; var warning = require("../results").warning; var uriToZipEntryName = require("./uris").uriToZipEntryName; -var lastInstrText = null; function BodyReader(options) { + var complexFieldData = []; var relationships = options.relationships; var contentTypes = options.contentTypes; var docxFile = options.docxFile; @@ -136,8 +136,11 @@ function BodyReader(options) { var properties = _.find(children, isRunProperties); var filteredChildren = children.filter(negate(isRunProperties)); - if (properties && properties.styleName === 'Hyperlink' && lastInstrText) { - return new documents.Hyperlink(filteredChildren, {href: parseHyperlink(lastInstrText)}); + if (properties && properties.styleId === 'Hyperlink' && complexFieldData.length === 1) { + return new documents.Run( + [new documents.Hyperlink(filteredChildren, {href: parseHyperlinkFieldCode(complexFieldData)})], + properties + ); } return new documents.Run(filteredChildren, properties); @@ -147,12 +150,12 @@ function BodyReader(options) { "w:fldChar": function(element) { var type = element.attributes["w:fldCharType"]; if (type === 'end') { - clearState(); + complexFieldData = []; } return emptyResult(); }, "w:instrText": function(element) { - lastInstrText = (element.children[0] || {}).value || ''; + complexFieldData.push(element.children[0].value); return emptyResult(); }, "w:t": function(element) { @@ -506,10 +509,6 @@ function identity(value) { return value; } -function clearState() { - lastInstrText = null; -} - -function parseHyperlink(instrText) { - return (instrText.match(/HYPERLINK "(.*)"/) || [])[1]; +function parseHyperlinkFieldCode(complexFieldData) { + return (complexFieldData[0].match(/HYPERLINK "(.*)"/) || [])[1]; } -- cgit v1.2.3