diff options
| author | Jonathan Lehman <jonathan.lehman91@gmail.com> | 2017-02-17 12:35:26 -0500 |
|---|---|---|
| committer | Jonathan Lehman <jonathan.lehman91@gmail.com> | 2017-02-17 12:35:26 -0500 |
| commit | c044f44f404bb4e38d38082d2989c5686604bb3e (patch) | |
| tree | 3bd786e0bbdbcca1448fedd3ea6922d072e3cb3f | |
| parent | 75e327ddfff69d7bdbaff994e4662f7c37cc2b85 (diff) | |
Handle nested complex fields
| -rw-r--r-- | lib/docx/body-reader.js | 13 | ||||
| -rw-r--r-- | test/docx/body-reader.tests.js | 44 |
2 files changed, 49 insertions, 8 deletions
diff --git a/lib/docx/body-reader.js b/lib/docx/body-reader.js index 9345e02..e671f53 100644 --- a/lib/docx/body-reader.js +++ b/lib/docx/body-reader.js @@ -20,7 +20,7 @@ function createBodyReader(options) { } function BodyReader(options) { - var fieldHyperlink = null; + var fieldHyperlinks = []; var currentInstrText = []; var relationships = options.relationships; var contentTypes = options.contentTypes; @@ -148,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 validFieldHyperlinks = _.compact(fieldHyperlinks); + if (validFieldHyperlinks.length !== 0) { + children = [new documents.Hyperlink(children, {href: _.last(validFieldHyperlinks)})]; } return new documents.Run(children, properties); @@ -159,12 +160,10 @@ function BodyReader(options) { "w:fldChar": function(element) { var type = element.attributes["w:fldCharType"]; if (type === "end") { - fieldHyperlink = null; + fieldHyperlinks.pop(); } else if (type === "separate") { var hyperlink = parseHyperlinkFieldCode(currentInstrText.join('')); - if (hyperlink !== null) { - fieldHyperlink = hyperlink; - } + fieldHyperlinks.push(hyperlink); currentInstrText = []; } return emptyResult(); diff --git a/test/docx/body-reader.tests.js b/test/docx/body-reader.tests.js index 4cb7bb3..1882f3a 100644 --- a/test/docx/body-reader.tests.js +++ b/test/docx/body-reader.tests.js @@ -154,6 +154,7 @@ test("complex fields", (function() { var hyperlinkInstrText = new XmlElement("w:instrText", {}, [ xml.text(' HYPERLINK "' + uri + '"') ]); + var hyperlinkRunXml = runOfText("this is a hyperlink"); return { "stores instrText returns empty result": function() { @@ -227,7 +228,6 @@ test("complex fields", (function() { }, "can handle split instrText elements": function() { - var hyperlinkRunXml = runOfText("this is a hyperlink"); var hyperlinkInstrTextPart1 = new XmlElement("w:instrText", {}, [ xml.text(" HYPE") ]); @@ -266,6 +266,48 @@ test("complex fields", (function() { }), isEmptyRun )); + }, + + "hyperlink with a nested complex field uses the outer hyperlink": function() { + var authorInstrText = new XmlElement("w:instrText", {}, [ + xml.text(' AUTHOR "John Doe"') + ]); + var paragraphXml = new XmlElement("w:p", {}, [ + beginXml, + beginXml, + authorInstrText, + endXml, + hyperlinkInstrText, + separateXml, + hyperlinkRunXml, + endXml + ]); + var paragraph = readXmlElementValue(paragraphXml); + + assertThat(paragraph.children, contains( + isEmptyRun, + isEmptyRun, + isEmptyRun, + isRun({ + children: contains( + isHyperlink({ + href: uri, + children: [] + }) + ) + }), + isRun({ + children: contains( + isHyperlink({ + href: uri, + children: contains( + isText("this is a hyperlink") + ) + }) + ) + }), + isEmptyRun + )); } }; })()); |
