diff options
| author | Michael Williamson <mike@zwobble.org> | 2015-08-16 14:21:25 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2015-08-16 14:22:46 +0100 |
| commit | 9ff57bb770e0296bb2f5b7a41711c53954eb8c4e (patch) | |
| tree | 048868d884058f59ed54b41f560d9c234de67c62 /lib | |
| parent | 883af29dfdbf98d1e0a61d459ffeb08b0b5a34a6 (diff) | |
Split xmlreader into nodes and reader modules
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/docx/office-xml-reader.js | 4 | ||||
| -rw-r--r-- | lib/xml/index.js | 2 | ||||
| -rw-r--r-- | lib/xml/nodes.js | 60 | ||||
| -rw-r--r-- | lib/xml/reader.js (renamed from lib/xmlreader.js) | 64 |
4 files changed, 69 insertions, 61 deletions
diff --git a/lib/docx/office-xml-reader.js b/lib/docx/office-xml-reader.js index 71f5b57..b5231eb 100644 --- a/lib/docx/office-xml-reader.js +++ b/lib/docx/office-xml-reader.js @@ -1,5 +1,5 @@ var promises = require("../promises"); -var xmlreader = require("../xmlreader"); +var xml = require("../xml"); exports.readXmlFromZipFile = readXmlFromZipFile; @@ -17,7 +17,7 @@ var xmlNamespaceMap = { function read(xmlString) { - return xmlreader.read(xmlString, xmlNamespaceMap); + return xml.readString(xmlString, xmlNamespaceMap); } diff --git a/lib/xml/index.js b/lib/xml/index.js new file mode 100644 index 0000000..6c35998 --- /dev/null +++ b/lib/xml/index.js @@ -0,0 +1,2 @@ +exports.Element = require("./nodes").Element; +exports.readString = require("./reader").readString; diff --git a/lib/xml/nodes.js b/lib/xml/nodes.js new file mode 100644 index 0000000..ed273cf --- /dev/null +++ b/lib/xml/nodes.js @@ -0,0 +1,60 @@ +var _ = require("underscore"); + + +exports.Element = Element; + + +var emptyElement = { + first: function() { + return null; + }, + firstOrEmpty: function() { + return emptyElement; + }, + attributes: {} +}; + +function Element(name, attributes, children) { + this.type = "element"; + this.name = name; + this.attributes = attributes || {}; + this.children = children || []; +} + +Element.prototype.first = function(name) { + return _.find(this.children, function(child) { + return child.name == name; + }); +}; + +Element.prototype.firstOrEmpty = function(name) { + return this.first(name) || emptyElement; +}; + +Element.prototype.getElementsByTagName = function(name) { + var elements = _.filter(this.children, function(child) { + return child.name == name; + }); + return toElementList(elements); +}; + +Element.prototype.text = function() { + if (this.children.length === 0) { + return ""; + } else if (this.children.length !== 1 || this.children[0].type !== "text") { + throw new Error("Not implemented"); + } + return this.children[0].value; +}; + +var elementListPrototype = { + getElementsByTagName: function(name) { + return toElementList(_.flatten(this.map(function(element) { + return element.getElementsByTagName(name); + }, true))); + } +}; + +function toElementList(array) { + return _.extend(array, elementListPrototype); +} diff --git a/lib/xmlreader.js b/lib/xml/reader.js index 551366f..ab175f2 100644 --- a/lib/xmlreader.js +++ b/lib/xml/reader.js @@ -1,11 +1,12 @@ -var promises = require("./promises"); +var promises = require("../promises"); var sax = require("sax"); var _ = require("underscore"); -exports.read = read; -exports.Element = Element; +var Element = require("./nodes").Element; -function read(xmlString, namespaceMap) { +exports.readString = readString; + +function readString(xmlString, namespaceMap) { namespaceMap = namespaceMap || {}; var finished = false; @@ -84,58 +85,3 @@ function mapObject(input, valueFunc, keyFunc) { return result; }, {}); } - -var emptyElement = { - first: function() { - return null; - }, - firstOrEmpty: function() { - return emptyElement; - }, - attributes: {} -}; - -function Element(name, attributes, children) { - this.type = "element"; - this.name = name; - this.attributes = attributes || {}; - this.children = children || []; -} - -Element.prototype.first = function(name) { - return _.find(this.children, function(child) { - return child.name == name; - }); -}; - -Element.prototype.firstOrEmpty = function(name) { - return this.first(name) || emptyElement; -}; - -Element.prototype.getElementsByTagName = function(name) { - var elements = _.filter(this.children, function(child) { - return child.name == name; - }); - return toElementList(elements); -}; - -Element.prototype.text = function() { - if (this.children.length === 0) { - return ""; - } else if (this.children.length !== 1 || this.children[0].type !== "text") { - throw new Error("Not implemented"); - } - return this.children[0].value; -}; - -var elementListPrototype = { - getElementsByTagName: function(name) { - return toElementList(_.flatten(this.map(function(element) { - return element.getElementsByTagName(name); - }, true))); - } -}; - -function toElementList(array) { - return _.extend(array, elementListPrototype); -} |
