| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360 |
- /**
- * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
- /* bender-tags: view, browser-only */
- 'use strict';
- import Position from '/ckeditor5/engine/view/position.js';
- import Node from '/ckeditor5/engine/view/node.js';
- import Element from '/ckeditor5/engine/view/element.js';
- import EditableElement from '/ckeditor5/engine/view/editableelement.js';
- import Document from '/ckeditor5/engine/view/document.js';
- import Text from '/ckeditor5/engine/view/text.js';
- import TextProxy from '/ckeditor5/engine/view/textproxy.js';
- import CKEditorError from '/ckeditor5/utils/ckeditorerror.js';
- import { parse } from '/tests/engine/_utils/view.js';
- describe( 'Position', () => {
- const parentMock = {};
- describe( 'constructor', () => {
- it( 'should create element without attributes', () => {
- const elem = new Position( parentMock, 5 );
- expect( elem ).to.have.property( 'parent' ).that.equals( parentMock );
- expect( elem ).to.have.property( 'offset' ).that.equals( 5 );
- } );
- } );
- describe( 'nodeBefore', () => {
- it( 'should equal to node that is before position', () => {
- const b1 = new Element( 'b' );
- const el = new Element( 'p', null, [ b1 ] );
- const position = new Position( el, 1 );
- expect( position.nodeBefore ).to.equal( b1 );
- } );
- it( 'should equal null if there is no node before', () => {
- const b1 = new Element( 'b' );
- const el = new Element( 'p', null, [ b1 ] );
- const position = new Position( el, 0 );
- expect( position.nodeBefore ).to.be.null;
- } );
- it( 'should equal null if position is located inside text node', () => {
- const text = new Text( 'foobar' );
- const position = new Position( text, 3 );
- expect( position.nodeBefore ).to.be.null;
- } );
- } );
- describe( 'nodeAfter', () => {
- it( 'should equal to node that is after position', () => {
- const b1 = new Element( 'b' );
- const el = new Element( 'p', null, [ b1 ] );
- const position = new Position( el, 0 );
- expect( position.nodeAfter ).to.equal( b1 );
- } );
- it( 'should equal null if there is no node before', () => {
- const b1 = new Element( 'b' );
- const el = new Element( 'p', null, [ b1 ] );
- const position = new Position( el, 1 );
- expect( position.nodeAfter ).to.be.null;
- } );
- it( 'should equal null if position is located inside text node', () => {
- const text = new Text( 'foobar' );
- const position = new Position( text, 3 );
- expect( position.nodeAfter ).to.be.null;
- } );
- } );
- describe( 'getShiftedBy', () => {
- it( 'returns new instance with shifted offset', () => {
- const position = new Position( parentMock, 10 );
- const shifted = position.getShiftedBy( 12 );
- expect( shifted.offset ).to.equal( 22 );
- } );
- it( 'accepts negative values', () => {
- const position = new Position( parentMock, 10 );
- const shifted = position.getShiftedBy( -5 );
- expect( shifted.offset ).to.equal( 5 );
- } );
- it( 'prevents offset to be a negative value', () => {
- const position = new Position( parentMock, 10 );
- const shifted = position.getShiftedBy( -20 );
- expect( shifted.offset ).to.equal( 0 );
- } );
- } );
- describe( 'createFromPosition', () => {
- it( 'creates new Position with same parent and offset', () => {
- const offset = 50;
- const position = new Position( parentMock, offset );
- const newPosition = Position.createFromPosition( position );
- expect( position ).to.not.equal( newPosition );
- expect( position.offset ).to.equal( offset );
- expect( position.parent ).to.equal( parentMock );
- } );
- } );
- describe( 'isEqual', () => {
- it( 'should return true for same object', () => {
- const position = new Position( {}, 12 );
- expect( position.isEqual( position ) ).to.be.true;
- } );
- it( 'should return true for positions with same parent and offset', () => {
- const parentMock = {};
- const position1 = new Position( parentMock, 12 );
- const position2 = new Position( parentMock, 12 );
- expect( position1.isEqual( position2 ) ).to.be.true;
- } );
- it( 'should return false for positions with different parents', () => {
- const position1 = new Position( {}, 12 );
- const position2 = new Position( {}, 12 );
- expect( position1.isEqual( position2 ) ).to.be.false;
- } );
- it( 'should return false for positions with different positions', () => {
- const parentMock = {};
- const position1 = new Position( parentMock, 12 );
- const position2 = new Position( parentMock, 2 );
- expect( position1.isEqual( position2 ) ).to.be.false;
- } );
- } );
- describe( 'isBefore', () => {
- it( 'should return false for same positions', () => {
- const node = new Node();
- const position1 = new Position( node, 10 );
- const position2 = new Position( node, 10 );
- expect( position1.isBefore( position1 ) ).to.be.false;
- expect( position1.isBefore( position2 ) ).to.be.false;
- expect( position2.isBefore( position1 ) ).to.be.false;
- } );
- it( 'should return false if no common ancestor is found', () => {
- const t1 = new Text( 'foo' );
- const t2 = new Text( 'bar' );
- const e1 = new Element( 'p', null, [ t1 ] );
- const e2 = new Element( 'p', null, [ t2 ] );
- const position1 = new Position( e1, 0 );
- const position2 = new Position( e2, 1 );
- expect( position1.isBefore( position2 ) );
- expect( position2.isBefore( position1 ) );
- } );
- it( 'should return true if position is before in same node', () => {
- const node = new Node();
- const p1 = new Position( node, 10 );
- const p2 = new Position( node, 5 );
- expect( p2.isBefore( p1 ) ).to.be.true;
- expect( p1.isBefore( p2 ) ).to.be.false;
- } );
- it( 'should compare positions that have common parent', () => {
- const t1 = new Text( 'foo' );
- const t2 = new Text( 'bar' );
- const root = new Element( 'p', null, [ t1, t2 ] );
- const position1 = new Position( t1, 2 );
- const position2 = new Position( t2, 0 );
- const position3 = new Position( root, 0 );
- const position4 = new Position( root, 2 );
- const position5 = new Position( t1, 0 );
- const position6 = new Position( root, 1 );
- expect( position1.isBefore( position2 ) ).to.be.true;
- expect( position2.isBefore( position1 ) ).to.be.false;
- expect( position3.isBefore( position1 ) ).to.be.true;
- expect( position3.isBefore( position2 ) ).to.be.true;
- expect( position1.isBefore( position3 ) ).to.be.false;
- expect( position2.isBefore( position3 ) ).to.be.false;
- expect( position4.isBefore( position1 ) ).to.be.false;
- expect( position4.isBefore( position3 ) ).to.be.false;
- expect( position3.isBefore( position4 ) ).to.be.true;
- expect( position3.isBefore( position5 ) ).to.be.true;
- expect( position6.isBefore( position2 ) ).to.be.true;
- expect( position1.isBefore( position6 ) ).to.be.true;
- } );
- } );
- describe( 'isAfter', () => {
- it( 'should return false for same positions', () => {
- const node = new Node();
- const position1 = new Position( node, 10 );
- const position2 = new Position( node, 10 );
- expect( position1.isAfter( position1 ) ).to.be.false;
- expect( position1.isAfter( position2 ) ).to.be.false;
- expect( position2.isAfter( position1 ) ).to.be.false;
- } );
- it( 'should return false if no common ancestor is found', () => {
- const t1 = new Text( 'foo' );
- const t2 = new Text( 'bar' );
- const e1 = new Element( 'p', null, [ t1 ] );
- const e2 = new Element( 'p', null, [ t2 ] );
- const position1 = new Position( e1, 0 );
- const position2 = new Position( e2, 1 );
- expect( position1.isAfter( position2 ) );
- expect( position2.isAfter( position1 ) );
- } );
- it( 'should return true if position is after in same node', () => {
- const node = new Node();
- const p1 = new Position( node, 10 );
- const p2 = new Position( node, 5 );
- expect( p2.isAfter( p1 ) ).to.be.false;
- expect( p1.isAfter( p2 ) ).to.be.true;
- } );
- it( 'should compare positions that have common parent', () => {
- const t1 = new Text( 'foo' );
- const t2 = new Text( 'bar' );
- const root = new Element( 'p', null, [ t1, t2 ] );
- const position1 = new Position( t1, 2 );
- const position2 = new Position( t2, 0 );
- const position3 = new Position( root, 0 );
- const position4 = new Position( root, 2 );
- const position5 = new Position( t1, 0 );
- const position6 = new Position( root, 1 );
- expect( position1.isAfter( position2 ) ).to.be.false;
- expect( position2.isAfter( position1 ) ).to.be.true;
- expect( position3.isAfter( position1 ) ).to.be.false;
- expect( position3.isAfter( position2 ) ).to.be.false;
- expect( position1.isAfter( position3 ) ).to.be.true;
- expect( position2.isAfter( position3 ) ).to.be.true;
- expect( position4.isAfter( position1 ) ).to.be.true;
- expect( position4.isAfter( position3 ) ).to.be.true;
- expect( position3.isAfter( position4 ) ).to.be.false;
- expect( position5.isAfter( position3 ) ).to.be.true;
- expect( position2.isAfter( position6 ) ).to.be.true;
- } );
- } );
- describe( 'compareWith', () => {
- it( 'should return same if positions are same', () => {
- const root = new Node();
- const position = new Position( root, 0 );
- const compared = new Position( root, 0 );
- expect( position.compareWith( compared ) ).to.equal( 'same' );
- } );
- it( 'should return before if the position is before compared one', () => {
- const root = new Node();
- const position = new Position( root, 0 );
- const compared = new Position( root, 1 );
- expect( position.compareWith( compared ) ).to.equal( 'before' );
- } );
- it( 'should return after if the position is after compared one', () => {
- const root = new Node();
- const position = new Position( root, 4 );
- const compared = new Position( root, 1 );
- expect( position.compareWith( compared ) ).to.equal( 'after' );
- } );
- it( 'should return different if positions are in different roots', () => {
- const root1 = new Node();
- const root2 = new Node();
- const position = new Position( root1, 4 );
- const compared = new Position( root2, 1 );
- expect( position.compareWith( compared ) ).to.equal( 'different' );
- } );
- } );
- describe( 'createBefore', () => {
- it( 'should throw error if one try to create positions before root', () => {
- expect( () => {
- Position.createBefore( parse( '<p></p>' ) );
- } ).to.throw( CKEditorError, /position-before-root/ );
- } );
- it( 'should create positions before `Node`', () => {
- const { selection } = parse( '<p>[]<b></b></p>' );
- const position = selection.getFirstPosition();
- const nodeAfter = position.nodeAfter;
- expect( Position.createBefore( nodeAfter ).isEqual( position ) ).to.be.true;
- } );
- it( 'should create positions before `TextProxy`', () => {
- const text = new Text( 'abc' );
- const textProxy = new TextProxy( text, 1, 1 );
- const position = new Position( text, 1 );
- expect( Position.createBefore( textProxy ) ).deep.equal( position );
- } );
- } );
- describe( 'createAfter', () => {
- it( 'should throw error if one try to create positions after root', () => {
- expect( () => {
- Position.createAfter( parse( '<p></p>' ) );
- } ).to.throw( CKEditorError, /position-after-root/ );
- } );
- it( 'should create positions after `Node`', () => {
- const { selection } = parse( '<p><b></b>[]</p>' );
- const position = selection.getFirstPosition();
- const nodeBefore = position.nodeBefore;
- expect( Position.createAfter( nodeBefore ).isEqual( position ) ).to.be.true;
- } );
- it( 'should create positions after `TextProxy`', () => {
- const text = new Text( 'abcd' );
- const textProxy = new TextProxy( text, 1, 2 );
- const position = new Position( text, 3 );
- expect( Position.createAfter( textProxy ) ).deep.equal( position );
- } );
- } );
- describe( 'getEditableElement', () => {
- it( 'should return null if position is not inside EditableElement', () => {
- const position = new Position( new Element( 'p' ), 0 );
- expect( position.getEditableElement() ).to.be.null;
- } );
- it( 'should return EditableElement when position is placed inside', () => {
- const document = new Document();
- const p = new Element( 'p' );
- const editable = new EditableElement( document, 'div', null, p );
- const position = new Position( p, 0 );
- expect( position.getEditableElement() ).to.equal( editable );
- } );
- } );
- } );
|