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

Commit

Permalink
Minor refactoring and added more tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kamil Piechaczek committed Feb 1, 2018
1 parent ab40690 commit a7d0108
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 9 deletions.
22 changes: 14 additions & 8 deletions src/model/utils/insertcontent.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,8 @@ class Insertion {
return;
}

const mergeLeft = canMergeLeft.call( this );
const mergeRight = canMergeRight.call( this );
const mergeLeft = canMergeLeft( this.canMergeWith, this.model.schema );
const mergeRight = canMergeRight( this.canMergeWith, this.model.schema );
const mergePosLeft = LivePosition.createBefore( node );
const mergePosRight = LivePosition.createAfter( node );

Expand Down Expand Up @@ -328,22 +328,28 @@ class Insertion {
mergePosLeft.detach();
mergePosRight.detach();

function canMergeLeft() {
// @param {Set} canMergeWith
// @param {module:engine/model/schema~Schema} schema
// @returns {Boolean}
function canMergeLeft( canMergeWith, schema ) {
const previousSibling = node.previousSibling;

return context.isFirst &&
( previousSibling instanceof Element ) &&
this.canMergeWith.has( previousSibling ) &&
this.model.schema.checkMerge( previousSibling, node );
canMergeWith.has( previousSibling ) &&
schema.checkMerge( previousSibling, node );
}

function canMergeRight() {
// @param {Set} canMergeWith
// @param {module:engine/model/schema~Schema} schema
// @returns {Boolean}
function canMergeRight( canMergeWith, schema ) {
const nextSibling = node.nextSibling;

return context.isLast &&
( nextSibling instanceof Element ) &&
this.canMergeWith.has( nextSibling ) &&
this.model.schema.checkMerge( node, nextSibling );
canMergeWith.has( nextSibling ) &&
schema.checkMerge( node, nextSibling );
}
}

Expand Down
40 changes: 39 additions & 1 deletion tests/model/utils/insertcontent.js
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ describe( 'DataController utils', () => {
);
} );

it( 'should not merge a paragraph wrapped in blockQuote with lists', () => {
it( 'should not merge a paragraph wrapped in blockQuote with list item (checking left merge)', () => {
model.schema.register( 'blockQuote', {
allowWhere: '$block',
allowContentOf: '$root',
Expand All @@ -426,6 +426,44 @@ describe( 'DataController utils', () => {
'<heading1>yyy[]o</heading1>'
);
} );

it( 'should not merge a paragraph wrapped in blockQuote with list item (checking right merge)', () => {
model.schema.register( 'blockQuote', {
allowWhere: '$block',
allowContentOf: '$root',
} );

setData( model, '<listItem>fo[]o</listItem>' );

insertHelper( '<heading1>yyy</heading1><blockQuote><paragraph>xxx</paragraph></blockQuote>' );

expect( getData( model ) ).to.equal(
'<listItem>foyyy</listItem>' +
'<blockQuote>' +
'<paragraph>xxx</paragraph>' +
'</blockQuote>' +
'<listItem>[]o</listItem>'
);
} );

it( 'should not merge a paragraph wrapped in blockQuote with list item (checking both merges)', () => {
model.schema.register( 'blockQuote', {
allowWhere: '$block',
allowContentOf: '$root',
} );

setData( model, '<listItem>fo[]o</listItem>' );

insertHelper( '<blockQuote><paragraph>xxx</paragraph></blockQuote>' );

expect( getData( model ) ).to.equal(
'<listItem>fo</listItem>' +
'<blockQuote>' +
'<paragraph>xxx</paragraph>' +
'</blockQuote>' +
'<listItem>[]o</listItem>'
);
} );
} );

describe( 'mixed content to block', () => {
Expand Down

0 comments on commit a7d0108

Please sign in to comment.