2011-12-08 10:59:44 +00:00
|
|
|
/* Front-end/Wrapper for a particular tree builder, in this case the
|
|
|
|
* parser/tree builder from the node 'html5' module. Feed it tokens using
|
|
|
|
* processToken, and it will build you a DOM tree retrievable using .document
|
|
|
|
* or .body(). */
|
2011-11-18 14:00:14 +00:00
|
|
|
|
2011-12-08 10:59:44 +00:00
|
|
|
var events = require('events');
|
2011-11-18 14:00:14 +00:00
|
|
|
var HTML5 = require('./html5/index');
|
|
|
|
|
2011-11-29 15:11:51 +00:00
|
|
|
FauxHTML5 = {};
|
2011-11-18 14:00:14 +00:00
|
|
|
|
|
|
|
|
2011-12-08 10:37:18 +00:00
|
|
|
FauxHTML5.TreeBuilder = function ( ) {
|
|
|
|
// The parser we are going to emit our tokens to
|
2011-11-18 14:00:14 +00:00
|
|
|
this.parser = new HTML5.Parser();
|
2011-12-08 10:37:18 +00:00
|
|
|
|
|
|
|
// Sets up the parser
|
2011-11-18 14:00:14 +00:00
|
|
|
this.parser.parse(this);
|
2011-12-08 10:37:18 +00:00
|
|
|
this.document = this.parser.document;
|
2011-11-18 14:00:14 +00:00
|
|
|
return this;
|
2011-11-29 15:11:51 +00:00
|
|
|
};
|
2011-11-18 14:00:14 +00:00
|
|
|
|
2011-12-08 10:37:18 +00:00
|
|
|
FauxHTML5.TreeBuilder.prototype = new events.EventEmitter();
|
2011-11-18 14:00:14 +00:00
|
|
|
|
2011-12-08 10:37:18 +00:00
|
|
|
// Adapt the token format to internal HTML tree builder format, call the actual
|
|
|
|
// html tree builder by emitting the token.
|
|
|
|
FauxHTML5.TreeBuilder.prototype.processToken = function (token) {
|
2011-11-18 14:00:14 +00:00
|
|
|
var att = function (maybeAttribs) {
|
2011-12-14 17:25:09 +00:00
|
|
|
if ( $.isArray( maybeAttribs ) ) {
|
2011-11-18 14:00:14 +00:00
|
|
|
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)});
|
2011-12-14 23:38:46 +00:00
|
|
|
this.emit('token', {type: 'EndTag',
|
|
|
|
name: token.name,
|
|
|
|
data: att(token.attribs)});
|
2011-11-18 14:00:14 +00:00
|
|
|
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');
|
2011-12-14 09:40:49 +00:00
|
|
|
this.document = this.parser.document;
|
2011-12-14 17:25:09 +00:00
|
|
|
if ( ! this.document.body ) {
|
|
|
|
// HACK: This should not be needed really.
|
|
|
|
this.document.body = this.parser.document.getElementsByTagName('body')[0];
|
|
|
|
}
|
2011-11-18 14:00:14 +00:00
|
|
|
break;
|
|
|
|
case "NEWLINE":
|
|
|
|
//this.emit('end');
|
2011-12-14 17:25:09 +00:00
|
|
|
this.emit('token', {type: 'Characters', data: "\n"});
|
2011-11-18 14:00:14 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
console.log("Unhandled token: " + JSON.stringify(token));
|
|
|
|
break;
|
|
|
|
}
|
2011-11-29 15:11:51 +00:00
|
|
|
};
|
|
|
|
|
2011-12-08 10:37:18 +00:00
|
|
|
|
|
|
|
|
2011-11-29 15:11:51 +00:00
|
|
|
if (typeof module == "object") {
|
|
|
|
module.exports.FauxHTML5 = FauxHTML5;
|
2011-11-18 14:00:14 +00:00
|
|
|
}
|