diff options
| author | Michael Williamson <mike@zwobble.org> | 2016-07-03 19:19:21 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2016-07-03 19:20:00 +0100 |
| commit | 0b56dfce42c3944cce47e284608e6806f4c839c3 (patch) | |
| tree | 5f96f9c2908f1c5b00a7192e71d4c192abd7cf47 /tests/docx | |
| parent | 627c4a755395e313234bdb1914d168392006c483 (diff) | |
Move tests to test for easier Mocha usage
Diffstat (limited to 'tests/docx')
| -rw-r--r-- | tests/docx/body-reader.tests.js | 724 | ||||
| -rw-r--r-- | tests/docx/content-types-reader.tests.js | 47 | ||||
| -rw-r--r-- | tests/docx/docx-reader.tests.js | 42 | ||||
| -rw-r--r-- | tests/docx/files.tests.js | 94 | ||||
| -rw-r--r-- | tests/docx/notes-reader.tests.js | 39 | ||||
| -rw-r--r-- | tests/docx/numbering-xml.tests.js | 32 | ||||
| -rw-r--r-- | tests/docx/office-xml-reader.tests.js | 26 | ||||
| -rw-r--r-- | tests/docx/style-map.tests.js | 115 | ||||
| -rw-r--r-- | tests/docx/styles-reader.tests.js | 92 |
9 files changed, 0 insertions, 1211 deletions
diff --git a/tests/docx/body-reader.tests.js b/tests/docx/body-reader.tests.js deleted file mode 100644 index 0a808b0..0000000 --- a/tests/docx/body-reader.tests.js +++ /dev/null @@ -1,724 +0,0 @@ -var assert = require("assert"); -var path = require("path"); - -var BodyReader = require("../../lib/docx/body-reader").BodyReader; -var documents = require("../../lib/documents"); -var xml = require("../../lib/xml"); -var XmlElement = xml.Element; -var Numbering = require("../../lib/docx/numbering-xml").Numbering; -var Styles = require("../../lib/docx/styles-reader").Styles; -var warning = require("../../lib/results").warning; - -var testing = require("../testing"); -var test = testing.test; -var createFakeDocxFile = testing.createFakeDocxFile; - -function readXmlElement(element, options) { - options = Object.create(options || {}); - options.styles = options.styles || new Styles({}, {}); - return new BodyReader(options).readXmlElement(element); -} - -function readXmlElementValue(element, options) { - var result = readXmlElement(element, options); - assert.deepEqual(result.messages, []); - return result.value; -} - -var fakeContentTypes = { - findContentType: function(filePath) { - var extensionTypes = { - ".png": "image/png", - ".emf": "image/x-emf" - }; - return extensionTypes[path.extname(filePath)]; - } -}; - -describe("readXmlElement: ", function() { - test("paragraph has no style if it has no properties", function() { - var paragraphXml = new XmlElement("w:p", {}, []); - var paragraph = readXmlElementValue(paragraphXml); - assert.deepEqual(paragraph.styleId, null); - }); - - test("paragraph has style ID and name read from paragraph properties if present", function() { - var styleXml = new XmlElement("w:pStyle", {"w:val": "Heading1"}, []); - var propertiesXml = new XmlElement("w:pPr", {}, [styleXml]); - var paragraphXml = new XmlElement("w:p", {}, [propertiesXml]); - - var styles = new Styles({"Heading1": {name: "Heading 1"}}, {}); - - var paragraph = readXmlElementValue(paragraphXml, {styles: styles}); - assert.deepEqual(paragraph.styleId, "Heading1"); - assert.deepEqual(paragraph.styleName, "Heading 1"); - }); - - test("warning is emitted when paragraph style cannot be found", function() { - var styleXml = new XmlElement("w:pStyle", {"w:val": "Heading1"}, []); - var propertiesXml = new XmlElement("w:pPr", {}, [styleXml]); - var paragraphXml = new XmlElement("w:p", {}, [propertiesXml]); - - var styles = new Styles({}, {}); - - var result = readXmlElement(paragraphXml, {styles: styles}); - var paragraph = result.value; - assert.deepEqual(paragraph.styleId, "Heading1"); - assert.deepEqual(paragraph.styleName, null); - assert.deepEqual(result.messages, [warning("Paragraph style with ID Heading1 was referenced but not defined in the document")]); - }); - - test("paragraph has justification read from paragraph properties if present", function() { - var justificationXml = new XmlElement("w:jc", {"w:val": "center"}, []); - var propertiesXml = new XmlElement("w:pPr", {}, [justificationXml]); - var paragraphXml = new XmlElement("w:p", {}, [propertiesXml]); - var paragraph = readXmlElementValue(paragraphXml); - assert.deepEqual(paragraph.alignment, "center"); - }); - - test("paragraph has numbering properties from paragraph properties if present", function() { - var numberingPropertiesXml = new XmlElement("w:numPr", {}, [ - new XmlElement("w:ilvl", {"w:val": "1"}), - new XmlElement("w:numId", {"w:val": "42"}) - ]); - var propertiesXml = new XmlElement("w:pPr", {}, [numberingPropertiesXml]); - var paragraphXml = new XmlElement("w:p", {}, [propertiesXml]); - - var numbering = new Numbering({"42": {"1": {isOrdered: true, level: "1"}}}); - - var paragraph = readXmlElementValue(paragraphXml, {numbering: numbering}); - assert.deepEqual(paragraph.numbering, {level: "1", isOrdered: true}); - }); - - test("numbering properties are converted to numbering at specified level", function() { - var numberingPropertiesXml = new XmlElement("w:numPr", {}, [ - new XmlElement("w:ilvl", {"w:val": "1"}), - new XmlElement("w:numId", {"w:val": "42"}) - ]); - - var numbering = new Numbering({"42": {"1": {isOrdered: true, level: "1"}}}); - - var reader = new BodyReader({ - numbering: numbering - }); - var numberingLevel = reader._readNumberingProperties(numberingPropertiesXml); - assert.deepEqual(numberingLevel, {level: "1", isOrdered: true}); - }); - - test("numbering properties are ignored if w:ilvl is missing", function() { - var numberingPropertiesXml = new XmlElement("w:numPr", {}, [ - new XmlElement("w:numId", {"w:val": "42"}) - ]); - - var numbering = new Numbering({"42": {"1": {isOrdered: true, level: "1"}}}); - - var reader = new BodyReader({ - numbering: numbering - }); - var numberingLevel = reader._readNumberingProperties(numberingPropertiesXml); - assert.equal(numberingLevel, null); - }); - - test("numbering properties are ignored if w:numId is missing", function() { - var numberingPropertiesXml = new XmlElement("w:numPr", {}, [ - new XmlElement("w:ilvl", {"w:val": "1"}) - ]); - - var numbering = new Numbering({"42": {"1": {isOrdered: true, level: "1"}}}); - - var reader = new BodyReader({ - numbering: numbering - }); - var numberingLevel = reader._readNumberingProperties(numberingPropertiesXml); - assert.equal(numberingLevel, null); - }); - - test("run has no style if it has no properties", function() { - var runXml = runWithProperties([]); - var run = readXmlElementValue(runXml); - assert.deepEqual(run.styleId, null); - }); - - test("run has style ID and name read from run properties if present", function() { - var runStyleXml = new XmlElement("w:rStyle", {"w:val": "Heading1Char"}); - var runXml = runWithProperties([runStyleXml]); - - var styles = new Styles({}, {"Heading1Char": {name: "Heading 1 Char"}}); - - var run = readXmlElementValue(runXml, {styles: styles}); - assert.deepEqual(run.styleId, "Heading1Char"); - assert.deepEqual(run.styleName, "Heading 1 Char"); - }); - - test("warning is emitted when run style cannot be found", function() { - var runStyleXml = new XmlElement("w:rStyle", {"w:val": "Heading1Char"}); - var runXml = runWithProperties([runStyleXml]); - - var styles = new Styles({}, {}); - - var result = readXmlElement(runXml, {styles: styles}); - var run = result.value; - assert.deepEqual(run.styleId, "Heading1Char"); - assert.deepEqual(run.styleName, null); - assert.deepEqual(result.messages, [warning("Run style with ID Heading1Char was referenced but not defined in the document")]); - }); - - test("isBold is false if bold element is not present", function() { - var runXml = runWithProperties([]); - var run = readXmlElementValue(runXml); - assert.deepEqual(run.isBold, false); - }); - - test("isBold is true if bold element is present", function() { - var boldXml = new XmlElement("w:b"); - var runXml = runWithProperties([boldXml]); - var run = readXmlElementValue(runXml); - assert.equal(run.isBold, true); - }); - - test("isUnderline is false if underline element is not present", function() { - var runXml = runWithProperties([]); - var run = readXmlElementValue(runXml); - assert.deepEqual(run.isUnderline, false); - }); - - test("isUnderline is true if underline element is present", function() { - var underlineXml = new XmlElement("w:u"); - var runXml = runWithProperties([underlineXml]); - var run = readXmlElementValue(runXml); - assert.equal(run.isUnderline, true); - }); - - test("isStrikethrough is false if strikethrough element is not present", function() { - var runXml = runWithProperties([]); - var run = readXmlElementValue(runXml); - assert.deepEqual(run.isStrikethrough, false); - }); - - test("isStrikethrough is true if strikethrough element is present", function() { - var strikethroughXml = new XmlElement("w:strike"); - var runXml = runWithProperties([strikethroughXml]); - var run = readXmlElementValue(runXml); - assert.equal(run.isStrikethrough, true); - }); - - test("isItalic is false if bold element is not present", function() { - var runXml = runWithProperties([]); - var run = readXmlElementValue(runXml); - assert.deepEqual(run.isItalic, false); - }); - - test("isItalic is true if bold element is present", function() { - var italicXml = new XmlElement("w:i"); - var runXml = runWithProperties([italicXml]); - var run = readXmlElementValue(runXml); - assert.equal(run.isItalic, true); - }); - - test("run has baseline vertical alignment by default", function() { - var runXml = runWithProperties([]); - var run = readXmlElementValue(runXml); - assert.deepEqual(run.verticalAlignment, documents.verticalAlignment.baseline); - }); - - test("run has vertical alignment read from properties", function() { - var verticalAlignmentXml = new XmlElement("w:vertAlign", {"w:val": "superscript"}); - var runXml = runWithProperties([verticalAlignmentXml]); - - var run = readXmlElementValue(runXml); - assert.deepEqual(run.verticalAlignment, documents.verticalAlignment.superscript); - }); - - test("run properties not included as child of run", function() { - var runStyleXml = new XmlElement("w:rStyle"); - var runPropertiesXml = new XmlElement("w:rPr", {}, [runStyleXml]); - var runXml = new XmlElement("w:r", {}, [runPropertiesXml]); - var result = readXmlElement(runXml); - assert.deepEqual(result.value.children, []); - }); - - test("w:tab is read as document tab element", function() { - var tabXml = new XmlElement("w:tab"); - var result = readXmlElement(tabXml); - assert.deepEqual(result.value, new documents.Tab()); - }); - - test("w:tbl is read as document table element", function() { - var tableXml = new XmlElement("w:tbl", {}, [ - new XmlElement("w:tr", {}, [ - new XmlElement("w:tc", {}, [ - new XmlElement("w:p", {}, []) - ]) - ]) - ]); - var result = readXmlElement(tableXml); - assert.deepEqual(result.value, new documents.Table([ - new documents.TableRow([ - new documents.TableCell([ - new documents.Paragraph([]) - ]) - ]) - ])); - }); - - test("w:gridSpan is read as colSpan for table cell", function() { - var tableXml = new XmlElement("w:tbl", {}, [ - new XmlElement("w:tr", {}, [ - new XmlElement("w:tc", {}, [ - new XmlElement("w:tcPr", {}, [ - new XmlElement("w:gridSpan", {"w:val": "2"}) - ]), - new XmlElement("w:p", {}, []) - ]) - ]) - ]); - var result = readXmlElement(tableXml); - assert.deepEqual(result.value, new documents.Table([ - new documents.TableRow([ - new documents.TableCell([ - new documents.Paragraph([]) - ], {colSpan: 2}) - ]) - ])); - }); - - test("w:vMerge is read as rowSpan for table cell", function() { - var tableXml = new XmlElement("w:tbl", {}, [ - row(emptyCell()), - row(emptyCell(vMerge("restart"))), - row(emptyCell(vMerge("continue"))), - row(emptyCell(vMerge("continue"))), - row(emptyCell()) - ]); - var result = readXmlElement(tableXml); - assert.deepEqual(result.value, new documents.Table([ - docRow([docEmptyCell()]), - docRow([docEmptyCell({rowSpan: 3})]), - docRow([]), - docRow([]), - docRow([docEmptyCell()]) - ])); - }); - - test("w:vMerge without val is treated as continue", function() { - var tableXml = new XmlElement("w:tbl", {}, [ - row(emptyCell(vMerge("restart"))), - row(emptyCell(vMerge())) - ]); - var result = readXmlElement(tableXml); - assert.deepEqual(result.value, new documents.Table([ - docRow([docEmptyCell({rowSpan: 2})]), - docRow([]) - ])); - }); - - test("w:vMerge accounts for cells spanning columns", function() { - var tableXml = new XmlElement("w:tbl", {}, [ - row(emptyCell(), emptyCell(), emptyCell(vMerge("restart"))), - row(emptyCell(gridSpan("2")), emptyCell(vMerge("continue"))), - row(emptyCell(), emptyCell(), emptyCell(vMerge("continue"))), - row(emptyCell(), emptyCell(), emptyCell()) - ]); - var result = readXmlElement(tableXml); - assert.deepEqual(result.value, new documents.Table([ - docRow([docEmptyCell(), docEmptyCell(), docEmptyCell({rowSpan: 3})]), - docRow([docEmptyCell({colSpan: 2})]), - docRow([docEmptyCell(), docEmptyCell()]), - docRow([docEmptyCell(), docEmptyCell(), docEmptyCell()]) - ])); - }); - - test("no vertical cell merging if merged cells do not line up", function() { - var tableXml = new XmlElement("w:tbl", {}, [ - row(emptyCell(gridSpan("2"), vMerge("restart"))), - row(emptyCell(), emptyCell(vMerge("continue"))) - ]); - var result = readXmlElement(tableXml); - assert.deepEqual(result.value, new documents.Table([ - docRow([docEmptyCell({colSpan: 2})]), - docRow([docEmptyCell(), docEmptyCell()]) - ])); - }); - - test("warning if non-row in table", function() { - var tableXml = new XmlElement("w:tbl", {}, [ - new XmlElement("w:p") - ]); - var result = readXmlElement(tableXml); - assert.deepEqual(result.messages, [warning("unexpected non-row element in table, cell merging may be incorrect")]); - }); - - test("warning if non-cell in table row", function() { - var tableXml = new XmlElement("w:tbl", {}, [ - row(new XmlElement("w:p")) - ]); - var result = readXmlElement(tableXml); - assert.deepEqual(result.messages, [warning("unexpected non-cell element in table row, cell merging may be incorrect")]); - }); - - function row() { - return new XmlElement("w:tr", {}, Array.prototype.slice.call(arguments)); - } - - function emptyCell() { - return new XmlElement("w:tc", {}, [ - new XmlElement("w:tcPr", {}, Array.prototype.slice.call(arguments)) - ]); - } - - function vMerge(val) { - return new XmlElement("w:vMerge", {"w:val": val}, []); - } - - function gridSpan(val) { - return new XmlElement("w:gridSpan", {"w:val": val}); - } - - function docRow(children) { - return new documents.TableRow(children); - } - - function docEmptyCell(properties) { - return new documents.TableCell([], properties); - } - - test("emits warning on unrecognised element", function() { - var unrecognisedElement = new XmlElement("w:not-an-element"); - var result = readXmlElement(unrecognisedElement); - assert.deepEqual( - result.messages, - [{ - type: "warning", - message: "An unrecognised element was ignored: w:not-an-element" - }] - ); - }); - - test("w:bookmarkStart is read as a bookmarkStart", function() { - var bookmarkStart = new XmlElement("w:bookmarkStart", {"w:name": "_Peter", "w:id": "42"}); - var result = readXmlElement(bookmarkStart); - assert.deepEqual(result.value.name, "_Peter"); - assert.deepEqual(result.value.type, "bookmarkStart"); - }); - - test('_GoBack bookmark is ignored', function() { - var bookmarkStart = new XmlElement("w:bookmarkStart", {"w:name": "_GoBack"}); - var result = readXmlElement(bookmarkStart); - assert.deepEqual(result.value, []); - }); - - test("can read imagedata elements with r:id attribute", function() { - var imagedataElement = new XmlElement("v:imagedata", {"r:id": "rId5", "o:title": "It's a hat"}); - - var imageBuffer = new Buffer("Not an image at all!"); - var reader = new BodyReader({ - relationships: { - "rId5": {target: "media/hat.png"} - }, - contentTypes: fakeContentTypes, - docxFile: createFakeDocxFile({ - "word/media/hat.png": imageBuffer - }) - }); - var result = reader.readXmlElement(imagedataElement); - assert.deepEqual(result.messages, []); - var element = result.value; - assert.equal("image", element.type); - assert.equal(element.altText, "It's a hat"); - assert.equal(element.contentType, "image/png"); - return element.read() - .then(function(readValue) { - assert.equal(readValue, imageBuffer); - }); - }); - - test("can read inline pictures", function() { - var drawing = createInlineImage({ - blip: createEmbeddedBlip("rId5"), - description: "It's a hat" - }); - - var imageBuffer = new Buffer("Not an image at all!"); - var reader = new BodyReader({ - relationships: { - "rId5": {target: "media/hat.png"} - }, - contentTypes: fakeContentTypes, - docxFile: createFakeDocxFile({ - "word/media/hat.png": imageBuffer - }) - }); - var result = reader.readXmlElement(drawing); - assert.deepEqual(result.messages, []); - var element = single(result.value); - assert.equal("image", element.type); - assert.equal(element.altText, "It's a hat"); - assert.equal(element.contentType, "image/png"); - return element.read() - .then(function(readValue) { - assert.equal(readValue, imageBuffer); - }); - }); - - test("can read anchored pictures", function() { - var drawing = new XmlElement("w:drawing", {}, [ - new XmlElement("wp:anchor", {}, [ - new XmlElement("wp:docPr", {descr: "It's a hat"}), - new XmlElement("a:graphic", {}, [ - new XmlElement("a:graphicData", {}, [ - new XmlElement("pic:pic", {}, [ - new XmlElement("pic:blipFill", {}, [ - new XmlElement("a:blip", {"r:embed": "rId5"}) - ]) - ]) - ]) - ]) - ]) - ]); - - var imageBuffer = new Buffer("Not an image at all!"); - var reader = new BodyReader({ - relationships: { - "rId5": {target: "media/hat.png"} - }, - contentTypes: fakeContentTypes, - docxFile: createFakeDocxFile({ - "word/media/hat.png": imageBuffer - }) - }); - var result = reader.readXmlElement(drawing); - assert.deepEqual(result.messages, []); - var element = single(result.value); - assert.equal("image", element.type); - assert.equal(element.altText, "It's a hat"); - return element.read() - .then(function(readValue) { - assert.equal(readValue, imageBuffer); - }); - }); - - test("can read linked pictures", function() { - var drawing = createInlineImage({ - blip: createLinkedBlip("rId5"), - description: "It's a hat" - }); - - var imageBuffer = new Buffer("Not an image at all!"); - var reader = new BodyReader({ - relationships: { - "rId5": {target: "file:///media/hat.png"} - }, - contentTypes: fakeContentTypes, - files: testing.createFakeFiles({ - "file:///media/hat.png": imageBuffer - }) - }); - var result = reader.readXmlElement(drawing); - assert.deepEqual(result.messages, []); - var element = single(result.value); - assert.equal("image", element.type); - assert.equal(element.altText, "It's a hat"); - assert.equal(element.contentType, "image/png"); - return element.read() - .then(function(readValue) { - assert.equal(readValue, imageBuffer); - }); - }); - - test("warning if unsupported image type", function() { - var drawing = createInlineImage({ - blip: createEmbeddedBlip("rId5"), - description: "It's a hat" - }); - - var imageBuffer = new Buffer("Not an image at all!"); - var reader = new BodyReader({ - relationships: { - "rId5": {target: "media/hat.emf"} - }, - contentTypes: fakeContentTypes, - docxFile: createFakeDocxFile({ - "word/media/hat.emf": imageBuffer - }) - }); - var result = reader.readXmlElement(drawing); - assert.deepEqual(result.messages, [warning("Image of type image/x-emf is unlikely to display in web browsers")]); - var element = single(result.value); - assert.equal(element.contentType, "image/x-emf"); - }); - - test("no elements created if image cannot be found in w:drawing", function() { - var drawing = new XmlElement("w:drawing", {}, []); - - var reader = new BodyReader({}); - var result = reader.readXmlElement(drawing); - assert.deepEqual(result.messages, []); - assert.deepEqual(result.value, []); - }); - - test("no elements created if image cannot be found in wp:inline", function() { - var drawing = new XmlElement("wp:inline", {}, []); - - var reader = new BodyReader({}); - var result = reader.readXmlElement(drawing); - assert.deepEqual(result.messages, []); - assert.deepEqual(result.value, []); - }); - - test("children of w:ins are converted normally", function() { - var runXml = new XmlElement("w:r", {}, []); - var insXml = new XmlElement("w:ins", {}, [runXml]); - var result = readXmlElement(insXml); - assert.deepEqual(result.value[0].type, "run"); - }); - - test("children of w:smartTag are converted normally", function() { - var runXml = new XmlElement("w:r", {}, []); - var smartTagXml = new XmlElement("w:smartTag", {}, [runXml]); - var result = readXmlElement(smartTagXml); - assert.deepEqual(result.value[0].type, "run"); - }); - - test("w:hyperlink is read as document hyperlink if it has a relationship ID", function() { - var runXml = new XmlElement("w:r", {}, []); - var hyperlinkXml = new XmlElement("w:hyperlink", {"r:id": "r42"}, [runXml]); - var relationships = { - "r42": {target: "http://example.com"} - }; - var result = readXmlElement(hyperlinkXml, {relationships: relationships}); - assert.deepEqual(result.value.href, "http://example.com"); - assert.deepEqual(result.value.children[0].type, "run"); - }); - - test("w:hyperlink is read as document hyperlink if it has an anchor", function() { - var runXml = new XmlElement("w:r", {}, []); - var hyperlinkXml = new XmlElement("w:hyperlink", {"w:anchor": "_Peter"}, [runXml]); - var result = readXmlElement(hyperlinkXml); - assert.deepEqual(result.value.anchor, "_Peter"); - assert.deepEqual(result.value.children[0].type, "run"); - }); - - test("w:hyperlink is ignored if it does not have a relationship ID nor anchor", function() { - var runXml = new XmlElement("w:r", {}, []); - var hyperlinkXml = new XmlElement("w:hyperlink", {}, [runXml]); - var result = readXmlElement(hyperlinkXml); - assert.deepEqual(result.value[0].type, "run"); - }); - - test("w:br is read as line break", function() { - var breakXml = new XmlElement("w:br", {}, []); - var result = readXmlElement(breakXml); - assert.deepEqual(result.value.type, "lineBreak"); - assert.deepEqual(result.messages, []); - }); - - test("w:bookmarkEnd is ignored without warning", function() { - var ignoredElement = new XmlElement("w:bookmarkEnd"); - var result = readXmlElement(ignoredElement); - assert.deepEqual(result.messages, []); - assert.deepEqual([], result.value); - }); - - test("warning on breaks that aren't line breaks", function() { - var breakXml = new XmlElement("w:br", {"w:type": "page"}, []); - var result = readXmlElement(breakXml); - assert.deepEqual(result.value, []); - assert.deepEqual(result.messages, [warning("Unsupported break type: page")]); - }); - - test("text boxes have content appended after containing paragraph", function() { - var textbox = new XmlElement("w:pict", {}, [ - new XmlElement("v:shape", {}, [ - new XmlElement("v:textbox", {}, [ - new XmlElement("w:txbxContent", {}, [ - paragraphWithStyleId("textbox-content") - ]) - ]) - ]) - ]); - var paragraph = new XmlElement("w:p", {}, [ - new XmlElement("w:r", {}, [textbox]) - ]); - var result = readXmlElement(paragraph); - assert.deepEqual(result.value[1].styleId, "textbox-content"); - }); - - test("mc:Fallback is used when mc:AlternateContent is read", function() { - var styles = new Styles({"first": {name: "First"}, "second": {name: "Second"}}, {}); - var textbox = new XmlElement("mc:AlternateContent", {}, [ - new XmlElement("mc:Choice", {"Requires": "wps"}, [ - paragraphWithStyleId("first") - ]), - new XmlElement("mc:Fallback", {}, [ - paragraphWithStyleId("second") - ]) - ]); - var result = readXmlElement(textbox, {styles: styles}); - assert.deepEqual(result.value[0].styleId, "second"); - }); - - test("w:sdtContent is used when w:sdt is read", function() { - var element = xml.element("w:sdt", {}, [ - xml.element("w:sdtContent", {}, [ - xml.element("w:t", {}, [xml.text("Blackdown")]) - ]) - ]); - var result = readXmlElement(element); - assert.deepEqual(result.value, [new documents.Text("Blackdown")]); - }); - - test("text nodes are ignored when reading children", function() { - var runXml = new XmlElement("w:r", {}, [xml.text("[text]")]); - var run = readXmlElementValue(runXml); - assert.deepEqual(run, new documents.Run([])); - }); -}); - -function paragraphWithStyleId(styleId) { - return new XmlElement("w:p", {}, [ - new XmlElement("w:pPr", {}, [ - new XmlElement("w:pStyle", {"w:val": styleId}, []) - ]) - ]); -} - -function runWithProperties(children) { - return new XmlElement("w:r", {}, [createRunPropertiesXml(children)]); -} - -function createRunPropertiesXml(children) { - return new XmlElement("w:rPr", {}, children); -} - -function single(array) { - if (array.length === 1) { - return array[0]; - } else { - throw new Error("Array has " + array.length + " elements"); - } -} - -function createInlineImage(options) { - return new XmlElement("w:drawing", {}, [ - new XmlElement("wp:inline", {}, [ - new XmlElement("wp:docPr", {descr: options.description}), - new XmlElement("a:graphic", {}, [ - new XmlElement("a:graphicData", {}, [ - new XmlElement("pic:pic", {}, [ - new XmlElement("pic:blipFill", {}, [ - options.blip - ]) - ]) - ]) - ]) - ]) - ]); -} - -function createEmbeddedBlip(relationshipId) { - return new XmlElement("a:blip", {"r:embed": relationshipId}); -} - -function createLinkedBlip(relationshipId) { - return new XmlElement("a:blip", {"r:link": relationshipId}); -} diff --git a/tests/docx/content-types-reader.tests.js b/tests/docx/content-types-reader.tests.js deleted file mode 100644 index a223d09..0000000 --- a/tests/docx/content-types-reader.tests.js +++ /dev/null @@ -1,47 +0,0 @@ -var assert = require("assert"); - -var readContentTypesFromXml = require("../../lib/docx/content-types-reader").readContentTypesFromXml; -var XmlElement = require("../../lib/xml").Element; -var test = require("../testing").test; - - -describe('readContentTypesFromXml', function() { - test('reads default-per-extension from XML', function() { - var contentTypes = readContentTypesFromXml({ - root: new XmlElement("content-types:Types", {}, [ - new XmlElement("content-types:Default", {Extension: "png", ContentType: "image/png"}) - ]) - }); - assert.equal(contentTypes.findContentType("word/media/hat.png"), "image/png"); - }); - - test('reads overrides in preference to defaults', function() { - var contentTypes = readContentTypesFromXml({ - root: new XmlElement("content-types:Types", {}, [ - new XmlElement("content-types:Default", {Extension: "png", ContentType: "image/png"}), - new XmlElement("content-types:Override", {PartName: "/word/media/hat.png", ContentType: "image/hat"}) - ]) - }); - assert.equal(contentTypes.findContentType("word/media/hat.png"), "image/hat"); - }); - - test('fallback content types have common image types', function() { - var contentTypes = readContentTypesFromXml({ - root: new XmlElement("content-types:Types", {}, []) - }); - assert.equal(contentTypes.findContentType("word/media/hat.png"), "image/png"); - assert.equal(contentTypes.findContentType("word/media/hat.gif"), "image/gif"); - assert.equal(contentTypes.findContentType("word/media/hat.jpg"), "image/jpeg"); - assert.equal(contentTypes.findContentType("word/media/hat.jpeg"), "image/jpeg"); - assert.equal(contentTypes.findContentType("word/media/hat.bmp"), "image/bmp"); - assert.equal(contentTypes.findContentType("word/media/hat.tif"), "image/tiff"); - assert.equal(contentTypes.findContentType("word/media/hat.tiff"), "image/tiff"); - }); - - test('fallback content types are case insensitive on extension', function() { - var contentTypes = readContentTypesFromXml({ - root: new XmlElement("content-types:Types", {}, []) - }); - assert.equal(contentTypes.findContentType("word/media/hat.PnG"), "image/png"); - }); -}); diff --git a/tests/docx/docx-reader.tests.js b/tests/docx/docx-reader.tests.js deleted file mode 100644 index 69ade7e..0000000 --- a/tests/docx/docx-reader.tests.js +++ /dev/null @@ -1,42 +0,0 @@ -var assert = require("assert"); - -var docxReader = require("../../lib/docx/docx-reader"); -var documents = require("../../lib/documents"); - -var testing = require("../testing"); -var test = testing.test; -var testData = testing.testData; -var createFakeDocxFile = testing.createFakeDocxFile; - - -describe("docx-reader", function() { - test("can read document with single paragraph with single run of text", function() { - var expectedDocument = documents.Document([ - documents.Paragraph([ - documents.Run([ - documents.Text("Hello.") - ]) - ]) - ]); - var docxFile = createFakeDocxFile({ - "word/document.xml": testData("simple/word/document.xml") - }); - return docxReader.read(docxFile).then(function(result) { - assert.deepEqual(expectedDocument, result.value); - }); - }); - - test("hyperlink hrefs are read from relationships file", function() { - var docxFile = createFakeDocxFile({ - "word/document.xml": testData("hyperlinks/word/document.xml"), - "word/_rels/document.xml.rels": testData("hyperlinks/word/_rels/document.xml.rels") - }); - return docxReader.read(docxFile).then(function(result) { - var paragraph = result.value.children[0]; - assert.equal(1, paragraph.children.length); - var hyperlink = paragraph.children[0]; - assert.equal(hyperlink.href, "http://www.example.com"); - assert.equal(hyperlink.children.length, 1); - }); - }); -}); diff --git a/tests/docx/files.tests.js b/tests/docx/files.tests.js deleted file mode 100644 index 2a0919c..0000000 --- a/tests/docx/files.tests.js +++ /dev/null @@ -1,94 +0,0 @@ -var path = require("path"); -var fs = require("fs"); -var assert = require("assert"); - -var promises = require("../../lib/promises"); -var Files = require("../../lib/docx/files").Files; -var uriToPath = require("../../lib/docx/files").uriToPath; - -var testing = require("../testing"); -var test = testing.test; - -var readFile = promises.promisify(fs.readFile.bind(fs)); - - -describe("Files", function() { - test("can open files with file URI", function() { - var filePath = path.resolve(testing.testPath("tiny-picture.png")); - var files = new Files(null); - return files.read("file:///" + filePath.replace(/^\//, ""), "base64").then(function(contents) { - return readFile(filePath, "base64").then(function(expectedContents) { - assert.deepEqual(contents, expectedContents); - }); - }); - }); - - test("can open files with relative URI", function() { - var filePath = path.resolve(testing.testPath("tiny-picture.png")); - var files = new Files(testing.testPath(".")); - return files.read("tiny-picture.png", "base64").then(function(contents) { - return readFile(filePath, "base64").then(function(expectedContents) { - assert.deepEqual(contents, expectedContents); - }); - }); - }); - - test("given base is not set when opening relative uri then error is raised", function() { - var files = new Files(null); - return assertError(files.read("not-a-real-file.png", "base64"), function(err) { - assert.equal(err.message, "could not find external image 'not-a-real-file.png', path of input document is unknown"); - }); - }); - - test("error if relative uri cannot be opened", function() { - var files = new Files("/tmp"); - return assertError(files.read("not-a-real-file.png", "base64"), function(err) { - assertRegex(err.message, /could not open external image: 'not-a-real-file.png' \(document directory: '\/tmp'\)\nENOENT.*\/tmp\/not-a-real-file.png.*/); - }); - }); -}); - -function assertError(promise, func) { - return promise.then(function() { - assert(false, "Expected error"); - }, func); -} - -function assertRegex(actual, expected) { - assert.ok(expected.test(actual), "Expected regex: " + expected + "\nbut was: " + actual); -} - - -describe("uriToPath", function() { - test("leading slash is retained on non-Windows file URIs", function() { - assert.equal(uriToPath("file:///a/b/c", "linux"), "/a/b/c"); - assert.equal(uriToPath("file:///a/b/c", "win32"), "/a/b/c"); - }); - - test("URI is unquoted", function() { - assert.equal(uriToPath("file:///a%20b"), "/a b"); - }); - - test("when host is set to localhost then path can be found", function() { - assert.equal(uriToPath("file://localhost/a/b/c"), "/a/b/c"); - }); - - test("when host is set but not localhost then path cannot be found", function() { - assert.throws(function() { - uriToPath("file://example/a/b/c"); - }, /Could not convert URI to path: file:\/\/example\/a\/b\/c/); - }); - - test("leading slash is not dropped on Windows file URIs when platform is not Windows", function() { - assert.equal(uriToPath("file:///c:/a", "linux"), "/c:/a"); - }); - - test("leading slash is dropped on Windows file URIs when platform is Windows", function() { - assert.equal(uriToPath("file:///c:/a", "win32"), "c:/a"); - assert.equal(uriToPath("file:///C:/a", "win32"), "C:/a"); - }); - - test("relative URI is unquoted", function() { - assert.equal(uriToPath("a%20b/c"), "a b/c"); - }); -}); diff --git a/tests/docx/notes-reader.tests.js b/tests/docx/notes-reader.tests.js deleted file mode 100644 index 79ffed3..0000000 --- a/tests/docx/notes-reader.tests.js +++ /dev/null @@ -1,39 +0,0 @@ -var assert = require("assert"); - -var createFootnotesReader = require("../../lib/docx/notes-reader").createFootnotesReader; -var BodyReader = require("../../lib/docx/body-reader").BodyReader; -var documents = require("../../lib/documents"); -var XmlElement = require("../../lib/xml").Element; -var test = require("../testing").test; - - -describe('readFootnotesXml', function() { - test('ID and body of footnote are read', function() { - var bodyReader = new BodyReader({}); - var footnoteBody = [new XmlElement("w:p", {}, [])]; - var footnotes = createFootnotesReader(bodyReader)({ - root: new XmlElement("w:footnotes", {}, [ - new XmlElement("w:footnote", {"w:id": "1"}, footnoteBody) - ]) - }); - assert.equal(footnotes.value.length, 1); - assert.deepEqual(footnotes.value[0].body, [new documents.Paragraph([])]); - assert.deepEqual(footnotes.value[0].noteId, "1"); - }); -}); - -footnoteTypeIsIgnored('continuationSeparator'); -footnoteTypeIsIgnored('separator'); - -function footnoteTypeIsIgnored(type) { - describe('footnotes of type ' + type + ' are ignored', function() { - test('ID and body of footnote are read', function() { - var footnotes = createFootnotesReader()({ - root: new XmlElement("w:footnotes", {}, [ - new XmlElement("w:footnote", {"w:id": "1", "w:type": type}, []) - ]) - }); - assert.equal(footnotes.value.length, 0); - }); - }); -} diff --git a/tests/docx/numbering-xml.tests.js b/tests/docx/numbering-xml.tests.js deleted file mode 100644 index 2fea5fb..0000000 --- a/tests/docx/numbering-xml.tests.js +++ /dev/null @@ -1,32 +0,0 @@ -var duck = require("duck"); - -var readNumberingXml = require("../../lib/docx/numbering-xml").readNumberingXml; -var XmlElement = require("../../lib/xml").Element; -var test = require("../testing").test; - - -describe('readNumberingXml', function() { - test('w:num element inherits levels from w:abstractNum', function() { - var numbering = readNumberingXml({ - root: new XmlElement("w:numbering", {}, [ - new XmlElement("w:abstractNum", {"w:abstractNumId": "42"}, [ - new XmlElement("w:lvl", {"w:ilvl": "0"}, [ - new XmlElement("w:numFmt", {"w:val": "bullet"}) - ]), - new XmlElement("w:lvl", {"w:ilvl": "1"}, [ - new XmlElement("w:numFmt", {"w:val": "decimal"}) - ]) - ]), - new XmlElement("w:num", {"w:numId": "47"}, [ - new XmlElement("w:abstractNumId", {"w:val": "42"}) - ]) - ]) - }); - duck.assertThat(numbering.findLevel("47", "0"), duck.hasProperties({ - isOrdered: false - })); - duck.assertThat(numbering.findLevel("47", "1"), duck.hasProperties({ - isOrdered: true - })); - }); -}); diff --git a/tests/docx/office-xml-reader.tests.js b/tests/docx/office-xml-reader.tests.js deleted file mode 100644 index 9d202ba..0000000 --- a/tests/docx/office-xml-reader.tests.js +++ /dev/null @@ -1,26 +0,0 @@ -var assert = require("assert"); - -var xml = require("../../lib/xml"); -var officeXmlReader = require("../../lib/docx/office-xml-reader"); -var test = require("../testing").test; - - -describe("office-xml-reader", function() { - test("mc:AlternateContent is replaced by contents of mc:Fallback", function() { - var xmlString = - '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' + - '<numbering xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006">' + - '<mc:AlternateContent>' + - '<mc:Choice Requires="w14">' + - '<choice/>' + - '</mc:Choice>' + - '<mc:Fallback>' + - '<fallback/>' + - '</mc:Fallback>' + - '</mc:AlternateContent>' + - '</numbering>'; - return officeXmlReader.read(xmlString).then(function(element) { - assert.deepEqual(element.root.children, [xml.element("fallback")]); - }); - }); -}); diff --git a/tests/docx/style-map.tests.js b/tests/docx/style-map.tests.js deleted file mode 100644 index 55171f8..0000000 --- a/tests/docx/style-map.tests.js +++ /dev/null @@ -1,115 +0,0 @@ -var assert = require("assert"); - -var JSZip = require("jszip"); - -var zipfile = require("../../lib/zipfile"); -var styleMap = require("../../lib/docx/style-map"); -var test = require("../testing").test; - -describe("style-map", function() { - test('reading embedded style map on document without embedded style map returns null', function() { - var zip = normalDocx(); - - return styleMap.readStyleMap(zip).then(function(contents) { - assert.equal(contents, null); - }); - }); - - test('embedded style map can be read after being written', function() { - var zip = normalDocx(); - - return styleMap.writeStyleMap(zip, "p => h1").then(function() { - return styleMap.readStyleMap(zip).then(function(contents) { - assert.equal(contents, "p => h1"); - }); - }); - }); - - test('embedded style map is written to separate file', function() { - var zip = normalDocx(); - - return styleMap.writeStyleMap(zip, "p => h1").then(function() { - return zip.read("mammoth/style-map", "utf8").then(function(contents) { - assert.equal(contents, "p => h1"); - }); - }); - }); - - test('embedded style map is referenced in relationships', function() { - var zip = normalDocx(); - - return styleMap.writeStyleMap(zip, "p => h1").then(function() { - return zip.read("word/_rels/document.xml.rels", "utf8").then(function(contents) { - assert.equal(contents, expectedRelationshipsXml); - }); - }); - }); - - test('re-embedding style map replaces original', function() { - var zip = normalDocx(); - - return styleMap.writeStyleMap(zip, "p => h1").then(function() { - return styleMap.writeStyleMap(zip, "p => h2"); - }).then(function() { - return zip.read("word/_rels/document.xml.rels", "utf8").then(function(contents) { - assert.equal(contents, expectedRelationshipsXml); - }); - }).then(function() { - return styleMap.readStyleMap(zip).then(function(contents) { - assert.equal(contents, "p => h2"); - }); - }); - }); - - test('embedded style map has override content type in [Content_Types].xml', function() { - var zip = normalDocx(); - - return styleMap.writeStyleMap(zip, "p => h1").then(function() { - return zip.read("[Content_Types].xml", "utf8").then(function(contents) { - assert.equal(contents, expectedContentTypesXml); - }); - }); - }); - - test('replacing style map keeps content type', function() { - var zip = normalDocx(); - - return styleMap.writeStyleMap(zip, "p => h1").then(function() { - return styleMap.writeStyleMap(zip, "p => h2"); - }).then(function() { - return zip.read("[Content_Types].xml", "utf8").then(function(contents) { - assert.equal(contents, expectedContentTypesXml); - }); - }); - }); - -}); - -var expectedRelationshipsXml = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' + - '<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">' + - '<Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings" Target="settings.xml"/>' + - '<Relationship Id="rMammothStyleMap" Type="http://schemas.zwobble.org/mammoth/style-map" Target="/mammoth/style-map"/>' + - '</Relationships>'; - -var expectedContentTypesXml = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' + - '<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">' + - '<Default Extension="png" ContentType="image/png"/>' + - '<Override PartName="/mammoth/style-map" ContentType="text/prs.mammoth.style-map"/>' + - '</Types>'; - -function normalDocx() { - var zip = new JSZip(); - var originalRelationshipsXml = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' + - '<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">' + - '<Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings" Target="settings.xml"/>' + - '</Relationships>'; - var originalContentTypesXml = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' + - '<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">' + - '<Default Extension="png" ContentType="image/png"/>' + - '</Types>'; - zip.file("word/_rels/document.xml.rels", originalRelationshipsXml); - zip.file("[Content_Types].xml", originalContentTypesXml); - var buffer = zip.generate({type: "arraybuffer"}); - return zipfile.openArrayBuffer(buffer); -} - diff --git a/tests/docx/styles-reader.tests.js b/tests/docx/styles-reader.tests.js deleted file mode 100644 index 4ff19c7..0000000 --- a/tests/docx/styles-reader.tests.js +++ /dev/null @@ -1,92 +0,0 @@ -var assert = require("assert"); - -var readStylesXml = require("../../lib/docx/styles-reader").readStylesXml; -var XmlElement = require("../../lib/xml").Element; -var test = require("../testing").test; - - -describe('readStylesXml', function() { - test('paragraph style is null if no style with that ID exists', function() { - var styles = readStylesXml({ - root: new XmlElement("w:styles", {}, []) - }); - assert.equal(styles.findParagraphStyleById("Heading1"), null); - }); - - test('paragraph style can be found by ID', function() { - var styles = readStylesXml({ - root: new XmlElement("w:styles", {}, [ - paragraphStyleElement("Heading1", "Heading 1") - ]) - }); - assert.equal(styles.findParagraphStyleById("Heading1").styleId, "Heading1"); - }); - - test('character style can be found by ID', function() { - var styles = readStylesXml({ - root: new XmlElement("w:styles", {}, [ - characterStyleElement("Heading1Char", "Heading 1 Char") - ]) - }); - assert.equal(styles.findCharacterStyleById("Heading1Char").styleId, "Heading1Char"); - }); - - test('paragraph and character styles are distinct', function() { - var styles = readStylesXml({ - root: new XmlElement("w:styles", {}, [ - paragraphStyleElement("Heading1", "Heading 1"), - characterStyleElement("Heading1Char", "Heading 1 Char") - ]) - }); - assert.equal(styles.findCharacterStyleById("Heading1"), null); - assert.equal(styles.findParagraphStyleById("Heading1Char"), null); - }); - - test('character and table styles are distinct', function() { - var styles = readStylesXml({ - root: new XmlElement("w:styles", {}, [ - styleElement("table", "Heading1", "Heading 1") - ]) - }); - assert.equal(styles.findCharacterStyleById("Heading1"), null); - }); - - test('styles include names', function() { - var styles = readStylesXml({ - root: new XmlElement("w:styles", {}, [ - paragraphStyleElement("Heading1", "Heading 1") - ]) - }); - assert.equal(styles.findParagraphStyleById("Heading1").name, "Heading 1"); - }); - - test('style name is null if w:name element does not exist', function() { - var styles = readStylesXml({ - root: new XmlElement("w:styles", {}, [ - styleWithoutWNameElement("paragraph", "Heading1"), - styleWithoutWNameElement("character", "Heading1Char") - ]) - }); - assert.equal(styles.findParagraphStyleById("Heading1").name, null); - assert.equal(styles.findCharacterStyleById("Heading1Char").name, null); - }); - -}); - -function paragraphStyleElement(id, name) { - return styleElement("paragraph", id, name); -} - -function characterStyleElement(id, name) { - return styleElement("character", id, name); -} - -function styleElement(type, id, name) { - return new XmlElement("w:style", {"w:type": type, "w:styleId": id}, [ - new XmlElement("w:name", {"w:val": name}, []) - ]); -} - -function styleWithoutWNameElement(type, id) { - return new XmlElement("w:style", {"w:type": type, "w:styleId": id}, []); -} |
