r93943 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r93942‎ | r93943 | r93944 >
Date:00:34, 5 August 2011
Author:inez
Status:deferred
Tags:
Comment:
Refactoring for getWikiDomLines
Modified paths:
  • /trunk/parsers/wikidom/lib/es/es.Content.js (modified) (history)

Diff [purge]

Index: trunk/parsers/wikidom/lib/es/es.Content.js
@@ -638,20 +638,11 @@
639639 *
640640 * @method
641641 */
642 -es.Content.prototype.handleAnnotation = function( bias, annotation, stack, index ) {
643 - if ( bias === 'open' ) {
644 - var annotation = es.Content.copyObject( annotation );
645 - annotation.range = { start: index };
646 - stack.push( annotation );
647 - } else if ( bias === 'close' ) {
648 - for ( var i = stack.length - 1; i >= 0; i-- ) {
649 - if ( stack[i].type === annotation.type ) {
650 - if ( es.Content.compareObjects( stack[i].data, annotation.data ) ) {
651 - stack[i].range.end = index;
652 - break;
653 - }
654 - }
655 - }
 642+es.Content.prototype.closeAnnotations = function( stack, index ) {
 643+ for ( var i = 0; i < stack.length; i++) {
 644+ if ( !stack[i].range.end ) {
 645+ stack[i].range.end = index;
 646+ }
656647 }
657648 };
658649
@@ -666,68 +657,63 @@
667658 right,
668659 leftPlain,
669660 rightPlain,
670 - i, j, // iterators
 661+ i, j, k, // iterators
671662 lines = [],
672663 line = null,
673664 offset = 0;
674665
675666 for ( i = 0; i < this.data.length; i++ ) {
 667+
676668 if ( line == null ) {
677669 line = { text : '', annotations : [] };
678670 }
 671+
679672 right = this.data[i];
680673 leftPlain = typeof left === 'string';
681674 rightPlain = typeof right === 'string';
 675+
682676 if ( rightPlain && right == "\n" ) {
683 - if ( left ) {
684 - for ( j = 1; j < left.length; j++ ) {
685 - this.handleAnnotation( 'close', left[j], line.annotations, i - offset );
686 - }
687 - }
688 - if ( !line.annotations.length ) {
689 - delete line.annotations;
690 - }
 677+ this.closeAnnotations( line.annotations, i - offset );
691678 lines.push(line);
692679 line = null;
693680 offset = i + 1;
694681 left = '';
695682 continue;
696683 }
 684+
697685 if ( !leftPlain && rightPlain ) {
698686 // [formatted][plain] pair, close any annotations for left
699 - for ( j = 1; j < left.length; j++ ) {
700 - this.handleAnnotation( 'close', left[j], line.annotations, i - offset );
701 - }
702 - } else if ( leftPlain && !rightPlain ) {
703 - // [plain][formatted] pair, open any annotations for right
704 - for ( j = 1; j < right.length; j++ ) {
705 - this.handleAnnotation( 'open', right[j], line.annotations, i - offset );
706 - }
707 - } else if ( !leftPlain && !rightPlain ) {
708 - // [formatted][formatted] pair, open/close any differences
709 - for ( j = 1; j < left.length; j++ ) {
710 - if ( this.indexOfAnnotation( i , left[j], true ) === -1 ) {
711 - this.handleAnnotation( 'close', left[j], line.annotations, i - offset );
 687+ this.closeAnnotations( line.annotations, i - offset );
 688+ } else if ( !rightPlain ) {
 689+ // [plain|formatted][formatted]
 690+ if ( !leftPlain ) {
 691+ for ( j = 1; j < left.length; j++ ) {
 692+ if ( this.indexOfAnnotation( i , left[j], true ) === -1 ) {
 693+ for ( k = line.annotations.length - 1; k >= 0; k-- ) {
 694+ if ( line.annotations[k].type === left[j].type ) {
 695+ if ( es.Content.compareObjects( line.annotations[k].data, left[j].data ) ) {
 696+ line.annotations[k].range.end = i - offset;
 697+ break;
 698+ }
 699+ }
 700+ }
 701+ }
712702 }
713703 }
714704 for ( j = 1; j < right.length; j++ ) {
715 - if ( this.indexOfAnnotation( i - 1, right[j], true ) === -1 ) {
716 - this.handleAnnotation( 'open', right[j], line.annotations, i - offset );
 705+ if ( leftPlain || this.indexOfAnnotation( i - 1, right[j], true ) === -1 ) {
 706+ var annotation = es.Content.copyObject( right[j] );
 707+ annotation.range = { start: i - offset };
 708+ line.annotations.push( annotation );
717709 }
718710 }
719711 }
720712 line.text += rightPlain ? right : right[0];
721713 left = right;
722714 }
 715+
723716 if ( line != null ) {
724 - if ( right ) {
725 - for ( j = 1; j < right.length; j++ ) {
726 - this.handleAnnotation( 'close', right[j], line.annotations, i - offset );
727 - }
728 - }
729 - if ( !line.annotations.length ) {
730 - delete line.annotations;
731 - }
 717+ this.closeAnnotations( line.annotations, i - offset );
732718 lines.push( line );
733719 }
734720 return lines;

Status & tagging log