/**
* @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.
*/
/* bender-tags: view */
import ModelDocument from '/ckeditor5/engine/model/document.js';
import DataController from '/ckeditor5/engine/datacontroller.js';
import HtmlDataProcessor from '/ckeditor5/engine/dataprocessor/htmldataprocessor.js';
import buildViewConverter from '/ckeditor5/engine/conversion/buildviewconverter.js';
import buildModelConverter from '/ckeditor5/engine/conversion/buildmodelconverter.js';
import { getData, setData, stringify } from '/tests/engine/_utils/model.js';
import count from '/ckeditor5/utils/count.js';
describe( 'DataController', () => {
let modelDocument, htmlDataProcessor, data, schema;
beforeEach( () => {
modelDocument = new ModelDocument();
modelDocument.createRoot();
modelDocument.createRoot( '$root', 'title' );
htmlDataProcessor = new HtmlDataProcessor();
data = new DataController( modelDocument, htmlDataProcessor );
schema = modelDocument.schema;
} );
describe( 'constructor', () => {
it( 'works without data processor', () => {
const data = new DataController( modelDocument );
expect( data.processor ).to.be.undefined;
} );
} );
describe( 'parse', () => {
it( 'should set text', () => {
schema.allow( { name: '$text', inside: '$root' } );
const model = data.parse( '
foobar
' );
expect( stringify( model ) ).to.equal( 'foobar' );
} );
it( 'should set paragraph', () => {
schema.registerItem( 'paragraph', '$block' );
buildViewConverter().for( data.viewToModel ).fromElement( 'p' ).toElement( 'paragraph' );
const model = data.parse( 'foobar
' );
expect( stringify( model ) ).to.equal( 'foobar' );
} );
it( 'should set two paragraphs', () => {
schema.registerItem( 'paragraph', '$block' );
buildViewConverter().for( data.viewToModel ).fromElement( 'p' ).toElement( 'paragraph' );
const model = data.parse( 'foo
bar
' );
expect( stringify( model ) ).to.equal(
'foobar' );
} );
it( 'should set paragraphs with bold', () => {
schema.registerItem( 'paragraph', '$block' );
schema.allow( { name: '$text', attributes: [ 'bold' ], inside: '$block' } );
buildViewConverter().for( data.viewToModel ).fromElement( 'p' ).toElement( 'paragraph' );
buildViewConverter().for( data.viewToModel ).fromElement( 'b' ).toAttribute( 'bold', true );
const model = data.parse( 'foobar
' );
expect( stringify( model ) ).to.equal(
'foo<$text bold="true">bar$text>' );
} );
} );
describe( 'set', () => {
it( 'should set data to root', () => {
schema.allow( { name: '$text', inside: '$root' } );
data.set( 'foo' );
expect( getData( modelDocument, { withoutSelection: true } ) ).to.equal( 'foo' );
} );
it( 'should create a batch', () => {
schema.allow( { name: '$text', inside: '$root' } );
data.set( 'foo' );
expect( count( modelDocument.history.getDeltas() ) ).to.equal( 1 );
} );
it( 'should fire #changesDone', () => {
const spy = sinon.spy();
schema.allow( { name: '$text', inside: '$root' } );
modelDocument.on( 'changesDone', spy );
data.set( 'foo' );
expect( spy.calledOnce ).to.be.true;
} );
it( 'should get root name as a parameter', () => {
schema.allow( { name: '$text', inside: '$root' } );
data.set( 'foo', 'main' );
data.set( 'Bar', 'title' );
expect( getData( modelDocument, { withoutSelection: true, rootName: 'main' } ) ).to.equal( 'foo' );
expect( getData( modelDocument, { withoutSelection: true, rootName: 'title' } ) ).to.equal( 'Bar' );
expect( count( modelDocument.history.getDeltas() ) ).to.equal( 2 );
} );
// This case was added when order of params was different and it really didn't work. Let's keep it
// if anyone will ever try to change this.
it( 'should allow setting empty data', () => {
schema.allow( { name: '$text', inside: '$root' } );
data.set( 'foo', 'title' );
expect( getData( modelDocument, { withoutSelection: true, rootName: 'title' } ) ).to.equal( 'foo' );
data.set( '', 'title' );
expect( getData( modelDocument, { withoutSelection: true, rootName: 'title' } ) ).to.equal( '' );
} );
} );
describe( 'get', () => {
it( 'should get paragraph with text', () => {
modelDocument.schema.registerItem( 'paragraph', '$block' );
setData( modelDocument, 'foo' );
buildModelConverter().for( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
expect( data.get() ).to.equal( 'foo
' );
} );
it( 'should get empty paragraph', () => {
modelDocument.schema.registerItem( 'paragraph', '$block' );
setData( modelDocument, '' );
buildModelConverter().for( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
expect( data.get() ).to.equal( '
' );
} );
it( 'should get two paragraphs', () => {
modelDocument.schema.registerItem( 'paragraph', '$block' );
setData( modelDocument, 'foobar' );
buildModelConverter().for( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
expect( data.get() ).to.equal( 'foo
bar
' );
} );
it( 'should get text directly in root', () => {
modelDocument.schema.allow( { name: '$text', inside: '$root' } );
setData( modelDocument, 'foo' );
expect( data.get() ).to.equal( 'foo' );
} );
it( 'should get paragraphs without bold', () => {
modelDocument.schema.registerItem( 'paragraph', '$block' );
setData( modelDocument, 'foo<$text bold="true">bar$text>' );
buildModelConverter().for( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
expect( data.get() ).to.equal( 'foobar
' );
} );
it( 'should get paragraphs with bold', () => {
modelDocument.schema.registerItem( 'paragraph', '$block' );
setData( modelDocument, 'foo<$text bold="true">bar$text>' );
buildModelConverter().for( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
buildModelConverter().for( data.modelToView ).fromAttribute( 'bold' ).toElement( 'b' );
expect( data.get() ).to.equal( 'foobar
' );
} );
it( 'should get root name as a parameter', () => {
modelDocument.schema.registerItem( 'paragraph', '$block' );
modelDocument.schema.allow( { name: '$text', inside: '$root' } );
setData( modelDocument, 'foo', { rootName: 'main' } );
setData( modelDocument, 'Bar', { rootName: 'title' } );
buildModelConverter().for( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
buildModelConverter().for( data.modelToView ).fromAttribute( 'bold' ).toElement( 'b' );
expect( data.get() ).to.equal( 'foo
' );
expect( data.get( 'main' ) ).to.equal( 'foo
' );
expect( data.get( 'title' ) ).to.equal( 'Bar' );
} );
} );
describe( 'destroy', () => {
it( 'should be there for you', () => {
// Should not throw.
data.destroy();
expect( data ).to.respondTo( 'destroy' );
} );
} );
} );