From 8e55e79b6702873aecc2918fa0be570d4128877c Mon Sep 17 00:00:00 2001 From: Gabriel Wicke Date: Tue, 13 Dec 2011 11:45:12 +0000 Subject: [PATCH] Rename TokenTransformer to TokenTransformDispatcher. --- modules/parser/ext.core.QuoteTransformer.js | 35 ++++++++++--------- ... => mediawiki.TokenTransformDispatcher.js} | 29 +++++++-------- tests/parser/parserTests.js | 14 ++++---- 3 files changed, 40 insertions(+), 38 deletions(-) rename modules/parser/{mediawiki.TokenTransformer.js => mediawiki.TokenTransformDispatcher.js} (87%) diff --git a/modules/parser/ext.core.QuoteTransformer.js b/modules/parser/ext.core.QuoteTransformer.js index 5c1faf93df..4970c8a309 100644 --- a/modules/parser/ext.core.QuoteTransformer.js +++ b/modules/parser/ext.core.QuoteTransformer.js @@ -9,19 +9,20 @@ */ function QuoteTransformer ( ) { + // Bold and italic tokens are collected in these lists, and then processed + // in onNewLine. this.italics = []; this.bolds = []; - this.inserted = 0; } // Register this transformer with the TokenTransformer -QuoteTransformer.prototype.register = function ( tokenTransformer ) { +QuoteTransformer.prototype.register = function ( dispatcher ) { // Register for NEWLINE and QUOTE tag tokens var self = this; - tokenTransformer.appendListener( function (ctx) { + dispatcher.appendListener( function (ctx) { return self.onNewLine(ctx); }, 'newline' ); - tokenTransformer.appendListener( function (ctx) { + dispatcher.appendListener( function (ctx) { return self.onQuote(ctx); }, 'tag', 'QUOTE' ); }; @@ -49,11 +50,11 @@ QuoteTransformer.prototype.onQuote = function ( tokenCTX ) { // Start a new accumulator, so we can later go back using the // reference to this accumulator and append our tags at the end of // it. - accum = tokenCTX.transformer.newAccumulator(accum); + accum = tokenCTX.dispatcher.newAccumulator(accum); this.italics.push(ctx); break; case 3: - accum = tokenCTX.transformer.newAccumulator(accum); + accum = tokenCTX.dispatcher.newAccumulator(accum); this.bolds.push(ctx); break; case 4: @@ -62,7 +63,7 @@ QuoteTransformer.prototype.onQuote = function ( tokenCTX ) { } else { out = {type: 'TEXT', value: "'"}; } - accum = tokenCTX.transformer.newAccumulator(accum); + accum = tokenCTX.dispatcher.newAccumulator(accum); this.bolds.push(ctx); break; case 5: @@ -71,7 +72,7 @@ QuoteTransformer.prototype.onQuote = function ( tokenCTX ) { // by the HTML 5 tree builder. This does not always result in the // prettiest result, but at least it is always correct and very // convenient. - accum = tokenCTX.transformer.newAccumulator(accum, 2); + accum = tokenCTX.dispatcher.newAccumulator(accum, 2); this.italics.push(ctx); ctx2 = this.ctx(tokenCTX); ctx2.token = {attribs: ctx.token.attribs}; @@ -84,7 +85,7 @@ QuoteTransformer.prototype.onQuote = function ( tokenCTX ) { } else { out = {type: 'TEXT', value: newvalue}; } - accum = tokenCTX.transformer.newAccumulator(accum, 2); + accum = tokenCTX.dispatcher.newAccumulator(accum, 2); this.italics.push(ctx); ctx2 = this.ctx(tokenCTX); ctx2.token = {attribs: ctx.token.attribs}; @@ -152,8 +153,8 @@ QuoteTransformer.prototype.onNewLine = function ( tokenCTX ) { } } - this.quotesToTags(this.italics, 'i', tokenCTX.transformer); - this.quotesToTags(this.bolds, 'b', tokenCTX.transformer); + this.quotesToTags(this.italics, 'i', tokenCTX.dispatcher); + this.quotesToTags(this.bolds, 'b', tokenCTX.dispatcher); this.bolds = []; this.italics = []; @@ -183,7 +184,7 @@ QuoteTransformer.prototype.convertBold = function ( i ) { }; // Convert italics/bolds into tags -QuoteTransformer.prototype.quotesToTags = function ( contexts, name, transformer ) { +QuoteTransformer.prototype.quotesToTags = function ( contexts, name, dispatcher ) { var toggle = true, t, out = []; @@ -194,7 +195,7 @@ QuoteTransformer.prototype.quotesToTags = function ( contexts, name, transformer // Slip in a text token from bold to italic rebalancing. Don't // count this callback towards completion. var realToken = t.pop(); - transformer.transformTokens( t, contexts[j].accum, 0 ); + dispatcher.transformTokens( t, contexts[j].accum, 0 ); t = realToken; } @@ -208,18 +209,18 @@ QuoteTransformer.prototype.quotesToTags = function ( contexts, name, transformer toggle = !toggle; // Re-add and process the new token with the original accumulator, but // don't yet count this callback towards callback completion. - transformer.transformTokens( [t], contexts[j].accum, 0 ); + dispatcher.transformTokens( [t], contexts[j].accum, 0 ); } var l = contexts.length; if (!toggle) { // Add end tag, but don't count it towards completion. - transformer.transformTokens( [{type: 'ENDTAG', name: name}], + dispatcher.transformTokens( [{type: 'ENDTAG', name: name}], contexts[contexts.length - 1].accum, 0 ); } // Now finally count the number of contexts towards completion, which - // causes the transformer to call its own callback if no more asynch + // causes the dispatcher to call its own callback if no more asynch // callbacks are outstanding. - transformer.finish( contexts.length ); + dispatcher.finish( contexts.length ); }; if (typeof module == "object") { diff --git a/modules/parser/mediawiki.TokenTransformer.js b/modules/parser/mediawiki.TokenTransformDispatcher.js similarity index 87% rename from modules/parser/mediawiki.TokenTransformer.js rename to modules/parser/mediawiki.TokenTransformDispatcher.js index 4d618d310b..37266510c3 100644 --- a/modules/parser/mediawiki.TokenTransformer.js +++ b/modules/parser/mediawiki.TokenTransformDispatcher.js @@ -17,7 +17,7 @@ * insert tokens in front of other ongoing expansion tasks. * */ -function TokenTransformer( callback ) { +function TokenTransformDispatcher( callback ) { this.cb = callback; // Called with transformed token list when done this.transformers = { tag: {}, // for TAG, ENDTAG, SELFCLOSINGTAG, keyed on name @@ -26,19 +26,20 @@ function TokenTransformer( callback ) { comment: [], end: [], // eof martian: [] // none of the above + // XXX: Add an any registration that always matches? }; this.reset(); return this; } -TokenTransformer.prototype.reset = function () { +TokenTransformDispatcher.prototype.reset = function () { this.accum = new TokenAccumulator(null); this.firstaccum = this.accum; this.outstanding = 1; // Number of outstanding processing steps // (e.g., async template fetches/expansions) }; -TokenTransformer.prototype.appendListener = function ( listener, type, name ) { +TokenTransformDispatcher.prototype.appendListener = function ( listener, type, name ) { if ( type === 'tag' ) { if ( $.isArray(this.transformers.tag.name) ) { this.transformers.tag[name].push(listener); @@ -50,7 +51,7 @@ TokenTransformer.prototype.appendListener = function ( listener, type, name ) { } }; -TokenTransformer.prototype.prependListener = function ( listener, type, name ) { +TokenTransformDispatcher.prototype.prependListener = function ( listener, type, name ) { if ( type === 'tag' ) { if ( $.isArray(this.transformers.tag.name) ) { this.transformers.tag[name].unshift(listener); @@ -62,7 +63,7 @@ TokenTransformer.prototype.prependListener = function ( listener, type, name ) { } }; -TokenTransformer.prototype.removeListener = function ( listener, type, name ) { +TokenTransformDispatcher.prototype.removeListener = function ( listener, type, name ) { var i = -1; var ts; if ( type === 'tag' ) { @@ -83,14 +84,14 @@ TokenTransformer.prototype.removeListener = function ( listener, type, name ) { * * @param token The token to precess * @param accum {TokenAccumulator} The active TokenAccumulator. - * @param processor {TokenTransformer} The TokenTransformer object. + * @param processor {TokenTransformDispatcher} The TokenTransformDispatcher object. * @param lastToken Last returned token or {undefined}. * @returns {TokenContext}. */ -function TokenContext ( token, accum, transformer, lastToken ) { +function TokenContext ( token, accum, dispatcher, lastToken ) { this.token = token; this.accum = accum; - this.transformer = transformer; + this.dispatcher = dispatcher; this.lastToken = lastToken; return this; } @@ -100,7 +101,7 @@ function TokenContext ( token, accum, transformer, lastToken ) { * @param {TokenContext} The current token and its context. * @returns {TokenContext} Context with updated token and/or accum. */ -TokenTransformer.prototype._transformTagToken = function ( tokenCTX ) { +TokenTransformDispatcher.prototype._transformTagToken = function ( tokenCTX ) { var ts = this.transformers.tag[tokenCTX.token.name]; if ( ts ) { for (var i = 0, l = ts.length; i < l; i++ ) { @@ -121,7 +122,7 @@ TokenTransformer.prototype._transformTagToken = function ( tokenCTX ) { * @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 ) { +TokenTransformDispatcher.prototype._transformToken = function ( tokenCTX, ts ) { if ( ts ) { for (var i = 0, l = ts.length; i < l; i++ ) { tokenCTX = ts[i]( tokenCTX ); @@ -147,7 +148,7 @@ TokenTransformer.prototype._transformToken = function ( tokenCTX, ts ) { * @returns nothing: Calls back registered callback if there are no more * outstanding asynchronous expansions. * */ -TokenTransformer.prototype.transformTokens = function ( tokens, accum, delta ) { +TokenTransformDispatcher.prototype.transformTokens = function ( tokens, accum, delta ) { if ( accum === undefined ) { this.reset(); accum = this.accum; @@ -207,7 +208,7 @@ TokenTransformer.prototype.transformTokens = function ( tokens, accum, delta ) { this.finish( delta ); }; -TokenTransformer.prototype.finish = function ( delta ) { +TokenTransformDispatcher.prototype.finish = function ( delta ) { this.outstanding -= delta; if ( this.outstanding === 0 ) { // Join the token accumulators back into a single token list @@ -224,7 +225,7 @@ TokenTransformer.prototype.finish = function ( delta ) { }; /* Start a new accumulator for asynchronous work. */ -TokenTransformer.prototype.newAccumulator = function ( accum, count ) { +TokenTransformDispatcher.prototype.newAccumulator = function ( accum, count ) { if ( count !== undefined ) { this.outstanding += count; } else { @@ -262,5 +263,5 @@ TokenAccumulator.prototype.insertAccumulator = function ( ) { }; if (typeof module == "object") { - module.exports.TokenTransformer = TokenTransformer; + module.exports.TokenTransformDispatcher = TokenTransformDispatcher; } diff --git a/tests/parser/parserTests.js b/tests/parser/parserTests.js index 880961728d..4e1d96d39b 100644 --- a/tests/parser/parserTests.js +++ b/tests/parser/parserTests.js @@ -57,7 +57,7 @@ var testWhiteList = require('./parserTests-whitelist.js').testWhiteList; _import(pj('parser', 'mediawiki.tokenizer.peg.js'), ['PegTokenizer']); _import(pj('parser', 'mediawiki.parser.environment.js'), ['MWParserEnvironment']); -_import(pj('parser', 'mediawiki.TokenTransformer.js'), ['TokenTransformer']); +_import(pj('parser', 'mediawiki.TokenTransformDispatcher.js'), ['TokenTransformDispatcher']); _import(pj('parser', 'ext.cite.taghook.ref.js'), ['MWRefTagHook']); _import(pj('parser', 'mediawiki.HTML5TreeBuilder.node.js'), ['FauxHTML5']); @@ -181,9 +181,9 @@ function ParserTests () { var pt = this; - // Set up the TokenTransformer with a callback for the remaining + // Set up the TokenTransformDispatcher with a callback for the remaining // processing. - this.tokenTransformer = new TokenTransformer ( function ( tokens ) { + this.tokenDispatcher = new TokenTransformDispatcher ( function ( tokens ) { //console.log("TOKENS: " + JSON.stringify(tokens, null, 2)); @@ -205,7 +205,7 @@ function ParserTests () { // Add token transformations.. var qt = new QuoteTransformer(); - qt.register(this.tokenTransformer); + qt.register(this.tokenDispatcher); // Test statistics this.passedTests = 0; @@ -434,10 +434,10 @@ ParserTests.prototype.processTest = function (item) { //Slightly better token output debugging: //console.log( util.inspect( tokens, false, null ).yellow); - // Transform tokens using the TokenTransformer. When done, the - // TokenTransformer calls buildTree() and checkResult() with the + // Transform tokens using the TokenTransformDispatcher. When done, the + // TokenTransformDispatcher calls buildTree() and checkResult() with the // transformed tokens. - this.tokenTransformer.transformTokens( res.tokens ); + this.tokenDispatcher.transformTokens( res.tokens ); } };