Index: trunk/parsers/wikidom/lib/es/es.Content.js |
— | — | @@ -587,10 +587,16 @@ |
588 | 588 | } |
589 | 589 | } |
590 | 590 | } |
| 591 | + |
591 | 592 | out += right[0] in es.Content.htmlCharacters |
592 | 593 | ? es.Content.htmlCharacters[right[0]] : right[0]; |
593 | 594 | left = right; |
594 | 595 | } |
| 596 | + if ( !rightPlain ) { |
| 597 | + for ( j = 1; j < right.length; j++ ) { |
| 598 | + out += es.Content.renderAnnotation( 'close', right[j], stack ); |
| 599 | + } |
| 600 | + } |
595 | 601 | return out; |
596 | 602 | }; |
597 | 603 | |
— | — | @@ -626,6 +632,23 @@ |
627 | 633 | return new es.Range( start, end ); |
628 | 634 | }; |
629 | 635 | |
| 636 | +es.Content.prototype.handleAnnotation = function( bias, annotation, stack, index ) { |
| 637 | + if ( bias === 'open' ) { |
| 638 | + var annotation = es.Content.copyObject( annotation ); |
| 639 | + annotation.range = { start: index }; |
| 640 | + stack.push( annotation ); |
| 641 | + } else if ( bias === 'close' ) { |
| 642 | + for ( var i = 0; i < stack.length; i++ ) { |
| 643 | + if ( stack[i].type === annotation.type ) { |
| 644 | + if ( es.Content.compareObjects( stack[i].data, annotation.data ) ) { |
| 645 | + stack[i].range.end = index; |
| 646 | + break; |
| 647 | + } |
| 648 | + } |
| 649 | + } |
| 650 | + } |
| 651 | +}; |
| 652 | + |
630 | 653 | /** |
631 | 654 | * Get WikiDom line objects from content. |
632 | 655 | * |
— | — | @@ -633,20 +656,18 @@ |
634 | 657 | * @returns {Array} List of WikiDom line objects |
635 | 658 | */ |
636 | 659 | es.Content.prototype.getWikiDomLines = function() { |
637 | | - var lines = [], |
638 | | - right = '', |
| 660 | + var left = '', |
| 661 | + right, |
| 662 | + leftPlain, |
639 | 663 | rightPlain, |
640 | | - left = '', |
641 | | - leftPlain, |
642 | | - line, |
643 | | - offset = 0, |
644 | | - i, j, k; |
| 664 | + i, j, // iterators |
| 665 | + lines = [], |
| 666 | + line = null, |
| 667 | + offset = 0; |
| 668 | + |
645 | 669 | for ( i = 0; i < this.data.length; i++ ) { |
646 | 670 | if ( line == null ) { |
647 | | - line = { |
648 | | - text : '', |
649 | | - annotations : [] |
650 | | - }; |
| 671 | + line = { text : '', annotations : [] }; |
651 | 672 | } |
652 | 673 | right = this.data[i]; |
653 | 674 | leftPlain = typeof left === 'string'; |
— | — | @@ -658,37 +679,41 @@ |
659 | 680 | left = ''; |
660 | 681 | continue; |
661 | 682 | } |
662 | | - if ( !leftPlain ) { |
| 683 | + if ( !leftPlain && rightPlain ) { |
| 684 | + // [formatted][plain] pair, close any annotations for left |
663 | 685 | for ( j = 1; j < left.length; j++ ) { |
664 | | - for ( k = line.annotations.length - 1; k >= 0; k-- ) { |
665 | | - if ( left[j].type === line.annotations[k].type ) { |
666 | | - if ( es.Content.compareObjects( left[j].data, line.annotations[k].data ) ) { |
667 | | - line.annotations[k].range.end = i - offset; |
668 | | - break; |
669 | | - } |
670 | | - } |
| 686 | + this.handleAnnotation( 'close', left[j], line.annotations, i - offset ); |
| 687 | + } |
| 688 | + } else if ( leftPlain && !rightPlain ) { |
| 689 | + // [plain][formatted] pair, open any annotations for right |
| 690 | + for ( j = 1; j < right.length; j++ ) { |
| 691 | + this.handleAnnotation( 'open', right[j], line.annotations, i - offset ); |
| 692 | + } |
| 693 | + } else if ( !leftPlain && !rightPlain ) { |
| 694 | + // [formatted][formatted] pair, open/close any differences |
| 695 | + for ( j = 1; j < left.length; j++ ) { |
| 696 | + if ( right.indexOf( left[j] ) === -1 ) { |
| 697 | + this.handleAnnotation( 'close', left[j], line.annotations, i - offset ); |
671 | 698 | } |
672 | 699 | } |
673 | | - } |
674 | | - if ( !rightPlain ) { |
675 | 700 | for ( j = 1; j < right.length; j++ ) { |
676 | | - if ( leftPlain || this.indexOfAnnotation( i - 1, right[j], true ) === -1 ) { |
677 | | - var annotation = es.Content.copyObject( right[j] ); |
678 | | - annotation.range = { |
679 | | - start : i - offset, |
680 | | - end : i + 1 - offset |
681 | | - }; |
682 | | - line.annotations.push( annotation ); |
| 701 | + if ( left.indexOf( right[j] ) === -1 ) { |
| 702 | + this.handleAnnotation( 'open', right[j], line.annotations, i - offset ); |
683 | 703 | } |
684 | 704 | } |
685 | 705 | } |
686 | 706 | line.text += rightPlain ? right : right[0]; |
687 | | - left = right; |
| 707 | + left = right; |
688 | 708 | } |
689 | 709 | if ( line != null ) { |
| 710 | + if ( right ) { |
| 711 | + for ( j = 1; j < right.length; j++ ) { |
| 712 | + this.handleAnnotation( 'close', right[j], line.annotations, i - offset ); |
| 713 | + } |
| 714 | + } |
690 | 715 | lines.push(line); |
691 | 716 | } |
692 | | - return lines; |
| 717 | + return lines; |
693 | 718 | }; |
694 | 719 | |
695 | 720 | /* Inheritance */ |