diff --git a/lib/docx/document-xml-reader.js b/lib/docx/document-xml-reader.js index bb912e43..c5472c2e 100644 --- a/lib/docx/document-xml-reader.js +++ b/lib/docx/document-xml-reader.js @@ -6,10 +6,10 @@ var Result = require("../results").Result; function DocumentXmlReader(options) { var bodyReader = options.bodyReader; - + function convertXmlToDocument(element) { var body = element.first("w:body"); - + var result = bodyReader.readXmlElements(body.children) .map(function(children) { return new documents.Document(children, { @@ -19,7 +19,7 @@ function DocumentXmlReader(options) { }); return new Result(result.value, result.messages); } - + return { convertXmlToDocument: convertXmlToDocument }; diff --git a/test/docx/body-reader.tests.js b/test/docx/body-reader.tests.js index c9fe7d3f..da9501de 100644 --- a/test/docx/body-reader.tests.js +++ b/test/docx/body-reader.tests.js @@ -19,27 +19,19 @@ var isText = documentMatchers.isText; var isTable = documentMatchers.isTable; var isRow = documentMatchers.isRow; -var createBodyReader = require("../../lib/docx/body-reader").createBodyReader; var _readNumberingProperties = require("../../lib/docx/body-reader")._readNumberingProperties; var documents = require("../../lib/documents"); var xml = require("../../lib/xml"); var XmlElement = xml.Element; -var defaultNumbering = require("../../lib/docx/numbering-xml").defaultNumbering; var Relationships = require("../../lib/docx/relationships-reader").Relationships; var Styles = require("../../lib/docx/styles-reader").Styles; var warning = require("../../lib/results").warning; var testing = require("../testing"); var test = require("../test")(module); +var createBodyReaderForTests = require("./testing").createBodyReaderForTests; var createFakeDocxFile = testing.createFakeDocxFile; -function createBodyReaderForTests(options) { - options = Object.create(options || {}); - options.styles = options.styles || new Styles({}, {}); - options.numbering = options.numbering || defaultNumbering; - return createBodyReader(options); -} - function readXmlElement(element, options) { return createBodyReaderForTests(options).readXmlElement(element); } diff --git a/test/docx/document-xml-reader.tests.js b/test/docx/document-xml-reader.tests.js new file mode 100644 index 00000000..08389e17 --- /dev/null +++ b/test/docx/document-xml-reader.tests.js @@ -0,0 +1,25 @@ +var assert = require("assert"); + +var documents = require("../../lib/documents"); +var DocumentXmlReader = require("../../lib/docx/document-xml-reader").DocumentXmlReader; +var xml = require("../../lib/xml"); +var test = require("../test")(module); +var createBodyReaderForTests = require("./testing").createBodyReaderForTests; + +test("when body element is present then body is read", function() { + var bodyReader = createBodyReaderForTests({}); + var documentXmlReader = new DocumentXmlReader({ + bodyReader: bodyReader + }); + var paragraphXml = xml.element("w:p", {}, []); + var bodyXml = xml.element("w:body", {}, [paragraphXml]); + var documentXml = xml.element("w:document", {}, [bodyXml]); + + var result = documentXmlReader.convertXmlToDocument(documentXml); + + assert.deepEqual(result.messages, []); + assert.deepEqual(result.value, documents.document( + [documents.paragraph([])], + {} + )); +}); diff --git a/test/docx/testing.js b/test/docx/testing.js new file mode 100644 index 00000000..4f1ed84a --- /dev/null +++ b/test/docx/testing.js @@ -0,0 +1,12 @@ +var createBodyReader = require("../../lib/docx/body-reader").createBodyReader; +var defaultNumbering = require("../../lib/docx/numbering-xml").defaultNumbering; +var Styles = require("../../lib/docx/styles-reader").Styles; + +function createBodyReaderForTests(options) { + options = Object.create(options || {}); + options.styles = options.styles || new Styles({}, {}); + options.numbering = options.numbering || defaultNumbering; + return createBodyReader(options); +} + +exports.createBodyReaderForTests = createBodyReaderForTests;