Index: trunk/parsers/wikidom/lib/es/es.Content.js |
— | — | @@ -638,20 +638,11 @@ |
639 | 639 | * |
640 | 640 | * @method |
641 | 641 | */ |
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 | + } |
656 | 647 | } |
657 | 648 | }; |
658 | 649 | |
— | — | @@ -666,68 +657,63 @@ |
667 | 658 | right, |
668 | 659 | leftPlain, |
669 | 660 | rightPlain, |
670 | | - i, j, // iterators |
| 661 | + i, j, k, // iterators |
671 | 662 | lines = [], |
672 | 663 | line = null, |
673 | 664 | offset = 0; |
674 | 665 | |
675 | 666 | for ( i = 0; i < this.data.length; i++ ) { |
| 667 | + |
676 | 668 | if ( line == null ) { |
677 | 669 | line = { text : '', annotations : [] }; |
678 | 670 | } |
| 671 | + |
679 | 672 | right = this.data[i]; |
680 | 673 | leftPlain = typeof left === 'string'; |
681 | 674 | rightPlain = typeof right === 'string'; |
| 675 | + |
682 | 676 | 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 ); |
691 | 678 | lines.push(line); |
692 | 679 | line = null; |
693 | 680 | offset = i + 1; |
694 | 681 | left = ''; |
695 | 682 | continue; |
696 | 683 | } |
| 684 | + |
697 | 685 | if ( !leftPlain && rightPlain ) { |
698 | 686 | // [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 | + } |
712 | 702 | } |
713 | 703 | } |
714 | 704 | 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 ); |
717 | 709 | } |
718 | 710 | } |
719 | 711 | } |
720 | 712 | line.text += rightPlain ? right : right[0]; |
721 | 713 | left = right; |
722 | 714 | } |
| 715 | + |
723 | 716 | 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 ); |
732 | 718 | lines.push( line ); |
733 | 719 | } |
734 | 720 | return lines; |