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

Commit

Permalink
Added more test cases about splitting parent element.
Browse files Browse the repository at this point in the history
  • Loading branch information
oskarwrobel committed Feb 6, 2018
1 parent 0274ec1 commit 0939429
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 16 deletions.
9 changes: 7 additions & 2 deletions src/converters.js
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,8 @@ export function viewModelConverter( evt, data, conversionApi ) {
// Try to find allowed parent for list item.
const splitResult = conversionApi.splitToAllowedParent( listItem, data.modelCursor );

// When there is no allowed parent it means that list item can not be converted at current modelCursor.
// When there is no allowed parent it means that list item cannot be converted at current model position
// and in any of position ancestors.
if ( !splitResult ) {
return;
}
Expand All @@ -390,19 +391,23 @@ export function viewModelConverter( evt, data, conversionApi ) {
if ( child.name == 'ul' || child.name == 'ol' ) {
nextPosition = conversionApi.convertItem( child, nextPosition ).modelCursor;
}
// If it was not a list it was a "regular" list item content. Just append it to `listItem`.
// If it was not a list it was a "regular" list item content. Just convert it to `listItem`.
else {
conversionApi.convertItem( child, ModelPosition.createAt( listItem, 'end' ) );
}
}

conversionStore.indent--;

// Result range starts before the first item and ends after the last.
data.modelRange = new ModelRange( data.modelCursor, nextPosition );

// When modelCursor parent had to be split to insert list item.
if ( splitResult.cursorParent ) {
// Then continue conversion in split element.
data.modelCursor = ModelPosition.createAt( splitResult.cursorParent );
} else {
// Otherwise continue conversion after last list item.
data.modelCursor = data.modelRange.end;
}
}
Expand Down
95 changes: 81 additions & 14 deletions tests/listengine.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import ModelText from '@ckeditor/ckeditor5-engine/src/model/text';
import ViewPosition from '@ckeditor/ckeditor5-engine/src/view/position';
import ViewContainerElement from '@ckeditor/ckeditor5-engine/src/view/containerelement';
import ViewUIElement from '@ckeditor/ckeditor5-engine/src/view/uielement';
import buildViewConverter from '@ckeditor/ckeditor5-engine/src/conversion/buildviewconverter';

import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
import BoldEngine from '@ckeditor/ckeditor5-basic-styles/src/boldengine';
import UndoEngine from '@ckeditor/ckeditor5-undo/src/undoengine';
import Clipboard from '@ckeditor/ckeditor5-clipboard/src/clipboard';
Expand All @@ -33,7 +33,7 @@ describe( 'ListEngine', () => {
beforeEach( () => {
return VirtualTestEditor
.create( {
plugins: [ Clipboard, BoldEngine, Paragraph, ListEngine, UndoEngine, BlockQuoteEngine ]
plugins: [ Clipboard, BoldEngine, ListEngine, UndoEngine, BlockQuoteEngine ]
} )
.then( newEditor => {
editor = newEditor;
Expand Down Expand Up @@ -3427,18 +3427,6 @@ describe( 'ListEngine', () => {
.to.equal( '<ul><li>Foo<span></span><ul><li>Xxx</li><li>Yyy</li></ul></li></ul>' );
} );

it( 'list should be not converted in disallowed context', () => {
model.document.createRoot( '$title', 'title' );
model.schema.register( '$title', {
disallow: '$block',
allow: 'inline'
} );

const data = editor.data;

expect( data.stringify( data.parse( '<ul><li>foo</li></ul>', [ 'title' ] ) ) ).to.equal( '' );
} );

describe( 'remove converter should properly handle ui elements', () => {
let uiElement, liFoo, liBar;

Expand Down Expand Up @@ -3488,6 +3476,85 @@ describe( 'ListEngine', () => {
} );
} );

describe( 'schema checking and parent splitting', () => {
beforeEach( () => {
// Since this part of test tests only view->model conversion editing pipeline is not necessary.
editor.editing.destroy();
} );

it( 'list should be not converted when modelCursor and its ancestors disallow to insert list', () => {
model.document.createRoot( '$title', 'title' );

model.schema.register( '$title', {
disallow: '$block',
allow: 'inline'
} );

editor.data.set( '<ul><li>foo</li></ul>', 'title' );

expect( getModelData( model, { rootName: 'title', withoutSelection: true } ) ).to.equal( '' );
} );

it( 'should split parent element when one of modelCursor ancestors allows to insert list - in the middle', () => {
buildViewConverter().for( editor.data.viewToModel ).fromElement( 'div' ).toElement( 'div' );
model.schema.register( 'div', { inheritAllFrom: '$block' } );

editor.setData(
'<div>' +
'abc' +
'<ul>' +
'<li>foo</li>' +
'</ul>' +
'def' +
'</div>'
);

expect( getModelData( model, { withoutSelection: true } ) ).to.equal(
'<div>abc</div>' +
'<listItem indent="0" type="bulleted">foo</listItem>' +
'<div>def</div>'
);
} );

it( 'should split parent element when one of modelCursor ancestors allows to insert list - at the end', () => {
buildViewConverter().for( editor.data.viewToModel ).fromElement( 'div' ).toElement( 'div' );
model.schema.register( 'div', { inheritAllFrom: '$block' } );

editor.setData(
'<div>' +
'abc' +
'<ul>' +
'<li>foo</li>' +
'</ul>' +
'</div>'
);

expect( getModelData( model, { withoutSelection: true } ) ).to.equal(
'<div>abc</div>' +
'<listItem indent="0" type="bulleted">foo</listItem>'
);
} );

it( 'should split parent element when one of modelCursor ancestors allows to insert list - at the beginning', () => {
buildViewConverter().for( editor.data.viewToModel ).fromElement( 'div' ).toElement( 'div' );
model.schema.register( 'div', { inheritAllFrom: '$block' } );

editor.setData(
'<div>' +
'<ul>' +
'<li>foo</li>' +
'</ul>' +
'def' +
'</div>'
);

expect( getModelData( model, { withoutSelection: true } ) ).to.equal(
'<listItem indent="0" type="bulleted">foo</listItem>' +
'<div>def</div>'
);
} );
} );

function getViewPosition( root, path ) {
let parent = root;

Expand Down

0 comments on commit 0939429

Please sign in to comment.