Skip to content

Commit

Permalink
Convert SDT checkboxes to checkbox inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
mwilliamson committed Dec 30, 2024
1 parent a6b7486 commit 777512b
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
20 changes: 19 additions & 1 deletion lib/docx/body-reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ function BodyReader(options) {
}
}

function readBooleanAttributeValue(value) {
return value !== "false" && value !== "0";
}

function readHighlightValue(value) {
if (!value || value === "none") {
return null;
Expand Down Expand Up @@ -381,7 +385,21 @@ function BodyReader(options) {
},

"w:sdt": function(element) {
return readXmlElements(element.firstOrEmpty("w:sdtContent").children);
var checkbox = element
.firstOrEmpty("w:sdtPr")
.first("wordml:checkbox");

if (checkbox) {
var checkedElement = checkbox.first("wordml:checked");
var isChecked = !!checkedElement && readBooleanAttributeValue(
checkedElement.attributes["wordml:val"]
);
return elementResult(documents.checkbox({
checked: isChecked
}));
} else {
return readXmlElements(element.firstOrEmpty("w:sdtContent").children);
}
},

"w:ins": readChildElements,
Expand Down
5 changes: 4 additions & 1 deletion lib/docx/office-xml-reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@ var xmlNamespaceMap = {
"http://schemas.openxmlformats.org/package/2006/relationships": "relationships",
"http://schemas.openxmlformats.org/markup-compatibility/2006": "mc",
"urn:schemas-microsoft-com:vml": "v",
"urn:schemas-microsoft-com:office:word": "office-word"
"urn:schemas-microsoft-com:office:word": "office-word",

// [MS-DOCX]: Word Extensions to the Office Open XML (.docx) File Format
// https://learn.microsoft.com/en-us/openspecs/office_standards/ms-docx/b839fe1f-e1ca-4fa6-8c26-5954d0abbccd
"http://schemas.microsoft.com/office/word/2010/wordml": "wordml"
};


Expand Down
40 changes: 40 additions & 0 deletions test/docx/body-reader.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,46 @@ test("checkboxes", {
)
})
));
},

"structured document tag checkbox without checked is not checked": function() {
var sdtXml = xml.element("w:sdt", {}, [
xml.element("w:sdtPr", {}, [
xml.element("wordml:checkbox")
])
]);

var result = readXmlElementValue(sdtXml);

assertThat(result, isCheckbox({checked: equalTo(false)}));
},

"structured document tag checkbox with checked=0 is not checked": function() {
var sdtXml = xml.element("w:sdt", {}, [
xml.element("w:sdtPr", {}, [
xml.element("wordml:checkbox", {}, [
xml.element("wordml:checked", {"wordml:val": "0"})
])
])
]);

var result = readXmlElementValue(sdtXml);

assertThat(result, isCheckbox({checked: equalTo(false)}));
},

"structured document tag checkbox with checked=1 is checked": function() {
var sdtXml = xml.element("w:sdt", {}, [
xml.element("w:sdtPr", {}, [
xml.element("wordml:checkbox", {}, [
xml.element("wordml:checked", {"wordml:val": "1"})
])
])
]);

var result = readXmlElementValue(sdtXml);

assertThat(result, isCheckbox({checked: equalTo(true)}));
}
});

Expand Down

0 comments on commit 777512b

Please sign in to comment.