/** * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved. * For licensing, see LICENSE.md. */ import Model from '../../src/model/model'; import ModelRange from '../../src/model/range'; import ViewRange from '../../src/view/range'; import DataController from '../../src/controller/datacontroller'; import HtmlDataProcessor from '../../src/dataprocessor/htmldataprocessor'; import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror'; import ModelDocumentFragment from '../../src/model/documentfragment'; import ViewDocumentFragment from '../../src/view/documentfragment'; import { getData, setData, stringify, parse as parseModel } from '../../src/dev-utils/model'; import { parse as parseView, stringify as stringifyView } from '../../src/dev-utils/view'; import count from '@ckeditor/ckeditor5-utils/src/count'; import UpcastHelpers from '../../src/conversion/upcasthelpers'; import DowncastHelpers from '../../src/conversion/downcasthelpers'; describe( 'DataController', () => { let model, modelDocument, htmlDataProcessor, data, schema, upcastHelpers, downcastHelpers; beforeEach( () => { model = new Model(); schema = model.schema; modelDocument = model.document; modelDocument.createRoot(); modelDocument.createRoot( '$title', 'title' ); schema.register( '$title', { inheritAllFrom: '$root' } ); htmlDataProcessor = new HtmlDataProcessor(); data = new DataController( model, htmlDataProcessor ); upcastHelpers = new UpcastHelpers( data.upcastDispatcher ); downcastHelpers = new DowncastHelpers( data.downcastDispatcher ); } ); describe( 'constructor()', () => { it( 'works without data processor', () => { const data = new DataController( model ); expect( data.processor ).to.be.undefined; } ); } ); describe( 'parse()', () => { it( 'should set text', () => { schema.extend( '$text', { allowIn: '$root' } ); const output = data.parse( '
foobar
' ); expect( output ).to.instanceof( ModelDocumentFragment ); expect( stringify( output ) ).to.equal( 'foobar' ); } ); it( 'should set paragraph', () => { schema.register( 'paragraph', { inheritAllFrom: '$block' } ); upcastHelpers.elementToElement( { view: 'p', model: 'paragraph' } ); const output = data.parse( 'foobar
' ); expect( output ).to.instanceof( ModelDocumentFragment ); expect( stringify( output ) ).to.equal( 'foo
bar
' ); expect( output ).to.instanceof( ModelDocumentFragment ); expect( stringify( output ) ).to.equal( 'foobar
' ); expect( output ).to.instanceof( ModelDocumentFragment ); expect( stringify( output ) ).to.equal( 'foo
' ); const output = data.toModel( viewElement ); expect( output ).to.instanceof( ModelDocumentFragment ); expect( stringify( output ) ).to.equal( 'foo
bar
' ); const output = data.toModel( viewFragment ); expect( output ).to.be.instanceOf( ModelDocumentFragment ); expect( stringify( output ) ).to.equal( 'Foo
' ); expect( () => { data.init( 'Bar
' ); } ).to.throw( CKEditorError, 'datacontroller-init-document-not-empty: Trying to set initial data to not empty document.' ); } ); it( 'should set data to default main root', () => { schema.extend( '$text', { allowIn: '$root' } ); data.init( 'foo' ); expect( getData( model, { withoutSelection: true } ) ).to.equal( 'foo' ); } ); it( 'should set data to multiple roots at once', () => { schema.extend( '$text', { allowIn: '$root' } ); data.init( { main: 'bar', title: 'baz' } ); expect( getData( model, { withoutSelection: true } ) ).to.equal( 'bar' ); expect( getData( model, { withoutSelection: true, rootName: 'title' } ) ).to.equal( 'baz' ); } ); it( 'should get root name as a parameter', () => { schema.extend( '$text', { allowIn: '$root' } ); data.init( { title: 'foo' } ); expect( getData( model, { withoutSelection: true, rootName: 'title' } ) ).to.equal( 'foo' ); } ); it( 'should create a batch', () => { schema.extend( '$text', { allowIn: '$root' } ); data.init( 'foo' ); expect( count( modelDocument.history.getOperations() ) ).to.equal( 1 ); } ); it( 'should cause firing change event', () => { const spy = sinon.spy(); schema.extend( '$text', { allowIn: '$root' } ); model.document.on( 'change', spy ); data.init( 'foo' ); expect( spy.calledOnce ).to.be.true; } ); it( 'should return a resolved Promise', () => { const promise = data.init( 'Foo
' ); expect( promise ).to.be.instanceof( Promise ); return promise; } ); } ); describe( 'set()', () => { it( 'should set data to default main root', () => { schema.extend( '$text', { allowIn: '$root' } ); data.set( 'foo' ); expect( getData( model, { withoutSelection: true } ) ).to.equal( 'foo' ); } ); it( 'should create a batch', () => { schema.extend( '$text', { allowIn: '$root' } ); data.set( 'foo' ); expect( count( modelDocument.history.getOperations() ) ).to.equal( 1 ); } ); it( 'should cause firing change event', () => { const spy = sinon.spy(); schema.extend( '$text', { allowIn: '$root' } ); model.document.on( 'change', spy ); data.set( 'foo' ); expect( spy.calledOnce ).to.be.true; } ); it( 'should get root name as a parameter', () => { schema.extend( '$text', { allowIn: '$root' } ); data.set( 'foo' ); data.set( { title: 'Bar' } ); expect( getData( model, { withoutSelection: true, rootName: 'main' } ) ).to.equal( 'foo' ); expect( getData( model, { withoutSelection: true, rootName: 'title' } ) ).to.equal( 'Bar' ); expect( count( modelDocument.history.getOperations() ) ).to.equal( 2 ); } ); it( 'should parse given data before set in a context of correct root', () => { schema.extend( '$text', { allowIn: '$title', disallowIn: '$root' } ); data.set( 'foo', 'main' ); data.set( { title: 'Bar' } ); expect( getData( model, { withoutSelection: true, rootName: 'main' } ) ).to.equal( '' ); expect( getData( model, { withoutSelection: true, rootName: 'title' } ) ).to.equal( 'Bar' ); expect( count( modelDocument.history.getOperations() ) ).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.extend( '$text', { allowIn: '$root' } ); data.set( { title: 'foo' } ); expect( getData( model, { withoutSelection: true, rootName: 'title' } ) ).to.equal( 'foo' ); data.set( { title: '' } ); expect( getData( model, { withoutSelection: true, rootName: 'title' } ) ).to.equal( '' ); } ); it( 'should throw an error when non-existent root is used (single)', () => { expect( () => { data.set( { nonexistent: 'Bar
' } ); } ).to.throw( CKEditorError, 'trying-to-set-data-on-non-existing-root: Attempting to set data on non-existing "nonexistent" root.' ); } ); it( 'should throw an error when non-existent root is used (one of many) without touching any roots data', () => { schema.extend( '$text', { allowIn: '$root' } ); data.set( 'foo' ); expect( () => { data.set( { main: 'bar', nonexistent: 'Bar
' } ); } ).to.throw( CKEditorError, 'trying-to-set-data-on-non-existing-root: Attempting to set data on non-existing "nonexistent" root.' ); expect( getData( model, { withoutSelection: true } ) ).to.equal( 'foo' ); } ); } ); describe( 'get()', () => { it( 'should get paragraph with text', () => { schema.register( 'paragraph', { inheritAllFrom: '$block' } ); setData( model, 'foo
' ); } ); it( 'should get empty paragraph', () => { schema.register( 'paragraph', { inheritAllFrom: '$block' } ); setData( model, '' ); } ); it( 'should get two paragraphs', () => { schema.register( 'paragraph', { inheritAllFrom: '$block' } ); setData( model, '
foo
bar
' ); } ); it( 'should get text directly in root', () => { schema.extend( '$text', { allowIn: '$root' } ); setData( model, 'foo' ); expect( data.get() ).to.equal( 'foo' ); } ); it( 'should get paragraphs without bold', () => { schema.register( 'paragraph', { inheritAllFrom: '$block' } ); setData( model, 'foobar
' ); } ); it( 'should get paragraphs with bold', () => { schema.register( 'paragraph', { inheritAllFrom: '$block' } ); setData( model, 'foobar
' ); } ); it( 'should get root name as a parameter', () => { schema.register( 'paragraph', { inheritAllFrom: '$block' } ); schema.extend( '$text', { allowIn: '$root' } ); setData( model, 'foo
' ); expect( data.get( 'main' ) ).to.equal( 'foo
' ); expect( data.get( 'title' ) ).to.equal( 'Bar' ); } ); } ); describe( 'stringify()', () => { beforeEach( () => { schema.register( 'paragraph', { inheritAllFrom: '$block' } ); schema.register( 'div' ); schema.extend( '$block', { allowIn: 'div' } ); schema.extend( 'div', { allowIn: '$root' } ); downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } ); } ); it( 'should stringify a content of an element', () => { const modelElement = parseModel( 'foo
' ); } ); it( 'should stringify a content of a document fragment', () => { const modelDocumentFragment = parseModel( 'foo
bar
' ); } ); } ); describe( 'toView()', () => { beforeEach( () => { schema.register( 'paragraph', { inheritAllFrom: '$block' } ); schema.register( 'div' ); schema.extend( '$block', { allowIn: 'div' } ); schema.extend( 'div', { allowIn: '$root' } ); downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } ); } ); it( 'should convert a content of an element', () => { const modelElement = parseModel( 'foobar
' ); } ); it( 'should correctly convert document markers #2', () => { const modelElement = parseModel( '