-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
configurable sax parsers (expat & saxjs backends)
- Loading branch information
Showing
5 changed files
with
151 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
var util = require('util'); | ||
var events = require('events'); | ||
var expat = require('node-expat'); | ||
|
||
var SaxExpat = module.exports = function SaxExpat() { | ||
events.EventEmitter.call(this); | ||
this.parser = new expat.Parser('UTF-8'); | ||
|
||
var that = this; | ||
this.parser.on('startElement', function(name, attrs) { | ||
that.emit('startElement', name, attrs); | ||
}); | ||
this.parser.on('endElement', function(name) { | ||
that.emit('endElement', name); | ||
}); | ||
this.parser.on('text', function(str) { | ||
that.emit('text', str); | ||
}); | ||
// TODO: other events, esp. entityDecl (billion laughs!) | ||
}; | ||
util.inherits(SaxExpat, events.EventEmitter); | ||
|
||
SaxExpat.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() { }; | ||
} | ||
}; | ||
|
||
SaxExpat.prototype.end = function(data) { | ||
if (!this.parser.parse('', true)) | ||
this.emit('error', new Error(this.parser.getError())); | ||
else | ||
this.emit('end'); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
var util = require('util'); | ||
var events = require('events'); | ||
var sax = require('sax'); | ||
|
||
var SaxSaxjs = module.exports = function SaxSaxjs() { | ||
events.EventEmitter.call(this); | ||
this.parser = sax.parser(true); | ||
|
||
var that = this; | ||
this.parser.onopentag = function(a) { | ||
that.emit('startElement', a.name, a.attributes); | ||
}; | ||
this.parser.onclosetag = function(name) { | ||
that.emit('endElement', name); | ||
}; | ||
this.parser.ontext = function(str) { | ||
that.emit('text', str); | ||
}; | ||
this.parser.onend = function() { | ||
that.emit('end'); | ||
}; | ||
this.parser.onerror = function(e) { | ||
that.emit('error', e); | ||
}; | ||
// TODO: other events, esp. entityDecl (billion laughs!) | ||
}; | ||
util.inherits(SaxSaxjs, events.EventEmitter); | ||
|
||
SaxSaxjs.prototype.write = function(data) { | ||
this.parser.write(data); | ||
}; | ||
|
||
SaxSaxjs.prototype.end = function(data) { | ||
if (data) | ||
this.parser.write(data); | ||
this.parser.close(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters