[Pkg-javascript-commits] [ltx] 165/469: parse (NEW)

Jonas Smedegaard dr at jones.dk
Wed Aug 31 13:01:20 UTC 2016


This is an automated email from the git hooks/post-receive script.

js pushed a commit to branch master
in repository ltx.

commit ef47529ef4d4e95922da43c90629fa51d92edf0d
Author: Astro <astro at spaceboyz.net>
Date:   Sun Nov 21 23:11:47 2010 +0100

    parse (NEW)
---
 lib/index.js       |  4 +++
 lib/parse.js       | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 test/test_parse.js | 18 +++++++++++++
 3 files changed, 97 insertions(+)

diff --git a/lib/index.js b/lib/index.js
index 3aa7fbe..a9614b5 100644
--- a/lib/index.js
+++ b/lib/index.js
@@ -1,4 +1,8 @@
 var element = require('./element');
+var parse = require('./parse');
 
 exports.Element = element.Element;
 exports.escapeXml = element.escapeXml;
+
+exports.Parser = parse.Parser;
+exports.parse = parse.parse;
diff --git a/lib/parse.js b/lib/parse.js
new file mode 100644
index 0000000..ac2563a
--- /dev/null
+++ b/lib/parse.js
@@ -0,0 +1,75 @@
+var events = require('events');
+var utils = require('utils');
+var expat = require('node-expat');
+var element = require('./element');
+
+exports.Parser = function() {
+    var that = this;
+
+    this.parser = new expat.Parser('UTF-8');
+
+    var el;
+    this.parser.addListener('startElement', function(name, attrs) {
+        var child = new element.Element(name, attrs);
+        if (!el) {
+            el = child;
+        } else {
+            el = el.cnode(child);
+        }
+    });
+    this.parser.addListener('endElement', function(name, attrs) {
+        if (!el) {
+            /* Err */
+        } else if (el && name == el.name) {
+            if (el.parent)
+                el = el.parent;
+            else if (!that.tree) {
+                that.tree = el;
+                el = undefined;
+            }
+        }
+    });
+    this.parser.addListener('text', function(str) {
+        if (el)
+            el.t(str);
+    });
+};
+utils.inherits(exports.Parser, events.EventEmitter);
+
+exports.Parser.prototype.write = function(data) {
+    if (!this.parser.parse(data, false)) {
+        this.emit('error', new Error(this.parser.getError()));
+
+        // Premature error thrown,
+        // disable all functionality:
+        this.write = function() { };
+        this.end = function() { };
+    }
+};
+
+exports.Parser.prototype.end = function() {
+    if (!this.parser.parse('', true))
+        this.emit('error', new Error(this.parser.getError()));
+    else
+        this.emit('tree', this.tree);
+};
+
+exports.parse = function(data) {
+    var p = new exports.Parser();
+    var result = null, error = null;
+
+    p.on('tree', function(tree) {
+        result = tree;
+    });
+    p.on('error', function(e) {
+        error = e;
+    });
+
+    p.write(data);
+    p.end();
+
+    if (error)
+        throw error;
+    else
+        return result;
+};
diff --git a/test/test_parse.js b/test/test_parse.js
new file mode 100644
index 0000000..9d479fa
--- /dev/null
+++ b/test/test_parse.js
@@ -0,0 +1,18 @@
+var vows = require('vows'),
+assert = require('assert'),
+ltx = require('./../lib/index');
+
+vows.describe('ltx').addBatch({
+    'parsing': {
+        'simple document': function() {
+            var el = ltx.parse('<root/>');
+            assert.equal(el.name, 'root');
+            assert.equal(0, el.children.length);
+        },
+        'raises error': function() {
+            assert.throws(function() {
+                ltx.parse('<root></toor>');
+            });
+        }
+    }
+}).run();

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/ltx.git



More information about the Pkg-javascript-commits mailing list