| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407 |
- /**
- * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
- import Node from '../../src/model/node';
- import Element from '../../src/model/element';
- import Text from '../../src/model/text';
- import TextProxy from '../../src/model/textproxy';
- import { jsonParseStringify } from '../../tests/model/_utils/utils';
- import count from '@ckeditor/ckeditor5-utils/src/count';
- import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
- describe( 'Element', () => {
- describe( 'constructor()', () => {
- it( 'should create empty element', () => {
- const element = new Element( 'elem' );
- expect( element ).to.be.an.instanceof( Node );
- expect( element ).to.have.property( 'name' ).that.equals( 'elem' );
- expect( count( element.getAttributes() ) ).to.equal( 0 );
- expect( count( element.getChildren() ) ).to.equal( 0 );
- } );
- it( 'should create element with attributes', () => {
- const element = new Element( 'elem', { foo: 'bar' } );
- expect( count( element.getAttributes() ) ).to.equal( 1 );
- expect( element.getAttribute( 'foo' ) ).to.equal( 'bar' );
- } );
- it( 'should create element with children', () => {
- const element = new Element( 'elem', [], new Text( 'foo' ) );
- expect( element.childCount ).to.equal( 1 );
- expect( element.maxOffset ).to.equal( 3 );
- expect( element.getChild( 0 ).data ).to.equal( 'foo' );
- } );
- } );
- describe( 'is', () => {
- let element;
- before( () => {
- element = new Element( 'paragraph' );
- } );
- it( 'should return true for element, element with same name and element name', () => {
- expect( element.is( 'element' ) ).to.be.true;
- expect( element.is( 'element', 'paragraph' ) ).to.be.true;
- expect( element.is( 'paragraph' ) ).to.be.true;
- } );
- it( 'should return false for other accept values', () => {
- expect( element.is( 'element', 'image' ) ).to.be.false;
- expect( element.is( 'image' ) ).to.be.false;
- expect( element.is( 'text' ) ).to.be.false;
- expect( element.is( 'textProxy' ) ).to.be.false;
- expect( element.is( 'documentFragment' ) ).to.be.false;
- expect( element.is( 'rootElement' ) ).to.be.false;
- } );
- } );
- describe( '_clone()', () => {
- it( 'should return an element with same name, attributes and same instances of children if clone was not deep', () => {
- const p = new Element( 'p' );
- const foo = new Text( 'foo' );
- const element = new Element( 'elem', { bold: true, italic: true }, [ p, foo ] );
- const copy = element._clone();
- expect( copy.name ).to.equal( 'elem' );
- expect( Array.from( copy.getAttributes() ) ).to.deep.equal( [ [ 'bold', true ], [ 'italic', true ] ] );
- expect( Array.from( copy.getChildren() ) ).to.deep.equal( [] );
- } );
- it( 'should clone children (deeply), if clone is deep', () => {
- const foo = new Text( 'foo' );
- const bar = new Text( 'bar' );
- const p = new Element( 'p', null, bar );
- const element = new Element( 'elem', { bold: true, italic: true }, [ p, foo ] );
- const copy = element._clone( true );
- expect( copy.name ).to.equal( 'elem' );
- expect( Array.from( copy.getAttributes() ) ).to.deep.equal( [ [ 'bold', true ], [ 'italic', true ] ] );
- expect( copy.childCount ).to.equal( 2 );
- expect( copy.getChild( 0 ) ).not.to.equal( p );
- expect( copy.getChild( 0 ).getChild( 0 ) ).not.to.equal( bar );
- expect( copy.getChild( 1 ) ).not.to.equal( foo );
- expect( copy.getChild( 0 ).name ).to.equal( 'p' );
- expect( copy.getChild( 0 ).getChild( 0 ).data ).to.equal( 'bar' );
- expect( copy.getChild( 1 ).data ).to.equal( 'foo' );
- } );
- } );
- describe( '_insertChildren', () => {
- it( 'should add a child to the element', () => {
- const element = new Element( 'elem', [], new Text( 'xy' ) );
- element._insertChildren( 1, new Text( 'foo' ) );
- expect( element.childCount ).to.equal( 2 );
- expect( element.maxOffset ).to.equal( 5 );
- expect( element.getChild( 0 ).data ).to.equal( 'xy' );
- expect( element.getChild( 1 ).data ).to.equal( 'foo' );
- } );
- it( 'should accept arrays and strings', () => {
- const element = new Element( 'elem' );
- element._insertChildren( 0, [ new Element( 'image' ), 'xy', new Element( 'list' ) ] );
- expect( element.childCount ).to.equal( 3 );
- expect( element.maxOffset ).to.equal( 4 );
- expect( element.getChild( 0 ).name ).to.equal( 'image' );
- expect( element.getChild( 1 ).data ).to.equal( 'xy' );
- expect( element.getChild( 2 ).name ).to.equal( 'list' );
- } );
- it( 'should accept strings', () => {
- const element = new Element( 'div' );
- element._insertChildren( 0, 'abc' );
- expect( element.childCount ).to.equal( 1 );
- expect( element.maxOffset ).to.equal( 3 );
- expect( element.getChild( 0 ) ).to.have.property( 'data' ).that.equals( 'abc' );
- } );
- it( 'should accept and correctly handle text proxies', () => {
- const element = new Element( 'div' );
- const text = new Text( 'abcxyz', { bold: true } );
- const textProxy = new TextProxy( text, 2, 3 );
- element._insertChildren( 0, textProxy );
- expect( element.childCount ).to.equal( 1 );
- expect( element.maxOffset ).to.equal( 3 );
- expect( element.getChild( 0 ) ).to.be.instanceof( Text );
- expect( element.getChild( 0 ).data ).to.equal( 'cxy' );
- expect( element.getChild( 0 ).getAttribute( 'bold' ) ).to.equal( true );
- } );
- } );
- describe( '_appendChildren', () => {
- it( 'should use _insertChildren to add children at the end of the element', () => {
- const element = new Element( 'elem', [], new Text( 'xy' ) );
- sinon.spy( element, '_insertChildren' );
- const text = new Text( 'foo' );
- element._appendChildren( text );
- expect( element._insertChildren.calledWithExactly( 0, text ) );
- } );
- } );
- describe( '_removeChildren', () => {
- it( 'should remove children from the element and return them as an array', () => {
- const element = new Element( 'elem', [], [ new Text( 'foobar' ), new Element( 'image' ) ] );
- const removed = element._removeChildren( 1, 1 );
- expect( element.childCount ).to.equal( 1 );
- expect( element.maxOffset ).to.equal( 6 );
- expect( element.getChild( 0 ).data ).to.equal( 'foobar' );
- expect( removed.length ).to.equal( 1 );
- expect( removed[ 0 ].name ).to.equal( 'image' );
- } );
- it( 'should remove one child when second parameter is not specified', () => {
- const element = new Element( 'element', [], [ new Text( 'foo' ), new Element( 'image' ) ] );
- const removed = element._removeChildren( 0 );
- expect( element.childCount ).to.equal( 1 );
- expect( element.maxOffset ).to.equal( 1 );
- expect( element.getChild( 0 ).name ).to.equal( 'image' );
- expect( removed.length ).to.equal( 1 );
- expect( removed[ 0 ].data ).to.equal( 'foo' );
- } );
- } );
- describe( 'getNodeByPath', () => {
- it( 'should return this node if path is empty', () => {
- const element = new Element( 'elem' );
- expect( element.getNodeByPath( [] ) ).to.equal( element );
- } );
- it( 'should return a descendant of this node', () => {
- const foo = new Text( 'foo' );
- const image = new Element( 'image' );
- const element = new Element( 'elem', [], [
- new Element( 'elem', [], [
- foo,
- image
- ] )
- ] );
- expect( element.getNodeByPath( [ 0, 0 ] ) ).to.equal( foo );
- expect( element.getNodeByPath( [ 0, 1 ] ) ).to.equal( foo );
- expect( element.getNodeByPath( [ 0, 2 ] ) ).to.equal( foo );
- expect( element.getNodeByPath( [ 0, 3 ] ) ).to.equal( image );
- } );
- it( 'works fine with offsets', () => {
- const bar = new Text( 'bar' );
- const foo = new Text( 'foo' );
- const bom = new Text( 'bom' );
- const bold = new Element( 'b', [], [
- bar
- ] );
- const paragraph = new Element( 'paragraph', [], [
- foo,
- bold,
- bom
- ] );
- // <paragraph>foo<bold>bar</bold>bom</paragraph>
- expect( paragraph.getNodeByPath( [ 0 ] ) ).to.equal( foo );
- expect( paragraph.getNodeByPath( [ 1 ] ) ).to.equal( foo );
- expect( paragraph.getNodeByPath( [ 2 ] ) ).to.equal( foo );
- expect( paragraph.getNodeByPath( [ 3 ] ) ).to.equal( bold );
- expect( paragraph.getNodeByPath( [ 3, 0 ] ) ).to.equal( bar );
- expect( paragraph.getNodeByPath( [ 3, 1 ] ) ).to.equal( bar );
- expect( paragraph.getNodeByPath( [ 3, 2 ] ) ).to.equal( bar );
- expect( paragraph.getNodeByPath( [ 3, 3 ] ) ).to.equal( null );
- expect( paragraph.getNodeByPath( [ 4 ] ) ).to.equal( bom );
- expect( paragraph.getNodeByPath( [ 5 ] ) ).to.equal( bom );
- expect( paragraph.getNodeByPath( [ 6 ] ) ).to.equal( bom );
- expect( paragraph.getNodeByPath( [ 7 ] ) ).to.equal( null );
- } );
- } );
- describe( 'getChildIndex', () => {
- it( 'should return child index', () => {
- const element = new Element( 'elem', [], [ new Element( 'p' ), new Text( 'bar' ), new Element( 'h' ) ] );
- const p = element.getChild( 0 );
- const bar = element.getChild( 1 );
- const h = element.getChild( 2 );
- expect( element.getChildIndex( p ) ).to.equal( 0 );
- expect( element.getChildIndex( bar ) ).to.equal( 1 );
- expect( element.getChildIndex( h ) ).to.equal( 2 );
- } );
- } );
- describe( 'getChildStartOffset', () => {
- it( 'should return child offset', () => {
- const element = new Element( 'elem', [], [ new Element( 'p' ), new Text( 'bar' ), new Element( 'h' ) ] );
- const p = element.getChild( 0 );
- const bar = element.getChild( 1 );
- const h = element.getChild( 2 );
- expect( element.getChildStartOffset( p ) ).to.equal( 0 );
- expect( element.getChildStartOffset( bar ) ).to.equal( 1 );
- expect( element.getChildStartOffset( h ) ).to.equal( 4 );
- } );
- } );
- describe( 'getChildCount', () => {
- it( 'should return number of children', () => {
- const element = new Element( 'elem', [], new Text( 'bar' ) );
- expect( element.childCount ).to.equal( 1 );
- } );
- } );
- describe( 'getMaxOffset', () => {
- it( 'should return offset number after the last child', () => {
- const element = new Element( 'elem', [], [ new Element( 'p' ), new Text( 'bar' ), new Element( 'h' ) ] );
- expect( element.maxOffset ).to.equal( 5 );
- } );
- } );
- describe( 'isEmpty', () => {
- it( 'checks whether element has no children', () => {
- expect( new Element( 'a' ).isEmpty ).to.be.true;
- expect( new Element( 'a', null, new Text( 'x' ) ).isEmpty ).to.be.false;
- } );
- } );
- describe( 'offsetToIndex', () => {
- let element;
- beforeEach( () => {
- element = new Element( 'elem', [], [ new Element( 'p' ), new Text( 'bar' ), new Element( 'h' ) ] );
- } );
- it( 'should return index of a node that occupies given offset in this element', () => {
- expect( element.offsetToIndex( 0 ) ).to.equal( 0 );
- expect( element.offsetToIndex( 1 ) ).to.equal( 1 );
- expect( element.offsetToIndex( 2 ) ).to.equal( 1 );
- expect( element.offsetToIndex( 3 ) ).to.equal( 1 );
- expect( element.offsetToIndex( 4 ) ).to.equal( 2 );
- } );
- it( 'should throw if given offset is too high or too low', () => {
- expect( () => {
- element.offsetToIndex( -1 );
- } ).to.throw( CKEditorError, /nodelist-offset-out-of-bounds/ );
- expect( () => {
- element.offsetToIndex( 55 );
- } ).to.throw( CKEditorError, /nodelist-offset-out-of-bounds/ );
- } );
- it( 'should return length if given offset is equal to maxOffset', () => {
- expect( element.offsetToIndex( 5 ) ).to.equal( 3 );
- } );
- } );
- describe( 'toJSON', () => {
- it( 'should serialize empty element', () => {
- const element = new Element( 'one' );
- expect( jsonParseStringify( element ) ).to.deep.equal( { name: 'one' } );
- } );
- it( 'should serialize element with attributes', () => {
- const element = new Element( 'one', { foo: true, bar: false } );
- expect( jsonParseStringify( element ) ).to.deep.equal( {
- attributes: [ [ 'foo', true ], [ 'bar', false ] ],
- name: 'one'
- } );
- } );
- it( 'should serialize node with children', () => {
- const img = new Element( 'img' );
- const one = new Element( 'one' );
- const two = new Element( 'two', null, [ new Text( 'ba' ), img, new Text( 'r' ) ] );
- const three = new Element( 'three' );
- const node = new Element( null, null, [ one, two, three ] );
- expect( jsonParseStringify( node ) ).to.deep.equal( {
- children: [
- { name: 'one' },
- {
- children: [
- { data: 'ba' },
- { name: 'img' },
- { data: 'r' }
- ],
- name: 'two'
- },
- { name: 'three' }
- ],
- name: null
- } );
- } );
- } );
- describe( 'fromJSON', () => {
- it( 'should create element without attributes', () => {
- const el = new Element( 'el' );
- const serialized = jsonParseStringify( el );
- const deserialized = Element.fromJSON( serialized );
- expect( deserialized.parent ).to.be.null;
- expect( deserialized.name ).to.equal( 'el' );
- expect( deserialized.childCount ).to.equal( 0 );
- } );
- it( 'should create element with attributes', () => {
- const el = new Element( 'el', { foo: true } );
- const serialized = jsonParseStringify( el );
- const deserialized = Element.fromJSON( serialized );
- expect( deserialized.parent ).to.be.null;
- expect( deserialized.name ).to.equal( 'el' );
- expect( deserialized.childCount ).to.equal( 0 );
- expect( deserialized.hasAttribute( 'foo' ) ).to.be.true;
- expect( deserialized.getAttribute( 'foo' ) ).to.be.true;
- } );
- it( 'should create element with children', () => {
- const p = new Element( 'p' );
- const text = new Text( 'foo' );
- const el = new Element( 'el', null, [ p, text ] );
- const serialized = jsonParseStringify( el );
- const deserialized = Element.fromJSON( serialized );
- expect( deserialized.parent ).to.be.null;
- expect( deserialized.name ).to.equal( 'el' );
- expect( deserialized.childCount ).to.equal( 2 );
- expect( deserialized.getChild( 0 ).name ).to.equal( 'p' );
- expect( deserialized.getChild( 0 ).parent ).to.equal( deserialized );
- expect( deserialized.getChild( 1 ).data ).to.equal( 'foo' );
- expect( deserialized.getChild( 1 ).parent ).to.equal( deserialized );
- } );
- } );
- } );
|