diff options
| author | Michael Williamson <mike@zwobble.org> | 2023-05-21 11:50:07 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2023-05-21 11:50:07 +0100 |
| commit | 2e234c7d6ef381423b4f7d0ea5dac92dfce58a6b (patch) | |
| tree | 5936ba975234f4879b5d2466d63e7abcfc29c780 /lib/docx | |
| parent | b0d21e0e84801dc742e7635973d0e4c70218746a (diff) | |
Don't read paragraph properties as document element
Diffstat (limited to 'lib/docx')
| -rw-r--r-- | lib/docx/body-reader.js | 65 |
1 files changed, 34 insertions, 31 deletions
diff --git a/lib/docx/body-reader.js b/lib/docx/body-reader.js index ca55bb7..dbb091c 100644 --- a/lib/docx/body-reader.js +++ b/lib/docx/body-reader.js @@ -54,6 +54,19 @@ function BodyReader(options) { return emptyResult(); } + function readParagraphProperties(element) { + return readParagraphStyle(element).map(function(style) { + return { + type: "paragraphProperties", + styleId: style.styleId, + styleName: style.name, + alignment: element.firstOrEmpty("w:jc").attributes["w:val"], + numbering: readNumberingProperties(style.styleId, element.firstOrEmpty("w:numPr"), numbering), + indent: readParagraphIndent(element.firstOrEmpty("w:ind")) + }; + }); + } + function readParagraphIndent(element) { return { start: element.attributes["w:start"] || element.attributes["w:left"], @@ -219,15 +232,14 @@ function BodyReader(options) { var xmlElementReaders = { "w:p": function(element) { - var isDeleted = !!element - .firstOrEmpty("w:pPr") + var paragraphPropertiesElement = element.firstOrEmpty("w:pPr"); + + var isDeleted = !!paragraphPropertiesElement .firstOrEmpty("w:rPr") .first("w:del"); if (isDeleted) { - element.children.filter(function(child) { - return child.name !== "w:pPr"; - }).forEach(function(child) { + element.children.forEach(function(child) { deletedParagraphContents.push(child); }); return emptyResult(); @@ -237,29 +249,15 @@ function BodyReader(options) { childrenXml = deletedParagraphContents.concat(childrenXml); deletedParagraphContents = []; } - return readXmlElements(childrenXml) - .map(function(children) { - var properties = _.find(children, isParagraphProperties); - return new documents.Paragraph( - children.filter(negate(isParagraphProperties)), - properties - ); - }) - .insertExtra(); + return ReadResult.map( + readParagraphProperties(paragraphPropertiesElement), + readXmlElements(childrenXml), + function(properties, children) { + return new documents.Paragraph(children, properties); + } + ).insertExtra(); } }, - "w:pPr": function(element) { - return readParagraphStyle(element).map(function(style) { - return { - type: "paragraphProperties", - styleId: style.styleId, - styleName: style.name, - alignment: element.firstOrEmpty("w:jc").attributes["w:val"], - numbering: readNumberingProperties(style.styleId, element.firstOrEmpty("w:numPr"), numbering), - indent: readParagraphIndent(element.firstOrEmpty("w:ind")) - }; - }); - }, "w:r": function(element) { return readXmlElements(element.children) .map(function(children) { @@ -594,16 +592,13 @@ var ignoreElements = { "w:del": true, "w:footnoteRef": true, "w:endnoteRef": true, + "w:pPr": true, "w:tblPr": true, "w:tblGrid": true, "w:trPr": true, "w:tcPr": true }; -function isParagraphProperties(element) { - return element.type === "paragraphProperties"; -} - function isRunProperties(element) { return element.type === "runProperties"; } @@ -633,7 +628,7 @@ function elementResultWithMessages(element, messages) { function ReadResult(element, extra, messages) { this.value = element || []; - this.extra = extra; + this.extra = extra || []; this._result = new Result({ element: this.value, extra: extra @@ -668,6 +663,14 @@ ReadResult.prototype.flatMap = function(func) { return new ReadResult(result.value.element, joinElements(this.extra, result.value.extra), result.messages); }; +ReadResult.map = function(first, second, func) { + return new ReadResult( + func(first.value, second.value), + joinElements(first.extra, second.extra), + first.messages.concat(second.messages) + ); +}; + function combineResults(results) { var result = Result.combine(_.pluck(results, "_result")); return new ReadResult( |
