2011-12-08 14:37:31 +00:00
|
|
|
/* Generic token transformation dispatcher with support for asynchronous token
|
|
|
|
* expansion. Individual transformations register for the token types they are
|
2011-12-12 10:01:47 +00:00
|
|
|
* interested in and are called on each matching token.
|
|
|
|
*
|
|
|
|
* A transformer might return null, a single token, or an array of tokens.
|
|
|
|
* - Null removes the token and stops further processing for this token.
|
|
|
|
* - A single token is further processed using the remaining transformations
|
|
|
|
* registered for this token, and finally placed in the output token list.
|
|
|
|
* - A list of tokens stops the processing for this token. Instead, processing
|
|
|
|
* restarts with the first returned token.
|
|
|
|
*
|
|
|
|
* Additionally, transformers performing asynchronous actions on a token can
|
|
|
|
* create a new TokenAccumulator using .newAccumulator(). This creates a new
|
|
|
|
* accumulator for each asynchronous result, with the asynchronously processed
|
|
|
|
* token last in its internal accumulator. This setup avoids the need to apply
|
|
|
|
* operational-transform-like index transformations when parallel expansions
|
|
|
|
* insert tokens in front of other ongoing expansion tasks.
|
|
|
|
* */
|
2011-12-08 14:37:31 +00:00
|
|
|
|
|
|
|
function TokenTransformer( callback ) {
|
|
|
|
this.cb = callback; // Called with transformed token list when done
|
|
|
|
this.transformers = {
|
|
|
|
tag: {}, // for TAG, ENDTAG, SELFCLOSINGTAG, keyed on name
|
|
|
|
text: [],
|
|
|
|
newline: [],
|
|
|
|
comment: [],
|
|
|
|
end: [], // eof
|
|
|
|
martian: [] // none of the above
|
|
|
|
};
|
2011-12-12 14:03:54 +00:00
|
|
|
this.reset();
|
2011-12-08 14:37:31 +00:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2011-12-12 14:03:54 +00:00
|
|
|
TokenTransformer.prototype.reset = function () {
|
|
|
|
this.accum = new TokenAccumulator();
|
|
|
|
this.firstaccum = this.accum;
|
|
|
|
this.outstanding = 1; // Number of outstanding processing steps
|
|
|
|
// (e.g., async template fetches/expansions)
|
|
|
|
};
|
|
|
|
|
2011-12-08 14:37:31 +00:00
|
|
|
TokenTransformer.prototype.appendListener = function ( listener, type, name ) {
|
|
|
|
if ( type === 'tag' ) {
|
|
|
|
if ( $.isArray(this.transformers.tag.name) ) {
|
|
|
|
this.transformers.tag[name].push(listener);
|
|
|
|
} else {
|
|
|
|
this.transformers.tag[name] = [listener];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.transformers[type].push(listener);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TokenTransformer.prototype.prependListener = function ( listener, type, name ) {
|
|
|
|
if ( type === 'tag' ) {
|
|
|
|
if ( $.isArray(this.transformers.tag.name) ) {
|
|
|
|
this.transformers.tag[name].unshift(listener);
|
|
|
|
} else {
|
|
|
|
this.transformers.tag[name] = [listener];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.transformers[type].unshift(listener);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TokenTransformer.prototype.removeListener = function ( listener, type, name ) {
|
|
|
|
var i = -1;
|
|
|
|
var ts;
|
|
|
|
if ( type === 'tag' ) {
|
|
|
|
if ( $.isArray(this.transformers.tag.name) ) {
|
|
|
|
ts = this.transformers.tag[name];
|
|
|
|
i = ts.indexOf(listener);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ts = this.transformers[type];
|
|
|
|
i = ts.indexOf(listener);
|
|
|
|
}
|
|
|
|
if ( i >= 0 ) {
|
|
|
|
ts.splice(i, 1);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-12-12 10:01:47 +00:00
|
|
|
/* Constructor for information context relevant to token transformers
|
|
|
|
*
|
|
|
|
* @param token The token to precess
|
|
|
|
* @param accum {TokenAccumulator} The active TokenAccumulator.
|
|
|
|
* @param processor {TokenTransformer} The TokenTransformer object.
|
|
|
|
* @param lastToken Last returned token or {undefined}.
|
|
|
|
* @returns {TokenContext}.
|
|
|
|
*/
|
|
|
|
function TokenContext ( token, accum, transformer, lastToken ) {
|
|
|
|
this.token = token;
|
|
|
|
this.accum = accum;
|
|
|
|
this.transformer = transformer;
|
|
|
|
this.lastToken = lastToken;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Call all transformers on a tag.
|
|
|
|
*
|
|
|
|
* @param {TokenContext} The current token and its context.
|
|
|
|
* @returns {TokenContext} Context with updated token and/or accum.
|
|
|
|
*/
|
|
|
|
TokenTransformer.prototype._transformTagToken = function ( tokenCTX ) {
|
2011-12-12 14:03:54 +00:00
|
|
|
var ts = this.transformers.tag[tokenCTX.token.name];
|
2011-12-08 14:37:31 +00:00
|
|
|
if ( ts ) {
|
|
|
|
for (var i = 0, l = ts.length; i < l; i++ ) {
|
2011-12-12 10:01:47 +00:00
|
|
|
// Transform token with side effects
|
|
|
|
tokenCTX = ts[i]( tokenCTX );
|
|
|
|
if ( tokenCTX.token === null || $.isArray(tokenCTX.token) ) {
|
2011-12-08 14:37:31 +00:00
|
|
|
break;
|
|
|
|
}
|
2011-12-12 10:01:47 +00:00
|
|
|
|
2011-12-08 14:37:31 +00:00
|
|
|
}
|
|
|
|
}
|
2011-12-12 10:01:47 +00:00
|
|
|
return tokenCTX;
|
2011-12-08 14:37:31 +00:00
|
|
|
};
|
|
|
|
|
2011-12-12 10:01:47 +00:00
|
|
|
/* Call all transformers on other token types.
|
|
|
|
*
|
|
|
|
* @param tokenCTX {TokenContext} The current token and its context.
|
|
|
|
* @param ts List of token transformers for this token type.
|
|
|
|
* @returns {TokenContext} Context with updated token and/or accum.
|
|
|
|
*/
|
|
|
|
TokenTransformer.prototype._transformToken = function ( tokenCTX, ts ) {
|
2011-12-08 14:37:31 +00:00
|
|
|
if ( ts ) {
|
|
|
|
for (var i = 0, l = ts.length; i < l; i++ ) {
|
2011-12-12 10:01:47 +00:00
|
|
|
tokenCTX = ts[i]( tokenCTX );
|
|
|
|
if ( tokenCTX.token === null || $.isArray(tokenCTX.token) ) {
|
2011-12-08 14:37:31 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-12-12 10:01:47 +00:00
|
|
|
return tokenCTX;
|
2011-12-08 14:37:31 +00:00
|
|
|
};
|
|
|
|
|
2011-12-12 10:01:47 +00:00
|
|
|
/* Transform and expand tokens.
|
|
|
|
*
|
|
|
|
* Normally called with undefined accum. Asynchronous expansions will call
|
|
|
|
* this with their known accum, which allows expanded tokens to be spliced in
|
|
|
|
* at the appropriate location in the token list, which is always at the tail
|
|
|
|
* end of the current accumulator.
|
|
|
|
*
|
|
|
|
* @param tokens {List of tokens} Tokens to process.
|
|
|
|
* @param accum {TokenAccumulator} object. Undefined for first call, set to
|
|
|
|
* accumulator with expanded token at tail for asynchronous
|
|
|
|
* expansions.
|
|
|
|
* @returns nothing: Calls back registered callback if there are no more
|
|
|
|
* outstanding asynchronous expansions.
|
|
|
|
* */
|
|
|
|
TokenTransformer.prototype.transformTokens = function ( tokens, accum ) {
|
|
|
|
if ( accum === undefined ) {
|
2011-12-12 14:03:54 +00:00
|
|
|
this.reset();
|
2011-12-12 10:01:47 +00:00
|
|
|
accum = this.accum;
|
|
|
|
} else {
|
|
|
|
// Prepare to replace the last token in the current accumulator.
|
|
|
|
accum.pop();
|
|
|
|
}
|
2011-12-12 10:09:13 +00:00
|
|
|
var tokenCTX = new TokenContext(undefined, accum, this, undefined);
|
2011-12-08 14:37:31 +00:00
|
|
|
for ( var i = 0, l = tokens.length; i < l; i++ ) {
|
2011-12-12 10:01:47 +00:00
|
|
|
tokenCTX.lastToken = tokenCTX.token;
|
|
|
|
tokenCTX.token = tokens[i];
|
2011-12-08 14:37:31 +00:00
|
|
|
var ts;
|
2011-12-12 10:01:47 +00:00
|
|
|
switch(tokenCTX.token.type) {
|
2011-12-08 14:37:31 +00:00
|
|
|
case 'TAG':
|
|
|
|
case 'ENDTAG':
|
|
|
|
case 'SELFCLOSINGTAG':
|
2011-12-12 10:01:47 +00:00
|
|
|
tokenCTX = this._transformTagToken( tokenCTX );
|
2011-12-08 14:37:31 +00:00
|
|
|
break;
|
|
|
|
case 'TEXT':
|
2011-12-12 10:01:47 +00:00
|
|
|
tokenCTX = this._transformToken( tokenCTX, this.transformers.text );
|
2011-12-08 14:37:31 +00:00
|
|
|
break;
|
|
|
|
case 'COMMENT':
|
2011-12-12 10:01:47 +00:00
|
|
|
tokenCTX = this._transformToken( tokenCTX, this.transformers.comment);
|
2011-12-08 14:37:31 +00:00
|
|
|
break;
|
|
|
|
case 'NEWLINE':
|
2011-12-12 10:01:47 +00:00
|
|
|
tokenCTX = this._transformToken( tokenCTX, this.transformers.newline );
|
2011-12-08 14:37:31 +00:00
|
|
|
break;
|
|
|
|
case 'END':
|
2011-12-12 10:01:47 +00:00
|
|
|
tokenCTX = this._transformToken( tokenCTX, this.transformers.end );
|
2011-12-08 14:37:31 +00:00
|
|
|
break;
|
|
|
|
default:
|
2011-12-12 10:01:47 +00:00
|
|
|
tokenCTX = this._transformToken( tokenCTX, this.transformers.martian );
|
2011-12-08 14:37:31 +00:00
|
|
|
break;
|
|
|
|
}
|
2011-12-12 10:01:47 +00:00
|
|
|
if( $.isArray(tokenCTX.token) ) {
|
|
|
|
// Splice in the returned tokens (while replacing the original
|
|
|
|
// token), and process them next.
|
|
|
|
[].splice.apply(tokens, [i, 1].concat(tokenCTX.token));
|
|
|
|
l += res.token.length - 1;
|
|
|
|
i--; // continue at first inserted token
|
|
|
|
} else if (tokenCTX.token) {
|
|
|
|
// push to accumulator (not necessarily the last one)
|
|
|
|
accum.push(tokenCTX.token);
|
2011-12-08 14:37:31 +00:00
|
|
|
}
|
2011-12-12 10:01:47 +00:00
|
|
|
// Update current accum, in case a new one was spliced in by a
|
|
|
|
// transformation starting asynch work.
|
|
|
|
accum = tokenCTX.accum;
|
2011-12-08 14:37:31 +00:00
|
|
|
}
|
|
|
|
this.finish();
|
|
|
|
};
|
|
|
|
|
|
|
|
TokenTransformer.prototype.finish = function ( ) {
|
|
|
|
this.outstanding--;
|
|
|
|
if ( this.outstanding === 0 ) {
|
2011-12-12 10:01:47 +00:00
|
|
|
// Join the token accumulators back into a single token list
|
2011-12-08 14:37:31 +00:00
|
|
|
var a = this.firstaccum;
|
2011-12-12 14:03:54 +00:00
|
|
|
var tokens = a.accum;
|
2011-12-08 14:37:31 +00:00
|
|
|
while ( a.next !== undefined ) {
|
|
|
|
a = a.next;
|
2011-12-12 14:03:54 +00:00
|
|
|
tokens.concat(a.accum);
|
2011-12-08 14:37:31 +00:00
|
|
|
}
|
2011-12-12 10:01:47 +00:00
|
|
|
// Call our callback with the flattened token list
|
2011-12-12 14:03:54 +00:00
|
|
|
this.cb(tokens);
|
2011-12-08 14:37:31 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-12-12 10:01:47 +00:00
|
|
|
/* Start a new accumulator for asynchronous work. */
|
|
|
|
TokenTransformer.prototype.newAccumulator = function ( ) {
|
|
|
|
this.outstanding++;
|
|
|
|
return this.accum.insertAccumulator( );
|
2011-12-08 14:37:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Token accumulators in a linked list. Using a linked list simplifies async
|
|
|
|
// callbacks for template expansions.
|
|
|
|
function TokenAccumulator ( next, tokens ) {
|
|
|
|
this.next = next;
|
|
|
|
if ( tokens )
|
|
|
|
this.accum = tokens;
|
|
|
|
else
|
|
|
|
this.accum = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
TokenAccumulator.prototype.push = function ( token ) {
|
|
|
|
this.accum.push(token);
|
|
|
|
};
|
|
|
|
|
2011-12-12 10:01:47 +00:00
|
|
|
TokenAccumulator.prototype.pop = function ( ) {
|
|
|
|
return this.accum.pop();
|
|
|
|
};
|
|
|
|
|
|
|
|
TokenAccumulator.prototype.insertAccumulator = function ( ) {
|
2011-12-08 14:37:31 +00:00
|
|
|
this.next = new TokenAccumulator(this.next, tokens);
|
|
|
|
return this.next;
|
|
|
|
};
|
2011-12-12 14:03:54 +00:00
|
|
|
|
|
|
|
if (typeof module == "object") {
|
|
|
|
module.exports.TokenTransformer = TokenTransformer;
|
|
|
|
}
|