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 #871 from ckeditor/t/870
Browse files Browse the repository at this point in the history
Feature: `DataController#insertContent()` now accepts also model items. Closes #870.
  • Loading branch information
Reinmar authored Mar 21, 2017
2 parents 3583cae + 9354c22 commit d00c973
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 8 deletions.
12 changes: 10 additions & 2 deletions src/controller/insertcontent.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import log from '@ckeditor/ckeditor5-utils/src/log';
*
* @param {module:engine/controller/datacontroller~DataController} dataController The data controller in context of which the insertion
* should be performed.
* @param {module:engine/model/documentfragment~DocumentFragment} content The content to insert.
* @param {module:engine/model/documentfragment~DocumentFragment|module:engine/model/item~Item} content The content to insert.
* @param {module:engine/model/selection~Selection} selection Selection into which the content should be inserted.
* @param {module:engine/model/batch~Batch} [batch] Batch to which deltas will be added. If not specified, then
* changes will be added to a new batch.
Expand All @@ -42,7 +42,15 @@ export default function insertContent( dataController, content, selection, batch

const insertion = new Insertion( dataController, batch, selection.anchor );

insertion.handleNodes( content.getChildren(), {
let nodesToInsert;

if ( content.is( 'documentFragment' ) ) {
nodesToInsert = content.getChildren();
} else {
nodesToInsert = [ content ];
}

insertion.handleNodes( nodesToInsert, {
// The set of children being inserted is the only set in this context
// so it's the first and last (it's a hack ;)).
isFirst: true,
Expand Down
40 changes: 34 additions & 6 deletions tests/controller/insertcontent.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ describe( 'DataController', () => {

describe( 'insertContent', () => {
it( 'uses the passed batch', () => {
doc = new Document();
const doc = new Document();
doc.createRoot();
doc.schema.allow( { name: '$text', inside: '$root' } );

dataController = new DataController( doc );
const dataController = new DataController( doc );

const batch = doc.batch();

Expand All @@ -32,6 +32,36 @@ describe( 'DataController', () => {
expect( batch.deltas.length ).to.be.above( 0 );
} );

it( 'accepts DocumentFragment', () => {
const doc = new Document();
const dataController = new DataController( doc );
const batch = doc.batch();

doc.createRoot();
doc.schema.allow( { name: '$text', inside: '$root' } );

setData( doc, 'x[]x' );

insertContent( dataController, new DocumentFragment( [ new Text( 'a' ) ] ), doc.selection, batch );

expect( getData( doc ) ).to.equal( 'xa[]x' );
} );

it( 'accepts Text', () => {
const doc = new Document();
const dataController = new DataController( doc );
const batch = doc.batch();

doc.createRoot();
doc.schema.allow( { name: '$text', inside: '$root' } );

setData( doc, 'x[]x' );

insertContent( dataController, new Text( 'a' ), doc.selection, batch );

expect( getData( doc ) ).to.equal( 'xa[]x' );
} );

describe( 'in simple scenarios', () => {
beforeEach( () => {
doc = new Document();
Expand Down Expand Up @@ -604,6 +634,8 @@ describe( 'DataController', () => {
} );
} );

// Helper function that parses given content and inserts it at the cursor position.
//
// @param {module:engine/model/item~Item|String} content
function insertHelper( content ) {
if ( typeof content == 'string' ) {
Expand All @@ -612,10 +644,6 @@ describe( 'DataController', () => {
} );
}

if ( !( content instanceof DocumentFragment ) ) {
content = new DocumentFragment( [ content ] );
}

insertContent( dataController, content, doc.selection );
}
} );

0 comments on commit d00c973

Please sign in to comment.