| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399 |
- /**
- * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
- import Model from '../../src/model/model';
- import Range from '../../src/model/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 { 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 {
- upcastElementToElement,
- upcastElementToAttribute
- } from '../../src/conversion/upcast-converters';
- import {
- downcastElementToElement,
- downcastAttributeToElement,
- downcastMarkerToHighlight
- } from '../../src/conversion/downcast-converters';
- describe( 'DataController', () => {
- let model, modelDocument, htmlDataProcessor, data, schema;
- 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 );
- } );
- 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( '<p>foo<b>bar</b></p>' );
- expect( output ).to.instanceof( ModelDocumentFragment );
- expect( stringify( output ) ).to.equal( 'foobar' );
- } );
- it( 'should set paragraph', () => {
- schema.register( 'paragraph', { inheritAllFrom: '$block' } );
- upcastElementToElement( { view: 'p', model: 'paragraph' } )( data.upcastDispatcher );
- const output = data.parse( '<p>foo<b>bar</b></p>' );
- expect( output ).to.instanceof( ModelDocumentFragment );
- expect( stringify( output ) ).to.equal( '<paragraph>foobar</paragraph>' );
- } );
- it( 'should set two paragraphs', () => {
- schema.register( 'paragraph', { inheritAllFrom: '$block' } );
- upcastElementToElement( { view: 'p', model: 'paragraph' } )( data.upcastDispatcher );
- const output = data.parse( '<p>foo</p><p>bar</p>' );
- expect( output ).to.instanceof( ModelDocumentFragment );
- expect( stringify( output ) ).to.equal( '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
- } );
- it( 'should set paragraphs with bold', () => {
- schema.register( 'paragraph', { inheritAllFrom: '$block' } );
- schema.extend( '$text', {
- allowAttributes: [ 'bold' ]
- } );
- upcastElementToElement( { view: 'p', model: 'paragraph' } )( data.upcastDispatcher );
- upcastElementToAttribute( { view: 'strong', model: 'bold' } )( data.upcastDispatcher );
- const output = data.parse( '<p>foo<strong>bar</strong></p>' );
- expect( output ).to.instanceof( ModelDocumentFragment );
- expect( stringify( output ) ).to.equal( '<paragraph>foo<$text bold="true">bar</$text></paragraph>' );
- } );
- it( 'should parse in the root context by default', () => {
- const output = data.parse( 'foo' );
- expect( stringify( output ) ).to.equal( '' );
- } );
- it( 'should accept parsing context', () => {
- const output = data.parse( 'foo', [ '$block' ] );
- expect( stringify( output ) ).to.equal( 'foo' );
- } );
- } );
- describe( 'toModel()', () => {
- beforeEach( () => {
- schema.register( 'paragraph', { inheritAllFrom: '$block' } );
- upcastElementToElement( { view: 'p', model: 'paragraph' } )( data.upcastDispatcher );
- } );
- it( 'should convert content of an element #1', () => {
- const viewElement = parseView( '<p>foo</p>' );
- const output = data.toModel( viewElement );
- expect( output ).to.instanceof( ModelDocumentFragment );
- expect( stringify( output ) ).to.equal( '<paragraph>foo</paragraph>' );
- } );
- it( 'should convert content of an element #2', () => {
- const viewFragment = parseView( '<p>foo</p><p>bar</p>' );
- const output = data.toModel( viewFragment );
- expect( output ).to.be.instanceOf( ModelDocumentFragment );
- expect( stringify( output ) ).to.equal( '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
- } );
- it( 'should accept parsing context', () => {
- modelDocument.createRoot( 'inlineRoot', 'inlineRoot' );
- schema.register( 'inlineRoot' );
- schema.extend( '$text', { allowIn: '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 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.getDeltas() ) ).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', 'main' );
- data.set( 'Bar', 'title' );
- expect( getData( model, { withoutSelection: true, rootName: 'main' } ) ).to.equal( 'foo' );
- expect( getData( model, { withoutSelection: true, rootName: 'title' } ) ).to.equal( 'Bar' );
- expect( count( modelDocument.history.getDeltas() ) ).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( 'Bar', 'title' );
- expect( getData( model, { withoutSelection: true, rootName: 'main' } ) ).to.equal( '' );
- expect( getData( model, { 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.extend( '$text', { allowIn: '$root' } );
- data.set( 'foo', 'title' );
- expect( getData( model, { withoutSelection: true, rootName: 'title' } ) ).to.equal( 'foo' );
- data.set( '', 'title' );
- expect( getData( model, { withoutSelection: true, rootName: 'title' } ) ).to.equal( '' );
- } );
- } );
- describe( 'get()', () => {
- it( 'should get paragraph with text', () => {
- schema.register( 'paragraph', { inheritAllFrom: '$block' } );
- setData( model, '<paragraph>foo</paragraph>' );
- downcastElementToElement( { model: 'paragraph', view: 'p' } )( data.downcastDispatcher );
- expect( data.get() ).to.equal( '<p>foo</p>' );
- } );
- it( 'should get empty paragraph', () => {
- schema.register( 'paragraph', { inheritAllFrom: '$block' } );
- setData( model, '<paragraph></paragraph>' );
- downcastElementToElement( { model: 'paragraph', view: 'p' } )( data.downcastDispatcher );
- expect( data.get() ).to.equal( '<p> </p>' );
- } );
- it( 'should get two paragraphs', () => {
- schema.register( 'paragraph', { inheritAllFrom: '$block' } );
- setData( model, '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
- downcastElementToElement( { model: 'paragraph', view: 'p' } )( data.downcastDispatcher );
- expect( data.get() ).to.equal( '<p>foo</p><p>bar</p>' );
- } );
- 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, '<paragraph>foo<$text bold="true">bar</$text></paragraph>' );
- downcastElementToElement( { model: 'paragraph', view: 'p' } )( data.downcastDispatcher );
- expect( data.get() ).to.equal( '<p>foobar</p>' );
- } );
- it( 'should get paragraphs with bold', () => {
- schema.register( 'paragraph', { inheritAllFrom: '$block' } );
- setData( model, '<paragraph>foo<$text bold="true">bar</$text></paragraph>' );
- downcastElementToElement( { model: 'paragraph', view: 'p' } )( data.downcastDispatcher );
- downcastAttributeToElement( 'bold', { view: 'strong' } )( data.downcastDispatcher );
- expect( data.get() ).to.equal( '<p>foo<strong>bar</strong></p>' );
- } );
- it( 'should get root name as a parameter', () => {
- schema.register( 'paragraph', { inheritAllFrom: '$block' } );
- schema.extend( '$text', { allowIn: '$root' } );
- setData( model, '<paragraph>foo</paragraph>', { rootName: 'main' } );
- setData( model, 'Bar', { rootName: 'title' } );
- downcastElementToElement( { model: 'paragraph', view: 'p' } )( data.downcastDispatcher );
- downcastAttributeToElement( 'bold', { view: 'strong' } )( data.downcastDispatcher );
- 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( () => {
- schema.register( 'paragraph', { inheritAllFrom: '$block' } );
- schema.register( 'div' );
- schema.extend( '$block', { allowIn: 'div' } );
- schema.extend( 'div', { allowIn: '$root' } );
- downcastElementToElement( { model: 'paragraph', view: 'p' } )( data.downcastDispatcher );
- } );
- it( 'should stringify a content of an element', () => {
- const modelElement = parseModel( '<div><paragraph>foo</paragraph></div>', 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>', schema );
- expect( data.stringify( modelDocumentFragment ) ).to.equal( '<p>foo</p><p>bar</p>' );
- } );
- } );
- describe( 'toView()', () => {
- beforeEach( () => {
- schema.register( 'paragraph', { inheritAllFrom: '$block' } );
- schema.register( 'div' );
- schema.extend( '$block', { allowIn: 'div' } );
- schema.extend( 'div', { allowIn: '$root' } );
- downcastElementToElement( { model: 'paragraph', view: 'p' } )( data.downcastDispatcher );
- } );
- it( 'should convert a content of an element', () => {
- const modelElement = parseModel( '<div><paragraph>foo</paragraph></div>', 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 correctly convert document markers #1', () => {
- const modelElement = parseModel( '<div><paragraph>foobar</paragraph></div>', schema );
- const modelRoot = model.document.getRoot();
- downcastMarkerToHighlight( { model: 'marker:a', view: { class: 'a' } } )( data.downcastDispatcher );
- model.change( writer => {
- writer.insert( modelElement, modelRoot, 0 );
- writer.setMarker( 'marker:a', Range.createFromParentsAndOffsets( modelRoot, 0, modelRoot, 1 ), { usingOperation: true } );
- } );
- const viewDocumentFragment = data.toView( modelElement );
- const viewElement = viewDocumentFragment.getChild( 0 );
- expect( stringifyView( viewElement ) ).to.equal( '<p><span class="a">foobar</span></p>' );
- } );
- it( 'should correctly convert document markers #2', () => {
- const modelElement = parseModel( '<div><paragraph>foo</paragraph><paragraph>bar</paragraph></div>', schema );
- const modelRoot = model.document.getRoot();
- downcastMarkerToHighlight( { model: 'marker:a', view: { class: 'a' } } )( data.downcastDispatcher );
- downcastMarkerToHighlight( { model: 'marker:b', view: { class: 'b' } } )( data.downcastDispatcher );
- const modelP1 = modelElement.getChild( 0 );
- const modelP2 = modelElement.getChild( 1 );
- model.change( writer => {
- writer.insert( modelElement, modelRoot, 0 );
- writer.setMarker( 'marker:a', Range.createFromParentsAndOffsets( modelP1, 1, modelP1, 3 ), { usingOperation: true } );
- writer.setMarker( 'marker:b', Range.createFromParentsAndOffsets( modelP2, 0, modelP2, 2 ), { usingOperation: true } );
- } );
- const viewDocumentFragment = data.toView( modelP1 );
- expect( stringifyView( viewDocumentFragment ) ).to.equal( 'f<span class="a">oo</span>' );
- } );
- it( 'should convert a document fragment', () => {
- const modelDocumentFragment = parseModel( '<paragraph>foo</paragraph><paragraph>bar</paragraph>', 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' );
- } );
- } );
- } );
|