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