From 53e02510f14a4ed91d1ef5f194be564a83e05fb4 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 22 Apr 2017 15:46:01 +0100 Subject: Handle complex fields that do not have a "separate" fldChar --- NEWS | 4 ++++ lib/docx/body-reader.js | 6 +++++- test/docx/body-reader.tests.js | 27 +++++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/NEWS b/NEWS index 57de2e1..5a12dca 100644 --- a/NEWS +++ b/NEWS @@ -1,3 +1,7 @@ +# 1.3.6 + +* Handle complex fields that do not have a "separate" fldChar. + # 1.3.5 * Add transforms.run. diff --git a/lib/docx/body-reader.js b/lib/docx/body-reader.js index 0bdd2a0..dd76c5c 100644 --- a/lib/docx/body-reader.js +++ b/lib/docx/body-reader.js @@ -99,15 +99,19 @@ function BodyReader(options) { return elementResultWithMessages({styleId: styleId, name: name}, messages); } + var unknownComplexField = {type: "unknown"}; + function readFldChar(element) { var type = element.attributes["w:fldCharType"]; if (type === "begin") { + complexFieldStack.push(unknownComplexField); currentInstrText = []; } else if (type === "end") { complexFieldStack.pop(); } else if (type === "separate") { var href = parseHyperlinkFieldCode(currentInstrText.join('')); - var complexField = href === null ? {type: "unknown"} : {type: "hyperlink", href: href}; + var complexField = href === null ? unknownComplexField : {type: "hyperlink", href: href}; + complexFieldStack.pop(); complexFieldStack.push(complexField); } return emptyResult(); diff --git a/test/docx/body-reader.tests.js b/test/docx/body-reader.tests.js index 1b9a392..ffcc7d8 100644 --- a/test/docx/body-reader.tests.js +++ b/test/docx/body-reader.tests.js @@ -309,6 +309,33 @@ test("complex fields", (function() { isEmptyHyperlinkedRun, isEmptyRun )); + }, + + "field without separate element is ignored": function() { + var hyperlinkRunXml = runOfText("this is a hyperlink"); + var paragraphXml = new XmlElement("w:p", {}, [ + beginXml, + hyperlinkInstrText, + separateXml, + beginXml, + endXml, + hyperlinkRunXml, + endXml + ]); + var paragraph = readXmlElementValue(paragraphXml); + + assertThat(paragraph.children, contains( + isEmptyRun, + isEmptyHyperlinkedRun, + isEmptyHyperlinkedRun, + isEmptyHyperlinkedRun, + isHyperlinkedRun({ + children: contains( + isText("this is a hyperlink") + ) + }), + isEmptyRun + )); } }; })()); -- cgit v1.2.3