Index: trunk/parsers/wikidom/lib/hype/models/es.DocumentModel.js |
— | — | @@ -794,16 +794,16 @@ |
795 | 795 | workingData = data.slice( 0 ); |
796 | 796 | } |
797 | 797 | workingData.splice( i, 1 ); |
| 798 | + } else { |
| 799 | + element = stack.pop(); |
| 800 | + if ( element != data[i].type.substr( 1 ) ) { |
| 801 | + // Closing doesn't match what's expected |
| 802 | + // This means the input is malformed and cannot possibly |
| 803 | + // have been a fragment taken from well-formed data |
| 804 | + throw 'Input is malformed: expected /' + element + ' but got ' + data[i].type + |
| 805 | + ' at index ' + i; |
| 806 | + } |
798 | 807 | } |
799 | | - |
800 | | - element = stack.pop(); |
801 | | - if ( element != data[i].type.substr( 1 ) ) { |
802 | | - // Closing doesn't match what's expected |
803 | | - // This means the input is malformed and cannot possibly |
804 | | - // have been a fragment taken from well-formed data |
805 | | - throw 'Input is malformed: expected /' + element + ' but got ' + data[i].type + |
806 | | - ' at index ' + i; |
807 | | - } |
808 | 808 | } |
809 | 809 | } |
810 | 810 | |
— | — | @@ -839,14 +839,23 @@ |
840 | 840 | } |
841 | 841 | |
842 | 842 | if ( es.DocumentModel.containsElementData( insertedData ) ) { |
843 | | - if ( isStructuralLoc ) { |
| 843 | + if ( insertedData[0].type !== undefined && insertedData[0].type.charAt( 0 ) != '/' ) { |
| 844 | + // insertedData starts with an opening, so this is really intended to insert structure |
| 845 | + // Balance it to make it sane, if it's not already |
| 846 | + // TODO we need an actual validator and check that the insertion is really valid |
844 | 847 | insertedData = balance( insertedData ); |
| 848 | + if ( !isStructuralLoc ) { |
| 849 | + // We're inserting structure at a content location, |
| 850 | + // so we need to split up the wrapping element |
| 851 | + wrappingElementType = this.getNodeFromOffset( offset ).getElementType(); |
| 852 | + var arr = [ { 'type': '/' + wrappingElementType }, { 'type': wrappingElementType } ]; |
| 853 | + es.insertIntoArray( arr, 1, insertedData ); |
| 854 | + insertedData = arr; |
| 855 | + } |
| 856 | + // else we're inserting structure at a structural location, which is fine |
845 | 857 | } else { |
846 | | - // We're inserting structure at a content location, |
847 | | - // so we need to split up the wrapping element |
848 | | - wrappingElementType = this.getNodeFromOffset( offset ).getElementType(); |
849 | | - insertedData = [ { 'type': '/' + wrappingElementType }, { 'type': wrappingElementType } ]; |
850 | | - es.insertIntoArray( insertedData, 1, data ); |
| 858 | + // insertedData starts with content but contains structure |
| 859 | + // TODO balance and validate, will be different for this case |
851 | 860 | } |
852 | 861 | } else { |
853 | 862 | if ( isStructuralLoc ) { |