Use single _serializeToken handler for both DOM and tokens

Change-Id: I45e1d90b53a5ddc678f7744f27274bebcfc375fe
This commit is contained in:
Gabriel Wicke 2012-05-17 13:20:39 +02:00
parent 8dbc2f573f
commit b7fd4498a9

View file

@ -166,37 +166,42 @@ WSP._serializeAttributes = function ( attribs ) {
* Serialize a chunk of tokens * Serialize a chunk of tokens
*/ */
WSP.serializeTokens = function( tokens, chunkCB ) { WSP.serializeTokens = function( tokens, chunkCB ) {
var state = $.extend({}, this.defaultOptions, this.options),
i, l;
if ( chunkCB === undefined ) { if ( chunkCB === undefined ) {
var out = []; var out = [];
this._serializeTokens( tokens, out.push.bind(out) ); chunkCB = out.push.bind(out);
for ( i = 0, l = tokens.length; i < l; i++ ) {
this._serializeToken( state, chunkCB, tokens[i] );
}
return out; return out;
} else { } else {
this._serializeTokens( tokens, chunkCB ); for ( i = 0, l = tokens.length; i < l; i++ ) {
this._serializeToken( state, chunkCB, tokens[i] );
}
} }
}; };
WSP._serializeTokens = function( tokens, chunkCB ) {
var state = $.extend({}, this.defaultOptions, this.options), /**
handler; * Serialize a token.
for ( var i = 0, l = tokens.length; i < l; i++ ) { */
WSP._serializeToken = function ( state, chunkCB, token ) {
state.lastToken = state.curToken; state.lastToken = state.curToken;
state.curToken = tokens[i]; state.curToken = token;
var handler;
switch( token.constructor ) { switch( token.constructor ) {
case TagTk: case TagTk:
case SelfclosingTagTk: case SelfclosingTagTk:
handler = this.tagToWikitext[token.name]; handler = this.tagToWikitext[token.name];
if ( handler && handler.start ) { if ( handler && handler.start ) {
chunkCB( handler.start( state, state.curToken ) ); chunkCB( handler.start( state, token ) );
} else {
console.warn( 'Unhandled tag token ' + JSON.stringify( token ) );
} }
break; break;
case EndTagTk: case EndTagTk:
handler = this.tagToWikitext[token.name]; handler = this.tagToWikitext[token.name];
if ( handler && handler.end ) { if ( handler && handler.end ) {
chunkCB( handler.end( state, state.curToken ) ); chunkCB( handler.end( state, token ) );
} else {
console.warn( 'Unhandled end tag token ' + JSON.stringify( token ) );
} }
break; break;
case String: case String:
@ -214,10 +219,8 @@ WSP._serializeTokens = function( tokens, chunkCB ) {
console.warn( 'Unhandled token type ' + JSON.stringify( token ) ); console.warn( 'Unhandled token type ' + JSON.stringify( token ) );
break; break;
} }
}
}; };
/** /**
* Serialize an HTML DOM document. * Serialize an HTML DOM document.
*/ */
@ -233,11 +236,12 @@ WSP.serializeDOM = function( node, chunkCB ) {
} }
}; };
/**
* Internal worker. Recursively serialize a DOM subtree by creating tokens and
* calling _serializeToken on each of these.
*/
WSP._serializeDOM = function( node, chunkCB, state ) { WSP._serializeDOM = function( node, chunkCB, state ) {
// serialize this node // serialize this node
var token;
state.lastToken = state.curToken;
state.curToken = null;
switch( node.nodeType ) { switch( node.nodeType ) {
case Node.ELEMENT_NODE: case Node.ELEMENT_NODE:
//console.warn( node.nodeName.toLowerCase() ); //console.warn( node.nodeName.toLowerCase() );
@ -245,37 +249,26 @@ WSP._serializeDOM = function( node, chunkCB, state ) {
name = node.nodeName.toLowerCase(), name = node.nodeName.toLowerCase(),
handler = this.tagToWikitext[name]; handler = this.tagToWikitext[name];
if ( handler ) { if ( handler ) {
var attribs = node.attributes, var tkAttribs = this._getDOMAttribs(node.attributes),
tkAttribs = this._getDOMAttribs(attribs), tkRTInfo = this._getDOMRTInfo(node.attributes);
tkRTInfo = this._getDOMRTInfo(attribs);
if ( handler.start ) { this._serializeToken( state, chunkCB,
state.curToken = new TagTk( name, tkAttribs, tkRTInfo ); new TagTk( name, tkAttribs, tkRTInfo ) );
// serialize the start tag
chunkCB( handler.start( state, state.curToken ) );
}
for ( var i = 0, l = children.length; i < l; i++ ) { for ( var i = 0, l = children.length; i < l; i++ ) {
// serialize all children // serialize all children
this._serializeDOM( children[i], chunkCB, state ); this._serializeDOM( children[i], chunkCB, state );
} }
state.lastToken = state.curToken; this._serializeToken( state, chunkCB,
if ( handler.end ) { new EndTagTk( name, tkAttribs, tkRTInfo ) );
state.curToken = new EndTagTk( name, tkAttribs, tkRTInfo );
// serialize the start tag
chunkCB( handler.end( state, state.curToken ) );
} else {
state.curToken = null;
}
} else { } else {
console.warn( 'Unhandled element: ' + node.innerHTML ); console.warn( 'Unhandled element: ' + node.innerHTML );
} }
break; break;
case Node.TEXT_NODE: case Node.TEXT_NODE:
state.curToken = node.data; this._serializeToken( state, chunkCB, node.data );
chunkCB( node.data );
break; break;
case Node.COMMENT_NODE: case Node.COMMENT_NODE:
state.curToken = new CommentTk( node.data ); this._serializeToken( state, chunkCB, new CommentTk( node.data ) );
chunkCB( '<!--' + node.data + '-->' );
break; break;
default: default:
console.warn( "Unhandled node type: " + console.warn( "Unhandled node type: " +