aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2024-12-30 10:05:30 +0000
committerMichael Williamson <mike@zwobble.org>2024-12-30 10:05:30 +0000
commit777512b73d0c9807ad654acaccacf587f4a9807a (patch)
treeb5f0438f5428e1f3acb98470eed0012357f91faa
parenta6b7486bb20a24c51c0d2ff6fe6c31325a99858a (diff)
Convert SDT checkboxes to checkbox inputs
-rw-r--r--lib/docx/body-reader.js20
-rw-r--r--lib/docx/office-xml-reader.js5
-rw-r--r--test/docx/body-reader.tests.js40
3 files changed, 63 insertions, 2 deletions
diff --git a/lib/docx/body-reader.js b/lib/docx/body-reader.js
index 3e97d3e..e78c2c4 100644
--- a/lib/docx/body-reader.js
+++ b/lib/docx/body-reader.js
@@ -119,6 +119,10 @@ function BodyReader(options) {
}
}
+ function readBooleanAttributeValue(value) {
+ return value !== "false" && value !== "0";
+ }
+
function readHighlightValue(value) {
if (!value || value === "none") {
return null;
@@ -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,
diff --git a/lib/docx/office-xml-reader.js b/lib/docx/office-xml-reader.js
index f076e2c..43e5250 100644
--- a/lib/docx/office-xml-reader.js
+++ b/lib/docx/office-xml-reader.js
@@ -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"
};
diff --git a/test/docx/body-reader.tests.js b/test/docx/body-reader.tests.js
index ece3af8..96f4922 100644
--- a/test/docx/body-reader.tests.js
+++ b/test/docx/body-reader.tests.js
@@ -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)}));
}
});