2011-11-18 14:00:14 +00:00
|
|
|
var events = require('events');
|
|
|
|
|
|
|
|
|
|
|
|
var HTML5 = require('./html5/index');
|
|
|
|
|
2011-11-29 15:11:51 +00:00
|
|
|
FauxHTML5 = {};
|
2011-11-18 14:00:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
FauxHTML5.Tokenizer = function ( ) {
|
|
|
|
this.parser = new HTML5.Parser();
|
|
|
|
this.parser.parse(this);
|
|
|
|
return this;
|
2011-11-29 15:11:51 +00:00
|
|
|
};
|
2011-11-18 14:00:14 +00:00
|
|
|
|
2011-11-29 15:11:51 +00:00
|
|
|
FauxHTML5.Tokenizer.prototype = new events.EventEmitter();
|
2011-11-18 14:00:14 +00:00
|
|
|
|
|
|
|
FauxHTML5.Tokenizer.prototype.processToken = function (token) {
|
|
|
|
var att = function (maybeAttribs) {
|
|
|
|
if ( $.isArray(maybeAttribs) ) {
|
|
|
|
var atts = [];
|
|
|
|
for(var i = 0, length = maybeAttribs.length; i < length; i++) {
|
|
|
|
var att = maybeAttribs[i];
|
|
|
|
atts.push({nodeName: att[0], nodeValue: att[1]});
|
|
|
|
}
|
|
|
|
return atts;
|
|
|
|
} else {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
switch (token.type) {
|
|
|
|
case "TEXT":
|
|
|
|
this.emit('token', {type: 'Characters', data: token.value});
|
|
|
|
break;
|
|
|
|
case "TAG":
|
|
|
|
this.emit('token', {type: 'StartTag',
|
|
|
|
name: token.name,
|
|
|
|
data: att(token.attribs)});
|
|
|
|
break;
|
|
|
|
case "ENDTAG":
|
|
|
|
this.emit('token', {type: 'EndTag',
|
|
|
|
name: token.name,
|
|
|
|
data: att(token.attribs)});
|
|
|
|
break;
|
|
|
|
case "SELFCLOSINGTAG":
|
2011-11-29 15:11:51 +00:00
|
|
|
this.emit('token', {type: 'StartTag',
|
2011-11-18 14:00:14 +00:00
|
|
|
name: token.name,
|
|
|
|
data: att(token.attribs)});
|
|
|
|
break;
|
|
|
|
case "COMMENT":
|
2011-11-21 09:22:30 +00:00
|
|
|
this.emit('token', {type: 'Comment',
|
2011-11-18 14:00:14 +00:00
|
|
|
data: token.value});
|
|
|
|
break;
|
|
|
|
case "END":
|
|
|
|
this.emit('end');
|
|
|
|
break;
|
|
|
|
case "NEWLINE":
|
|
|
|
//this.emit('end');
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
console.log("Unhandled token: " + JSON.stringify(token));
|
|
|
|
break;
|
|
|
|
}
|
2011-11-29 15:11:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if (typeof module == "object") {
|
|
|
|
module.exports.FauxHTML5 = FauxHTML5;
|
2011-11-18 14:00:14 +00:00
|
|
|
}
|