Index: trunk/parsers/wikidom/lib/hype/models/es.DocumentModel.js |
— | — | @@ -556,12 +556,46 @@ |
557 | 557 | * @returns {es.Transaction} |
558 | 558 | */ |
559 | 559 | es.DocumentModel.prototype.prepareInsertion = function( offset, data ) { |
560 | | - var tx = new es.Transaction(); |
| 560 | + function containsStructuralElements( data ) { |
| 561 | + var i; |
| 562 | + for ( i = 0; i < data.length; i++ ) { |
| 563 | + if ( data[i].type !== undefined ) { |
| 564 | + return true; |
| 565 | + } |
| 566 | + } |
| 567 | + return false; |
| 568 | + } |
| 569 | + |
| 570 | + function isStructuralLocation( offset, data ) { |
| 571 | + // The following are structural locations: |
| 572 | + // * The beginning of the document (offset 0) |
| 573 | + // * The end of the document (offset length-1) |
| 574 | + // * Any location between elements, i.e. the item before is a closing and the item after is an opening |
| 575 | + return offset <= 0 || offset >= data.length - 1 || ( |
| 576 | + data[offset - 1].type !== undefined && data[offset - 1].type.charAt( 0 ) != '/' && |
| 577 | + data[offset].type !== undefined && data[offset].type.charAt( 0 ) == '/' |
| 578 | + ); |
| 579 | + } |
| 580 | + |
| 581 | + var tx = new es.Transaction(), insertedData = data; |
561 | 582 | if ( offset > 0 ) { |
562 | 583 | tx.pushRetain( offset ); |
563 | 584 | } |
564 | 585 | // TODO check for structural changes |
565 | | - tx.pushInsert( data ); |
| 586 | + if ( containsStructuralElements( insertedData ) ) { |
| 587 | + // TODO |
| 588 | + } else { |
| 589 | + if ( isStructuralLocation( offset, insertedData ) ) { |
| 590 | + // We're inserting content into a structural location, |
| 591 | + // so we need to wrap the inserted content in a paragraph. |
| 592 | + insertedData = [ { 'type': 'paragraph' }, { 'type': '/paragraph' } ]; |
| 593 | + es.spliceArray( insertedData, 1, data ); |
| 594 | + } else { |
| 595 | + // Content being inserted in content is fine, do nothing |
| 596 | + } |
| 597 | + } |
| 598 | + |
| 599 | + tx.pushInsert( insertedData ); |
566 | 600 | if ( offset < this.data.length ) { |
567 | 601 | tx.pushRetain( this.data.length - offset ); |
568 | 602 | } |