| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- /**
- * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
- import TextProxy from '../../src/view/textproxy';
- import Text from '../../src/view/text';
- import ContainerElement from '../../src/view/containerelement';
- import DocumentFragment from '../../src/view/documentfragment';
- import RootEditableElement from '../../src/view/rooteditableelement';
- import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
- import createDocumentMock from '../../tests/view/_utils/createdocumentmock';
- describe( 'TextProxy', () => {
- let text, parent, wrapper, textProxy;
- beforeEach( () => {
- text = new Text( 'abcdefgh' );
- parent = new ContainerElement( 'p', [], [ text ] );
- wrapper = new ContainerElement( 'div', [], parent );
- textProxy = new TextProxy( text, 2, 3 );
- } );
- describe( 'constructor()', () => {
- it( 'should create TextProxy instance with specified properties', () => {
- expect( textProxy ).to.have.property( 'parent' ).to.equal( parent );
- expect( textProxy ).to.have.property( 'data' ).to.equal( 'cde' );
- expect( textProxy ).to.have.property( 'textNode' ).to.equal( text );
- expect( textProxy ).to.have.property( 'offsetInText' ).to.equal( 2 );
- } );
- it( 'should have isPartial property', () => {
- const startTextProxy = new TextProxy( text, 0, 4 );
- const fullTextProxy = new TextProxy( text, 0, 8 );
- expect( textProxy.isPartial ).to.be.true;
- expect( startTextProxy.isPartial ).to.be.true;
- expect( fullTextProxy.isPartial ).to.be.false;
- } );
- it( 'should throw if wrong offsetInText is passed', () => {
- expect( () => {
- new TextProxy( text, -1, 2 ); // eslint-disable-line no-new
- } ).to.throw( CKEditorError, /view-textproxy-wrong-offsetintext/ );
- expect( () => {
- new TextProxy( text, 9, 1 ); // eslint-disable-line no-new
- } ).to.throw( CKEditorError, /view-textproxy-wrong-offsetintext/ );
- } );
- it( 'should throw if wrong length is passed', () => {
- expect( () => {
- new TextProxy( text, 2, -1 ); // eslint-disable-line no-new
- } ).to.throw( CKEditorError, /view-textproxy-wrong-length/ );
- expect( () => {
- new TextProxy( text, 2, 9 ); // eslint-disable-line no-new
- } ).to.throw( CKEditorError, /view-textproxy-wrong-length/ );
- } );
- } );
- describe( 'is', () => {
- it( 'should return true for textProxy', () => {
- expect( textProxy.is( 'textProxy' ) ).to.be.true;
- } );
- it( 'should return false for other accept values', () => {
- expect( textProxy.is( 'text' ) ).to.be.false;
- expect( textProxy.is( 'element' ) ).to.be.false;
- expect( textProxy.is( 'containerElement' ) ).to.be.false;
- expect( textProxy.is( 'attributeElement' ) ).to.be.false;
- expect( textProxy.is( 'uiElement' ) ).to.be.false;
- expect( textProxy.is( 'emptyElement' ) ).to.be.false;
- expect( textProxy.is( 'rootElement' ) ).to.be.false;
- expect( textProxy.is( 'documentFragment' ) ).to.be.false;
- } );
- } );
- describe( 'offsetSize', () => {
- it( 'should be equal to the number of characters in text proxy', () => {
- expect( textProxy.offsetSize ).to.equal( 3 );
- } );
- } );
- describe( 'getDocument', () => {
- it( 'should return null if any parent has not set Document', () => {
- expect( textProxy.document ).to.be.null;
- } );
- it( 'should return Document attached to the parent element', () => {
- const docMock = createDocumentMock();
- const root = new RootEditableElement( 'div' );
- root._document = docMock;
- wrapper.parent = root;
- expect( textProxy.document ).to.equal( docMock );
- } );
- it( 'should return null if element is inside DocumentFragment', () => {
- new DocumentFragment( [ wrapper ] ); // eslint-disable-line no-new
- expect( textProxy.document ).to.be.null;
- } );
- } );
- describe( 'getRoot', () => {
- it( 'should return root element', () => {
- const root = new RootEditableElement( 'div' );
- root._document = createDocumentMock();
- wrapper.parent = root;
- expect( textProxy.root ).to.equal( root );
- } );
- } );
- describe( 'getAncestors', () => {
- it( 'should return array of ancestors', () => {
- const result = textProxy.getAncestors();
- expect( result ).to.be.an( 'array' );
- expect( result ).to.length( 2 );
- expect( result[ 0 ] ).to.equal( wrapper );
- expect( result[ 1 ] ).to.equal( parent );
- } );
- it( 'should return array of ancestors starting from parent `parentFirst`', () => {
- const result = textProxy.getAncestors( { parentFirst: true } );
- expect( result.length ).to.equal( 2 );
- expect( result[ 0 ] ).to.equal( parent );
- expect( result[ 1 ] ).to.equal( wrapper );
- } );
- it( 'should return array including node itself `includeSelf`', () => {
- const result = textProxy.getAncestors( { includeSelf: true } );
- expect( result ).to.be.an( 'array' );
- expect( result ).to.length( 3 );
- expect( result[ 0 ] ).to.equal( wrapper );
- expect( result[ 1 ] ).to.equal( parent );
- expect( result[ 2 ] ).to.equal( text );
- } );
- it( 'should return array of ancestors including node itself `includeSelf` starting from parent `parentFirst`', () => {
- const result = textProxy.getAncestors( { includeSelf: true, parentFirst: true } );
- expect( result.length ).to.equal( 3 );
- expect( result[ 0 ] ).to.equal( text );
- expect( result[ 1 ] ).to.equal( parent );
- expect( result[ 2 ] ).to.equal( wrapper );
- } );
- } );
- } );
|