Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Rewritten UpcastHelpers#elementToElement using getSplitParts().
Browse files Browse the repository at this point in the history
  • Loading branch information
scofalik committed Mar 25, 2019
1 parent a84e84a commit c0d3b40
Showing 1 changed file with 13 additions and 15 deletions.
28 changes: 13 additions & 15 deletions src/conversion/upcasthelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -560,32 +560,30 @@ function prepareToElementConverter( config ) {
conversionApi.writer.insert( modelElement, splitResult.position );

// Convert children and insert to element.
const childrenResult = conversionApi.convertChildren( data.viewItem, conversionApi.writer.createPositionAt( modelElement, 0 ) );
conversionApi.convertChildren( data.viewItem, conversionApi.writer.createPositionAt( modelElement, 0 ) );

// Consume appropriate value from consumable values list.
conversionApi.consumable.consume( data.viewItem, match.match );
conversionApi.consumable.consume( data.viewItem, match );

const parts = conversionApi.getSplitParts( modelElement );

// Set conversion result range.
data.modelRange = new ModelRange(
// Range should start before inserted element
conversionApi.writer.createPositionBefore( modelElement ),
// Should end after but we need to take into consideration that children could split our
// element, so we need to move range after parent of the last converted child.
// before: <allowed>[]</allowed>
// after: <allowed>[<converted><child></child></converted><child></child><converted>]</converted></allowed>
conversionApi.writer.createPositionAfter( childrenResult.modelCursor.parent )
conversionApi.writer.createPositionAfter( parts.last )
);

// Now we need to check where the modelCursor should be.
// If we had to split parent to insert our element then we want to continue conversion inside split parent.
//
// before: <allowed><notAllowed>[]</notAllowed></allowed>
// after: <allowed><notAllowed></notAllowed><converted></converted><notAllowed>[]</notAllowed></allowed>
// Now we need to check where the `modelCursor` should be.
if ( splitResult.cursorParent ) {
data.modelCursor = conversionApi.writer.createPositionAt( splitResult.cursorParent, 0 );
// If we split parent to insert our element then we want to continue conversion in the new part of the split parent.
//
// before: <allowed><notAllowed>foo[]</notAllowed></allowed>
// after: <allowed><notAllowed>foo</notAllowed><converted></converted><notAllowed>[]</notAllowed></allowed>

// Otherwise just continue after inserted element.
data.modelCursor = conversionApi.writer.createPositionAt( splitResult.cursorParent, 0 );
} else {
// Otherwise just continue after inserted element.

data.modelCursor = data.modelRange.end;
}
};
Expand Down

0 comments on commit c0d3b40

Please sign in to comment.