/**
* @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.
*/
import Model from '../../../src/model/model';
import Document from '../../../src/model/document';
import RootElement from '../../../src/model/rootelement';
import Batch from '../../../src/model/batch';
import Delta from '../../../src/model/delta/delta';
import NoOperation from '../../../src/model/operation/nooperation';
import deltaTransform from '../../../src/model/delta/transform';
import Range from '../../../src/model/range';
import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
import count from '@ckeditor/ckeditor5-utils/src/count';
import { jsonParseStringify } from '../../../tests/model/_utils/utils';
import { setData, getData } from '../../../src/dev-utils/model';
describe( 'Document', () => {
let model, doc;
beforeEach( () => {
model = new Model();
doc = new Document( model );
// Normally Model is the one who creates Document instance and keeps it as reference.
// We have to be sure that Model uses the right Document instance.
model.document = doc;
} );
describe( 'constructor()', () => {
it( 'should create Document with no data, empty graveyard and selection set to default range', () => {
const doc = new Document( model );
expect( doc ).to.have.property( 'model' ).to.equal( model );
expect( doc ).to.have.property( 'roots' ).that.is.instanceof( Map );
expect( doc.roots.size ).to.equal( 1 );
expect( doc.graveyard ).to.be.instanceof( RootElement );
expect( doc.graveyard.maxOffset ).to.equal( 0 );
expect( count( doc.selection.getRanges() ) ).to.equal( 1 );
} );
} );
describe( 'getRootNames()', () => {
it( 'should return empty iterator if no roots exist', () => {
expect( count( doc.getRootNames() ) ).to.equal( 0 );
} );
it( 'should return an iterator of all roots without the graveyard', () => {
doc.createRoot( '$root', 'a' );
doc.createRoot( '$root', 'b' );
expect( Array.from( doc.getRootNames() ) ).to.deep.equal( [ 'a', 'b' ] );
} );
} );
describe( 'createRoot()', () => {
it( 'should create a new RootElement with default element and root names, add it to roots map and return it', () => {
const root = doc.createRoot();
expect( doc.roots.size ).to.equal( 2 );
expect( root ).to.be.instanceof( RootElement );
expect( root.maxOffset ).to.equal( 0 );
expect( root ).to.have.property( 'name', '$root' );
expect( root ).to.have.property( 'rootName', 'main' );
} );
it( 'should create a new RootElement with custom element and root names, add it to roots map and return it', () => {
const root = doc.createRoot( 'customElementName', 'customRootName' );
expect( doc.roots.size ).to.equal( 2 );
expect( root ).to.be.instanceof( RootElement );
expect( root.maxOffset ).to.equal( 0 );
expect( root ).to.have.property( 'name', 'customElementName' );
expect( root ).to.have.property( 'rootName', 'customRootName' );
} );
it( 'should throw an error when trying to create a second root with the same name', () => {
doc.createRoot( '$root', 'rootName' );
expect(
() => {
doc.createRoot( '$root', 'rootName' );
}
).to.throw( CKEditorError, /model-document-createRoot-name-exists/ );
} );
} );
describe( 'getRoot()', () => {
it( 'should return a RootElement previously created with given name', () => {
const newRoot = doc.createRoot();
const getRoot = doc.getRoot();
expect( getRoot ).to.equal( newRoot );
} );
it( 'should throw an error when trying to get non-existent root', () => {
expect(
() => {
doc.getRoot( 'root' );
}
).to.throw( CKEditorError, /model-document-getRoot-root-not-exist/ );
} );
} );
describe( 'hasRoot()', () => {
it( 'should return true when Document has RootElement with given name', () => {
doc.createRoot();
expect( doc.hasRoot( 'main' ) ).to.be.true;
} );
it( 'should return false when Document does not have RootElement with given name', () => {
expect( doc.hasRoot( 'noroot' ) ).to.be.false;
} );
} );
describe.skip( 'applyOperation()', () => {
it( 'should increase document version, execute operation and fire event with proper data ' +
'when operation is a document operation', () => {
const changeCallback = sinon.spy();
const type = 't';
const data = { data: 'x' };
const batch = new Batch();
const delta = new Delta();
delta.type = 'type';
const operation = {
type,
baseVersion: 0,
isDocumentOperation: true,
_execute: sinon.stub().returns( data )
};
delta.addOperation( operation );
batch.addDelta( delta );
doc.on( 'change', changeCallback );
model.applyOperation( operation );
expect( doc.version ).to.equal( 1 );
expect( doc.history._deltas.length ).to.equal( 1 );
sinon.assert.calledOnce( operation._execute );
sinon.assert.calledOnce( changeCallback );
expect( changeCallback.args[ 0 ][ 1 ] ).to.equal( type );
expect( changeCallback.args[ 0 ][ 2 ] ).to.equal( data );
expect( changeCallback.args[ 0 ][ 3 ] ).to.deep.equal( batch );
expect( changeCallback.args[ 0 ][ 4 ] ).to.equal( delta.type );
} );
it( 'should execute operation, not fire event and not increase document version ' +
'when operation is not a document operation', () => {
const changeCallback = sinon.spy();
const type = 't';
const data = { data: 'x' };
const batch = new Batch();
const delta = new Delta();
delta.type = 'type';
const operation = {
type,
baseVersion: 0,
isDocumentOperation: false,
_execute: sinon.stub().returns( data )
};
delta.addOperation( operation );
batch.addDelta( delta );
doc.on( 'change', changeCallback );
model.applyOperation( operation );
expect( doc.version ).to.equal( 0 );
expect( doc.history._deltas.length ).to.equal( 0 );
sinon.assert.calledOnce( operation._execute );
sinon.assert.notCalled( changeCallback );
} );
it( 'should throw an error on the operation base version and the document version is different', () => {
const operation = {
baseVersion: 1
};
expect(
() => {
model.applyOperation( operation );
}
).to.throw( CKEditorError, /^model-document-applyOperation-wrong-version/ );
} );
} );
describe( 'selection', () => {
it( 'should get updated attributes whenever attribute operation is applied', () => {
sinon.spy( doc.selection, '_updateAttributes' );
doc.fire( 'change', 'addAttribute' );
expect( doc.selection._updateAttributes.called ).to.be.true;
} );
it( 'should throw if one of ranges starts or ends inside surrogate pair', () => {
const root = doc.createRoot();
root.appendChildren( '\uD83D\uDCA9' );
expect( () => {
doc.selection.setRanges( [ Range.createFromParentsAndOffsets( root, 0, root, 1 ) ] );
} ).to.throw( CKEditorError, /document-selection-wrong-position/ );
expect( () => {
doc.selection.setRanges( [ Range.createFromParentsAndOffsets( root, 1, root, 2 ) ] );
} ).to.throw( CKEditorError, /document-selection-wrong-position/ );
} );
it( 'should throw if one of ranges starts or ends between base character and combining mark', () => {
const root = doc.createRoot();
root.appendChildren( 'foo̻̐ͩbar' );
expect( () => {
doc.selection.setRanges( [ Range.createFromParentsAndOffsets( root, 3, root, 9 ) ] );
} ).to.throw( CKEditorError, /document-selection-wrong-position/ );
expect( () => {
doc.selection.setRanges( [ Range.createFromParentsAndOffsets( root, 4, root, 9 ) ] );
} ).to.throw( CKEditorError, /document-selection-wrong-position/ );
expect( () => {
doc.selection.setRanges( [ Range.createFromParentsAndOffsets( root, 5, root, 9 ) ] );
} ).to.throw( CKEditorError, /document-selection-wrong-position/ );
expect( () => {
doc.selection.setRanges( [ Range.createFromParentsAndOffsets( root, 1, root, 3 ) ] );
} ).to.throw( CKEditorError, /document-selection-wrong-position/ );
expect( () => {
doc.selection.setRanges( [ Range.createFromParentsAndOffsets( root, 1, root, 4 ) ] );
} ).to.throw( CKEditorError, /document-selection-wrong-position/ );
expect( () => {
doc.selection.setRanges( [ Range.createFromParentsAndOffsets( root, 1, root, 5 ) ] );
} ).to.throw( CKEditorError, /document-selection-wrong-position/ );
} );
} );
describe( 'getNearestSelectionRange()', () => {
let selection;
beforeEach( () => {
model.schema.registerItem( 'paragraph', '$block' );
model.schema.registerItem( 'emptyBlock' );
model.schema.allow( { name: 'emptyBlock', inside: '$root' } );
model.schema.registerItem( 'widget' );
model.schema.allow( { name: 'widget', inside: '$root' } );
model.schema.objects.add( 'widget' );
model.schema.registerItem( 'blockWidget', '$block' );
model.schema.allow( { name: 'blockWidget', inside: '$root' } );
model.schema.objects.add( 'blockWidget' );
doc.createRoot();
selection = doc.selection;
} );
test(
'should return collapsed range if text node can be placed at that position - both',
'[]',
'both',
'[]'
);
test(
'should return collapsed range if text node can be placed at that position - forward',
'[]',
'forward',
'[]'
);
test(
'should return collapsed range if text node can be placed at that position - backward',
'[]',
'backward',
'[]'
);
test( 'should return null in empty document - both', '', 'both', null );
test( 'should return null in empty document - backward', '', 'backward', null );
test( 'should return null in empty document - forward', '', 'forward', null );
test(
'should find range before when searching both ways',
'[]',
'both',
'[]'
);
test(
'should find range before when searching backward',
'[]',
'backward',
'[]'
);
test(
'should find range after when searching forward',
'[]',
'forward',
'[]'
);
test(
'should find range after when searching both ways when it is closer',
'[]',
'both',
'[]'
);
test(
'should find range before when searching both ways when it is closer',
'[]',
'both',
'[]'
);
test(
'should return null if there is no valid range',
'[]',
'both',
null
);
test(
'should return null if there is no valid range in given direction - backward',
'[]',
'backward',
null
);
test(
'should return null if there is no valid range in given direction - forward',
'[]',
'forward',
null
);
test(
'should move forward when placed at root start',
'[]',
'both',
'[]'
);
test(
'should move backward when placed at root end',
'[]',
'both',
'[]'
);
describe( 'in case of objects which do not allow text inside', () => {
test(
'should select nearest object (o[]o) - both',
'[]',
'both',
'[]'
);
test(
'should select nearest object (o[]o) - forward',
'[]',
'forward',
'[]'
);
test(
'should select nearest object (o[]o) - backward',
'[]',
'both',
'[]'
);
test(
'should select nearest object (p[]o) - forward',
'[]',
'forward',
'[]'
);
test(
'should select nearest object (o[]p) - both',
'[]',
'both',
'[]'
);
test(
'should select nearest object (o[]p) - backward',
'[]',
'backward',
'[]'
);
test(
'should select nearest object ([]o) - both',
'[]',
'both',
'[]'
);
test(
'should select nearest object ([]o) - forward',
'[]',
'forward',
'[]'
);
test(
'should select nearest object (o[]) - both',
'[]',
'both',
'[]'
);
test(
'should select nearest object (o[]) - backward',
'[]',
'both',
'[]'
);
} );
describe( 'in case of objects which allow text inside', () => {
test(
'should select nearest object which allows text (o[]o) - both',
'[]',
'both',
'[]'
);
test(
'should select nearest object (o[]p) - both',
'[]',
'both',
'[]'
);
test(
'should select nearest object which allows text ([]o) - both',
'[]',
'both',
'[]'
);
} );
function test( testName, data, direction, expected ) {
it( testName, () => {
setData( model, data );
const range = doc.getNearestSelectionRange( selection.anchor, direction );
if ( expected === null ) {
expect( range ).to.be.null;
} else {
selection.setRanges( [ range ] );
expect( getData( model ) ).to.equal( expected );
}
} );
}
} );
describe( 'transformDeltas', () => {
it( 'should use deltaTransform.transformDeltaSets', () => {
sinon.spy( deltaTransform, 'transformDeltaSets' );
// Prepare some empty-ish deltas so the transformation won't throw an error.
const deltasA = [ new Delta() ];
deltasA[ 0 ].addOperation( new NoOperation( 0 ) );
const deltasB = [ new Delta() ];
deltasB[ 0 ].addOperation( new NoOperation( 0 ) );
doc.transformDeltas( deltasA, deltasB );
expect( deltaTransform.transformDeltaSets.calledOnce ).to.be.true;
expect( deltaTransform.transformDeltaSets.calledWith( deltasA, deltasB, null ) ).to.be.true;
deltaTransform.transformDeltaSets.restore();
} );
it( 'should pass itself to transformDeltaSets if useContext was set to true', () => {
sinon.spy( deltaTransform, 'transformDeltaSets' );
// Prepare some empty-ish deltas so the transformation won't throw an error.
const deltasA = [ new Delta() ];
deltasA[ 0 ].addOperation( new NoOperation( 0 ) );
const deltasB = [ new Delta() ];
deltasB[ 0 ].addOperation( new NoOperation( 0 ) );
doc.transformDeltas( deltasA, deltasB, true );
expect( deltaTransform.transformDeltaSets.calledOnce ).to.be.true;
expect( deltaTransform.transformDeltaSets.calledWith( deltasA, deltasB, doc ) ).to.be.true;
deltaTransform.transformDeltaSets.restore();
} );
} );
describe( '_getDefaultRoot()', () => {
it( 'should return graveyard root if there are no other roots in the document', () => {
expect( doc._getDefaultRoot() ).to.equal( doc.graveyard );
} );
it( 'should return the first root added to the document', () => {
const rootA = doc.createRoot( '$root', 'rootA' );
doc.createRoot( '$root', 'rootB' );
doc.createRoot( '$root', 'rootC' );
expect( doc._getDefaultRoot() ).to.equal( rootA );
} );
} );
// @TODO: What for is this test?
it.skip( 'should be correctly converted to json', () => {
expect( jsonParseStringify( doc ).selection ).to.equal( '[engine.model.DocumentSelection]' );
} );
} );