Index: trunk/parsers/wikidom/tests/hype/es.DocumentModel.test.js |
— | — | @@ -433,7 +433,7 @@ |
434 | 434 | ); |
435 | 435 | } ); |
436 | 436 | |
437 | | -test( 'es.DocumentModel.prepareInsertion', 6, function() { |
| 437 | +test( 'es.DocumentModel.prepareInsertion', 10, function() { |
438 | 438 | var documentModel = es.DocumentModel.newFromPlainObject( obj ); |
439 | 439 | |
440 | 440 | // Test 1 |
— | — | @@ -532,7 +532,63 @@ |
533 | 533 | 'prepareInsertion splits up paragraph when inserting a paragraph closing and opening into a paragraph' |
534 | 534 | ); |
535 | 535 | |
536 | | - // TODO add test cases for expected exceptions for bad offset and malformed input |
| 536 | + // Test 7 |
| 537 | + deepEqual( |
| 538 | + documentModel.prepareInsertion( |
| 539 | + 0, |
| 540 | + [ { 'type': 'paragraph' }, 'f', 'o', 'o', { 'type': '/paragraph' } ] |
| 541 | + ), |
| 542 | + [ |
| 543 | + { |
| 544 | + 'type': 'insert', |
| 545 | + 'data': [ { 'type': 'paragraph' }, 'f', 'o', 'o', { 'type': '/paragraph' } ] |
| 546 | + }, |
| 547 | + { 'type': 'retain', 'length': 28 } |
| 548 | + ], |
| 549 | + 'prepareInsertion inserts at the beginning, then retains up to the end' |
| 550 | + ); |
| 551 | + |
| 552 | + // Test 8 |
| 553 | + deepEqual( |
| 554 | + documentModel.prepareInsertion( |
| 555 | + 28, |
| 556 | + [ { 'type': 'paragraph' }, 'f', 'o', 'o', { 'type': '/paragraph' } ] |
| 557 | + ), |
| 558 | + [ |
| 559 | + { 'type': 'retain', 'length': 28 }, |
| 560 | + { |
| 561 | + 'type': 'insert', |
| 562 | + 'data': [ { 'type': 'paragraph' }, 'f', 'o', 'o', { 'type': '/paragraph' } ] |
| 563 | + } |
| 564 | + ], |
| 565 | + 'prepareInsertion inserts at the end' |
| 566 | + ); |
| 567 | + |
| 568 | + // Test 9 |
| 569 | + raises( |
| 570 | + function() { |
| 571 | + documentModel.prepareInsertion( |
| 572 | + -1, |
| 573 | + [ { 'type': 'paragraph' }, 'f', 'o', 'o', { 'type': '/paragraph' } ] |
| 574 | + ); |
| 575 | + }, |
| 576 | + function ( e ) { |
| 577 | + return /^Offset -1 out of bounds/.test( e ); }, |
| 578 | + 'prepareInsertion throws exception for negative offset' |
| 579 | + ); |
| 580 | + |
| 581 | + // Test 10 |
| 582 | + raises( |
| 583 | + function() { |
| 584 | + documentModel.prepareInsertion( |
| 585 | + 29, |
| 586 | + [ { 'type': 'paragraph' }, 'f', 'o', 'o', { 'type': '/paragraph' } ] |
| 587 | + ); |
| 588 | + }, |
| 589 | + function ( e ) { |
| 590 | + return /^Offset 29 out of bounds/.test( e ); }, |
| 591 | + 'prepareInsertion throws exception for offset past the end' |
| 592 | + ); |
537 | 593 | } ); |
538 | 594 | |
539 | 595 | test( 'es.DocumentModel.commit, es.DocumentModel.rollback', 10, function() { |
Index: trunk/parsers/wikidom/lib/hype/models/es.DocumentModel.js |
— | — | @@ -722,13 +722,16 @@ |
723 | 723 | |
724 | 724 | var tx = new es.Transaction(), |
725 | 725 | insertedData = data, // may be cloned and modified |
726 | | - isStructuralLoc = es.DocumentModel.isStructuralOffset( this.data, offset ), |
| 726 | + isStructuralLoc, |
727 | 727 | wrappingElementType; |
728 | 728 | |
729 | 729 | if ( offset < 0 || offset > this.data.length ) { |
730 | 730 | throw 'Offset ' + offset + ' out of bounds [0..' + this.data.length + ']'; |
731 | 731 | } |
732 | 732 | |
| 733 | + // Has to be after the bounds check, because isStructuralOffset doesn't like out-of-bounds offsets |
| 734 | + isStructuralLoc = es.DocumentModel.isStructuralOffset( this.data, offset ); |
| 735 | + |
733 | 736 | if ( offset > 0 ) { |
734 | 737 | tx.pushRetain( offset ); |
735 | 738 | } |