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

Commit

Permalink
Merge pull request #1742 from ckeditor/t/ckeditor5/1380
Browse files Browse the repository at this point in the history
Fix: Selection will not change during forbidden copy-paste operation inside table cell. Closes ckeditor/ckeditor5#1380.
  • Loading branch information
jodator authored Jun 10, 2019
2 parents f312bdb + d7ec424 commit ab15b17
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/model/utils/insertcontent.js
Original file line number Diff line number Diff line change
Expand Up @@ -561,8 +561,15 @@ class Insertion {
this.position = this.writer.createPositionBefore( parent );

// Special case – parent is empty (<p>^</p>).
//
// 1. parent.isEmpty
// We can remove the element after moving insertion position out of it.
if ( parent.isEmpty ) {
//
// 2. parent.parent === allowedIn
// However parent should remain in place when allowed element is above limit element in document tree.
// For example there shouldn't be allowed to remove empty paragraph from tableCell, when is pasted
// content allowed in $root.
if ( parent.isEmpty && parent.parent === allowedIn ) {
this.writer.remove( parent );
}
} else if ( this.position.isAtEnd ) {
Expand Down
37 changes: 37 additions & 0 deletions tests/model/utils/insertcontent.js
Original file line number Diff line number Diff line change
Expand Up @@ -1254,6 +1254,43 @@ describe( 'DataController utils', () => {
expect( getData( model ) ).to.equal( '<limit>ab[]foo</limit>' );
expect( stringify( root, affectedRange ) ).to.equal( '<limit>[ab]foo</limit>' );
} );

describe( 'when allowed element is above limit element in document tree', () => {
// $root > table > tableRow > tableCell > paragraph
// After inserting new table ( allowed in root ), empty paragraph shouldn't be removed from current tableCell.
beforeEach( () => {
const schema = model.schema;

schema.register( 'wrapper', {
isLimit: true,
isBlock: true,
isObject: true,
allowWhere: '$block'
} );

schema.extend( 'paragraph', { allowIn: 'limit' } );
schema.extend( 'limit', { allowIn: 'wrapper' } );
} );

it( 'should not remove empty elements when not-allowed element is paste', () => {
setData( model, '<wrapper><limit><paragraph>[]</paragraph></limit></wrapper>' );

// pasted content is forbidden in current selection
const affectedRange = insertHelper( '<wrapper><limit><paragraph>foo</paragraph></limit></wrapper>' );

expect( getData( model ) ).to.equal( '<wrapper><limit>[]<paragraph></paragraph></limit></wrapper>' );
expect( stringify( root, affectedRange ) ).to.equal( '<wrapper><limit><paragraph>[]</paragraph></limit></wrapper>' );
} );

it( 'should correctly paste allowed nodes', () => {
setData( model, '<wrapper><limit><paragraph>[]</paragraph></limit></wrapper>' );

const affectedRange = insertHelper( '<paragraph>foo</paragraph>' );

expect( getData( model ) ).to.equal( '<wrapper><limit><paragraph>foo</paragraph>[]</limit></wrapper>' );
expect( stringify( root, affectedRange ) ).to.equal( '<wrapper><limit>[<paragraph>foo</paragraph>]</limit></wrapper>' );
} );
} );
} );

// Helper function that parses given content and inserts it at the cursor position.
Expand Down

0 comments on commit ab15b17

Please sign in to comment.