Skip to content

Commit

Permalink
Test parsing of document XML
Browse files Browse the repository at this point in the history
  • Loading branch information
mwilliamson committed Feb 18, 2024
1 parent 1b4c1ed commit 7194953
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 12 deletions.
6 changes: 3 additions & 3 deletions lib/docx/document-xml-reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand All @@ -19,7 +19,7 @@ function DocumentXmlReader(options) {
});
return new Result(result.value, result.messages);
}

return {
convertXmlToDocument: convertXmlToDocument
};
Expand Down
10 changes: 1 addition & 9 deletions test/docx/body-reader.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
25 changes: 25 additions & 0 deletions test/docx/document-xml-reader.tests.js
Original file line number Diff line number Diff line change
@@ -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([])],
{}
));
});
12 changes: 12 additions & 0 deletions test/docx/testing.js
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 7194953

Please sign in to comment.