diff options
| author | Michael Williamson <mike@zwobble.org> | 2014-04-22 11:41:11 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2014-04-22 11:41:11 +0100 |
| commit | baafeb9304ad9505d76d57962711060035e40558 (patch) | |
| tree | 1e784fc2788915878d6e520ed26332f9f173a9f3 /lib/xmlreader.js | |
| parent | 15bf7ff1863049a7cad09719fa90b0e416b7715b (diff) | |
Add safer method for text extraction from elements
Diffstat (limited to 'lib/xmlreader.js')
| -rw-r--r-- | lib/xmlreader.js | 9 |
1 files changed, 9 insertions, 0 deletions
diff --git a/lib/xmlreader.js b/lib/xmlreader.js index 51f24a7..f219d2b 100644 --- a/lib/xmlreader.js +++ b/lib/xmlreader.js @@ -105,6 +105,15 @@ Element.prototype.getElementsByTagName = function(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) { |
