| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215 |
- /**
- * @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( '<p>foo<b>bar</b></p>' );
- 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( 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( 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( stringify( model ) ).to.equal(
- '<paragraph>foo<$text bold="true">bar</$text></paragraph>' );
- } );
- } );
- 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( 'destroy', () => {
- it( 'should be there for you', () => {
- // Should not throw.
- data.destroy();
- expect( data ).to.respondTo( 'destroy' );
- } );
- } );
- } );
|