Skip to content

Commit

Permalink
Handle complex field checkboxes without separate field char
Browse files Browse the repository at this point in the history
  • Loading branch information
mwilliamson committed Nov 30, 2024
1 parent ce471de commit 888a3bd
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 6 deletions.
19 changes: 13 additions & 6 deletions lib/docx/body-reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,19 +165,17 @@ function BodyReader(options) {
currentInstrText = [];
} else if (type === "end") {
var complexFieldEnd = complexFieldStack.pop();
if (complexFieldEnd.type === "begin") {
complexFieldEnd = parseCurrentInstrText(complexFieldEnd);
}
if (complexFieldEnd.type === "checkbox") {
return elementResult(documents.checkbox({
checked: complexFieldEnd.checked
}));
}
} else if (type === "separate") {
var complexFieldSeparate = complexFieldStack.pop();
var complexField = parseInstrText(
currentInstrText.join(''),
complexFieldSeparate.type === "begin"
? complexFieldSeparate.fldChar
: xml.emptyElement
);
var complexField = parseCurrentInstrText(complexFieldSeparate);
complexFieldStack.push(complexField);
}
return emptyResult();
Expand All @@ -190,6 +188,15 @@ function BodyReader(options) {
return topHyperlink ? topHyperlink.options : null;
}

function parseCurrentInstrText(complexField) {
return parseInstrText(
currentInstrText.join(''),
complexField.type === "begin"
? complexField.fldChar
: xml.emptyElement
);
}

function parseInstrText(instrText, fldChar) {
var externalLinkResult = /\s*HYPERLINK "(.*)"/.exec(instrText);
if (externalLinkResult) {
Expand Down
54 changes: 54 additions & 0 deletions test/docx/body-reader.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,60 @@ test("complex fields", (function() {
})());

test("checkboxes", {
"complex field checkbox without separate is read": function() {
var paragraphXml = xml.element("w:p", {}, [
xml.element("w:r", {}, [
xml.element("w:fldChar", {"w:fldCharType": "begin"})
]),
xml.element("w:instrText", {}, [
xml.text(' FORMCHECKBOX ')
]),
xml.element("w:r", {}, [
xml.element("w:fldChar", {"w:fldCharType": "end"})
])
]);

var paragraph = readXmlElementValue(paragraphXml);

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

"complex field checkbox with separate is read": function() {
var paragraphXml = xml.element("w:p", {}, [
xml.element("w:r", {}, [
xml.element("w:fldChar", {"w:fldCharType": "begin"})
]),
xml.element("w:instrText", {}, [
xml.text(' FORMCHECKBOX ')
]),
xml.element("w:r", {}, [
xml.element("w:fldChar", {"w:fldCharType": "separate"})
]),
xml.element("w:r", {}, [
xml.element("w:fldChar", {"w:fldCharType": "end"})
])
]);

var paragraph = readXmlElementValue(paragraphXml);

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

"complex field checkbox without w:default nor w:checked is unchecked": function() {
var paragraphXml = complexFieldCheckboxParagraph([
xml.element("w:checkBox")
Expand Down

0 comments on commit 888a3bd

Please sign in to comment.