[Pkg-javascript-commits] [ltx] 227/469: start implementing sax_ltx

Jonas Smedegaard dr at jones.dk
Wed Aug 31 13:03:06 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 0426b662d9688d4385a1c906271fbfb419681506
Author: Astro <astro at spaceboyz.net>
Date:   Tue Mar 20 04:15:21 2012 +0100

    start implementing sax_ltx
---
 lib/index-browserify.js |   1 +
 lib/parse.js            |   2 +-
 lib/sax_ltx.js          | 149 ++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 151 insertions(+), 1 deletion(-)

diff --git a/lib/index-browserify.js b/lib/index-browserify.js
index 94dea99..928667c 100644
--- a/lib/index-browserify.js
+++ b/lib/index-browserify.js
@@ -1,6 +1,7 @@
 /* Cause browserify to bundle SAX parsers: */
 require('./sax_easysax');
 require('./sax_saxjs');
+require('./sax_ltx');
 
 /* SHIM */
 module.exports = require('./index');
\ No newline at end of file
diff --git a/lib/parse.js b/lib/parse.js
index 6b37684..e63990d 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_node-xml.js',*/ './sax_saxjs.js'].forEach(function(modName) {
+['./sax_ltx.js', './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_ltx.js b/lib/sax_ltx.js
new file mode 100644
index 0000000..4c9a60c
--- /dev/null
+++ b/lib/sax_ltx.js
@@ -0,0 +1,149 @@
+var util = require('util');
+var events = require('events');
+
+var STATE_TEXT = 0,
+    STATE_TAG_NAME = 2,
+    STATE_TAG = 3,
+    STATE_ATTR_NAME = 4,
+    STATE_ATTR_EQ = 5,
+    STATE_ATTR_QUOT = 6,
+    STATE_ATTR_VALUE = 7;
+
+var RE_TAG_NAME = /^[^\s\/>]+$/,
+    RE_ATTR_NAME = /^[^\s=]+$/;
+
+var SaxLtx = module.exports = function SaxLtx() {
+    events.EventEmitter.call(this);
+
+    this.recordStart = 0;
+    this.state = STATE_TEXT;
+
+
+    var origEmit = this.emit;
+    this.emit = function() {
+	//console.log('ltx', arguments);
+	origEmit.apply(this, arguments);
+    };
+};
+util.inherits(SaxLtx, events.EventEmitter);
+
+SaxLtx.prototype.write = function(data) {
+    if (typeof data !== 'string')
+	data = data.toString();
+    var pos = 0;
+
+    /* Anything from previous write()? */
+    if (this.remainder) {
+	data = this.remainder + data;
+	pos += this.remainder.length;
+	delete this.remainder;
+    }
+
+    var that = this;
+    function endRecording() {
+	if (that.hasOwnProperty('recordStart')) {
+	    var recorded = data.slice(that.recordStart, pos);
+	    delete that.recordStart;
+	    return recorded;
+	}
+    }
+
+    for(; pos < data.length; pos++) {
+	var c = data[pos];
+	//console.log("state", this.state, "c", c);
+	switch(this.state) {
+	case STATE_TEXT:
+	    if (c === "<") {
+		var text = endRecording();
+		if (text)
+		    this.emit('text', unescapeXml(text));
+		this.state = STATE_TAG_NAME;
+		this.recordStart = pos + 1;
+		this.attrs = {};
+	    }
+	    break;
+	case STATE_TAG_NAME:
+	    if (c === "/" && this.recordStart === pos) {
+		this.recordStart = pos + 1;
+		this.endTag = true;
+	    } else if (!RE_TAG_NAME.test(c)) {
+		this.tagName = endRecording();
+		pos--;
+		this.state = STATE_TAG;
+	    }
+	    break;
+	case STATE_TAG:
+	    if (c === ">") {
+		if (!this.endTag) {
+		    this.emit('startElement', this.tagName, this.attrs);
+		    if (this.selfClosing)
+			this.emit('endElement', this.tagName);
+		} else
+		    this.emit('endElement', this.tagName);
+		delete this.tagName;
+		delete this.attrs;
+		delete this.endTag;
+		delete this.selfClosing;
+		this.state = STATE_TEXT;
+		this.recordStart = pos + 1;
+	    } else if (c === "/") {
+		this.selfClosing = true;
+	    } else if (RE_ATTR_NAME.test(c)) {
+		this.recordStart = pos;
+		this.state = STATE_ATTR_NAME;
+	    }
+	    break;
+	case STATE_ATTR_NAME:
+	    if (!RE_ATTR_NAME.test(c)) {
+		this.attrName = endRecording();
+		pos--;
+		this.state = STATE_ATTR_EQ;
+	    }
+	    break;
+	case STATE_ATTR_EQ:
+	    if (c === "=") {
+		this.state = STATE_ATTR_QUOT;
+	    }
+	    break;
+	case STATE_ATTR_QUOT:
+	    if (c === '"' || c === "'") {
+		this.attrQuote = c;
+		this.state = STATE_ATTR_VALUE;
+		this.recordStart = pos + 1;
+	    }
+	    break;
+	case STATE_ATTR_VALUE:
+	    if (c === this.attrQuote) {
+		var value = unescapeXml(endRecording());
+		this.attrs[this.attrName] = value;
+		delete this.attrName;
+		this.state = STATE_TAG;
+	    }
+	    break;
+	}
+    }
+
+    if (this.hasOwnProperty('recordStart') &&
+	this.recordStart <= data.length) {
+	this.remainder = data.slice(this.recordStart);
+	this.recordStart = 0;
+    }
+};
+
+SaxLtx.prototype.end = function(data) {
+    if (data)
+	this.write(data);
+
+    /* Uh, yeah */
+    this.write = function() {
+    };
+};
+
+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