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
|
|
|
|
2012-01-04 08:39:45 +00:00
|
|
|
var events = require('events'),
|
|
|
|
HTML5 = require('./html5/index');
|
2011-11-18 14:00:14 +00:00
|
|
|
|
2011-11-29 15:11:51 +00:00
|
|
|
FauxHTML5 = {};
|
2011-11-18 14:00:14 +00:00
|
|
|
|
|
|
|
|
2012-04-16 13:47:03 +00:00
|
|
|
FauxHTML5.TreeBuilder = function ( env ) {
|
2011-12-08 10:37:18 +00:00
|
|
|
// 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);
|
2012-01-03 18:44:31 +00:00
|
|
|
|
|
|
|
// implicitly start a new document
|
2012-02-01 18:37:48 +00:00
|
|
|
this.processToken(new TagTk( 'body' ));
|
2012-04-16 13:47:03 +00:00
|
|
|
|
|
|
|
this.env = env;
|
2011-11-29 15:11:51 +00:00
|
|
|
};
|
2011-11-18 14:00:14 +00:00
|
|
|
|
2012-01-04 12:28:41 +00:00
|
|
|
// Inherit from EventEmitter
|
2011-12-08 10:37:18 +00:00
|
|
|
FauxHTML5.TreeBuilder.prototype = new events.EventEmitter();
|
2012-01-04 12:28:41 +00:00
|
|
|
FauxHTML5.TreeBuilder.prototype.constructor = FauxHTML5.TreeBuilder;
|
2011-11-18 14:00:14 +00:00
|
|
|
|
2012-01-04 11:00:54 +00:00
|
|
|
/**
|
|
|
|
* Register for (token) 'chunk' and 'end' events from a token emitter,
|
|
|
|
* normally the TokenTransformDispatcher.
|
|
|
|
*/
|
2012-04-25 14:35:59 +00:00
|
|
|
FauxHTML5.TreeBuilder.prototype.addListenersOn = function ( emitter ) {
|
2012-01-03 18:44:31 +00:00
|
|
|
emitter.addListener('chunk', this.onChunk.bind( this ) );
|
|
|
|
emitter.addListener('end', this.onEnd.bind( this ) );
|
|
|
|
};
|
|
|
|
|
|
|
|
FauxHTML5.TreeBuilder.prototype.onChunk = function ( tokens ) {
|
2012-04-16 13:47:03 +00:00
|
|
|
this.env.dp( 'chunk: ' + JSON.stringify( tokens, null, 2 ) );
|
2012-01-03 18:44:31 +00:00
|
|
|
for (var i = 0, length = tokens.length; i < length; i++) {
|
|
|
|
this.processToken(tokens[i]);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
FauxHTML5.TreeBuilder.prototype.onEnd = function ( ) {
|
2012-02-14 20:56:14 +00:00
|
|
|
//console.warn('Fauxhtml5 onEnd');
|
2012-01-03 18:44:31 +00:00
|
|
|
// FIXME HACK: For some reason the end token is not processed sometimes,
|
|
|
|
// which normally fixes the body reference up.
|
2012-01-18 23:46:01 +00:00
|
|
|
var document = this.parser.document;
|
|
|
|
document.body = document.getElementsByTagName('body')[0];
|
2012-01-03 18:44:31 +00:00
|
|
|
|
2012-02-14 20:56:14 +00:00
|
|
|
//console.warn( 'onEnd: ' + document.body.innerHTML );
|
2012-01-18 23:46:01 +00:00
|
|
|
|
|
|
|
this.emit( 'document', document );
|
2012-01-04 11:00:54 +00:00
|
|
|
|
2012-01-03 18:44:31 +00:00
|
|
|
// XXX: more clean up to allow reuse.
|
|
|
|
this.parser.setup();
|
2012-02-01 18:37:48 +00:00
|
|
|
this.processToken(new TagTk( 'body' ));
|
2012-01-03 18:44:31 +00:00
|
|
|
};
|
|
|
|
|
2012-01-27 13:32:44 +00:00
|
|
|
FauxHTML5.TreeBuilder.prototype._att = function (maybeAttribs) {
|
|
|
|
var atts = [];
|
|
|
|
if ( maybeAttribs && $.isArray( maybeAttribs ) ) {
|
|
|
|
for(var i = 0, length = maybeAttribs.length; i < length; i++) {
|
|
|
|
var att = maybeAttribs[i];
|
2012-02-01 16:30:43 +00:00
|
|
|
atts.push({nodeName: att.k, nodeValue: att.v});
|
2012-01-27 13:32:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return atts;
|
|
|
|
};
|
2012-01-03 18:44:31 +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) {
|
2012-06-05 21:44:23 +00:00
|
|
|
//console.warn( 'processToken: ' + JSON.stringify( token ));
|
|
|
|
|
2012-05-10 08:04:24 +00:00
|
|
|
var attribs = token.attribs || [];
|
2012-02-11 16:43:25 +00:00
|
|
|
if ( token.dataAttribs ) {
|
2012-06-04 10:26:03 +00:00
|
|
|
var dataMW = JSON.stringify( token.dataAttribs );
|
|
|
|
if ( dataMW !== '{}' ) {
|
|
|
|
attribs = attribs.concat([
|
|
|
|
{
|
|
|
|
// Mediawiki-specific round-trip / non-semantic information
|
2012-07-16 19:10:08 +00:00
|
|
|
k: 'data-rt',
|
2012-06-04 10:26:03 +00:00
|
|
|
v: dataMW
|
|
|
|
} ] );
|
2012-02-11 16:43:25 +00:00
|
|
|
}
|
|
|
|
}
|
2011-11-18 14:00:14 +00:00
|
|
|
|
2012-02-01 17:03:08 +00:00
|
|
|
switch( token.constructor ) {
|
|
|
|
case String:
|
2012-06-17 14:10:30 +00:00
|
|
|
if ( token.match(/^[ \t\r\n\f]+$/) ) {
|
|
|
|
// Treat space characters specially so that the tree builder
|
|
|
|
// doesn't apply the foster parenting algorithm
|
|
|
|
this.emit('token', {type: 'SpaceCharacters', data: token});
|
|
|
|
} else {
|
|
|
|
this.emit('token', {type: 'Characters', data: token});
|
|
|
|
}
|
2012-02-01 17:03:08 +00:00
|
|
|
break;
|
|
|
|
case NlTk:
|
2012-06-17 14:10:30 +00:00
|
|
|
this.emit('token', {type: 'SpaceCharacters', data: '\n'});
|
2012-02-01 17:03:08 +00:00
|
|
|
break;
|
2012-02-01 18:37:48 +00:00
|
|
|
case TagTk:
|
|
|
|
this.emit('token', {type: 'StartTag',
|
|
|
|
name: token.name,
|
2012-05-10 08:04:24 +00:00
|
|
|
data: this._att(attribs)});
|
2012-02-01 18:37:48 +00:00
|
|
|
break;
|
|
|
|
case SelfclosingTagTk:
|
|
|
|
this.emit('token', {type: 'StartTag',
|
|
|
|
name: token.name,
|
2012-05-10 08:04:24 +00:00
|
|
|
data: this._att(attribs)});
|
2012-02-01 18:37:48 +00:00
|
|
|
if ( HTML5.VOID_ELEMENTS.indexOf( token.name.toLowerCase() ) < 0 ) {
|
|
|
|
// VOID_ELEMENTS are automagically treated as self-closing by
|
|
|
|
// the tree builder
|
|
|
|
this.emit('token', {type: 'EndTag',
|
|
|
|
name: token.name,
|
2012-05-10 08:04:24 +00:00
|
|
|
data: this._att(attribs)});
|
2012-02-01 18:37:48 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case EndTagTk:
|
|
|
|
this.emit('token', {type: 'EndTag',
|
|
|
|
name: token.name,
|
2012-05-10 08:04:24 +00:00
|
|
|
data: this._att(attribs)});
|
2012-02-01 18:37:48 +00:00
|
|
|
break;
|
2012-03-07 20:06:54 +00:00
|
|
|
case CommentTk:
|
|
|
|
this.emit('token', {type: 'Comment',
|
|
|
|
data: token.value});
|
|
|
|
break;
|
|
|
|
case EOFTk:
|
|
|
|
this.emit('end');
|
|
|
|
this.emit('token', { type: 'EOF' } );
|
|
|
|
this.document = this.parser.document;
|
|
|
|
if ( ! this.document.body ) {
|
|
|
|
// HACK: This should not be needed really.
|
|
|
|
this.document.body = this.parser.document.getElementsByTagName('body')[0];
|
2012-02-01 17:03:08 +00:00
|
|
|
}
|
2012-03-07 20:06:54 +00:00
|
|
|
// Emit the document to consumers
|
|
|
|
//this.emit('document', this.document);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
console.warn("Unhandled token: " + JSON.stringify(token));
|
|
|
|
break;
|
2011-11-18 14:00:14 +00:00
|
|
|
}
|
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
|
|
|
}
|