Skip to content

Commit

Permalink
Handle highlight value of none
Browse files Browse the repository at this point in the history
  • Loading branch information
mwilliamson committed Jun 13, 2024
1 parent 0546ccd commit d12d00f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
10 changes: 9 additions & 1 deletion lib/docx/body-reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ function BodyReader(options) {
isStrikethrough: readBooleanElement(element.first("w:strike")),
isAllCaps: readBooleanElement(element.first("w:caps")),
isSmallCaps: readBooleanElement(element.first("w:smallCaps")),
highlight: element.firstOrEmpty("w:highlight").attributes["w:val"]
highlight: readHighlightValue(element.firstOrEmpty("w:highlight").attributes["w:val"])
};
});
}
Expand All @@ -118,6 +118,14 @@ function BodyReader(options) {
}
}

function readHighlightValue(value) {
if (!value || value === "none") {
return null;
} else {
return value;
}
}

function readParagraphStyle(element) {
return readStyle(element, "w:pStyle", "Paragraph", styles.findParagraphStyleById);
}
Expand Down
12 changes: 10 additions & 2 deletions test/docx/body-reader.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -758,13 +758,21 @@ test("run has no highlight by default", function() {
});

test("run has highlight read from properties", function() {
var fontXml = new XmlElement("w:highlight", {"w:val": "yellow"});
var runXml = runWithProperties([fontXml]);
var highlightXml = new XmlElement("w:highlight", {"w:val": "yellow"});
var runXml = runWithProperties([highlightXml]);

var run = readXmlElementValue(runXml);
assert.deepEqual(run.highlight, "yellow");
});

test("when highlight is none then run has no highlight", function() {
var highlightXml = new XmlElement("w:highlight", {"w:val": "none"});
var runXml = runWithProperties([highlightXml]);

var run = readXmlElementValue(runXml);
assert.deepEqual(run.highlight, null);
});

test("run properties not included as child of run", function() {
var runStyleXml = new XmlElement("w:rStyle");
var runPropertiesXml = new XmlElement("w:rPr", {}, [runStyleXml]);
Expand Down

0 comments on commit d12d00f

Please sign in to comment.