Skip to content

Commit

Permalink
Read checked for complex field checkboxes
Browse files Browse the repository at this point in the history
  • Loading branch information
mwilliamson committed Nov 30, 2024
1 parent 9272cde commit 4289436
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,9 @@ private ComplexField parseInstrText(String instrText, XmlElementLike fldChar) {
.findChildOrEmpty("w:ffData")
.findChildOrEmpty("w:checkBox");

boolean checked = readBooleanElement(checkboxElement, "w:default");
boolean checked = checkboxElement.hasChild("w:checked")
? readBooleanElement(checkboxElement, "w:checked")
: readBooleanElement(checkboxElement, "w:default");

return ComplexField.checkbox(checked);
}
Expand Down
40 changes: 40 additions & 0 deletions src/test/java/org/zwobble/mammoth/tests/docx/BodyXmlTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,46 @@ public void complexFieldCheckboxWithDefault1AndWithoutCheckedIsChecked() {
)));
}

@Test
public void complexFieldCheckboxWithDefault1AndChecked0IsUnchecked() {
XmlElement element = complexFieldCheckboxParagraph(list(
element("w:checkBox", list(
element("w:default", map("w:val", "1")),
element("w:checked", map("w:val", "0"))
))
));

DocumentElement paragraph = readSuccess(bodyReader(), element);

assertThat(paragraph, isParagraph(hasChildren(
isEmptyRun(),
isEmptyRun(),
isRun(hasChildren(
isCheckbox(false)
))
)));
}

@Test
public void complexFieldCheckboxWithDefault0AndChecked1IsUnchecked() {
XmlElement element = complexFieldCheckboxParagraph(list(
element("w:checkBox", list(
element("w:default", map("w:val", "0")),
element("w:checked", map("w:val", "1"))
))
));

DocumentElement paragraph = readSuccess(bodyReader(), element);

assertThat(paragraph, isParagraph(hasChildren(
isEmptyRun(),
isEmptyRun(),
isRun(hasChildren(
isCheckbox(true)
))
)));
}

private XmlElement complexFieldCheckboxParagraph(List<XmlNode> ffDataChildren) {
return element("w:p", list(
element("w:r", list(
Expand Down

0 comments on commit 4289436

Please sign in to comment.