| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442 |
- /**
- * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
- import ModelDocument from '../../src/model/document';
- import DataController from '../../src/controller/datacontroller';
- import HtmlDataProcessor from '../../src/dataprocessor/htmldataprocessor';
- import buildViewConverter from '../../src/conversion/buildviewconverter';
- import buildModelConverter from '../../src/conversion/buildmodelconverter';
- import ModelDocumentFragment from '../../src/model/documentfragment';
- import ModelText from '../../src/model/text';
- import ModelSelection from '../../src/model/selection';
- import ViewDocumentFragment from '../../src/view/documentfragment';
- import { getData, setData, stringify, parse as parseModel } from '../../src/dev-utils/model';
- import { parse as parseView } from '../../src/dev-utils/view';
- import count from '@ckeditor/ckeditor5-utils/src/count';
- 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( '<p>foo<b>bar</b></p>' );
- expect( model ).to.instanceof( ModelDocumentFragment );
- 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( '<p>foo<b>bar</b></p>' );
- expect( model ).to.instanceof( ModelDocumentFragment );
- expect( stringify( model ) ).to.equal( '<paragraph>foobar</paragraph>' );
- } );
- it( 'should set two paragraphs', () => {
- schema.registerItem( 'paragraph', '$block' );
- buildViewConverter().for( data.viewToModel ).fromElement( 'p' ).toElement( 'paragraph' );
- const model = data.parse( '<p>foo</p><p>bar</p>' );
- expect( model ).to.instanceof( ModelDocumentFragment );
- expect( stringify( model ) ).to.equal( '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
- } );
- 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( '<p>foo<b>bar</b></p>' );
- expect( model ).to.instanceof( ModelDocumentFragment );
- expect( stringify( model ) ).to.equal( '<paragraph>foo<$text bold="true">bar</$text></paragraph>' );
- } );
- it( 'should parse in the root context by default', () => {
- const model = data.parse( 'foo' );
- expect( stringify( model ) ).to.equal( '' );
- } );
- it( 'should accept parsing context', () => {
- const model = data.parse( 'foo', '$block' );
- expect( stringify( model ) ).to.equal( 'foo' );
- } );
- } );
- describe( 'toModel', () => {
- beforeEach( () => {
- schema.registerItem( 'paragraph', '$block' );
- buildViewConverter().for( data.viewToModel ).fromElement( 'p' ).toElement( 'paragraph' );
- } );
- it( 'should convert content of an element #1', () => {
- const viewElement = parseView( '<p>foo</p>' );
- const model = data.toModel( viewElement );
- expect( model ).to.instanceof( ModelDocumentFragment );
- expect( stringify( model ) ).to.equal( '<paragraph>foo</paragraph>' );
- } );
- it( 'should convert content of an element #2', () => {
- const viewFragment = parseView( '<p>foo</p><p>bar</p>' );
- const model = data.toModel( viewFragment );
- expect( model ).to.be.instanceOf( ModelDocumentFragment );
- expect( stringify( model ) ).to.equal( '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
- } );
- it( 'should accept parsing context', () => {
- modelDocument.createRoot( 'inlineRoot', 'inlineRoot' );
- schema.registerItem( 'inlineRoot' );
- schema.allow( { name: '$text', inside: 'inlineRoot' } );
- const viewFragment = new ViewDocumentFragment( [ parseView( 'foo' ) ] );
- // Model fragment in root.
- expect( stringify( data.toModel( viewFragment ) ) ).to.equal( '' );
- // Model fragment in inline root.
- expect( stringify( data.toModel( viewFragment, 'inlineRoot' ) ) ).to.equal( 'foo' );
- } );
- } );
- 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, '<paragraph>foo</paragraph>' );
- buildModelConverter().for( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
- expect( data.get() ).to.equal( '<p>foo</p>' );
- } );
- it( 'should get empty paragraph', () => {
- modelDocument.schema.registerItem( 'paragraph', '$block' );
- setData( modelDocument, '<paragraph></paragraph>' );
- buildModelConverter().for( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
- expect( data.get() ).to.equal( '<p> </p>' );
- } );
- it( 'should get two paragraphs', () => {
- modelDocument.schema.registerItem( 'paragraph', '$block' );
- setData( modelDocument, '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
- buildModelConverter().for( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
- expect( data.get() ).to.equal( '<p>foo</p><p>bar</p>' );
- } );
- 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, '<paragraph>foo<$text bold="true">bar</$text></paragraph>' );
- buildModelConverter().for( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
- expect( data.get() ).to.equal( '<p>foobar</p>' );
- } );
- it( 'should get paragraphs with bold', () => {
- modelDocument.schema.registerItem( 'paragraph', '$block' );
- setData( modelDocument, '<paragraph>foo<$text bold="true">bar</$text></paragraph>' );
- buildModelConverter().for( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
- buildModelConverter().for( data.modelToView ).fromAttribute( 'bold' ).toElement( 'b' );
- expect( data.get() ).to.equal( '<p>foo<b>bar</b></p>' );
- } );
- it( 'should get root name as a parameter', () => {
- modelDocument.schema.registerItem( 'paragraph', '$block' );
- modelDocument.schema.allow( { name: '$text', inside: '$root' } );
- setData( modelDocument, '<paragraph>foo</paragraph>', { 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( '<p>foo</p>' );
- expect( data.get( 'main' ) ).to.equal( '<p>foo</p>' );
- expect( data.get( 'title' ) ).to.equal( 'Bar' );
- } );
- } );
- describe( 'stringify', () => {
- beforeEach( () => {
- modelDocument.schema.registerItem( 'paragraph', '$block' );
- modelDocument.schema.registerItem( 'div' );
- modelDocument.schema.allow( { name: '$block', inside: 'div' } );
- modelDocument.schema.allow( { name: 'div', inside: '$root' } );
- buildModelConverter().for( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
- } );
- it( 'should stringify a content of an element', () => {
- const modelElement = parseModel( '<div><paragraph>foo</paragraph></div>', modelDocument.schema );
- expect( data.stringify( modelElement ) ).to.equal( '<p>foo</p>' );
- } );
- it( 'should stringify a content of a document fragment', () => {
- const modelDocumentFragment = parseModel( '<paragraph>foo</paragraph><paragraph>bar</paragraph>', modelDocument.schema );
- expect( data.stringify( modelDocumentFragment ) ).to.equal( '<p>foo</p><p>bar</p>' );
- } );
- } );
- describe( 'toView', () => {
- beforeEach( () => {
- modelDocument.schema.registerItem( 'paragraph', '$block' );
- modelDocument.schema.registerItem( 'div' );
- modelDocument.schema.allow( { name: '$block', inside: 'div' } );
- modelDocument.schema.allow( { name: 'div', inside: '$root' } );
- buildModelConverter().for( data.modelToView ).fromElement( 'paragraph' ).toElement( 'p' );
- } );
- it( 'should convert a content of an element', () => {
- const modelElement = parseModel( '<div><paragraph>foo</paragraph></div>', modelDocument.schema );
- const viewDocumentFragment = data.toView( modelElement );
- expect( viewDocumentFragment ).to.be.instanceOf( ViewDocumentFragment );
- const viewElement = viewDocumentFragment.getChild( 0 );
- expect( viewElement.name ).to.equal( 'p' );
- expect( viewElement.childCount ).to.equal( 1 );
- expect( viewElement.getChild( 0 ).data ).to.equal( 'foo' );
- } );
- it( 'should convert a document fragment', () => {
- const modelDocumentFragment = parseModel( '<paragraph>foo</paragraph><paragraph>bar</paragraph>', modelDocument.schema );
- const viewDocumentFragment = data.toView( modelDocumentFragment );
- expect( viewDocumentFragment ).to.be.instanceOf( ViewDocumentFragment );
- expect( viewDocumentFragment ).to.have.property( 'childCount', 2 );
- const viewElement = viewDocumentFragment.getChild( 0 );
- expect( viewElement.name ).to.equal( 'p' );
- expect( viewElement.childCount ).to.equal( 1 );
- expect( viewElement.getChild( 0 ).data ).to.equal( 'foo' );
- } );
- } );
- describe( 'destroy', () => {
- it( 'should be there for you', () => {
- // Should not throw.
- data.destroy();
- expect( data ).to.respondTo( 'destroy' );
- } );
- } );
- describe( 'insertContent', () => {
- it( 'should be decorated', () => {
- schema.allow( { name: '$text', inside: '$root' } ); // To surpress warnings.
- const spy = sinon.spy();
- data.on( 'insertContent', spy );
- data.insertContent( new ModelText( 'a' ), modelDocument.selection );
- expect( spy.calledOnce ).to.be.true;
- } );
- it( 'should insert content (item)', () => {
- schema.registerItem( 'paragraph', '$block' );
- setData( modelDocument, '<paragraph>fo[]ar</paragraph>' );
- data.insertContent( new ModelText( 'ob' ), modelDocument.selection );
- expect( getData( modelDocument ) ).to.equal( '<paragraph>foob[]ar</paragraph>' );
- } );
- it( 'should insert content (document fragment)', () => {
- schema.registerItem( 'paragraph', '$block' );
- setData( modelDocument, '<paragraph>fo[]ar</paragraph>' );
- data.insertContent( new ModelDocumentFragment( [ new ModelText( 'ob' ) ] ), modelDocument.selection );
- expect( getData( modelDocument ) ).to.equal( '<paragraph>foob[]ar</paragraph>' );
- } );
- } );
- describe( 'deleteContent', () => {
- it( 'should be decorated', () => {
- const spy = sinon.spy();
- data.on( 'deleteContent', spy );
- data.deleteContent( modelDocument.selection );
- expect( spy.calledOnce ).to.be.true;
- } );
- it( 'should delete selected content', () => {
- schema.registerItem( 'paragraph', '$block' );
- setData( modelDocument, '<paragraph>fo[ob]ar</paragraph>' );
- data.deleteContent( modelDocument.selection, modelDocument.batch() );
- expect( getData( modelDocument ) ).to.equal( '<paragraph>fo[]ar</paragraph>' );
- } );
- } );
- describe( 'modifySelection', () => {
- it( 'should be decorated', () => {
- schema.registerItem( 'paragraph', '$block' );
- setData( modelDocument, '<paragraph>fo[ob]ar</paragraph>' );
- const spy = sinon.spy();
- data.on( 'modifySelection', spy );
- data.modifySelection( modelDocument.selection );
- expect( spy.calledOnce ).to.be.true;
- } );
- it( 'should modify a selection', () => {
- schema.registerItem( 'paragraph', '$block' );
- setData( modelDocument, '<paragraph>fo[ob]ar</paragraph>' );
- data.modifySelection( modelDocument.selection, { direction: 'backward' } );
- expect( getData( modelDocument ) ).to.equal( '<paragraph>fo[o]bar</paragraph>' );
- } );
- } );
- describe( 'getSelectedContent', () => {
- it( 'should be decorated', () => {
- const spy = sinon.spy();
- const sel = new ModelSelection();
- data.on( 'getSelectedContent', spy );
- data.getSelectedContent( sel );
- expect( spy.calledOnce ).to.be.true;
- } );
- it( 'should return selected content', () => {
- schema.registerItem( 'paragraph', '$block' );
- setData( modelDocument, '<paragraph>fo[ob]ar</paragraph>' );
- const content = data.getSelectedContent( modelDocument.selection );
- expect( stringify( content ) ).to.equal( 'ob' );
- } );
- } );
- } );
|