/** * @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 DocumentFragment from '../../src/model/documentfragment'; import Element from '../../src/model/element'; import Text from '../../src/model/text'; import TextProxy from '../../src/model/textproxy'; import { default as Position, getTextNodeAtPosition, getNodeAfterPosition, getNodeBeforePosition } from '../../src/model/position'; import Range from '../../src/model/range'; import MarkerOperation from '../../src/model/operation/markeroperation'; import AttributeOperation from '../../src/model/operation/attributeoperation'; import InsertOperation from '../../src/model/operation/insertoperation'; import MoveOperation from '../../src/model/operation/moveoperation'; import RenameOperation from '../../src/model/operation/renameoperation'; import MergeOperation from '../../src/model/operation/mergeoperation'; import SplitOperation from '../../src/model/operation/splitoperation'; import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils'; import LivePosition from '../../src/model/liveposition'; import { expectToThrowCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils'; describe( 'Position', () => { let doc, model, root, otherRoot, p, ul, li1, li2, f, o, z, b, a, r, foz, bar; testUtils.createSinonSandbox(); // root // |- p Before: [ 0 ] After: [ 1 ] // |- ul Before: [ 1 ] After: [ 2 ] // |- li Before: [ 1, 0 ] After: [ 1, 1 ] // | |- f Before: [ 1, 0, 0 ] After: [ 1, 0, 1 ] // | |- o Before: [ 1, 0, 1 ] After: [ 1, 0, 2 ] // | |- z Before: [ 1, 0, 2 ] After: [ 1, 0, 3 ] // |- li Before: [ 1, 1 ] After: [ 1, 2 ] // |- b Before: [ 1, 1, 0 ] After: [ 1, 1, 1 ] // |- a Before: [ 1, 1, 1 ] After: [ 1, 1, 2 ] // |- r Before: [ 1, 1, 2 ] After: [ 1, 1, 3 ] beforeEach( () => { model = new Model(); doc = model.document; root = doc.createRoot(); otherRoot = doc.createRoot( '$root', 'otherRoot' ); foz = new Text( 'foz' ); li1 = new Element( 'li', [], foz ); f = new TextProxy( foz, 0, 1 ); o = new TextProxy( foz, 1, 1 ); z = new TextProxy( foz, 2, 1 ); bar = new Text( 'bar' ); li2 = new Element( 'li', [], bar ); b = new TextProxy( bar, 0, 1 ); a = new TextProxy( bar, 1, 1 ); r = new TextProxy( bar, 2, 1 ); ul = new Element( 'ul', [], [ li1, li2 ] ); p = new Element( 'p' ); root._insertChild( 0, [ p, ul ] ); } ); describe( 'constructor()', () => { it( 'should create a position with path and document', () => { const position = new Position( root, [ 0 ] ); expect( position ).to.have.property( 'path' ).that.deep.equals( [ 0 ] ); expect( position ).to.have.property( 'root' ).that.equals( root ); } ); it( 'should accept DocumentFragment as a root', () => { const frag = new DocumentFragment(); const pos = new Position( frag, [ 0 ] ); expect( pos ).to.have.property( 'root', frag ); } ); it( 'should accept detached Element as a root', () => { const el = new Element( 'p' ); const pos = new Position( el, [ 0 ] ); expect( pos ).to.have.property( 'root', el ); expect( pos.path ).to.deep.equal( [ 0 ] ); } ); it( 'should normalize attached Element as a root', () => { const pos = new Position( li1, [ 0, 2 ] ); expect( pos ).to.have.property( 'root', root ); expect( pos.isEqual( Position._createAt( li1, 0, 2 ) ) ); } ); it( 'should normalize Element from a detached branch as a root', () => { const rootEl = new Element( 'p', null, [ new Element( 'a' ) ] ); const elA = rootEl.getChild( 0 ); const pos = new Position( elA, [ 0 ] ); expect( pos ).to.have.property( 'root', rootEl ); expect( pos.isEqual( Position._createAt( elA, 0 ) ) ); } ); it( 'should throw error if given path is incorrect', () => { expectToThrowCKEditorError( () => { new Position( root, {} ); // eslint-disable-line no-new }, /model-position-path-incorrect-format/, model ); expectToThrowCKEditorError( () => { new Position( root, [] ); // eslint-disable-line no-new }, /model-position-path-incorrect-format/, model ); } ); it( 'should throw error if given root is invalid', () => { expectToThrowCKEditorError( () => { new Position( new Text( 'a' ) ); // eslint-disable-line no-new }, /model-position-root-invalid/ ); expect( () => { new Position(); // eslint-disable-line no-new } ).to.throw(); } ); } ); describe( 'is()', () => { let position; beforeEach( () => { position = new Position( root, [ 0 ] ); } ); it( 'should return true for "position"', () => { expect( position.is( 'position' ) ).to.be.true; expect( position.is( 'model:position' ) ).to.be.true; } ); it( 'should return false for incorrect values', () => { expect( position.is( 'model' ) ).to.be.false; expect( position.is( 'model:node' ) ).to.be.false; expect( position.is( '$text' ) ).to.be.false; expect( position.is( 'element', 'paragraph' ) ).to.be.false; } ); } ); describe( 'static creators', () => { describe( '_createAt()', () => { it( 'should throw if no offset is passed', () => { expectToThrowCKEditorError( () => { Position._createAt( ul ); }, 'model-createpositionat-offset-required', model ); } ); it( 'should create positions from positions', () => { const position = Position._createAt( ul, 0 ); const positionCopy = Position._createAt( position ); expect( positionCopy ).to.have.property( 'path' ).that.deep.equals( [ 1, 0 ] ); expect( positionCopy ).to.have.property( 'root' ).that.equals( position.root ); expect( positionCopy ).to.not.equal( position ); } ); it( 'should create positions from LivePosition', () => { const position = new LivePosition( root, [ 0, 0 ] ); const created = Position._createAt( position ); expect( created.isEqual( position ) ).to.be.true; expect( created ).to.not.be.equal( position ); expect( created ).to.be.instanceof( Position ); expect( created ).to.not.be.instanceof( LivePosition ); } ); it( 'should create positions from node and offset', () => { expect( Position._createAt( root, 0 ) ).to.have.property( 'path' ).that.deep.equals( [ 0 ] ); expect( Position._createAt( root, 1 ) ).to.have.property( 'path' ).that.deep.equals( [ 1 ] ); expect( Position._createAt( root, 2 ) ).to.have.property( 'path' ).that.deep.equals( [ 2 ] ); expect( Position._createAt( p, 0 ) ).to.have.property( 'path' ).that.deep.equals( [ 0, 0 ] ); expect( Position._createAt( ul, 0 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0 ] ); expect( Position._createAt( ul, 1 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1 ] ); expect( Position._createAt( ul, 2 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 2 ] ); expect( Position._createAt( li1, 0 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 0 ] ); expect( Position._createAt( li1, 1 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 1 ] ); expect( Position._createAt( li1, 2 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 2 ] ); expect( Position._createAt( li1, 3 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 3 ] ); } ); it( 'should create positions from node and flag', () => { expect( Position._createAt( root, 'end' ) ).to.have.property( 'path' ).that.deep.equals( [ 2 ] ); expect( Position._createAt( p, 'before' ) ).to.have.property( 'path' ).that.deep.equals( [ 0 ] ); expect( Position._createAt( a, 'before' ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1, 1 ] ); expect( Position._createAt( p, 'after' ) ).to.have.property( 'path' ).that.deep.equals( [ 1 ] ); expect( Position._createAt( a, 'after' ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1, 2 ] ); expect( Position._createAt( ul, 'end' ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 2 ] ); } ); it( 'should set stickiness (if not cloning other position)', () => { expect( Position._createAt( root, 'end', 'toPrevious' ) ).to.have.property( 'stickiness' ).that.equals( 'toPrevious' ); } ); it( 'throws when parent is not an element', () => { expectToThrowCKEditorError( () => { Position._createAt( b, 0 ); }, /^model-position-parent-incorrect/, model ); } ); it( 'works with a doc frag', () => { const frag = new DocumentFragment(); expect( Position._createAt( frag, 0 ) ).to.have.property( 'root', frag ); } ); } ); describe( '_createBefore()', () => { it( 'should create positions before elements', () => { expect( Position._createBefore( p ) ).to.have.property( 'path' ).that.deep.equals( [ 0 ] ); expect( Position._createBefore( ul ) ).to.have.property( 'path' ).that.deep.equals( [ 1 ] ); expect( Position._createBefore( li1 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0 ] ); expect( Position._createBefore( f ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 0 ] ); expect( Position._createBefore( o ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 1 ] ); expect( Position._createBefore( z ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 2 ] ); expect( Position._createBefore( li2 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1 ] ); expect( Position._createBefore( b ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1, 0 ] ); expect( Position._createBefore( a ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1, 1 ] ); expect( Position._createBefore( r ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1, 2 ] ); } ); it( 'should set stickiness', () => { expect( Position._createBefore( p, 'toPrevious' ) ).to.have.property( 'stickiness' ).that.equals( 'toPrevious' ); } ); it( 'should throw error if one try to create positions before root', () => { expectToThrowCKEditorError( () => { Position._createBefore( root ); }, /model-position-before-root/, model ); } ); } ); describe( '_createAfter()', () => { it( 'should create positions after elements', () => { expect( Position._createAfter( p ) ).to.have.property( 'path' ).that.deep.equals( [ 1 ] ); expect( Position._createAfter( ul ) ).to.have.property( 'path' ).that.deep.equals( [ 2 ] ); expect( Position._createAfter( li1 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1 ] ); expect( Position._createAfter( f ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 1 ] ); expect( Position._createAfter( o ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 2 ] ); expect( Position._createAfter( z ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 3 ] ); expect( Position._createAfter( li2 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 2 ] ); expect( Position._createAfter( b ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1, 1 ] ); expect( Position._createAfter( a ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1, 2 ] ); expect( Position._createAfter( r ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1, 3 ] ); } ); it( 'should set stickiness', () => { expect( Position._createAfter( p, 'toPrevious' ) ).to.have.property( 'stickiness' ).that.equals( 'toPrevious' ); } ); it( 'should throw error if one try to make positions after root', () => { expectToThrowCKEditorError( () => { Position._createAfter( root ); }, /model-position-after-root/, model ); } ); } ); } ); describe( '#parent', () => { it( 'should have parent', () => { expect( new Position( root, [ 0 ] ) ).to.have.property( 'parent' ).that.equals( root ); expect( new Position( root, [ 1 ] ) ).to.have.property( 'parent' ).that.equals( root ); expect( new Position( root, [ 2 ] ) ).to.have.property( 'parent' ).that.equals( root ); expect( new Position( root, [ 0, 0 ] ) ).to.have.property( 'parent' ).that.equals( p ); expect( new Position( root, [ 1, 0 ] ) ).to.have.property( 'parent' ).that.equals( ul ); expect( new Position( root, [ 1, 1 ] ) ).to.have.property( 'parent' ).that.equals( ul ); expect( new Position( root, [ 1, 2 ] ) ).to.have.property( 'parent' ).that.equals( ul ); expect( new Position( root, [ 1, 0, 0 ] ) ).to.have.property( 'parent' ).that.equals( li1 ); expect( new Position( root, [ 1, 0, 1 ] ) ).to.have.property( 'parent' ).that.equals( li1 ); expect( new Position( root, [ 1, 0, 2 ] ) ).to.have.property( 'parent' ).that.equals( li1 ); expect( new Position( root, [ 1, 0, 3 ] ) ).to.have.property( 'parent' ).that.equals( li1 ); } ); it( 'should work with positions rooted in document fragment', () => { const docFrag = new DocumentFragment(); expect( new Position( docFrag, [ 0 ] ) ).to.have.property( 'parent' ).that.equals( docFrag ); } ); it( 'should throw when path out of bounds', () => { const position = new Position( root, [ 0, 0 ] ); expect( position ).to.have.property( 'parent' ).that.equals( p ); root._removeChildren( 0, 2 ); expectToThrowCKEditorError( () => { position.parent; }, 'model-position-path-incorrect', position, { position } ); } ); it( 'should throw when based on a path, the parent would be a text node', () => { // 1,0,0 points at: