/** * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license */ 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 ModelDocumentFragment from '../../src/model/documentfragment'; import ViewDocumentFragment from '../../src/view/documentfragment'; import ViewDocument from '../../src/view/document'; 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'; import { expectToThrowCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils'; import { StylesProcessor } from '../../src/view/stylesmap'; describe( 'DataController', () => { let model, modelDocument, htmlDataProcessor, data, schema, upcastHelpers, downcastHelpers, viewDocument; beforeEach( () => { const stylesProcessor = new StylesProcessor(); model = new Model(); schema = model.schema; modelDocument = model.document; modelDocument.createRoot(); modelDocument.createRoot( '$title', 'title' ); schema.register( '$title', { inheritAllFrom: '$root' } ); viewDocument = new ViewDocument( stylesProcessor ); htmlDataProcessor = new HtmlDataProcessor( viewDocument ); data = new DataController( model, stylesProcessor ); data.processor = htmlDataProcessor; upcastHelpers = new UpcastHelpers( [ data.upcastDispatcher ] ); downcastHelpers = new DowncastHelpers( [ data.downcastDispatcher ] ); } ); describe( 'constructor()', () => { it( 'sets the model and styles processor properties', () => { const stylesProcessor = new StylesProcessor(); const data = new DataController( model, stylesProcessor ); expect( data.model ).to.equal( model ); expect( data.stylesProcessor ).to.equal( stylesProcessor ); } ); it( 'should create the #viewDocument property', () => { const stylesProcessor = new StylesProcessor(); const data = new DataController( model, stylesProcessor ); expect( data.viewDocument ).to.be.instanceOf( ViewDocument ); } ); } ); 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
' ); expectToThrowCKEditorError( () => { data.init( 'Bar
' ); }, /datacontroller-init-document-not-empty:/, model ); } ); 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; } ); it( 'should throw an error when non-existent root is used (single)', () => { expectToThrowCKEditorError( () => { data.init( { nonexistent: 'Bar
' } ); }, /^datacontroller-init-non-existent-root:/ ); } ); it( 'should throw an error when non-existent root is used (one of many)', () => { schema.extend( '$text', { allowIn: '$root' } ); expectToThrowCKEditorError( () => { data.init( { main: 'bar', nonexistent: 'Bar
' } ); }, /^datacontroller-init-non-existent-root:/, model ); expect( getData( model, { withoutSelection: true } ) ).to.equal( '' ); } ); } ); 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)', () => { expectToThrowCKEditorError( () => { data.set( { nonexistent: 'Bar
' } ); }, /datacontroller-set-non-existent-root:/, model ); } ); 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' ); expectToThrowCKEditorError( () => { data.set( { main: 'bar', nonexistent: 'Bar
' } ); }, /datacontroller-set-non-existent-root:/, model ); expect( getData( model, { withoutSelection: true } ) ).to.equal( 'foo' ); } ); // https://github.com/ckeditor/ckeditor5-engine/issues/1721. it( 'should not throw when setting the data with markers that already exist in the editor', () => { schema.extend( '$text', { allowIn: '$root' } ); data.set( 'foo' ); downcastHelpers.markerToElement( { model: 'marker', view: 'marker' } ); upcastHelpers.elementToMarker( { view: 'marker', model: 'marker' } ); model.change( writer => { writer.addMarker( 'marker', { range: writer.createRangeIn( modelDocument.getRoot() ), usingOperation: true } ); } ); expect( () => { data.set( data.get() ); } ).not.to.throw(); } ); } ); describe( 'get()', () => { it( 'should get paragraph with text', () => { schema.register( 'paragraph', { inheritAllFrom: '$block' } ); setData( model, 'foo
' ); expect( data.get( { trim: 'empty' } ) ).to.equal( 'foo
' ); } ); it( 'should trim empty paragraph by default', () => { schema.register( 'paragraph', { inheritAllFrom: '$block' } ); setData( model, '' ); } ); it( 'should get two paragraphs', () => { schema.register( 'paragraph', { inheritAllFrom: '$block' } ); setData( model, '
foo
bar
' ); expect( data.get( { trim: 'empty' } ) ).to.equal( 'foo
bar
' ); } ); it( 'should get text directly in root', () => { schema.extend( '$text', { allowIn: '$root' } ); setData( model, 'foo' ); expect( data.get() ).to.equal( 'foo' ); expect( data.get( { trim: 'empty' } ) ).to.equal( 'foo' ); } ); it( 'should get paragraphs without bold', () => { schema.register( 'paragraph', { inheritAllFrom: '$block' } ); setData( model, 'foobar
' ); expect( data.get( { trim: 'empty' } ) ).to.equal( 'foobar
' ); } ); it( 'should get paragraphs with bold', () => { schema.register( 'paragraph', { inheritAllFrom: '$block' } ); setData( model, 'foobar
' ); expect( data.get( { trim: 'empty' } ) ).to.equal( '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( { rootName: 'main' } ) ).to.equal( 'foo
' ); expect( data.get( { rootName: 'title' } ) ).to.equal( 'Bar' ); } ); it( 'should throw an error when non-existent root is used', () => { expectToThrowCKEditorError( () => { data.get( { rootName: 'nonexistent' } ); }, /datacontroller-get-non-existent-root:/ ); } ); } ); 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 use #viewDocument as a parent for returned document fragments', () => { const modelElement = parseModel( 'foobar
' ); } ); it( 'should correctly convert document markers #2', () => { const modelElement = parseModel( '