2012-01-09 17:49:16 +00:00
|
|
|
/**
|
|
|
|
* Token transformation managers with a (mostly) abstract
|
|
|
|
* TokenTransformManager base class and AsyncTokenTransformManager and
|
|
|
|
* SyncTokenTransformManager implementation subclasses. Individual
|
|
|
|
* transformations register for the token types they are interested in and are
|
|
|
|
* called on each matching token.
|
|
|
|
*
|
|
|
|
* Async token transformations are supported by the TokenAccumulator class,
|
|
|
|
* that manages as-early-as-possible and in-order return of tokens including
|
|
|
|
* buffering.
|
2011-12-12 10:01:47 +00:00
|
|
|
*
|
2012-01-03 18:44:31 +00:00
|
|
|
* See
|
2012-03-07 19:29:53 +00:00
|
|
|
* https://www.mediawiki.org/wiki/Parsoid/Token_stream_transformations
|
2012-01-03 18:44:31 +00:00
|
|
|
* for more documentation.
|
2011-12-14 09:33:25 +00:00
|
|
|
*
|
2012-01-03 18:44:31 +00:00
|
|
|
* @author Gabriel Wicke <gwicke@wikimedia.org>
|
|
|
|
*/
|
2011-12-08 14:37:31 +00:00
|
|
|
|
2012-01-03 18:44:31 +00:00
|
|
|
var events = require('events');
|
2011-12-28 01:37:06 +00:00
|
|
|
|
2011-12-13 20:13:09 +00:00
|
|
|
/**
|
2012-01-09 17:49:16 +00:00
|
|
|
* Base class for token transform managers
|
2011-12-13 20:13:09 +00:00
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @constructor
|
|
|
|
* @param {Function} callback, a callback function accepting a token list as
|
|
|
|
* its only argument.
|
|
|
|
*/
|
2012-04-25 14:35:59 +00:00
|
|
|
function TokenTransformManager( env, isInclude, pipeFactory, phaseEndRank, attributeType ) {
|
2012-01-09 17:49:16 +00:00
|
|
|
// Separate the constructor, so that we can call it from subclasses.
|
|
|
|
this._construct();
|
2011-12-08 14:37:31 +00:00
|
|
|
}
|
|
|
|
|
2012-01-03 18:44:31 +00:00
|
|
|
// Inherit from EventEmitter
|
2012-01-09 17:49:16 +00:00
|
|
|
TokenTransformManager.prototype = new events.EventEmitter();
|
|
|
|
TokenTransformManager.prototype.constructor = TokenTransformManager;
|
|
|
|
|
|
|
|
TokenTransformManager.prototype._construct = function () {
|
|
|
|
this.transformers = {
|
2012-03-07 20:06:54 +00:00
|
|
|
tag: {}, // for TagTk, EndTagTk, SelfclosingTagTk, keyed on name
|
2012-01-09 17:49:16 +00:00
|
|
|
text: [],
|
|
|
|
newline: [],
|
|
|
|
comment: [],
|
|
|
|
end: [], // eof
|
|
|
|
martian: [], // none of the above (unknown token type)
|
|
|
|
any: [] // all tokens, before more specific handlers are run
|
|
|
|
};
|
|
|
|
};
|
2012-01-03 18:44:31 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Register to a token source, normally the tokenizer.
|
2012-01-04 11:00:54 +00:00
|
|
|
* The event emitter emits a 'chunk' event with a chunk of tokens,
|
2012-01-03 18:44:31 +00:00
|
|
|
* and signals the end of tokens by triggering the 'end' event.
|
2012-01-09 17:49:16 +00:00
|
|
|
* XXX: Perform registration directly in the constructor?
|
2012-01-03 18:44:31 +00:00
|
|
|
*
|
2012-01-09 17:49:16 +00:00
|
|
|
* @method
|
2012-01-03 18:44:31 +00:00
|
|
|
* @param {Object} EventEmitter token even emitter.
|
|
|
|
*/
|
2012-04-25 14:35:59 +00:00
|
|
|
TokenTransformManager.prototype.addListenersOn = function ( tokenEmitter ) {
|
2012-01-09 17:49:16 +00:00
|
|
|
tokenEmitter.addListener('chunk', this.onChunk.bind( this ) );
|
2012-01-03 18:44:31 +00:00
|
|
|
tokenEmitter.addListener('end', this.onEndEvent.bind( this ) );
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-12-13 20:13:09 +00:00
|
|
|
/**
|
2012-01-03 18:44:31 +00:00
|
|
|
* Add a transform registration.
|
2011-12-13 20:13:09 +00:00
|
|
|
*
|
|
|
|
* @method
|
2012-01-03 18:44:31 +00:00
|
|
|
* @param {Function} transform.
|
2012-01-04 09:48:24 +00:00
|
|
|
* @param {Number} rank, [0,3) with [0,1) in-order on input token stream,
|
|
|
|
* [1,2) out-of-order and [2,3) in-order on output token stream
|
2011-12-13 20:13:09 +00:00
|
|
|
* @param {String} type, one of 'tag', 'text', 'newline', 'comment', 'end',
|
|
|
|
* 'martian' (unknown token), 'any' (any token, matched before other matches).
|
|
|
|
* @param {String} tag name for tags, omitted for non-tags
|
|
|
|
*/
|
2012-01-09 17:49:16 +00:00
|
|
|
TokenTransformManager.prototype.addTransform = function ( transformation, rank, type, name ) {
|
|
|
|
var transArr,
|
2012-01-03 18:44:31 +00:00
|
|
|
transformer = {
|
|
|
|
transform: transformation,
|
|
|
|
rank: rank
|
|
|
|
};
|
2011-12-08 14:37:31 +00:00
|
|
|
if ( type === 'tag' ) {
|
2011-12-13 14:48:47 +00:00
|
|
|
name = name.toLowerCase();
|
2012-01-09 17:49:16 +00:00
|
|
|
transArr = this.transformers.tag[name];
|
2012-01-03 18:44:31 +00:00
|
|
|
if ( ! transArr ) {
|
2012-01-09 17:49:16 +00:00
|
|
|
transArr = this.transformers.tag[name] = [];
|
2011-12-08 14:37:31 +00:00
|
|
|
}
|
|
|
|
} else {
|
2012-01-09 17:49:16 +00:00
|
|
|
transArr = this.transformers[type];
|
2011-12-08 14:37:31 +00:00
|
|
|
}
|
2012-01-03 18:44:31 +00:00
|
|
|
transArr.push(transformer);
|
|
|
|
// sort ascending by rank
|
2012-01-04 14:09:05 +00:00
|
|
|
transArr.sort( this._cmpTransformations );
|
2012-04-12 13:42:09 +00:00
|
|
|
//this.env.dp( 'transforms: ', this.transformers );
|
2011-12-08 14:37:31 +00:00
|
|
|
};
|
|
|
|
|
2011-12-13 20:13:09 +00:00
|
|
|
/**
|
2012-01-03 18:44:31 +00:00
|
|
|
* Remove a transform registration
|
2011-12-13 20:13:09 +00:00
|
|
|
*
|
|
|
|
* @method
|
2012-01-04 09:48:24 +00:00
|
|
|
* @param {Function} transform.
|
|
|
|
* @param {Number} rank, [0,3) with [0,1) in-order on input token stream,
|
|
|
|
* [1,2) out-of-order and [2,3) in-order on output token stream
|
2011-12-13 20:13:09 +00:00
|
|
|
* @param {String} type, one of 'tag', 'text', 'newline', 'comment', 'end',
|
|
|
|
* 'martian' (unknown token), 'any' (any token, matched before other matches).
|
|
|
|
* @param {String} tag name for tags, omitted for non-tags
|
|
|
|
*/
|
2012-01-09 17:49:16 +00:00
|
|
|
TokenTransformManager.prototype.removeTransform = function ( rank, type, name ) {
|
2012-01-03 18:44:31 +00:00
|
|
|
var i = -1,
|
|
|
|
ts;
|
|
|
|
|
|
|
|
function rankUnEqual ( i ) {
|
|
|
|
return i.rank !== rank;
|
|
|
|
}
|
|
|
|
|
2011-12-08 14:37:31 +00:00
|
|
|
if ( type === 'tag' ) {
|
2011-12-13 14:48:47 +00:00
|
|
|
name = name.toLowerCase();
|
2012-01-09 17:49:16 +00:00
|
|
|
var maybeTransArr = this.transformers.tag.name;
|
2012-01-03 18:44:31 +00:00
|
|
|
if ( maybeTransArr ) {
|
2012-01-09 17:49:16 +00:00
|
|
|
this.transformers.tag.name = maybeTransArr.filter( rankUnEqual );
|
2011-12-08 14:37:31 +00:00
|
|
|
}
|
|
|
|
} else {
|
2012-01-09 17:49:16 +00:00
|
|
|
this.transformers[type] = this.transformers[type].filter( rankUnEqual ) ;
|
2011-12-08 14:37:31 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-02-28 13:24:35 +00:00
|
|
|
TokenTransformManager.prototype.setTokensRank = function ( tokens, rank ) {
|
|
|
|
for ( var i = 0, l = tokens.length; i < l; i++ ) {
|
2012-04-25 14:35:59 +00:00
|
|
|
tokens[i] = this.env.setTokenRank( tokens[i], rank );
|
2012-02-28 13:24:35 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-01-04 14:09:05 +00:00
|
|
|
/**
|
2012-04-25 14:35:59 +00:00
|
|
|
* Predicate for sorting transformations by ascending rank.
|
2012-01-04 14:09:05 +00:00
|
|
|
*/
|
2012-01-09 17:49:16 +00:00
|
|
|
TokenTransformManager.prototype._cmpTransformations = function ( a, b ) {
|
2012-01-04 14:09:05 +00:00
|
|
|
return a.rank - b.rank;
|
|
|
|
};
|
2011-12-12 10:01:47 +00:00
|
|
|
|
2012-04-25 14:35:59 +00:00
|
|
|
/**
|
|
|
|
* Get all transforms for a given token
|
2011-12-12 10:01:47 +00:00
|
|
|
*/
|
2012-04-25 14:35:59 +00:00
|
|
|
TokenTransformManager.prototype._getTransforms = function ( token ) {
|
|
|
|
var ts;
|
|
|
|
switch ( token.constructor ) {
|
|
|
|
case String:
|
|
|
|
ts = this.transformers.text;
|
|
|
|
break;
|
|
|
|
case NlTk:
|
|
|
|
ts = this.transformers.newline;
|
|
|
|
break;
|
|
|
|
case CommentTk:
|
|
|
|
ts = this.transformers.comment;
|
|
|
|
break;
|
|
|
|
case EOFTk:
|
|
|
|
ts = this.transformers.end;
|
|
|
|
break;
|
|
|
|
case TagTk:
|
|
|
|
case EndTagTk:
|
|
|
|
case SelfclosingTagTk:
|
|
|
|
ts = this.transformers.tag[token.name.toLowerCase()];
|
|
|
|
if ( ! ts ) {
|
|
|
|
ts = [];
|
2012-01-03 18:44:31 +00:00
|
|
|
}
|
2012-04-25 14:35:59 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ts = this.transformers.martian;
|
|
|
|
break;
|
2011-12-08 14:37:31 +00:00
|
|
|
}
|
2012-04-26 16:18:08 +00:00
|
|
|
// XXX: cache this to avoid constant re-sorting?
|
|
|
|
if ( this.transformers.any.length ) {
|
|
|
|
ts = ts.concat( this.transformers.any );
|
|
|
|
ts.sort( this._cmpTransformations );
|
|
|
|
}
|
2012-04-25 14:35:59 +00:00
|
|
|
return ts;
|
2011-12-08 14:37:31 +00:00
|
|
|
};
|
2012-04-25 14:35:59 +00:00
|
|
|
|
2012-01-09 17:49:16 +00:00
|
|
|
/******************** Async token transforms: Phase 2 **********************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Asynchronous and potentially out-of-order token transformations, used in phase 2.
|
|
|
|
*
|
|
|
|
* return protocol for individual transforms:
|
|
|
|
* { tokens: [tokens], async: true }: async expansion -> outstanding++ in parent
|
|
|
|
* { tokens: [tokens] }: fully expanded, tokens will be reprocessed
|
|
|
|
* { token: token }: single-token return
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @constructor
|
|
|
|
* @param {Function} childFactory: A function that can be used to create a
|
|
|
|
* new, nested transform manager:
|
|
|
|
* nestedAsyncTokenTransformManager = manager.newChildPipeline( inputType, args );
|
|
|
|
* @param {Object} args, the argument map for templates
|
|
|
|
* @param {Object} env, the environment.
|
|
|
|
*/
|
2012-04-25 14:35:59 +00:00
|
|
|
function AsyncTokenTransformManager ( env, isInclude, pipeFactory, phaseEndRank, attributeType ) {
|
|
|
|
this.env = env;
|
2012-04-11 14:34:27 +00:00
|
|
|
this.isInclude = isInclude;
|
2012-04-25 14:35:59 +00:00
|
|
|
this.pipeFactory = pipeFactory;
|
|
|
|
this.phaseEndRank = phaseEndRank;
|
|
|
|
this.attributeType = attributeType;
|
|
|
|
this.setFrame( null, null, {} );
|
|
|
|
this._construct();
|
2012-01-09 17:49:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Inherit from TokenTransformManager, and thus also from EventEmitter.
|
|
|
|
AsyncTokenTransformManager.prototype = new TokenTransformManager();
|
|
|
|
AsyncTokenTransformManager.prototype.constructor = AsyncTokenTransformManager;
|
|
|
|
|
2012-01-10 01:09:50 +00:00
|
|
|
|
2012-01-03 18:44:31 +00:00
|
|
|
/**
|
2012-01-09 17:49:16 +00:00
|
|
|
* Reset the internal token and outstanding-callback state of the
|
|
|
|
* TokenTransformManager, but keep registrations untouched.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {Object} args, template arguments
|
|
|
|
* @param {Object} The environment.
|
2012-01-03 18:44:31 +00:00
|
|
|
*/
|
2012-04-25 14:35:59 +00:00
|
|
|
AsyncTokenTransformManager.prototype.setFrame = function ( parentFrame, title, args ) {
|
|
|
|
// First piggy-back some reset action
|
2012-01-09 17:49:16 +00:00
|
|
|
this.tailAccumulator = undefined;
|
2012-03-07 15:39:18 +00:00
|
|
|
// initial top-level callback, emits chunks
|
2012-01-09 17:49:16 +00:00
|
|
|
this.tokenCB = this._returnTokens.bind( this );
|
2012-04-25 14:35:59 +00:00
|
|
|
|
|
|
|
// now actually set up the frame
|
|
|
|
if (parentFrame) {
|
|
|
|
if ( title === null ) {
|
|
|
|
// attribute, simply reuse the parent frame
|
|
|
|
this.frame = parentFrame;
|
|
|
|
} else {
|
|
|
|
this.frame = parentFrame.newChild( title, this, args );
|
2012-01-09 17:49:16 +00:00
|
|
|
}
|
|
|
|
} else {
|
2012-04-25 14:35:59 +00:00
|
|
|
this.frame = new Frame(title, this, args );
|
2011-12-12 10:01:47 +00:00
|
|
|
}
|
2012-01-03 18:44:31 +00:00
|
|
|
};
|
2011-12-12 20:53:14 +00:00
|
|
|
|
2012-04-25 14:35:59 +00:00
|
|
|
|
|
|
|
|
2012-01-11 19:48:49 +00:00
|
|
|
/**
|
|
|
|
* Simplified wrapper that processes all tokens passed in
|
|
|
|
*/
|
|
|
|
AsyncTokenTransformManager.prototype.process = function ( tokens ) {
|
|
|
|
if ( ! $.isArray ( tokens ) ) {
|
|
|
|
tokens = [tokens];
|
|
|
|
}
|
|
|
|
this.onChunk( tokens );
|
|
|
|
this.onEndEvent();
|
|
|
|
};
|
2012-01-04 09:48:24 +00:00
|
|
|
|
2012-01-09 17:49:16 +00:00
|
|
|
/**
|
|
|
|
* Transform and expand tokens. Transformed token chunks will be emitted in
|
|
|
|
* the 'chunk' event.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {Array} chunk of tokens
|
|
|
|
*/
|
|
|
|
AsyncTokenTransformManager.prototype.onChunk = function ( tokens ) {
|
|
|
|
// Set top-level callback to next transform phase
|
|
|
|
var res = this.transformTokens ( tokens, this.tokenCB );
|
2012-04-25 14:35:59 +00:00
|
|
|
this.env.dp( 'AsyncTokenTransformManager onChunk res=', res );
|
2012-01-19 23:43:39 +00:00
|
|
|
|
|
|
|
if ( ! this.tailAccumulator ) {
|
|
|
|
this.emit( 'chunk', res.tokens );
|
|
|
|
} else {
|
2012-01-22 23:21:53 +00:00
|
|
|
this.tailAccumulator.append( res.tokens );
|
2012-01-19 23:43:39 +00:00
|
|
|
}
|
|
|
|
|
2012-01-09 17:49:16 +00:00
|
|
|
if ( res.async ) {
|
2012-01-19 23:43:39 +00:00
|
|
|
this.tailAccumulator = res.async;
|
2012-01-09 17:49:16 +00:00
|
|
|
this.tokenCB = res.async.getParentCB ( 'sibling' );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-01-03 18:44:31 +00:00
|
|
|
/**
|
2012-01-04 09:48:24 +00:00
|
|
|
* Run transformations from phases 0 and 1. This includes starting and
|
|
|
|
* managing asynchronous transformations.
|
|
|
|
*
|
2012-01-03 18:44:31 +00:00
|
|
|
*/
|
2012-01-09 17:49:16 +00:00
|
|
|
AsyncTokenTransformManager.prototype.transformTokens = function ( tokens, parentCB ) {
|
2011-12-12 20:53:14 +00:00
|
|
|
|
2012-02-14 20:56:14 +00:00
|
|
|
//console.warn('AsyncTokenTransformManager.transformTokens: ' + JSON.stringify(tokens) );
|
2012-01-03 18:44:31 +00:00
|
|
|
|
2012-04-26 16:18:08 +00:00
|
|
|
var res,
|
2012-01-03 18:44:31 +00:00
|
|
|
localAccum = [],
|
2012-04-25 14:35:59 +00:00
|
|
|
transforming = true,
|
2012-01-03 18:44:31 +00:00
|
|
|
activeAccum = null,
|
2012-04-26 16:18:08 +00:00
|
|
|
accum = new TokenAccumulator( this, parentCB ),
|
|
|
|
token, ts, transformer, aborted,
|
2012-04-25 14:35:59 +00:00
|
|
|
maybeSyncReturn = function ( asyncCB, ret ) {
|
|
|
|
if ( transforming ) {
|
|
|
|
this.env.dp( 'maybeSyncReturn transforming', ret );
|
|
|
|
// transformTokens is ongoing
|
|
|
|
if ( false && ret.tokens && ! ret.async && ret.allTokensProcessed && ! activeAccum ) {
|
|
|
|
localAccum.push.apply(localAccum, res.tokens );
|
|
|
|
} else if ( ret.tokens ) {
|
|
|
|
if ( res.tokens ) {
|
|
|
|
res.tokens = res.tokens.concat( ret.tokens );
|
|
|
|
res.async = ret.async;
|
|
|
|
} else {
|
|
|
|
res = ret;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ( ! res.tokens ) {
|
|
|
|
res = ret;
|
|
|
|
} else {
|
|
|
|
res.async = ret.async;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.env.dp( 'maybeSyncReturn async', ret );
|
|
|
|
asyncCB( ret );
|
|
|
|
}
|
|
|
|
},
|
|
|
|
cb = maybeSyncReturn.bind( this, accum.getParentCB( 'child' ) );
|
2012-01-11 19:48:49 +00:00
|
|
|
|
2012-04-26 16:18:08 +00:00
|
|
|
for ( var i = 0, l = tokens.length; i < l; i++ ) {
|
2012-01-03 18:44:31 +00:00
|
|
|
token = tokens[i];
|
|
|
|
|
2012-04-25 14:35:59 +00:00
|
|
|
aborted = false;
|
|
|
|
|
|
|
|
ts = this._getTransforms( token );
|
|
|
|
|
|
|
|
if ( ts.length ) {
|
|
|
|
res = { };
|
|
|
|
|
2012-04-26 16:18:08 +00:00
|
|
|
//this.env.tp( 'async trans' );
|
|
|
|
this.env.dp( token, ts );
|
2012-04-25 14:35:59 +00:00
|
|
|
for (var j = 0, lts = ts.length; j < lts; j++ ) {
|
|
|
|
transformer = ts[j];
|
|
|
|
if ( token.rank && transformer.rank <= token.rank ) {
|
|
|
|
// skip transformation, was already applied.
|
|
|
|
//console.warn( 'skipping transform');
|
|
|
|
res.token = token;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Transform the token.
|
|
|
|
transformer.transform( token, this.frame, cb );
|
2012-04-26 16:18:08 +00:00
|
|
|
if ( res.tokens && res.tokens.length === 1 &&
|
|
|
|
token.constructor === res.tokens[0].constructor &&
|
|
|
|
token.name === res.tokens[0].name )
|
|
|
|
{
|
|
|
|
res.token = res.tokens[0];
|
|
|
|
}
|
2012-04-25 14:35:59 +00:00
|
|
|
if ( res.token === undefined ) {
|
|
|
|
aborted = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// XXX: factor the conversion to String out into a generic _setRank
|
|
|
|
// method? Would need to add to the string prototype for that..
|
|
|
|
token = this.env.setTokenRank( res.token, transformer.rank );
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
res = { token: token };
|
2011-12-08 14:37:31 +00:00
|
|
|
}
|
2012-01-03 18:44:31 +00:00
|
|
|
|
2012-04-25 14:35:59 +00:00
|
|
|
this.env.dp( 'res: ', res);
|
|
|
|
|
|
|
|
if ( ! aborted && res.token ) {
|
|
|
|
res.token = this.env.setTokenRank( res.token, this.phaseEndRank );
|
|
|
|
// token is done.
|
|
|
|
if ( activeAccum ) {
|
|
|
|
// push to accumulator
|
|
|
|
activeAccum.push( res.token );
|
|
|
|
} else {
|
|
|
|
// If there is no accumulator yet, then directly return the
|
|
|
|
// token to the parent. Collect them in localAccum for this
|
|
|
|
// purpose.
|
|
|
|
localAccum.push( res.token );
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
} else if( res.tokens ) {
|
|
|
|
if ( res.tokens.length > 1 ) {
|
|
|
|
// Splice in the returned tokens (while replacing the original
|
|
|
|
// token), and process them next.
|
|
|
|
//if ( ! res.allTokensProcessed ) {
|
|
|
|
[].splice.apply( tokens, [i, 1].concat(res.tokens) );
|
2012-04-26 16:18:08 +00:00
|
|
|
l = tokens.length;
|
2012-04-25 14:35:59 +00:00
|
|
|
i--; // continue at first inserted token
|
|
|
|
//} else {
|
|
|
|
// skip fully processed tokens
|
|
|
|
//}
|
|
|
|
} else if ( res.tokens.length === 1 ) {
|
|
|
|
if ( res.tokens[0].rank === this.phaseEndRank ) {
|
|
|
|
// token is done.
|
|
|
|
if ( activeAccum ) {
|
|
|
|
// push to accumulator
|
|
|
|
activeAccum.push( res.tokens[0] );
|
|
|
|
} else {
|
|
|
|
// If there is no accumulator yet, then directly return the
|
|
|
|
// token to the parent. Collect them in localAccum for this
|
|
|
|
// purpose.
|
|
|
|
localAccum.push( res.tokens[0] );
|
|
|
|
}
|
2012-01-03 18:44:31 +00:00
|
|
|
} else {
|
2012-04-25 14:35:59 +00:00
|
|
|
// re-process token.
|
|
|
|
tokens[i] = res.tokens[0];
|
|
|
|
i--;
|
2012-01-03 18:44:31 +00:00
|
|
|
}
|
|
|
|
}
|
2012-04-25 14:35:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( res.async ) {
|
|
|
|
this.env.dp( 'res.async' );
|
2012-01-03 18:44:31 +00:00
|
|
|
// The child now switched to activeAccum, we have to create a new
|
|
|
|
// accumulator for the next potential child.
|
|
|
|
activeAccum = accum;
|
2012-01-21 20:38:13 +00:00
|
|
|
accum = new TokenAccumulator( this, activeAccum.getParentCB( 'sibling' ) );
|
2012-04-25 14:35:59 +00:00
|
|
|
cb = maybeSyncReturn.bind( this, accum.getParentCB( 'child' ) );
|
2011-12-08 14:37:31 +00:00
|
|
|
}
|
2011-12-12 20:53:14 +00:00
|
|
|
}
|
2012-04-25 14:35:59 +00:00
|
|
|
transforming = false;
|
2011-12-12 20:53:14 +00:00
|
|
|
|
2012-01-03 18:44:31 +00:00
|
|
|
// Return finished tokens directly to caller, and indicate if further
|
|
|
|
// async actions are outstanding. The caller needs to point a sibling to
|
|
|
|
// the returned accumulator, or call .siblingDone() to mark the end of a
|
|
|
|
// chain.
|
|
|
|
return { tokens: localAccum, async: activeAccum };
|
2011-12-08 14:37:31 +00:00
|
|
|
};
|
|
|
|
|
2011-12-13 20:13:09 +00:00
|
|
|
/**
|
2012-03-07 19:29:53 +00:00
|
|
|
* Top-level callback for tokens which are now free to be emitted iff they are
|
|
|
|
* indeed fully processed for sync01 and async12. If there were asynchronous
|
|
|
|
* expansions, then only the first TokenAccumulator has its callback set to
|
|
|
|
* this method. An exhausted TokenAccumulator passes its callback on to its
|
|
|
|
* siblings until the last accumulator is reached, so that the head
|
|
|
|
* accumulator will always call this method directly.
|
2012-01-09 17:49:16 +00:00
|
|
|
*
|
|
|
|
* @method
|
2012-04-25 14:35:59 +00:00
|
|
|
* @param {Object} tokens, async, allTokensProcessed
|
2012-03-07 19:29:53 +00:00
|
|
|
* @returns {Mixed} new parent callback for caller or falsy value.
|
2011-12-13 20:13:09 +00:00
|
|
|
*/
|
2012-04-25 14:35:59 +00:00
|
|
|
AsyncTokenTransformManager.prototype._returnTokens = function ( ret ) {
|
2012-01-09 17:49:16 +00:00
|
|
|
//tokens = this._transformPhase2( this.frame, tokens, this.parentCB );
|
2012-01-21 03:03:33 +00:00
|
|
|
|
2012-04-26 16:18:08 +00:00
|
|
|
this.env.dp( 'AsyncTokenTransformManager._returnTokens, emitting chunk: ',
|
2012-04-25 14:35:59 +00:00
|
|
|
ret );
|
2012-01-03 18:44:31 +00:00
|
|
|
|
2012-04-25 14:35:59 +00:00
|
|
|
if( !ret.allTokensProcessed ) {
|
|
|
|
var res = this.transformTokens( ret.tokens, this._returnTokens.bind(this) );
|
2012-03-07 15:39:18 +00:00
|
|
|
this.emit( 'chunk', res.tokens );
|
|
|
|
if ( res.async ) {
|
2012-04-25 14:35:59 +00:00
|
|
|
// XXX: this looks fishy
|
2012-03-07 15:39:18 +00:00
|
|
|
if ( ! this.tailAccumulator ) {
|
|
|
|
this.tailAccumulator = res.async;
|
|
|
|
this.tokenCB = res.async.getParentCB ( 'sibling' );
|
|
|
|
}
|
2012-04-25 14:35:59 +00:00
|
|
|
if ( ret.async ) {
|
2012-03-07 15:39:18 +00:00
|
|
|
// return sibling callback
|
|
|
|
return this.tokenCB;
|
|
|
|
} else {
|
|
|
|
// signal done-ness to last accum
|
|
|
|
res.async.siblingDone();
|
|
|
|
}
|
2012-04-25 14:35:59 +00:00
|
|
|
} else if ( !ret.async ) {
|
2012-03-07 15:39:18 +00:00
|
|
|
this.emit( 'end' );
|
|
|
|
// and reset internal state.
|
2012-04-25 14:35:59 +00:00
|
|
|
//this._reset();
|
2012-03-07 15:39:18 +00:00
|
|
|
}
|
|
|
|
} else {
|
2012-04-25 14:35:59 +00:00
|
|
|
this.emit( 'chunk', ret.tokens );
|
2012-03-07 15:39:18 +00:00
|
|
|
|
2012-04-25 14:35:59 +00:00
|
|
|
if ( ! ret.async ) {
|
|
|
|
//console.trace();
|
2012-03-07 15:39:18 +00:00
|
|
|
// signal our done-ness to consumers.
|
|
|
|
this.emit( 'end' );
|
|
|
|
// and reset internal state.
|
2012-04-25 14:35:59 +00:00
|
|
|
//this._reset();
|
2012-03-07 15:39:18 +00:00
|
|
|
}
|
2011-12-08 14:37:31 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-01-09 17:49:16 +00:00
|
|
|
/**
|
|
|
|
* Callback for the end event emitted from the tokenizer.
|
|
|
|
* Either signals the end of input to the tail of an ongoing asynchronous
|
|
|
|
* processing pipeline, or directly emits 'end' if the processing was fully
|
|
|
|
* synchronous.
|
|
|
|
*/
|
|
|
|
AsyncTokenTransformManager.prototype.onEndEvent = function () {
|
|
|
|
if ( this.tailAccumulator ) {
|
2012-01-21 03:03:33 +00:00
|
|
|
this.env.dp( 'AsyncTokenTransformManager.onEndEvent: calling siblingDone',
|
2012-04-26 16:18:08 +00:00
|
|
|
this.frame );
|
2012-01-09 17:49:16 +00:00
|
|
|
this.tailAccumulator.siblingDone();
|
|
|
|
} else {
|
|
|
|
// nothing was asynchronous, so we'll have to emit end here.
|
2012-01-21 03:03:33 +00:00
|
|
|
this.env.dp( 'AsyncTokenTransformManager.onEndEvent: synchronous done',
|
2012-04-26 16:18:08 +00:00
|
|
|
this.frame );
|
2012-01-09 17:49:16 +00:00
|
|
|
this.emit('end');
|
2012-04-25 14:35:59 +00:00
|
|
|
//this._reset();
|
2012-01-09 17:49:16 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*************** In-order, synchronous transformer (phase 1 and 3) ***************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Subclass for phase 3, in-order and synchronous processing.
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @constructor
|
|
|
|
* @param {Object} environment.
|
|
|
|
*/
|
2012-04-25 14:35:59 +00:00
|
|
|
function SyncTokenTransformManager ( env, isInclude, pipeFactory, phaseEndRank, attributeType ) {
|
2012-01-09 17:49:16 +00:00
|
|
|
this.env = env;
|
2012-04-11 14:34:27 +00:00
|
|
|
this.isInclude = isInclude;
|
2012-04-25 14:35:59 +00:00
|
|
|
this.pipeFactory = pipeFactory;
|
|
|
|
this.phaseEndRank = phaseEndRank;
|
|
|
|
this.attributeType = attributeType;
|
|
|
|
this._construct();
|
2012-01-09 17:49:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Inherit from TokenTransformManager, and thus also from EventEmitter.
|
|
|
|
SyncTokenTransformManager.prototype = new TokenTransformManager();
|
|
|
|
SyncTokenTransformManager.prototype.constructor = SyncTokenTransformManager;
|
|
|
|
|
2012-01-03 18:44:31 +00:00
|
|
|
|
2012-01-11 19:48:49 +00:00
|
|
|
SyncTokenTransformManager.prototype.process = function ( tokens ) {
|
|
|
|
if ( ! $.isArray ( tokens ) ) {
|
|
|
|
tokens = [tokens];
|
|
|
|
}
|
|
|
|
this.onChunk( tokens );
|
2012-04-12 13:42:09 +00:00
|
|
|
//console.warn( JSON.stringify( this.transformers ) )
|
2012-01-11 19:48:49 +00:00
|
|
|
this.onEndEvent();
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-12-13 20:13:09 +00:00
|
|
|
/**
|
2012-01-09 17:49:16 +00:00
|
|
|
* Global in-order and synchronous traversal on token stream. Emits
|
|
|
|
* transformed chunks of tokens in the 'chunk' event.
|
2011-12-13 20:13:09 +00:00
|
|
|
*
|
2012-01-09 17:49:16 +00:00
|
|
|
* @method
|
|
|
|
* @param {Array} Token chunk.
|
2012-01-03 18:44:31 +00:00
|
|
|
*/
|
2012-01-09 17:49:16 +00:00
|
|
|
SyncTokenTransformManager.prototype.onChunk = function ( tokens ) {
|
2012-02-21 18:49:43 +00:00
|
|
|
this.env.dp( 'SyncTokenTransformManager.onChunk, input: ', tokens );
|
2012-01-03 18:44:31 +00:00
|
|
|
var res,
|
|
|
|
localAccum = [],
|
|
|
|
localAccumLength = 0,
|
|
|
|
token,
|
2012-01-09 17:49:16 +00:00
|
|
|
// Top-level frame only in phase 3, as everything is already expanded.
|
2012-04-25 14:35:59 +00:00
|
|
|
ts, transformer,
|
|
|
|
aborted;
|
2012-01-03 18:44:31 +00:00
|
|
|
|
2012-04-26 16:18:08 +00:00
|
|
|
for ( var i = 0, l = tokens.length; i < l; i++ ) {
|
2012-04-25 14:35:59 +00:00
|
|
|
aborted = false;
|
2012-01-03 18:44:31 +00:00
|
|
|
token = tokens[i];
|
2012-04-25 14:35:59 +00:00
|
|
|
res = { token: token };
|
2012-02-01 16:30:43 +00:00
|
|
|
|
2012-04-25 14:35:59 +00:00
|
|
|
ts = this._getTransforms( token );
|
2012-04-26 16:18:08 +00:00
|
|
|
var minRank = token.rank || 0;
|
2012-04-25 14:35:59 +00:00
|
|
|
for (var j = 0, lts = ts.length; j < lts; j++ ) {
|
|
|
|
transformer = ts[j];
|
2012-04-26 16:18:08 +00:00
|
|
|
if ( transformer.rank < minRank ) {
|
2012-04-25 14:35:59 +00:00
|
|
|
// skip transformation, was already applied.
|
|
|
|
//console.warn( 'skipping transform');
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Transform the token.
|
|
|
|
res = transformer.transform( res.token, this, this.prevToken );
|
|
|
|
if ( !res.token ||
|
|
|
|
res.token.constructor !== token.constructor ||
|
|
|
|
( token.name && res.token.name && res.token.name !== token.name ) ) {
|
|
|
|
aborted = true;
|
|
|
|
break;
|
|
|
|
}
|
2012-04-26 16:18:08 +00:00
|
|
|
minRank = transformer.rank;
|
2012-04-25 14:35:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! aborted ) {
|
|
|
|
res.token = this.env.setTokenRank( res.token, this.phaseEndRank );
|
2012-01-03 18:44:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( res.tokens ) {
|
|
|
|
// Splice in the returned tokens (while replacing the original
|
|
|
|
// token), and process them next.
|
|
|
|
[].splice.apply( tokens, [i, 1].concat(res.tokens) );
|
2012-04-26 16:18:08 +00:00
|
|
|
l = tokens.length;
|
2012-01-03 18:44:31 +00:00
|
|
|
i--; // continue at first inserted token
|
|
|
|
} else if ( res.token ) {
|
2012-01-13 18:48:25 +00:00
|
|
|
if ( res.token.rank === this.phaseEndRank ) {
|
2012-01-03 18:44:31 +00:00
|
|
|
// token is done.
|
|
|
|
localAccum.push(res.token);
|
|
|
|
this.prevToken = res.token;
|
|
|
|
} else {
|
|
|
|
// re-process token.
|
|
|
|
tokens[i] = res.token;
|
|
|
|
i--;
|
|
|
|
}
|
|
|
|
}
|
2011-12-12 20:53:14 +00:00
|
|
|
}
|
2012-02-21 18:49:43 +00:00
|
|
|
this.env.dp( 'SyncTokenTransformManager.onChunk: emitting ', localAccum );
|
2012-01-09 17:49:16 +00:00
|
|
|
this.emit( 'chunk', localAccum );
|
|
|
|
};
|
|
|
|
|
2012-04-25 14:35:59 +00:00
|
|
|
|
2012-01-09 17:49:16 +00:00
|
|
|
/**
|
|
|
|
* Callback for the end event emitted from the tokenizer.
|
|
|
|
* Either signals the end of input to the tail of an ongoing asynchronous
|
|
|
|
* processing pipeline, or directly emits 'end' if the processing was fully
|
|
|
|
* synchronous.
|
|
|
|
*/
|
|
|
|
SyncTokenTransformManager.prototype.onEndEvent = function () {
|
|
|
|
// This phase is fully synchronous, so just pass the end along and prepare
|
|
|
|
// for the next round.
|
|
|
|
this.emit('end');
|
2011-12-08 14:37:31 +00:00
|
|
|
};
|
|
|
|
|
2012-01-03 18:44:31 +00:00
|
|
|
|
2012-01-10 01:09:50 +00:00
|
|
|
/********************** AttributeTransformManager *************************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Utility transformation manager for attributes, using an attribute
|
|
|
|
* transformation pipeline (normally phase1 SyncTokenTransformManager and
|
|
|
|
* phase2 AsyncTokenTransformManager). This pipeline needs to be independent
|
|
|
|
* of the containing TokenTransformManager to isolate transforms from each
|
2012-01-11 00:05:51 +00:00
|
|
|
* other. The AttributeTransformManager returns its result by calling the
|
|
|
|
* supplied callback.
|
2012-01-10 01:09:50 +00:00
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @constructor
|
2012-04-26 16:18:08 +00:00
|
|
|
* @param {Object} Containing AsyncTokenTransformManager
|
|
|
|
* @param {Function} Callback function, called with expanded attribute array.
|
2012-01-10 01:09:50 +00:00
|
|
|
*/
|
|
|
|
function AttributeTransformManager ( manager, callback ) {
|
2012-01-11 19:48:49 +00:00
|
|
|
this.manager = manager;
|
2012-04-25 14:35:59 +00:00
|
|
|
this.frame = this.manager.frame;
|
2012-01-10 01:09:50 +00:00
|
|
|
this.callback = callback;
|
2012-01-20 22:02:23 +00:00
|
|
|
this.outstanding = 1;
|
2012-01-11 00:05:51 +00:00
|
|
|
this.kvs = [];
|
2012-01-19 23:43:39 +00:00
|
|
|
//this.pipe = manager.getAttributePipeline( manager.args );
|
2012-01-10 01:09:50 +00:00
|
|
|
}
|
|
|
|
|
2012-04-26 16:18:08 +00:00
|
|
|
/**
|
|
|
|
* Expand both key and values of all key/value pairs. Used for generic
|
|
|
|
* (non-template) tokens in the AttributeExpander handler, which runs after
|
|
|
|
* templates are already expanded.
|
|
|
|
*/
|
2012-01-11 19:48:49 +00:00
|
|
|
AttributeTransformManager.prototype.process = function ( attributes ) {
|
2012-01-11 00:05:51 +00:00
|
|
|
// Potentially need to use multiple pipelines to support concurrent async expansion
|
|
|
|
//this.pipe.process(
|
|
|
|
var pipe,
|
|
|
|
ref;
|
2012-02-14 20:56:14 +00:00
|
|
|
//console.warn( 'AttributeTransformManager.process: ' + JSON.stringify( attributes ) );
|
2012-01-11 00:05:51 +00:00
|
|
|
|
|
|
|
// transform each argument (key and value), and handle asynchronous returns
|
|
|
|
for ( var i = 0, l = attributes.length; i < l; i++ ) {
|
2012-02-08 15:10:30 +00:00
|
|
|
var cur = attributes[i];
|
|
|
|
|
2012-04-25 14:35:59 +00:00
|
|
|
|
|
|
|
// fast path for string-only attributes
|
|
|
|
if ( cur.k.constructor === String && cur.v.constructor === String ) {
|
|
|
|
this.kvs.push( cur );
|
2012-02-09 16:36:29 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-04-25 14:35:59 +00:00
|
|
|
|
|
|
|
var kv = new KV( [], [] );
|
|
|
|
this.kvs.push( kv );
|
|
|
|
|
2012-04-12 13:42:09 +00:00
|
|
|
if ( cur.k.constructor === Array && cur.k.length ) {
|
2012-02-08 15:10:30 +00:00
|
|
|
// Assume that the return is async, will be decremented in callback
|
|
|
|
this.outstanding++;
|
|
|
|
|
|
|
|
// transform the key
|
2012-04-25 14:35:59 +00:00
|
|
|
pipe = this.manager.pipeFactory.getPipeline( this.manager.attributeType,
|
|
|
|
this.manager.isInclude );
|
|
|
|
pipe.setFrame( this.manager.frame, null );
|
2012-03-05 15:34:27 +00:00
|
|
|
pipe.on( 'chunk',
|
2012-04-25 14:35:59 +00:00
|
|
|
this._returnAttributeKey.bind( this, i, true )
|
2012-02-20 12:56:00 +00:00
|
|
|
);
|
2012-03-05 15:34:27 +00:00
|
|
|
pipe.on( 'end',
|
2012-04-25 14:35:59 +00:00
|
|
|
this._returnAttributeKey.bind( this, i, false, [] )
|
2012-02-20 12:56:00 +00:00
|
|
|
);
|
2012-04-18 12:30:59 +00:00
|
|
|
pipe.process( this.manager.env.cloneTokens( cur.k ).concat([ new EOFTk() ]) );
|
2012-02-08 15:10:30 +00:00
|
|
|
} else {
|
2012-04-25 14:35:59 +00:00
|
|
|
kv.k = cur.k;
|
2012-02-08 15:10:30 +00:00
|
|
|
}
|
2012-01-11 00:05:51 +00:00
|
|
|
|
2012-04-12 13:42:09 +00:00
|
|
|
if ( cur.v.constructor === Array && cur.v.length ) {
|
2012-02-08 15:10:30 +00:00
|
|
|
// Assume that the return is async, will be decremented in callback
|
|
|
|
this.outstanding++;
|
|
|
|
|
|
|
|
// transform the value
|
2012-04-25 14:35:59 +00:00
|
|
|
pipe = this.manager.pipeFactory.getPipeline( this.manager.attributeType,
|
|
|
|
this.manager.isInclude );
|
|
|
|
pipe.setFrame( this.manager.frame, null );
|
|
|
|
//pipe = this.manager.getAttributePipeline( this.manager.inputType,
|
|
|
|
// this.manager.args );
|
2012-03-05 15:34:27 +00:00
|
|
|
pipe.on( 'chunk',
|
2012-04-25 14:35:59 +00:00
|
|
|
this._returnAttributeValue.bind( this, i, true )
|
2012-02-08 15:10:30 +00:00
|
|
|
);
|
2012-03-05 15:34:27 +00:00
|
|
|
pipe.on( 'end',
|
2012-04-25 14:35:59 +00:00
|
|
|
this._returnAttributeValue.bind( this, i, false, [] )
|
2012-02-08 15:10:30 +00:00
|
|
|
);
|
2012-02-14 20:56:14 +00:00
|
|
|
//console.warn('starting attribute transform of ' + JSON.stringify( attributes[i].v ) );
|
2012-04-18 12:30:59 +00:00
|
|
|
pipe.process( this.manager.env.cloneTokens( cur.v ).concat([ new EOFTk() ]) );
|
2012-02-08 15:10:30 +00:00
|
|
|
} else {
|
2012-04-25 14:35:59 +00:00
|
|
|
kv.v = cur.v;
|
|
|
|
if ( !cur.v.to ) {
|
|
|
|
if ( cur.v.constructor === String ) {
|
|
|
|
if ( ! cur.v.rank ) {
|
|
|
|
cur.v = new String( cur.v );
|
|
|
|
}
|
|
|
|
Object.defineProperty( cur.v, 'to',
|
|
|
|
{
|
|
|
|
value: function() { return cur.v },
|
|
|
|
enumerable: false
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
Object.defineProperty( cur.v, 'to',
|
|
|
|
{
|
|
|
|
value: this.manager.frame.convert,
|
|
|
|
enumerable: false
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2012-02-08 15:10:30 +00:00
|
|
|
}
|
2012-01-20 22:02:23 +00:00
|
|
|
}
|
|
|
|
this.outstanding--;
|
2012-02-01 16:30:43 +00:00
|
|
|
if ( this.outstanding === 0 ) {
|
2012-04-25 14:35:59 +00:00
|
|
|
// synchronous, done
|
|
|
|
this.callback( this.kvs );
|
2012-01-11 00:05:51 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-04-18 15:53:04 +00:00
|
|
|
/**
|
2012-04-26 16:18:08 +00:00
|
|
|
* Expand only keys of key/value pairs. This is generally used for template
|
|
|
|
* parameters to avoid expanding unused values, which is very important for
|
|
|
|
* constructs like switches.
|
2012-04-18 15:53:04 +00:00
|
|
|
*/
|
|
|
|
AttributeTransformManager.prototype.processKeys = function ( attributes ) {
|
|
|
|
// Potentially need to use multiple pipelines to support concurrent async expansion
|
|
|
|
//this.pipe.process(
|
|
|
|
var pipe,
|
|
|
|
ref;
|
|
|
|
//console.warn( 'AttributeTransformManager.process: ' + JSON.stringify( attributes ) );
|
|
|
|
|
|
|
|
// transform each argument (key and value), and handle asynchronous returns
|
|
|
|
for ( var i = 0, l = attributes.length; i < l; i++ ) {
|
|
|
|
var cur = attributes[i];
|
2012-04-25 14:35:59 +00:00
|
|
|
var kv = new KV([], cur.v);
|
|
|
|
if ( !cur.v.to ) {
|
|
|
|
if ( cur.v.constructor === String ) {
|
|
|
|
cur.v = new String( cur.v );
|
|
|
|
Object.defineProperty( cur.v, 'to',
|
|
|
|
{
|
|
|
|
value: function() { return cur.v },
|
|
|
|
enumerable: false
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
Object.defineProperty( cur.v, 'to',
|
|
|
|
{
|
|
|
|
value: this.manager.frame.convert,
|
|
|
|
enumerable: false
|
|
|
|
});
|
|
|
|
}
|
2012-04-18 15:53:04 +00:00
|
|
|
}
|
2012-04-25 14:35:59 +00:00
|
|
|
this.kvs.push( kv );
|
2012-04-18 15:53:04 +00:00
|
|
|
|
2012-04-25 14:35:59 +00:00
|
|
|
if ( cur.k.constructor === Array && cur.k.length && ! cur.k.to ) {
|
2012-04-18 15:53:04 +00:00
|
|
|
// Assume that the return is async, will be decremented in callback
|
|
|
|
this.outstanding++;
|
|
|
|
|
|
|
|
// transform the key
|
2012-04-25 14:35:59 +00:00
|
|
|
pipe = this.manager.pipeFactory.getPipeline( this.manager.attributeType,
|
|
|
|
this.manager.isInclude );
|
|
|
|
pipe.setFrame( this.manager.frame, null );
|
|
|
|
//pipe = this.manager.getAttributePipeline( this.manager.inputType,
|
|
|
|
// this.manager.args );
|
2012-04-18 15:53:04 +00:00
|
|
|
pipe.on( 'chunk',
|
2012-04-25 14:35:59 +00:00
|
|
|
this._returnAttributeKey.bind( this, i, true )
|
2012-04-18 15:53:04 +00:00
|
|
|
);
|
|
|
|
pipe.on( 'end',
|
2012-04-25 14:35:59 +00:00
|
|
|
this._returnAttributeKey.bind( this, i, false, [] )
|
2012-04-18 15:53:04 +00:00
|
|
|
);
|
|
|
|
pipe.process( this.manager.env.cloneTokens( cur.k ).concat([ new EOFTk() ]) );
|
|
|
|
} else {
|
2012-04-25 14:35:59 +00:00
|
|
|
kv.k = cur.k;
|
2012-04-18 15:53:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
this.outstanding--;
|
|
|
|
if ( this.outstanding === 0 ) {
|
2012-04-25 14:35:59 +00:00
|
|
|
// synchronously done
|
|
|
|
this.callback( this.kvs );
|
2012-04-18 15:53:04 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-04-26 16:18:08 +00:00
|
|
|
/**
|
|
|
|
* Only expand values of attribute key/value pairs.
|
|
|
|
*/
|
2012-04-18 15:53:04 +00:00
|
|
|
AttributeTransformManager.prototype.processValues = function ( attributes ) {
|
|
|
|
// Potentially need to use multiple pipelines to support concurrent async expansion
|
|
|
|
//this.pipe.process(
|
|
|
|
var pipe,
|
|
|
|
ref;
|
|
|
|
//console.warn( 'AttributeTransformManager.process: ' + JSON.stringify( attributes ) );
|
|
|
|
|
|
|
|
// transform each argument (key and value), and handle asynchronous returns
|
|
|
|
for ( var i = 0, l = attributes.length; i < l; i++ ) {
|
|
|
|
var cur = attributes[i];
|
2012-04-25 14:35:59 +00:00
|
|
|
var kv = new KV( cur.k, [] );
|
|
|
|
this.kvs.push( kv );
|
2012-04-18 15:53:04 +00:00
|
|
|
|
|
|
|
if ( ! cur ) {
|
|
|
|
console.warn( JSON.stringify( attributes ) );
|
|
|
|
console.trace();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( cur.v.constructor === Array && cur.v.length ) {
|
|
|
|
// Assume that the return is async, will be decremented in callback
|
|
|
|
this.outstanding++;
|
|
|
|
|
|
|
|
// transform the value
|
2012-04-25 14:35:59 +00:00
|
|
|
pipe = this.manager.pipeFactory.getPipeline( this.manager.attributeType,
|
|
|
|
this.manager.isInclude );
|
|
|
|
pipe.setFrame( this.manager.frame, null );
|
|
|
|
//pipe = this.manager.getAttributePipeline( this.manager.inputType,
|
|
|
|
// this.manager.args );
|
2012-04-18 15:53:04 +00:00
|
|
|
pipe.on( 'chunk',
|
2012-04-25 14:35:59 +00:00
|
|
|
this._returnAttributeValue.bind( this, i, true )
|
2012-04-18 15:53:04 +00:00
|
|
|
);
|
|
|
|
pipe.on( 'end',
|
2012-04-25 14:35:59 +00:00
|
|
|
this._returnAttributeValue.bind( this, i, false, [] )
|
2012-04-18 15:53:04 +00:00
|
|
|
);
|
|
|
|
//console.warn('starting attribute transform of ' + JSON.stringify( attributes[i].v ) );
|
|
|
|
pipe.process( this.manager.env.cloneTokens( cur.v ).concat([ new EOFTk() ]) );
|
|
|
|
} else {
|
|
|
|
kv.value = cur.v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.outstanding--;
|
|
|
|
if ( this.outstanding === 0 ) {
|
2012-04-25 14:35:59 +00:00
|
|
|
// synchronously done
|
|
|
|
this.callback( this.kvs );
|
2012-04-18 15:53:04 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-01-09 17:49:16 +00:00
|
|
|
|
2012-01-11 00:05:51 +00:00
|
|
|
/**
|
|
|
|
* Callback for async argument value expansions
|
|
|
|
*/
|
2012-04-25 14:35:59 +00:00
|
|
|
AttributeTransformManager.prototype._returnAttributeValue = function ( ref, notYetDone, tokens ) {
|
|
|
|
this.manager.env.dp( 'check _returnAttributeValue: ', ref, tokens,
|
|
|
|
' notYetDone:', notYetDone );
|
|
|
|
this.kvs[ref].v = this.kvs[ref].v.concat( tokens );
|
2012-01-11 00:05:51 +00:00
|
|
|
if ( ! notYetDone ) {
|
2012-04-25 14:35:59 +00:00
|
|
|
var res = this.kvs[ref].v;
|
|
|
|
this.manager.env.stripEOFTkfromTokens( res );
|
2012-01-11 19:48:49 +00:00
|
|
|
this.outstanding--;
|
2012-04-26 16:18:08 +00:00
|
|
|
// Add the 'to' conversion method to the chunk for easy conversion in
|
|
|
|
// later processing (parser functions and template argument
|
|
|
|
// processing).
|
2012-04-25 14:35:59 +00:00
|
|
|
if ( !res.to ) {
|
|
|
|
Object.defineProperty( res, 'to',
|
|
|
|
{
|
|
|
|
value: function() { return res },
|
|
|
|
enumerable: false
|
|
|
|
});
|
|
|
|
}
|
2012-01-11 19:48:49 +00:00
|
|
|
if ( this.outstanding === 0 ) {
|
2012-04-25 14:35:59 +00:00
|
|
|
this.callback( this.kvs );
|
2012-01-11 00:05:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback for async argument key expansions
|
|
|
|
*/
|
2012-04-25 14:35:59 +00:00
|
|
|
AttributeTransformManager.prototype._returnAttributeKey = function ( ref, notYetDone, tokens ) {
|
2012-02-14 20:56:14 +00:00
|
|
|
//console.warn( 'check _returnAttributeKey: ' + JSON.stringify( tokens ) +
|
2012-01-20 22:02:23 +00:00
|
|
|
// ' notYetDone:' + notYetDone );
|
2012-04-25 14:35:59 +00:00
|
|
|
this.kvs[ref].k = this.kvs[ref].k.concat( tokens );
|
2012-01-11 00:05:51 +00:00
|
|
|
if ( ! notYetDone ) {
|
2012-04-25 14:35:59 +00:00
|
|
|
this.manager.env.stripEOFTkfromTokens( this.kvs[ref].k );
|
2012-01-11 19:48:49 +00:00
|
|
|
this.outstanding--;
|
|
|
|
if ( this.outstanding === 0 ) {
|
2012-04-25 14:35:59 +00:00
|
|
|
this.callback( this.kvs );
|
2012-01-11 00:05:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2012-01-09 17:49:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
/******************************* TokenAccumulator *************************/
|
2011-12-13 20:13:09 +00:00
|
|
|
/**
|
2012-01-03 18:44:31 +00:00
|
|
|
* Token accumulators buffer tokens between asynchronous processing points,
|
|
|
|
* and return fully processed token chunks in-order and as soon as possible.
|
2012-01-09 17:49:16 +00:00
|
|
|
* They support the AsyncTokenTransformManager.
|
2011-12-13 20:13:09 +00:00
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @constructor
|
|
|
|
* @param {Object} next TokenAccumulator to link to
|
|
|
|
* @param {Array} (optional) tokens, init accumulator with tokens or []
|
|
|
|
*/
|
2012-01-21 20:38:13 +00:00
|
|
|
function TokenAccumulator ( manager, parentCB ) {
|
|
|
|
this.manager = manager;
|
2012-01-03 18:44:31 +00:00
|
|
|
this.parentCB = parentCB;
|
|
|
|
this.accum = [];
|
|
|
|
// Wait for child and sibling by default
|
|
|
|
// Note: Need to decrement outstanding on last accum
|
|
|
|
// in a chain.
|
|
|
|
this.outstanding = 2;
|
2011-12-08 14:37:31 +00:00
|
|
|
}
|
|
|
|
|
2011-12-13 20:13:09 +00:00
|
|
|
/**
|
2012-01-03 18:44:31 +00:00
|
|
|
* Curry a parentCB with the object and reference.
|
2011-12-13 20:13:09 +00:00
|
|
|
*
|
2012-01-04 12:28:41 +00:00
|
|
|
* @method
|
2012-01-03 18:44:31 +00:00
|
|
|
* @param {Object} TokenAccumulator
|
|
|
|
* @param {misc} Reference / key for callback
|
|
|
|
* @returns {Function}
|
2011-12-13 20:13:09 +00:00
|
|
|
*/
|
2012-01-03 18:44:31 +00:00
|
|
|
TokenAccumulator.prototype.getParentCB = function ( reference ) {
|
2012-01-09 17:49:16 +00:00
|
|
|
return this._returnTokens.bind( this, reference );
|
2011-12-08 14:37:31 +00:00
|
|
|
};
|
|
|
|
|
2011-12-13 20:13:09 +00:00
|
|
|
/**
|
2012-01-03 18:44:31 +00:00
|
|
|
* Pass tokens to an accumulator
|
2011-12-13 20:13:09 +00:00
|
|
|
*
|
|
|
|
* @method
|
2012-04-26 16:18:08 +00:00
|
|
|
* @param {String}: reference, 'child' or 'sibling'.
|
|
|
|
* @param {Object}: { tokens, async, allTokensProcessed }
|
|
|
|
* @returns {Mixed}: new parent callback for caller or falsy value
|
2012-01-03 18:44:31 +00:00
|
|
|
*/
|
2012-03-07 15:39:18 +00:00
|
|
|
TokenAccumulator.prototype._returnTokens =
|
2012-04-25 14:35:59 +00:00
|
|
|
function ( reference, ret ) {
|
2012-03-07 16:29:06 +00:00
|
|
|
var cb,
|
2012-01-03 18:44:31 +00:00
|
|
|
returnTokens = [];
|
|
|
|
|
2012-03-07 16:29:06 +00:00
|
|
|
|
2012-04-25 14:35:59 +00:00
|
|
|
if ( ! ret.async ) {
|
2012-01-03 18:44:31 +00:00
|
|
|
this.outstanding--;
|
|
|
|
}
|
|
|
|
|
2012-04-25 14:35:59 +00:00
|
|
|
this.manager.env.dp( 'TokenAccumulator._returnTokens', ret );
|
|
|
|
|
|
|
|
// FIXME
|
|
|
|
if ( ret.tokens === undefined ) {
|
|
|
|
if ( this.manager.env.debug ) {
|
|
|
|
console.trace();
|
|
|
|
}
|
|
|
|
if ( ret.token ) {
|
|
|
|
ret.tokens = [ret.token];
|
|
|
|
} else {
|
|
|
|
ret.tokens = [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-03 18:44:31 +00:00
|
|
|
if ( reference === 'child' ) {
|
2012-03-07 16:29:06 +00:00
|
|
|
var res = {};
|
2012-04-25 14:35:59 +00:00
|
|
|
if( !ret.allTokensProcessed ) {
|
2012-03-07 16:29:06 +00:00
|
|
|
// There might be transformations missing on the returned tokens,
|
|
|
|
// re-transform to make sure those are applied too.
|
2012-04-25 14:35:59 +00:00
|
|
|
res = this.manager.transformTokens( ret.tokens, this.parentCB );
|
|
|
|
ret.tokens = res.tokens;
|
2012-03-07 16:29:06 +00:00
|
|
|
}
|
|
|
|
|
2012-04-25 14:35:59 +00:00
|
|
|
if ( !ret.async ) {
|
2012-03-07 16:29:06 +00:00
|
|
|
// empty accum too
|
2012-04-25 14:35:59 +00:00
|
|
|
ret.tokens = ret.tokens.concat( this.accum );
|
2012-03-07 16:29:06 +00:00
|
|
|
this.accum = [];
|
|
|
|
}
|
2012-04-12 13:42:09 +00:00
|
|
|
//this.manager.env.dp( 'TokenAccumulator._returnTokens child: ',
|
|
|
|
// tokens, ' outstanding: ', this.outstanding );
|
2012-04-25 14:35:59 +00:00
|
|
|
ret.allTokensProcessed = true;
|
|
|
|
ret.async = this.outstanding;
|
|
|
|
|
|
|
|
this.parentCB( ret );
|
2012-03-07 16:29:06 +00:00
|
|
|
|
|
|
|
if ( res.async ) {
|
|
|
|
this.parentCB = res.async.getParentCB( 'sibling' );
|
|
|
|
}
|
2012-01-03 18:44:31 +00:00
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
// sibling
|
|
|
|
if ( this.outstanding === 0 ) {
|
2012-04-25 14:35:59 +00:00
|
|
|
ret.tokens = this.accum.concat( ret.tokens );
|
2012-01-03 18:44:31 +00:00
|
|
|
// A sibling will transform tokens, so we don't have to do this
|
|
|
|
// again.
|
2012-04-12 13:42:09 +00:00
|
|
|
//this.manager.env.dp( 'TokenAccumulator._returnTokens: ',
|
|
|
|
// 'sibling done and parentCB ',
|
|
|
|
// tokens );
|
2012-04-25 14:35:59 +00:00
|
|
|
ret.allTokensProcessed = true;
|
|
|
|
ret.async = false;
|
|
|
|
this.parentCB( ret );
|
2012-01-03 18:44:31 +00:00
|
|
|
return null;
|
2012-04-25 14:35:59 +00:00
|
|
|
} else if ( this.outstanding === 1 && ret.async ) {
|
2012-04-12 13:42:09 +00:00
|
|
|
//this.manager.env.dp( 'TokenAccumulator._returnTokens: ',
|
2012-04-25 14:35:59 +00:00
|
|
|
// 'sibling done and parentCB but async ',
|
2012-04-12 13:42:09 +00:00
|
|
|
// tokens );
|
2012-01-03 18:44:31 +00:00
|
|
|
// Sibling is not yet done, but child is. Return own parentCB to
|
|
|
|
// allow the sibling to go direct, and call back parent with
|
|
|
|
// tokens. The internal accumulator is empty at this stage, as its
|
|
|
|
// tokens are passed to the parent when the child is done.
|
2012-04-25 14:35:59 +00:00
|
|
|
ret.allTokensProcessed = true;
|
|
|
|
return this.parentCB( ret );
|
2012-01-19 23:43:39 +00:00
|
|
|
} else {
|
2012-04-25 14:35:59 +00:00
|
|
|
this.accum = this.accum.concat( ret.tokens );
|
|
|
|
//this.manager.env.dp( 'TokenAccumulator._returnTokens: sibling done, but not overall. async=',
|
|
|
|
// res.async, ', this.outstanding=', this.outstanding,
|
2012-04-12 13:42:09 +00:00
|
|
|
// ', this.accum=', this.accum, ' manager.title=', this.manager.title );
|
2012-01-03 18:44:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mark the sibling as done (normally at the tail of a chain).
|
2011-12-13 20:13:09 +00:00
|
|
|
*/
|
2012-01-03 18:44:31 +00:00
|
|
|
TokenAccumulator.prototype.siblingDone = function () {
|
2012-02-14 20:56:14 +00:00
|
|
|
//console.warn( 'TokenAccumulator.siblingDone: ' );
|
2012-04-25 14:35:59 +00:00
|
|
|
this._returnTokens ( 'sibling', { tokens: [], async: false, allTokensProcessed: true} );
|
2011-12-12 10:01:47 +00:00
|
|
|
};
|
|
|
|
|
2012-01-03 18:44:31 +00:00
|
|
|
|
2011-12-13 20:13:09 +00:00
|
|
|
/**
|
2012-01-03 18:44:31 +00:00
|
|
|
* Push a token into the accumulator
|
2011-12-13 20:13:09 +00:00
|
|
|
*
|
|
|
|
* @method
|
2012-01-03 18:44:31 +00:00
|
|
|
* @param {Object} token
|
2011-12-13 20:13:09 +00:00
|
|
|
*/
|
2012-01-03 18:44:31 +00:00
|
|
|
TokenAccumulator.prototype.push = function ( token ) {
|
|
|
|
return this.accum.push(token);
|
2011-12-08 14:37:31 +00:00
|
|
|
};
|
2011-12-12 14:03:54 +00:00
|
|
|
|
2012-01-22 23:21:53 +00:00
|
|
|
/**
|
|
|
|
* Append tokens to an accumulator
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {Object} token
|
|
|
|
*/
|
|
|
|
TokenAccumulator.prototype.append = function ( token ) {
|
|
|
|
this.accum = this.accum.concat( token );
|
|
|
|
};
|
|
|
|
|
2012-01-03 18:44:31 +00:00
|
|
|
|
2012-04-26 16:18:08 +00:00
|
|
|
/******************************* Frame ******************************/
|
|
|
|
|
2012-04-25 14:35:59 +00:00
|
|
|
/**
|
2012-04-26 16:18:08 +00:00
|
|
|
* The Frame object
|
|
|
|
*
|
|
|
|
* A frame represents a template expansion scope including parameters passed
|
|
|
|
* to the template (args). It provides a generic 'convert' method which
|
|
|
|
* expands / converts individual parameter values in its scope. It also
|
|
|
|
* provides methods to check if another expansion would lead to loops or
|
|
|
|
* exceed the maximum expansion depth.
|
2012-04-25 14:35:59 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
function Frame ( title, manager, args, parentFrame ) {
|
|
|
|
this.title = title;
|
|
|
|
this.manager = manager;
|
|
|
|
this.args = new Params( this.manager.env, args );
|
|
|
|
if ( parentFrame ) {
|
2012-04-26 16:18:08 +00:00
|
|
|
this.parentFrame = parentFrame;
|
|
|
|
this.depth = parentFrame.depth + 1;
|
2012-04-25 14:35:59 +00:00
|
|
|
} else {
|
2012-04-26 16:18:08 +00:00
|
|
|
this.parentFrame = null;
|
|
|
|
this.depth = 0;
|
2012-04-25 14:35:59 +00:00
|
|
|
}
|
|
|
|
var self = this;
|
|
|
|
this.convert = function ( format, cb ) {
|
2012-04-26 16:18:08 +00:00
|
|
|
self._convertThunk( this, format, cb );
|
2012-04-25 14:35:59 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2012-04-26 16:18:08 +00:00
|
|
|
/**
|
|
|
|
* Create a new child frame
|
|
|
|
*/
|
2012-04-25 14:35:59 +00:00
|
|
|
Frame.prototype.newChild = function ( title, manager, args ) {
|
|
|
|
return new Frame( title, manager, args, this );
|
|
|
|
};
|
|
|
|
|
2012-04-26 16:18:08 +00:00
|
|
|
/**
|
|
|
|
* Expand / convert a thunk (a chunk of tokens not yet fully expanded).
|
|
|
|
*/
|
|
|
|
Frame.prototype._convertThunk = function ( chunk, format, cb ) {
|
2012-04-25 14:35:59 +00:00
|
|
|
this.manager.env.dp( 'convertChunk', chunk );
|
|
|
|
|
2012-04-26 16:18:08 +00:00
|
|
|
// Set up a conversion cache on the chunk, if it does not yet exist
|
2012-04-25 14:35:59 +00:00
|
|
|
if ( chunk.toCache === undefined ) {
|
|
|
|
Object.defineProperty( chunk, 'toCache', { value: {}, enumerable: false } );
|
|
|
|
} else {
|
|
|
|
if ( chunk.toCache[format] !== undefined ) {
|
|
|
|
cb( chunk.toCache[format] );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-26 16:18:08 +00:00
|
|
|
// XXX: Should perhaps create a generic from..to conversion map in
|
|
|
|
// mediawiki.parser.js, at least for forward conversions.
|
2012-04-25 18:19:43 +00:00
|
|
|
if ( format === 'tokens/x-mediawiki/expanded' ) {
|
2012-04-25 14:35:59 +00:00
|
|
|
var pipeline = this.manager.pipeFactory.getPipeline(
|
2012-04-25 18:19:43 +00:00
|
|
|
this.manager.attributeType || 'tokens/x-mediawiki', true
|
2012-04-25 14:35:59 +00:00
|
|
|
);
|
|
|
|
pipeline.setFrame( this, null );
|
2012-04-26 16:18:08 +00:00
|
|
|
// In the interest of interface simplicity, we accumulate all emitted
|
|
|
|
// chunks in a single accumulator.
|
2012-04-25 14:35:59 +00:00
|
|
|
var accum = [];
|
2012-04-26 16:18:08 +00:00
|
|
|
// Callback used to cache the result of the conversion
|
2012-04-25 14:35:59 +00:00
|
|
|
var cacheIt = function ( res ) { chunk.toCache[format] = res; };
|
2012-04-26 16:18:08 +00:00
|
|
|
pipeline.addListener( 'chunk',
|
|
|
|
this.onThunkEvent.bind( this, cacheIt, accum, true, cb ) );
|
|
|
|
pipeline.addListener( 'end',
|
|
|
|
this.onThunkEvent.bind( this, cacheIt, accum, false, cb ) );
|
2012-04-25 14:35:59 +00:00
|
|
|
pipeline.process( chunk.concat( [new EOFTk()] ), this.title );
|
|
|
|
} else {
|
2012-04-26 16:18:08 +00:00
|
|
|
throw "Frame._convertThunk: Unsupported format " + format;
|
2012-04-25 14:35:59 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-04-26 16:18:08 +00:00
|
|
|
/**
|
|
|
|
* Event handler for chunk conversion pipelines
|
|
|
|
*/
|
2012-04-25 14:35:59 +00:00
|
|
|
Frame.prototype.onThunkEvent = function ( cacheIt, accum, notYetDone, cb, ret ) {
|
|
|
|
if ( notYetDone ) {
|
|
|
|
//this.manager.env.dp( 'Frame.onThunkEvent accum:', accum );
|
|
|
|
accum.push.apply( accum, ret );
|
|
|
|
} else {
|
|
|
|
this.manager.env.stripEOFTkfromTokens( accum );
|
|
|
|
this.manager.env.dp( 'Frame.onThunkEvent:', accum );
|
|
|
|
cacheIt( accum );
|
|
|
|
cb ( accum );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-01-10 01:09:50 +00:00
|
|
|
|
|
|
|
/**
|
2012-04-26 16:18:08 +00:00
|
|
|
* Check if expanding <title> would lead to a loop, or would exceed the
|
|
|
|
* maximum expansion depth.
|
2012-01-10 01:09:50 +00:00
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {String} Title to check.
|
|
|
|
*/
|
2012-04-26 16:18:08 +00:00
|
|
|
Frame.prototype.loopAndDepthCheck = function ( title, maxDepth ) {
|
2012-01-19 23:43:39 +00:00
|
|
|
// XXX: set limit really low for testing!
|
2012-04-25 14:35:59 +00:00
|
|
|
//console.warn( 'Loopcheck: ' + title + JSON.stringify( this, null, 2 ) );
|
2012-01-22 19:32:28 +00:00
|
|
|
if ( this.depth > maxDepth ) {
|
2012-01-19 23:43:39 +00:00
|
|
|
// too deep
|
2012-02-14 20:56:14 +00:00
|
|
|
//console.warn( 'Loopcheck: ' + JSON.stringify( this, null, 2 ) );
|
2012-01-22 07:07:16 +00:00
|
|
|
return 'Error: Expansion depth limit exceeded at ';
|
2012-01-19 23:43:39 +00:00
|
|
|
}
|
2012-01-10 01:09:50 +00:00
|
|
|
var elem = this;
|
|
|
|
do {
|
2012-02-14 20:56:14 +00:00
|
|
|
//console.warn( 'loop check: ' + title + ' vs ' + elem.title );
|
2012-01-10 01:09:50 +00:00
|
|
|
if ( elem.title === title ) {
|
|
|
|
// Loop detected
|
2012-01-22 07:07:16 +00:00
|
|
|
return 'Error: Expansion loop detected at ';
|
2012-01-10 01:09:50 +00:00
|
|
|
}
|
2012-04-26 16:18:08 +00:00
|
|
|
elem = elem.parentFrame;
|
2012-01-10 01:09:50 +00:00
|
|
|
} while ( elem );
|
|
|
|
// No loop detected.
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2011-12-12 14:03:54 +00:00
|
|
|
if (typeof module == "object") {
|
2012-01-09 17:49:16 +00:00
|
|
|
module.exports.AsyncTokenTransformManager = AsyncTokenTransformManager;
|
|
|
|
module.exports.SyncTokenTransformManager = SyncTokenTransformManager;
|
2012-01-11 19:48:49 +00:00
|
|
|
module.exports.AttributeTransformManager = AttributeTransformManager;
|
2011-12-12 14:03:54 +00:00
|
|
|
}
|