r100733 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r100732‎ | r100733 | r100734 >
Date:19:26, 25 October 2011
Author:catrope
Status:deferred
Tags:
Comment:
Beef up prepareInsertion() somewhat. It's still mostly a skeleton, but the test case for wrapping content in a paragraph passes now
Modified paths:
  • /trunk/parsers/wikidom/lib/hype/models/es.DocumentModel.js (modified) (history)

Diff [purge]

Index: trunk/parsers/wikidom/lib/hype/models/es.DocumentModel.js
@@ -556,12 +556,46 @@
557557 * @returns {es.Transaction}
558558 */
559559 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;
561582 if ( offset > 0 ) {
562583 tx.pushRetain( offset );
563584 }
564585 // 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 );
566600 if ( offset < this.data.length ) {
567601 tx.pushRetain( this.data.length - offset );
568602 }

Status & tagging log