Skip to content

Commit

Permalink
Ignore run boolean elements that have value of false
Browse files Browse the repository at this point in the history
  • Loading branch information
mwilliamson committed Oct 4, 2016
1 parent 90351d1 commit 095dc45
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 4 deletions.
5 changes: 5 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# 1.2.5

* Ignore bold, italic, underline and strikethrough elements that have a value of
false or 0.

# 1.2.4

* Ignore v:imagedata elements without relationship ID with warning.
Expand Down
17 changes: 13 additions & 4 deletions lib/docx/body-reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ function BodyReader(options) {
properties.verticalAlignment = verticalAlignmentElement.attributes["w:val"];
}

properties.isBold = !!element.first("w:b");
properties.isUnderline = !!element.first("w:u");
properties.isItalic = !!element.first("w:i");
properties.isStrikethrough = !!element.first("w:strike");
properties.isBold = readBooleanElement(element.first("w:b"));
properties.isUnderline = readBooleanElement(element.first("w:u"));
properties.isItalic = readBooleanElement(element.first("w:i"));
properties.isStrikethrough = readBooleanElement(element.first("w:strike"));

return readRunStyle(element).map(function(style) {
properties.styleId = style.styleId;
Expand All @@ -55,6 +55,15 @@ function BodyReader(options) {
});
}

function readBooleanElement(element) {
if (element) {
var value = element.attributes["w:val"];
return value !== "false" && value !== "0";
} else {
return false;
}
}

function readParagraphStyle(element) {
return readStyle(element, "w:pStyle", "Paragraph", styles.findParagraphStyleById);
}
Expand Down
44 changes: 44 additions & 0 deletions test/docx/body-reader.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,13 @@ test("isBold is true if bold element is present", function() {
assert.equal(run.isBold, true);
});

test("isBold is false if bold element is present and w:val is false", function() {
var boldXml = new XmlElement("w:b", {"w:val": "false"});
var runXml = runWithProperties([boldXml]);
var run = readXmlElementValue(runXml);
assert.equal(run.isBold, false);
});

test("isUnderline is false if underline element is not present", function() {
var runXml = runWithProperties([]);
var run = readXmlElementValue(runXml);
Expand Down Expand Up @@ -223,6 +230,43 @@ test("isItalic is true if bold element is present", function() {
assert.equal(run.isItalic, true);
});

var booleanRunProperties = [
{name: "isBold", tagName: "w:b"},
{name: "isUnderline", tagName: "w:u"},
{name: "isItalic", tagName: "w:i"},
{name: "isStrikethrough", tagName: "w:strike"},
];

booleanRunProperties.forEach(function(runProperty) {
test(runProperty.name + " is false if " + runProperty.tagName + " is present and w:val is false", function() {
var propertyXml = new XmlElement(runProperty.tagName, {"w:val": "false"});
var runXml = runWithProperties([propertyXml]);
var run = readXmlElementValue(runXml);
assert.equal(run[runProperty.name], false);
});

test(runProperty.name + " is false if " + runProperty.tagName + " is present and w:val is 0", function() {
var propertyXml = new XmlElement(runProperty.tagName, {"w:val": "0"});
var runXml = runWithProperties([propertyXml]);
var run = readXmlElementValue(runXml);
assert.equal(run[runProperty.name], false);
});

test(runProperty.name + " is true if " + runProperty.tagName + " is present and w:val is true", function() {
var propertyXml = new XmlElement(runProperty.tagName, {"w:val": "true"});
var runXml = runWithProperties([propertyXml]);
var run = readXmlElementValue(runXml);
assert.equal(run[runProperty.name], true);
});

test(runProperty.name + " is true if " + runProperty.tagName + " is present and w:val is 1", function() {
var propertyXml = new XmlElement(runProperty.tagName, {"w:val": "1"});
var runXml = runWithProperties([propertyXml]);
var run = readXmlElementValue(runXml);
assert.equal(run[runProperty.name], true);
});
});

test("run has baseline vertical alignment by default", function() {
var runXml = runWithProperties([]);
var run = readXmlElementValue(runXml);
Expand Down

0 comments on commit 095dc45

Please sign in to comment.