diff options
| author | privatmamtora <privat.mamtora@gmail.com> | 2017-11-23 14:33:23 -0500 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2017-11-23 19:33:23 +0000 |
| commit | 7b8e6250f4a088e3bfc16403ae303406b9134726 (patch) | |
| tree | 56d509826a8ce80c04e6390c99194cc55fc68f56 | |
| parent | e164d0ca975584ecdca37fe47ec44e4da6deab0b (diff) | |
Parse paragraph indents
| -rw-r--r-- | lib/documents.js | 9 | ||||
| -rw-r--r-- | lib/docx/body-reader.js | 12 | ||||
| -rw-r--r-- | test/docx/body-reader.tests.js | 53 |
3 files changed, 72 insertions, 2 deletions
diff --git a/lib/documents.js b/lib/documents.js index 100d004..518db6e 100644 --- a/lib/documents.js +++ b/lib/documents.js @@ -31,13 +31,20 @@ function Document(children, options) { function Paragraph(children, properties) { properties = properties || {}; + var indent = properties.indent || {}; return { type: types.paragraph, children: children, styleId: properties.styleId || null, styleName: properties.styleName || null, numbering: properties.numbering || null, - alignment: properties.alignment || null + alignment: properties.alignment || null, + indent: { + start: indent.start || null, + end: indent.end || null, + firstLine: indent.firstLine || null, + hanging: indent.hanging || null + } }; } diff --git a/lib/docx/body-reader.js b/lib/docx/body-reader.js index e21b422..5958efe 100644 --- a/lib/docx/body-reader.js +++ b/lib/docx/body-reader.js @@ -47,6 +47,15 @@ function BodyReader(options) { return emptyResult(); } + function readParagraphIndent(element) { + return { + start: element.attributes["w:start"] || element.attributes["w:left"], + end: element.attributes["w:end"] || element.attributes["w:right"], + firstLine: element.attributes["w:firstLine"], + hanging: element.attributes["w:hanging"] + }; + } + function readRunProperties(element) { return readRunStyle(element).map(function(style) { return { @@ -182,7 +191,8 @@ function BodyReader(options) { styleId: style.styleId, styleName: style.name, alignment: element.firstOrEmpty("w:jc").attributes["w:val"], - numbering: readNumberingProperties(element.firstOrEmpty("w:numPr"), numbering) + numbering: readNumberingProperties(element.firstOrEmpty("w:numPr"), numbering), + indent: readParagraphIndent(element.firstOrEmpty("w:ind")) }; }); }, diff --git a/test/docx/body-reader.tests.js b/test/docx/body-reader.tests.js index 5b6f95b..039a9db 100644 --- a/test/docx/body-reader.tests.js +++ b/test/docx/body-reader.tests.js @@ -94,6 +94,59 @@ test("paragraph has justification read from paragraph properties if present", fu assert.deepEqual(paragraph.alignment, "center"); }); +test("paragraph indent", { + "when w:start is set then start indent is read from w:start": function() { + var paragraphXml = paragraphWithIndent({"w:start": "720", "w:left": "40"}); + var paragraph = readXmlElementValue(paragraphXml); + assert.equal(paragraph.indent.start, "720"); + }, + + "when w:start is not set then start indent is read from w:left": function() { + var paragraphXml = paragraphWithIndent({"w:left": "720"}); + var paragraph = readXmlElementValue(paragraphXml); + assert.equal(paragraph.indent.start, "720"); + }, + + "when w:end is set then end indent is read from w:end": function() { + var paragraphXml = paragraphWithIndent({"w:end": "720", "w:right": "40"}); + var paragraph = readXmlElementValue(paragraphXml); + assert.equal(paragraph.indent.end, "720"); + }, + + "when w:end is not set then end indent is read from w:right": function() { + var paragraphXml = paragraphWithIndent({"w:right": "720"}); + var paragraph = readXmlElementValue(paragraphXml); + assert.equal(paragraph.indent.end, "720"); + }, + + "paragraph has indent firstLine read from paragraph properties if present": function() { + var paragraphXml = paragraphWithIndent({"w:firstLine": "720"}); + var paragraph = readXmlElementValue(paragraphXml); + assert.equal(paragraph.indent.firstLine, "720"); + }, + + "paragraph has indent hanging read from paragraph properties if present": function() { + var paragraphXml = paragraphWithIndent({"w:hanging": "720"}); + var paragraph = readXmlElementValue(paragraphXml); + assert.equal(paragraph.indent.hanging, "720"); + }, + + "when indent attributes aren't set then indents are null": function() { + var paragraphXml = paragraphWithIndent({}); + var paragraph = readXmlElementValue(paragraphXml); + assert.equal(paragraph.indent.start, null); + assert.equal(paragraph.indent.end, null); + assert.equal(paragraph.indent.firstLine, null); + assert.equal(paragraph.indent.hanging, null); + } +}); + +function paragraphWithIndent(indentAttributes) { + var indentXml = new XmlElement("w:ind", indentAttributes, []); + var propertiesXml = new XmlElement("w:pPr", {}, [indentXml]); + return new XmlElement("w:p", {}, [propertiesXml]); +} + test("paragraph has numbering properties from paragraph properties if present", function() { var numberingPropertiesXml = new XmlElement("w:numPr", {}, [ new XmlElement("w:ilvl", {"w:val": "1"}), |
