From 4289436b8210836f6025386ff4d60cf2f8705994 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 30 Nov 2024 14:30:22 +0000 Subject: [PATCH] Read checked for complex field checkboxes --- .../internal/docx/StatefulBodyXmlReader.java | 4 +- .../mammoth/tests/docx/BodyXmlTests.java | 40 +++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/zwobble/mammoth/internal/docx/StatefulBodyXmlReader.java b/src/main/java/org/zwobble/mammoth/internal/docx/StatefulBodyXmlReader.java index 8519192..cdbc96a 100644 --- a/src/main/java/org/zwobble/mammoth/internal/docx/StatefulBodyXmlReader.java +++ b/src/main/java/org/zwobble/mammoth/internal/docx/StatefulBodyXmlReader.java @@ -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); } diff --git a/src/test/java/org/zwobble/mammoth/tests/docx/BodyXmlTests.java b/src/test/java/org/zwobble/mammoth/tests/docx/BodyXmlTests.java index 0336bf3..30796ef 100644 --- a/src/test/java/org/zwobble/mammoth/tests/docx/BodyXmlTests.java +++ b/src/test/java/org/zwobble/mammoth/tests/docx/BodyXmlTests.java @@ -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 ffDataChildren) { return element("w:p", list( element("w:r", list(