r95916 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r95915‎ | r95916 | r95917 >
Date:23:04, 31 August 2011
Author:inez
Status:deferred
Tags:
Comment:
Refactored version of getWikiDom for Lists and solid tests for it
Modified paths:
  • /trunk/parsers/wikidom/lib/es/es.ListBlock.js (modified) (history)
  • /trunk/parsers/wikidom/tests/serializers/test.js (modified) (history)

Diff [purge]

Index: trunk/parsers/wikidom/tests/serializers/test.js
@@ -284,6 +284,38 @@
285285 { 'line': { 'text': '2' } }
286286 ]
287287 } ] }
 288+ },
 289+ {
 290+ 'subject': 'mixed-style nested lists (3)',
 291+ 'dom': { 'blocks': [ {
 292+ 'type': 'list',
 293+ 'style': 'number',
 294+ 'items': [
 295+ {
 296+ 'lists' : [
 297+ {
 298+ 'style': 'number',
 299+ 'items': [
 300+ {
 301+ 'lists' : [
 302+ {
 303+ 'style': 'number',
 304+ 'items': [
 305+ {
 306+ 'line' : {
 307+ 'text' : 'test'
 308+ }
 309+ }
 310+ ]
 311+ }
 312+ ]
 313+ }
 314+ ]
 315+ }
 316+ ]
 317+ }
 318+ ]
 319+ } ] }
288320 }
289321 ], true );
290322 } );
Index: trunk/parsers/wikidom/lib/es/es.ListBlock.js
@@ -42,12 +42,22 @@
4343 return new es.ListBlock( es.ListBlockList.newFromWikiDomList( wikidomListBlock ) );
4444 };
4545
46 -es.ListBlock.prototype.renderContent = function( offset ) {
47 - this.list.renderContent( offset );
 46+es.ListBlock.wikiDomPushPop = function( stack ) {
 47+ if ( stack[stack.length - 2].items.length === 0 ) {
 48+ stack[stack.length - 2].items.push( { 'lists' : [] } );
 49+ } else if( !stack[stack.length - 2].items[stack[stack.length - 2].items.length - 1].lists ) {
 50+ stack[stack.length - 2].items[stack[stack.length - 2].items.length - 1].lists = [];
 51+ }
 52+ stack[stack.length - 2].items[stack[stack.length - 2].items.length - 1].lists.push( stack.pop() );
 53+ return stack;
4854 };
4955
5056 /* Public Methods */
5157
 58+es.ListBlock.prototype.renderContent = function( offset ) {
 59+ this.list.renderContent( offset );
 60+};
 61+
5262 es.ListBlock.prototype.enumerate = function() {
5363 var itemLevel,
5464 levels = [];
@@ -363,66 +373,44 @@
364374 for ( var i = 0; i < items.length; i++ ) {
365375 var item = items[i];
366376 var itemLevel = item.getLevel();
367 -
368 - if ( itemLevel + 1 === stack.length ) {
369 - if ( lastStyle !== null && lastStyle != item.getStyle() ) {
370 - if ( stack[stack.length - 2].items.length === 0 ) {
371 - stack[stack.length - 2].items.push( {
372 - 'lists' : []
373 - } );
374 - } else if( !stack[stack.length - 2].items[stack[stack.length - 2].items.length - 1].lists ) {
375 - stack[stack.length - 2].items[stack[stack.length - 2].items.length - 1].lists = [];
376 - }
377 - stack[stack.length - 2].items[stack[stack.length - 2].items.length - 1].lists.push( stack.pop() );
378 - stack.push( {
379 - 'style' : item.getStyle(),
380 - 'items' : []
381 - } );
382 - //lastStyle = item.getStyle();
383 - }
 377+
 378+ // if the current element level is the same as the previous one, but its style is different
 379+ if ( itemLevel + 1 === stack.length && lastStyle != item.getStyle() ) {
 380+ stack = es.ListBlock.wikiDomPushPop( stack );
 381+ stack.push( {
 382+ 'style' : item.getStyle(),
 383+ 'items' : []
 384+ } );
384385 }
385386
 387+ // if the current element level is higher than the previous one
386388 if ( itemLevel + 1 > stack.length ) {
387 - for( var j = stack.length; j < itemLevel + 1; j++ ) {
 389+ for ( var j = stack.length; j < itemLevel + 1; j++ ) {
388390 stack.push( {
389391 'style' : item.getStyle(j),
390392 'items' : []
391393 } );
392394 }
393395 }
394 -
 396+
 397+ // if the current element level is lower then the previous one
395398 if ( itemLevel + 1 < stack.length ) {
396 - for( var j = stack.length; j > itemLevel + 1; j-- ) {
397 - if ( stack[stack.length - 2].items.length === 0 ) {
398 - stack[stack.length - 2].items.push( {
399 - 'lists' : []
400 - } );
401 - } else if( !stack[stack.length - 2].items[stack[stack.length - 2].items.length - 1].lists ) {
402 - stack[stack.length - 2].items[stack[stack.length - 2].items.length - 1].lists = [];
403 - }
404 - stack[stack.length - 2].items[stack[stack.length - 2].items.length - 1].lists.push( stack.pop() );
 399+ for ( var j = stack.length; j > itemLevel + 1; j-- ) {
 400+ stack = es.ListBlock.wikiDomPushPop( stack );
405401 }
406402 }
407403
 404+ // if the current element level is the same as the previous one
408405 if ( itemLevel + 1 === stack.length ) {
409 - stack[stack.length - 1].items.push( {
410 - 'line' : item.content.getWikiDomLines()[0] }
411 - );
 406+ stack[stack.length - 1].items.push( { 'line' : item.content.getWikiDomLines()[0] } );
412407 lastStyle = item.getStyle();
413408 }
414409 }
415 -
416 - for( var i = stack.length; i > 1; i-- ) {
417 - if ( stack[stack.length - 2].items.length === 0 ) {
418 - stack[stack.length - 2].items.push( {
419 - 'lists' : []
420 - } );
421 - } else if( !stack[stack.length - 2].items[stack[stack.length - 2].items.length - 1].lists ) {
422 - stack[stack.length - 2].items[stack[stack.length - 2].items.length - 1].lists = [];
423 - }
424 - stack[stack.length - 2].items[stack[stack.length - 2].items.length - 1].lists.push( stack.pop() );
 410+
 411+ for ( var i = stack.length; i > 1; i-- ) {
 412+ stack = es.ListBlock.wikiDomPushPop( stack );
425413 }
426 -
 414+
427415 stack[0].type = 'list';
428416 return stack[0];
429417 };

Status & tagging log