/** * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved. * For licensing, see LICENSE.md. */ import { insert } from '../../../src/view/writer'; import ContainerElement from '../../../src/view/containerelement'; import Element from '../../../src/view/element'; import EmptyElement from '../../../src/view/emptyelement'; import UIElement from '../../../src/view/uielement'; import Position from '../../../src/view/position'; import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror'; import { stringify, parse } from '../../../src/dev-utils/view'; import AttributeElement from '../../../src/view/attributeelement'; describe( 'writer', () => { /** * Executes test using `parse` and `stringify` utils functions. * * @param {String} input * @param {Array.} nodesToInsert * @param {String} expected */ function test( input, nodesToInsert, expected ) { nodesToInsert = nodesToInsert.map( node => parse( node ) ); const { view, selection } = parse( input ); const newRange = insert( selection.getFirstPosition(), nodesToInsert ); expect( stringify( view.root, newRange, { showType: true, showPriority: true } ) ).to.equal( expected ); } describe( 'insert', () => { it( 'should return collapsed range in insertion position when using empty array', () => { test( 'foo{}bar', [], 'foo{}bar' ); } ); it( 'should insert text into another text node #1', () => { test( 'foo{}bar', [ 'baz' ], 'foo{baz}bar' ); } ); it( 'should insert text into another text node #2', () => { test( 'foobar{}', [ 'baz' ], 'foobar{baz]' ); } ); it( 'should insert text into another text node #3', () => { test( '{}foobar', [ 'baz' ], '[baz}foobar' ); } ); it( 'should break attributes when inserting into text node', () => { test( 'foo{}bar', [ 'baz' ], 'foo[baz]bar' ); } ); it( 'should merge text nodes', () => { test( '[]foobar', [ 'baz' ], '[baz}foobar' ); } ); it( 'should merge same attribute nodes', () => { test( 'foo{}bar', [ 'baz' ], 'foo{baz}bar' ); } ); it( 'should not merge different attributes', () => { test( 'foo{}bar', [ 'baz' ], '' + '' + 'foo' + '' + '[' + '' + 'baz' + '' + ']' + '' + 'bar' + '' + '' ); } ); it( 'should allow to insert multiple nodes', () => { test( '[]', [ 'foo', 'bar' ], '[foobar]' ); } ); it( 'should merge after inserting multiple nodes', () => { test( 'qux[]baz', [ 'foo', 'bar' ], 'qux{foobar}baz' ); } ); it( 'should insert text into in document fragment', () => { test( 'foo{}bar', [ 'baz' ], 'foo{baz}bar' ); } ); it( 'should merge same attribute nodes in document fragment', () => { test( 'foo[]', [ 'bar' ], 'foo[bar]' ); } ); it( 'should insert unicode text into unicode text', () => { test( 'நி{}க்கு', [ 'லை' ], 'நி{லை}க்கு' ); } ); it( 'should throw when inserting Element', () => { const element = new Element( 'b' ); const container = new ContainerElement( 'p' ); const position = new Position( container, 0 ); expect( () => { insert( position, element ); } ).to.throw( CKEditorError, 'view-writer-insert-invalid-node' ); } ); it( 'should throw when Element is inserted as child node', () => { const element = new Element( 'b' ); const root = new ContainerElement( 'p', null, element ); const container = new ContainerElement( 'p' ); const position = new Position( container, 0 ); expect( () => { insert( position, root ); } ).to.throw( CKEditorError, 'view-writer-insert-invalid-node' ); } ); it( 'should throw when position is not placed inside container', () => { const element = new Element( 'b' ); const position = new Position( element, 0 ); const attributeElement = new AttributeElement( 'i' ); expect( () => { insert( position, attributeElement ); } ).to.throw( CKEditorError, 'view-writer-invalid-position-container' ); } ); it( 'should allow to insert EmptyElement into container', () => { test( '[]', [ '' ], '[]' ); } ); it( 'should throw if trying to insert inside EmptyElement', () => { const emptyElement = new EmptyElement( 'img' ); new ContainerElement( 'p', null, emptyElement ); // eslint-disable-line no-new const position = new Position( emptyElement, 0 ); const attributeElement = new AttributeElement( 'i' ); expect( () => { insert( position, attributeElement ); } ).to.throw( CKEditorError, 'view-writer-cannot-break-empty-element' ); } ); it( 'should throw if trying to insert inside UIElement', () => { const uiElement = new UIElement( 'span' ); new ContainerElement( 'p', null, uiElement ); // eslint-disable-line no-new const position = new Position( uiElement, 0 ); const attributeElement = new AttributeElement( 'i' ); expect( () => { insert( position, attributeElement ); } ).to.throw( CKEditorError, 'view-writer-cannot-break-ui-element' ); } ); } ); } );