Skip to content

Commit

Permalink
Prefer numbering properties to numbering from paragraph style
Browse files Browse the repository at this point in the history
  • Loading branch information
mwilliamson committed Mar 20, 2024
1 parent f1227a9 commit b826c4a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 8 deletions.
6 changes: 6 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# 1.7.1

* Switch the precedence of numbering properties in paragraph properties and the
numbering in paragraph styles so that the numbering properties in paragraph
properties takes precedence.

# 1.7.0

* Support attributes in HTML paths in style mappings.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,13 @@ private InternalResult<Optional<Style>> findStyleById(
}

private Optional<NumberingLevel> readNumbering(Optional<Style> style, XmlElementLike properties) {
XmlElementLike numberingProperties = properties.findChildOrEmpty("w:numPr");
Optional<String> numId = readVal(numberingProperties, "w:numId");
Optional<String> levelIndex = readVal(numberingProperties, "w:ilvl");
if (numId.isPresent() && levelIndex.isPresent()) {
return numbering.findLevel(numId.get(), levelIndex.get());
}

if (style.isPresent()) {
String styleId = style.get().getStyleId();
Optional<NumberingLevel> level = numbering.findLevelByParagraphStyleId(styleId);
Expand All @@ -362,11 +369,7 @@ private Optional<NumberingLevel> readNumbering(Optional<Style> style, XmlElement
}
}

XmlElementLike numberingProperties = properties.findChildOrEmpty("w:numPr");
return Optionals.flatMap(
readVal(numberingProperties, "w:numId"),
readVal(numberingProperties, "w:ilvl"),
numbering::findLevel);
return Optional.empty();
}

private ParagraphIndent readParagraphIndent(XmlElementLike properties) {
Expand Down
30 changes: 27 additions & 3 deletions src/test/java/org/zwobble/mammoth/tests/docx/BodyXmlTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,31 @@ public void paragraphHasNumberingPropertiesFromParagraphPropertiesIfPresent() {
}

@Test
public void numberingOnParagraphStyleTakesPrecedenceOverNumPr() {
public void paragraphHasNumberingFromParagraphStyleIfPresent() {
XmlElement element = paragraphXml(list(
element("w:pPr", list(
element("w:pStyle", map("w:val", "List"))
))
));

Numbering numbering = numberingMap(map(
"43", map("1", new Numbering.AbstractNumLevel("1", true, Optional.of("List")))
));
Styles styles = new Styles(
map("List", new Style("List", Optional.empty())),
map(),
map(),
map()
);

assertThat(
readSuccess(bodyReader(numbering, styles), element),
hasNumbering(NumberingLevel.ordered("1"))
);
}

@Test
public void numberingPropertiesInParagraphPropertiesTakesPrecedenceOverNumberingInParagraphStyle() {
XmlElement element = paragraphXml(list(
element("w:pPr", list(
element("w:pStyle", map("w:val", "List")),
Expand All @@ -218,8 +242,8 @@ public void numberingOnParagraphStyleTakesPrecedenceOverNumPr() {
));

Numbering numbering = numberingMap(map(
"42", map("1", new Numbering.AbstractNumLevel("1", false, Optional.empty())),
"43", map("1", new Numbering.AbstractNumLevel("1", true, Optional.of("List")))
"42", map("1", new Numbering.AbstractNumLevel("1", true, Optional.empty())),
"43", map("1", new Numbering.AbstractNumLevel("2", true, Optional.of("List")))
));
Styles styles = new Styles(
map("List", new Style("List", Optional.empty())),
Expand Down

0 comments on commit b826c4a

Please sign in to comment.