UNPKG

1.63 kBJavaScriptView Raw
1var Parser = require("./Parser.js");
2
3var Stream = function(options){
4 Parser.call(this, new cbs(this), options);
5};
6
7require("util").inherits(Stream, require("stream").Stream);
8
9//util.inherits would overwrite the prototype when called twice,
10//so we need a different approach
11Object.getOwnPropertyNames(Parser.prototype).forEach(function(name){
12 Stream.prototype[name] = Parser.prototype[name];
13});
14
15Stream.prototype.writable = true;
16Stream.prototype.readable = true;
17
18var cbs = function(scope){
19 this.scope = scope;
20};
21
22cbs.prototype = {
23 oncdataend: function(){
24 this.scope.emit("cdataend");
25 },
26 oncdatastart: function(){
27 this.scope.emit("cdatastart");
28 },
29 onclosetag: function(name){
30 this.scope.emit("closetag", name);
31 },
32 oncomment: function(text){
33 this.scope.emit("comment", text);
34 },
35 oncommentend: function(){
36 this.scope.emit("commentend");
37 },
38 onerror: function(err){
39 this.scope.emit("error", err);
40 },
41 onopentag: function(name, attribs, type){
42 this.scope.emit("opentag", name, attribs, type);
43 },
44 onopentagname: function(name){
45 this.scope.emit("opentagname", name);
46 },
47 onattribute: function(name, value){
48 this.scope.emit("attribute", name, value);
49 },
50 onprocessinginstruction: function(name, data){
51 this.scope.emit("processinginstruction", name, data);
52 },
53 onend: function(){
54 this.scope.emit("end");
55 },
56 onreset: function(){
57 this.scope.emit("reset");
58 },
59 ontext: function(text){
60 this.scope.emit("text", text);
61 //let the 'pipe' function do something useful
62 //this.scope.emit("data", text);
63 }
64};
65
66module.exports = Stream;
\No newline at end of file