diff options
| author | Jonathan Lehman <jonathan.lehman91@gmail.com> | 2017-02-17 11:59:38 -0500 |
|---|---|---|
| committer | Jonathan Lehman <jonathan.lehman91@gmail.com> | 2017-02-17 11:59:38 -0500 |
| commit | 75e327ddfff69d7bdbaff994e4662f7c37cc2b85 (patch) | |
| tree | 318fe55904110bdec9131af14e6d2ade3100acf9 | |
| parent | c626ef8e1047fb9f27fc4db8c17783a711bd566a (diff) | |
Handle split instrText elements
| -rw-r--r-- | lib/docx/body-reader.js | 12 | ||||
| -rw-r--r-- | test/docx/body-reader.tests.js | 64 |
2 files changed, 71 insertions, 5 deletions
diff --git a/lib/docx/body-reader.js b/lib/docx/body-reader.js index e92f8b4..9345e02 100644 --- a/lib/docx/body-reader.js +++ b/lib/docx/body-reader.js @@ -21,6 +21,7 @@ function createBodyReader(options) { function BodyReader(options) { var fieldHyperlink = null; + var currentInstrText = []; var relationships = options.relationships; var contentTypes = options.contentTypes; var docxFile = options.docxFile; @@ -159,14 +160,17 @@ function BodyReader(options) { var type = element.attributes["w:fldCharType"]; if (type === "end") { fieldHyperlink = null; + } else if (type === "separate") { + var hyperlink = parseHyperlinkFieldCode(currentInstrText.join('')); + if (hyperlink !== null) { + fieldHyperlink = hyperlink; + } + 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) { diff --git a/test/docx/body-reader.tests.js b/test/docx/body-reader.tests.js index ab4995c..4cb7bb3 100644 --- a/test/docx/body-reader.tests.js +++ b/test/docx/body-reader.tests.js @@ -148,6 +148,9 @@ test("complex fields", (function() { var endXml = new XmlElement("w:r", {}, [ new XmlElement("w:fldChar", {"w:fldCharType": "end"}) ]); + var separateXml = new XmlElement("w:r", {}, [ + new XmlElement("w:fldChar", {"w:fldCharType": "separate"}) + ]); var hyperlinkInstrText = new XmlElement("w:instrText", {}, [ xml.text(' HYPERLINK "' + uri + '"') ]); @@ -160,10 +163,10 @@ test("complex fields", (function() { "runs in a complex field for hyperlinks are read as hyperlinks": function() { var hyperlinkRunXml = runOfText("this is a hyperlink"); - var afterEndXml = runOfText("this will not be a hyperlink"); var paragraphXml = new XmlElement("w:p", {}, [ beginXml, hyperlinkInstrText, + separateXml, hyperlinkRunXml, endXml ]); @@ -175,6 +178,14 @@ test("complex fields", (function() { children: contains( isHyperlink({ href: uri, + children: [] + }) + ) + }), + isRun({ + children: contains( + isHyperlink({ + href: uri, children: contains( isText("this is a hyperlink") ) @@ -190,6 +201,7 @@ test("complex fields", (function() { var paragraphXml = new XmlElement("w:p", {}, [ beginXml, hyperlinkInstrText, + separateXml, endXml, afterEndXml ]); @@ -197,6 +209,14 @@ test("complex fields", (function() { assertThat(paragraph.children, contains( isEmptyRun, + isRun({ + children: contains( + isHyperlink({ + href: uri, + children: [] + }) + ) + }), isEmptyRun, isRun({ children: contains( @@ -204,6 +224,48 @@ 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") + ]); + var hyperlinkInstrTextPart2 = new XmlElement("w:instrText", {}, [ + xml.text('RLINK "' + uri + '"') + ]); + var paragraphXml = new XmlElement("w:p", {}, [ + beginXml, + hyperlinkInstrTextPart1, + hyperlinkInstrTextPart2, + separateXml, + hyperlinkRunXml, + endXml + ]); + var paragraph = readXmlElementValue(paragraphXml); + + assertThat(paragraph.children, contains( + isEmptyRun, + isRun({ + children: contains( + isHyperlink({ + href: uri, + children: [] + }) + ) + }), + isRun({ + children: contains( + isHyperlink({ + href: uri, + children: contains( + isText("this is a hyperlink") + ) + }) + ) + }), + isEmptyRun + )); } }; })()); |
