/** * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved. * For licensing, see LICENSE.md. */ /* bender-tags: treeview */ 'use strict'; import Writer from '/ckeditor5/engine/treeview/writer.js'; import { stringify, parse } from '/tests/engine/_utils/view.js'; describe( 'Writer', () => { let 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 = writer.insert( selection.getFirstPosition(), nodesToInsert ); expect( stringify( view, newRange, { showType: true, showPriority: true } ) ).to.equal( expected ); } beforeEach( () => { writer = new Writer(); } ); 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' ); } ); } ); } );