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

Commit

Permalink
Merge 2bdf581 into c8f7e57
Browse files Browse the repository at this point in the history
  • Loading branch information
Piotr Jasiun committed Sep 17, 2018
2 parents c8f7e57 + 2bdf581 commit 7af27cb
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 12 deletions.
11 changes: 10 additions & 1 deletion src/converters.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import ModelElement from '@ckeditor/ckeditor5-engine/src/model/element';
import ModelPosition from '@ckeditor/ckeditor5-engine/src/model/position';
import ModelRange from '@ckeditor/ckeditor5-engine/src/model/range';
import ModelSelection from '@ckeditor/ckeditor5-engine/src/model/selection';

import ViewPosition from '@ckeditor/ckeditor5-engine/src/view/position';
import ViewRange from '@ckeditor/ckeditor5-engine/src/view/range';
Expand Down Expand Up @@ -749,14 +750,22 @@ export function modelChangePostFixer( model, writer ) {
* @param {module:utils/eventinfo~EventInfo} evt An object containing information about the fired event.
* @param {Array} args Arguments of {@link module:engine/model/model~Model#insertContent}.
*/
export function modelIndentPasteFixer( evt, [ content, selection ] ) {
export function modelIndentPasteFixer( evt, [ content, selectable ] ) {
// Check whether inserted content starts from a `listItem`. If it does not, it means that there are some other
// elements before it and there is no need to fix indents, because even if we insert that content into a list,
// that list will be broken.
// Note: we also need to handle singular elements because inserting item with indent 0 into 0,1,[],2
// would create incorrect model.
let item = content.is( 'documentFragment' ) ? content.getChild( 0 ) : content;

let selection;

if ( !selectable ) {
selection = this.document.selection;
} else {
selection = new ModelSelection( selectable );
}

if ( item && item.is( 'listItem' ) ) {
// Get a reference list item. Inserted list items will be fixed according to that item.
const pos = selection.getFirstPosition();
Expand Down
39 changes: 28 additions & 11 deletions tests/listediting.js
Original file line number Diff line number Diff line change
Expand Up @@ -3309,8 +3309,7 @@ describe( 'ListEditing', () => {
'<listItem listType="bulleted" listIndent="0">X</listItem>' +
'<listItem listType="bulleted" listIndent="1">Y</listItem>',
model.schema
),
modelDoc.selection
)
);

expect( getModelData( model ) ).to.equal(
Expand All @@ -3321,22 +3320,26 @@ describe( 'ListEditing', () => {
);
} );

// Just checking that it doesn't crash. #69
it( 'should work if an element is passed to DataController#insertContent()', () => {
it( 'should be triggered when selectable is passed', () => {
setModelData( model,
'<listItem listType="bulleted" listIndent="0">A</listItem>' +
'<listItem listType="bulleted" listIndent="1">B[]</listItem>' +
'<listItem listType="bulleted" listIndent="2">C</listItem>'
);

editor.model.insertContent(
new ModelElement( 'listItem', { listType: 'bulleted', listIndent: '0' }, 'X' ),
modelDoc.selection
parseModel(
'<listItem listType="bulleted" listIndent="0">X</listItem>' +
'<listItem listType="bulleted" listIndent="1">Y</listItem>',
model.schema
),
new ModelRange( new ModelPosition( modelRoot, [ 1, 1 ] ), new ModelPosition( modelRoot, [ 1, 1 ] ) )
);

expect( getModelData( model ) ).to.equal(
'<listItem listIndent="0" listType="bulleted">A</listItem>' +
'<listItem listIndent="1" listType="bulleted">BX[]</listItem>' +
'<listItem listIndent="1" listType="bulleted">B[]X</listItem>' +
'<listItem listIndent="2" listType="bulleted">Y</listItem>' +
'<listItem listIndent="2" listType="bulleted">C</listItem>'
);
} );
Expand All @@ -3349,11 +3352,25 @@ describe( 'ListEditing', () => {
'<listItem listType="bulleted" listIndent="2">C</listItem>'
);

editor.model.insertContent(
new ModelText( 'X' ),
modelDoc.selection
editor.model.insertContent( new ModelElement( 'listItem', { listType: 'bulleted', listIndent: '0' }, 'X' ) );

expect( getModelData( model ) ).to.equal(
'<listItem listIndent="0" listType="bulleted">A</listItem>' +
'<listItem listIndent="1" listType="bulleted">BX[]</listItem>' +
'<listItem listIndent="2" listType="bulleted">C</listItem>'
);
} );

// Just checking that it doesn't crash. #69
it( 'should work if an element is passed to DataController#insertContent()', () => {
setModelData( model,
'<listItem listType="bulleted" listIndent="0">A</listItem>' +
'<listItem listType="bulleted" listIndent="1">B[]</listItem>' +
'<listItem listType="bulleted" listIndent="2">C</listItem>'
);

editor.model.insertContent( new ModelText( 'X' ) );

expect( getModelData( model ) ).to.equal(
'<listItem listIndent="0" listType="bulleted">A</listItem>' +
'<listItem listIndent="1" listType="bulleted">BX[]</listItem>' +
Expand Down Expand Up @@ -3494,7 +3511,7 @@ describe( 'ListEditing', () => {
setModelData( model, '<paragraph>[]</paragraph>' );

expect( () => {
editor.model.insertContent( new ModelDocumentFragment(), modelDoc.selection );
editor.model.insertContent( new ModelDocumentFragment() );
} ).not.to.throw();
} );

Expand Down

0 comments on commit 7af27cb

Please sign in to comment.