[Pkg-javascript-commits] [ltx] 215/469: add sax_node-xml, disabled by default

Jonas Smedegaard dr at jones.dk
Wed Aug 31 13:03:05 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 717062d6ed61491f18e2cd7dbbb02d64fd490205
Author: Astro <astro at spaceboyz.net>
Date:   Mon Mar 19 18:14:03 2012 +0100

    add sax_node-xml, disabled by default
    
    https://github.com/robrighter/node-xml/pull/25
---
 lib/parse.js        |  2 +-
 lib/sax_node-xml.js | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 64 insertions(+), 1 deletion(-)

diff --git a/lib/parse.js b/lib/parse.js
index 49f919e..e196179 100644
--- a/lib/parse.js
+++ b/lib/parse.js
@@ -3,7 +3,7 @@ var util = require('util');
 
 exports.availableSaxParsers = [];
 exports.bestSaxParser = null;
-['./sax_expat.js', './sax_easysax.js', './sax_saxjs.js'].forEach(function(modName) {
+['./sax_expat.js', './sax_easysax.js', /*'./sax_node-xml.js',*/ './sax_saxjs.js'].forEach(function(modName) {
     var mod;
     try {
 	mod = require(modName);
diff --git a/lib/sax_node-xml.js b/lib/sax_node-xml.js
new file mode 100644
index 0000000..16c64cc
--- /dev/null
+++ b/lib/sax_node-xml.js
@@ -0,0 +1,63 @@
+var util = require('util');
+var events = require('events');
+var xml = require('node-xml');
+
+/**
+ * This cannot be used as long as node-xml starts parsing only after
+ * setTimeout(f, 0);
+ */
+var SaxNodeXML = module.exports = function SaxNodeXML() {
+    events.EventEmitter.call(this);
+    var that = this;
+    this.parser = new xml.SaxParser(function(handler) {
+	handler.onStartElementNS(function(elem, attrs, prefix, uri, namespaces) {
+	    var i, attrsHash = {};
+	    if (prefix)
+		elem = prefix + ":" + elem;
+	    for(i = 0; i < attrs.length; i++) {
+		var attr = attrs[i];
+		attrsHash[attr[0]] = unescapeXml(attr[1]);
+	    }
+	    for(i = 0; i < namespaces.length; i++) {
+		var namespace = namespaces[i];
+		var k = !namespace[0] ? "xmlns" : "xmlns:" + namespace[0];
+		attrsHash[k] = unescapeXml(namespace[1]);
+	    }
+	    that.emit('startElement', elem, attrsHash);
+	});
+	handler.onEndElementNS(function(elem, prefix, uri) {
+	    if (prefix)
+		elem = prefix + ":" + elem;
+	    that.emit('endElement', elem);
+	});
+	handler.onCharacters(function(str) {
+	    that.emit('text', str);
+	});
+	handler.onCdata(function(str) {
+	    that.emit('text', str);
+	});
+	handler.onError(function(e) {
+	    that.emit('error', e);
+	});
+	// TODO: other events, esp. entityDecl (billion laughs!)
+    });
+};
+util.inherits(SaxNodeXML, events.EventEmitter);
+
+SaxNodeXML.prototype.write = function(data) {
+    this.parser.parseString(data);
+};
+
+SaxNodeXML.prototype.end = function(data) {
+    if (data)
+	this.write(data);
+};
+
+function unescapeXml(s) {
+    return s.
+        replace(/\&/g, '&').
+        replace(/\</g, '<').
+        replace(/\>/g, '>').
+        replace(/\"/g, '"').
+        replace(/\'/g, '\'');
+}

-- 
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