Skip to content

Commit

Permalink
feat: add paragraph border properties
Browse files Browse the repository at this point in the history
  • Loading branch information
tripodsan committed Apr 22, 2024
1 parent b842263 commit ea25955
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
3 changes: 2 additions & 1 deletion lib/documents.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ function Paragraph(children, properties) {
end: indent.end || null,
firstLine: indent.firstLine || null,
hanging: indent.hanging || null
}
},
border: properties.border || null
};
}

Expand Down
31 changes: 30 additions & 1 deletion lib/docx/body-reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,40 @@ function BodyReader(options) {
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"))
indent: readParagraphIndent(element.firstOrEmpty("w:ind")),
border: readParagraphBorderProperties(element.firstOrEmpty("w:pBdr"))
};
});
}

function readParagraphBorderProperties(element) {
var top = readBorderProperties(element.first("w:top"));
var bottom = readBorderProperties(element.first("w:bottom"));
if (!top && !bottom) {
return null;
}
var ret = {};
if (top) {
ret.top = top;
}
if (bottom) {
ret.bottom = bottom;
}
return ret;
}

function readBorderProperties(element) {
if (element) {
return {
type: element.attributes["w:val"],
size: element.attributes["w:sz"],
space: element.attributes["w:space"],
color: element.attributes["w:color"]
};
}
return null;
}

function readParagraphIndent(element) {
return {
start: element.attributes["w:start"] || element.attributes["w:left"],
Expand Down
15 changes: 15 additions & 0 deletions test/docx/body-reader.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,21 @@ test("paragraph has numbering from paragraph style if present", function() {
assert.deepEqual(paragraph.numbering, {level: "1", isOrdered: true});
});

test("paragraph can have top and border ", function() {
var propertiesXml = new XmlElement("w:pPr", {}, [
new XmlElement("w:pBdr", {}, [
new XmlElement("w:top", {"w:val": "single", "w:sz": "6", "w:space": "1", "w:color": "auto"}, []),
new XmlElement("w:bottom", {"w:val": "single", "w:sz": "5", "w:space": "2", "w:color": "red"}, [])
])
]);
var paragraphXml = new XmlElement("w:p", {}, [propertiesXml]);
var paragraph = readXmlElementValue(paragraphXml);
assert.deepEqual(paragraph.border, {
top: {type: "single", size: 6, space: 1, color: "auto"},
bottom: {type: "single", size: 5, space: 2, color: "red"}
});
});

test("numbering properties in paragraph properties takes precedence over numbering in paragraph style", function() {
var numberingPropertiesXml = new XmlElement("w:numPr", {}, [
new XmlElement("w:ilvl", {"w:val": "1"}),
Expand Down

0 comments on commit ea25955

Please sign in to comment.