Skip to content

Commit

Permalink
Parse checked value of complex field checkbox
Browse files Browse the repository at this point in the history
  • Loading branch information
mwilliamson committed Nov 30, 2024
1 parent 0a86099 commit 500e1c2
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/docx/body-reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,10 @@ function BodyReader(options) {
var checkboxElement = fldChar
.firstOrEmpty("w:ffData")
.firstOrEmpty("w:checkBox");
var defaultValue = readBooleanElement(
checkboxElement.first("w:default")
);
var checked = defaultValue;
var checkedElement = checkboxElement.first("w:checked");
var checked = checkedElement == null
? readBooleanElement(checkboxElement.first("w:default"))
: readBooleanElement(checkedElement);
return {type: "checkbox", checked: checked};
}

Expand Down
42 changes: 42 additions & 0 deletions test/docx/body-reader.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,48 @@ test("checkboxes", {

var paragraph = readXmlElementValue(paragraphXml);

assertThat(paragraph.children, contains(
isEmptyRun,
isEmptyRun,
isRun({
children: contains(
isCheckbox({checked: equalTo(true)})
)
})
));
},

"complex field checkbox with w:default=1 and w:checked=0 is unchecked": function() {
var paragraphXml = complexFieldCheckboxParagraph([
xml.element("w:checkBox", {}, [
xml.element("w:default", {"w:val": "1"}),
xml.element("w:checked", {"w:val": "0"})
])
]);

var paragraph = readXmlElementValue(paragraphXml);

assertThat(paragraph.children, contains(
isEmptyRun,
isEmptyRun,
isRun({
children: contains(
isCheckbox({checked: equalTo(false)})
)
})
));
},

"complex field checkbox with w:default=0 and w:checked=1 is checked": function() {
var paragraphXml = complexFieldCheckboxParagraph([
xml.element("w:checkBox", {}, [
xml.element("w:default", {"w:val": "0"}),
xml.element("w:checked", {"w:val": "1"})
])
]);

var paragraph = readXmlElementValue(paragraphXml);

assertThat(paragraph.children, contains(
isEmptyRun,
isEmptyRun,
Expand Down

0 comments on commit 500e1c2

Please sign in to comment.