/** * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved. * For licensing, see LICENSE.md. */ /* bender-tags: view, browser-only */ import { breakContainer } from 'ckeditor5/engine/view/writer.js'; import { stringify, parse } from 'ckeditor5/engine/dev-utils/view.js'; import CKEditorError from 'ckeditor5/utils/ckeditorerror.js'; import ContainerElement from 'ckeditor5/engine/view/containerelement.js'; import Position from 'ckeditor5/engine/view/position.js'; describe( 'writer', () => { /** * Executes test using `parse` and `stringify` utils functions. Uses range delimiters `[]{}` to create and * test break position. * * @param {String} input * @param {String} expected */ function test( input, expected ) { let { view, selection } = parse( input ); const newPosition = breakContainer( selection.getFirstPosition() ); expect( stringify( view.root, newPosition, { showType: true, showPriority: false } ) ).to.equal( expected ); } describe( 'breakContainer', () => { it( 'break inside element - should break container element at given position', () => { test( '' + '' + 'foo[]bar' + '' + '', '' + '' + 'foo' + '' + '[]' + 'bar' + '' + '' ); } ); it( 'break at start of element - should not break container and place returned position before element', () => { test( '[]foobar', '[]foobar' ); } ); it( 'break at the end of element - should not break container and place returned position after element', () => { test( 'foobar[]', 'foobar[]' ); } ); it( 'should throw if position parent is not container', () => { let { selection } = parse( 'foo{}bar' ); expect( () => { breakContainer( selection.getFirstPosition() ); } ).to.throw( CKEditorError, /view-writer-break-non-container-element/ ); } ); it( 'should throw if position parent is root', () => { const element = new ContainerElement( 'div' ); const position = Position.createAt( element, 0 ); expect( () => { breakContainer( position ); } ).to.throw( CKEditorError, /view-writer-break-root/ ); } ); } ); } );