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

Commit

Permalink
Increased CC of ViewConversionDispatcher.
Browse files Browse the repository at this point in the history
  • Loading branch information
oskarwrobel committed Jan 31, 2018
1 parent 83ecc45 commit 430a6e2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/conversion/viewconversiondispatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,23 +273,23 @@ export default class ViewConversionDispatcher {
*/
_splitToAllowedParent( element, cursorPosition ) {
// Try to find allowed parent.
const allowedParent = this.conversionApi.schema.findAllowedParent( element, cursorPosition, this._modelCursor.parent );
const allowedParent = this.conversionApi.schema.findAllowedParent( element, cursorPosition );

// When there is no parent that allows to insert element then return `null`.
if ( !allowedParent ) {
return null;
}

// When allowed parent is in context tree.
if ( this._modelCursor.parent.getAncestors().includes( allowedParent ) ) {
return null;
}

// When current position parent allows to insert element then return this position.
if ( allowedParent === cursorPosition.parent ) {
return { position: cursorPosition };
}

// When allowed parent is in context tree.
if ( this._modelCursor.parent.getAncestors().includes( allowedParent ) ) {
return null;
}

// Split element to allowed parent.
const splitResult = this.conversionApi.writer.split( cursorPosition, allowedParent );

Expand Down
19 changes: 19 additions & 0 deletions tests/conversion/viewconversiondispatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,25 @@ describe( 'ViewConversionDispatcher', () => {
dispatcher.convert( new ViewDocumentFragment() );
sinon.assert.calledOnce( spy );
} );

it( 'should return null if element is not allowed in position and any of ancestors but is allowed in context tree', () => {
const spy = sinon.spy();

model.schema.register( 'div', {
allowIn: '$root',
} );

dispatcher.on( 'documentFragment', ( evt, data, conversionApi ) => {
const code = conversionApi.writer.createElement( 'div' );
const result = conversionApi.splitToAllowedParent( code, data.cursorPosition );

expect( result ).to.null;
spy();
} );

dispatcher.convert( new ViewDocumentFragment(), [ '$root', 'paragraph' ] );
sinon.assert.calledOnce( spy );
} );
} );
} );
} );

0 comments on commit 430a6e2

Please sign in to comment.