| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304 |
- /**
- * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
- import Element from 'ckeditor5-engine/src/view/element';
- import Text from 'ckeditor5-engine/src/view/text';
- import DocumentFragment from 'ckeditor5-engine/src/view/documentfragment';
- import RootEditableElement from 'ckeditor5-engine/src/view/rooteditableelement';
- import CKEditorError from 'ckeditor5-utils/src/ckeditorerror';
- import createDocumentMock from 'ckeditor5-engine/tests/view/_utils/createdocumentmock';
- describe( 'Node', () => {
- let root;
- let one, two, three;
- let charB, charA, charR, img;
- before( () => {
- charB = new Text( 'b' );
- charA = new Text( 'a' );
- img = new Element( 'img' );
- charR = new Text( 'r' );
- one = new Element( 'one' );
- two = new Element( 'two', null, [ charB, charA, img, charR ] );
- three = new Element( 'three' );
- root = new Element( null, null, [ one, two, three ] );
- } );
- describe( 'getNextSibling/getPreviousSibling', () => {
- it( 'should return next sibling', () => {
- expect( root.nextSibling ).to.be.null;
- expect( one.nextSibling ).to.equal( two );
- expect( two.nextSibling ).to.equal( three );
- expect( three.nextSibling ).to.be.null;
- expect( charB.nextSibling ).to.equal( charA );
- expect( charA.nextSibling ).to.equal( img );
- expect( img.nextSibling ).to.equal( charR );
- expect( charR.nextSibling ).to.be.null;
- } );
- it( 'should return previous sibling', () => {
- expect( root.previousSibling ).to.be.null;
- expect( one.previousSibling ).to.be.null;
- expect( two.previousSibling ).to.equal( one );
- expect( three.previousSibling ).to.equal( two );
- expect( charB.previousSibling ).to.be.null;
- expect( charA.previousSibling ).to.equal( charB );
- expect( img.previousSibling ).to.equal( charA );
- expect( charR.previousSibling ).to.equal( img );
- } );
- } );
- describe( 'getAncestors', () => {
- it( 'should return empty array for node without ancestors', () => {
- const result = root.getAncestors();
- expect( result ).to.be.an( 'array' );
- expect( result.length ).to.equal( 0 );
- } );
- it( 'should return array including node itself if requested', () => {
- const result = root.getAncestors( { includeNode: true } );
- expect( result ).to.be.an( 'array' );
- expect( result.length ).to.equal( 1 );
- expect( result[ 0 ] ).to.equal( root );
- } );
- it( 'should return array of ancestors', () => {
- const result = charR.getAncestors();
- expect( result.length ).to.equal( 2 );
- expect( result[ 0 ] ).to.equal( root );
- expect( result[ 1 ] ).to.equal( two );
- const result2 = charR.getAncestors( { includeNode: true } );
- expect( result2.length ).to.equal( 3 );
- expect( result2[ 0 ] ).to.equal( root );
- expect( result2[ 1 ] ).to.equal( two );
- expect( result2[ 2 ] ).to.equal( charR );
- } );
- it( 'should return array of ancestors starting from parent', () => {
- const result = charR.getAncestors( { parentFirst: true } );
- expect( result.length ).to.equal( 2 );
- expect( result[ 0 ] ).to.equal( two );
- expect( result[ 1 ] ).to.equal( root );
- const result2 = charR.getAncestors( { includeNode: true, parentFirst: true } );
- expect( result2.length ).to.equal( 3 );
- expect( result2[ 2 ] ).to.equal( root );
- expect( result2[ 1 ] ).to.equal( two );
- expect( result2[ 0 ] ).to.equal( charR );
- } );
- it( 'should return ancestors including DocumentFragment', () => {
- const fragment = new DocumentFragment( root );
- const result = img.getAncestors();
- root.remove();
- expect( result.length ).to.equal( 3 );
- expect( result[ 0 ] ).to.equal( fragment );
- expect( result[ 1 ] ).to.equal( root );
- expect( result[ 2 ] ).to.equal( two );
- } );
- } );
- describe( 'getIndex', () => {
- it( 'should return null if the parent is null', () => {
- expect( root.index ).to.be.null;
- } );
- it( 'should return index in the parent', () => {
- expect( one.index ).to.equal( 0 );
- expect( two.index ).to.equal( 1 );
- expect( three.index ).to.equal( 2 );
- expect( charB.index ).to.equal( 0 );
- expect( charA.index ).to.equal( 1 );
- expect( img.index ).to.equal( 2 );
- expect( charR.index ).to.equal( 3 );
- } );
- it( 'should throw an error if parent does not contain element', () => {
- let f = new Text( 'f' );
- let bar = new Element( 'bar', [], [] );
- f.parent = bar;
- expect(
- () => {
- f.index;
- }
- ).to.throw( CKEditorError, /view-node-not-found-in-parent/ );
- } );
- } );
- describe( 'getDocument', () => {
- it( 'should return null if any parent has not set Document', () => {
- expect( charA.document ).to.be.null;
- } );
- it( 'should return Document attached to the parent element', () => {
- const docMock = createDocumentMock();
- const parent = new RootEditableElement( 'div' );
- parent.document = docMock;
- const child = new Element( 'p' );
- child.parent = parent;
- expect( parent.document ).to.equal( docMock );
- expect( child.document ).to.equal( docMock );
- } );
- it( 'should return null if element is inside DocumentFragment', () => {
- const child = new Element( 'p' );
- new DocumentFragment( [ child ] );
- expect( child.document ).to.be.null;
- } );
- } );
- describe( 'getRoot', () => {
- it( 'should return this element if it has no parent', () => {
- const child = new Element( 'p' );
- expect( child.root ).to.equal( child );
- } );
- it( 'should return root element', () => {
- const parent = new RootEditableElement( 'div' );
- parent.document = createDocumentMock();
- const child = new Element( 'p' );
- child.parent = parent;
- expect( parent.root ).to.equal( parent );
- expect( child.root ).to.equal( parent );
- } );
- } );
- describe( 'remove', () => {
- it( 'should remove node from its parent', () => {
- const char = new Text( 'a' );
- const parent = new Element( 'p', null, [ char ] );
- char.remove();
- expect( parent.getChildIndex( char ) ).to.equal( -1 );
- } );
- it( 'uses parent.removeChildren method', () => {
- const char = new Text( 'a' );
- const parent = new Element( 'p', null, [ char ] );
- const removeChildrenSpy = sinon.spy( parent, 'removeChildren' );
- const index = char.index;
- char.remove();
- removeChildrenSpy.restore();
- sinon.assert.calledOnce( removeChildrenSpy );
- sinon.assert.calledWithExactly( removeChildrenSpy, index );
- } );
- } );
- describe( 'change event', () => {
- let root, text, img;
- let rootChangeSpy;
- before( () => {
- rootChangeSpy = sinon.spy();
- } );
- beforeEach( () => {
- text = new Text( 'foo' );
- img = new Element( 'img' );
- img.setAttribute( 'src', 'img.png' );
- root = new Element( 'p', { renderer: { markToSync: rootChangeSpy } } );
- root.appendChildren( [ text, img ] );
- root.on( 'change:children', ( evt, node ) => rootChangeSpy( 'children', node ) );
- root.on( 'change:attributes', ( evt, node ) => rootChangeSpy( 'attributes', node ) );
- root.on( 'change:text', ( evt, node ) => rootChangeSpy( 'text', node ) );
- rootChangeSpy.reset();
- } );
- it( 'should be fired on the node', () => {
- const imgChangeSpy = sinon.spy();
- img.on( 'change:attributes', ( evt, node ) => {
- imgChangeSpy( 'attributes', node );
- } );
- img.setAttribute( 'width', 100 );
- sinon.assert.calledOnce( imgChangeSpy );
- sinon.assert.calledWith( imgChangeSpy, 'attributes', img );
- } );
- it( 'should be fired on the parent', () => {
- img.setAttribute( 'width', 100 );
- sinon.assert.calledOnce( rootChangeSpy );
- sinon.assert.calledWith( rootChangeSpy, 'attributes', img );
- } );
- describe( 'setAttribute', () => {
- it( 'should fire change event', () => {
- img.setAttribute( 'width', 100 );
- sinon.assert.calledOnce( rootChangeSpy );
- sinon.assert.calledWith( rootChangeSpy, 'attributes', img );
- } );
- } );
- describe( 'removeAttribute', () => {
- it( 'should fire change event', () => {
- img.removeAttribute( 'src' );
- sinon.assert.calledOnce( rootChangeSpy );
- sinon.assert.calledWith( rootChangeSpy, 'attributes', img );
- } );
- } );
- describe( 'insertChildren', () => {
- it( 'should fire change event', () => {
- root.insertChildren( 1, new Element( 'img' ) );
- sinon.assert.calledOnce( rootChangeSpy );
- sinon.assert.calledWith( rootChangeSpy, 'children', root );
- } );
- } );
- describe( 'appendChildren', () => {
- it( 'should fire change event', () => {
- root.appendChildren( new Element( 'img' ) );
- sinon.assert.calledOnce( rootChangeSpy );
- sinon.assert.calledWith( rootChangeSpy, 'children', root );
- } );
- } );
- describe( 'removeChildren', () => {
- it( 'should fire change event', () => {
- root.removeChildren( 1, 1 );
- sinon.assert.calledOnce( rootChangeSpy );
- sinon.assert.calledWith( rootChangeSpy, 'children', root );
- } );
- } );
- describe( 'removeChildren', () => {
- it( 'should fire change event', () => {
- text.data = 'bar';
- sinon.assert.calledOnce( rootChangeSpy );
- sinon.assert.calledWith( rootChangeSpy, 'text', text );
- } );
- } );
- } );
- } );
|