diff options
| author | Michael Williamson <mike@zwobble.org> | 2016-10-04 21:24:17 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2016-10-04 21:24:17 +0100 |
| commit | 095dc4513c3eeadb4dbe23d830156c9b0c39e875 (patch) | |
| tree | 3f0e545986db9a25a135fcf69a553bf0dd5e81e5 | |
| parent | 90351d1f4a8d7bf89cfc09e5abe83564257c45d9 (diff) | |
Ignore run boolean elements that have value of false
| -rw-r--r-- | NEWS | 5 | ||||
| -rw-r--r-- | lib/docx/body-reader.js | 17 | ||||
| -rw-r--r-- | test/docx/body-reader.tests.js | 44 |
3 files changed, 62 insertions, 4 deletions
@@ -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. diff --git a/lib/docx/body-reader.js b/lib/docx/body-reader.js index 4a1655e..30f8bc9 100644 --- a/lib/docx/body-reader.js +++ b/lib/docx/body-reader.js @@ -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; @@ -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); } diff --git a/test/docx/body-reader.tests.js b/test/docx/body-reader.tests.js index 62178ec..01b6503 100644 --- a/test/docx/body-reader.tests.js +++ b/test/docx/body-reader.tests.js @@ -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); @@ -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); |
