/** * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved. * For licensing, see LICENSE.md. */ import { move } from '../../../src/view/writer'; import { stringify, parse } from '../../../src/dev-utils/view'; import ContainerElement from '../../../src/view/containerelement'; import AttributeElement from '../../../src/view/attributeelement'; import EmptyElement from '../../../src/view/emptyelement'; import UIElement from '../../../src/view/uielement'; import Range from '../../../src/view/range'; import Position from '../../../src/view/position'; import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror'; describe( 'writer', () => { /** * Executes test using `parse` and `stringify` utils functions. Uses range delimiters `[]{}` to create and * test ranges. * * @param {String} input * @param {String} expectedResult * @param {String} expectedRemoved */ function test( source, destination, sourceAfterMove, destinationAfterMove ) { const { view: srcView, selection: srcSelection } = parse( source ); const { view: dstView, selection: dstSelection } = parse( destination ); const newRange = move( srcSelection.getFirstRange(), dstSelection.getFirstPosition() ); expect( stringify( dstView, newRange, { showType: true, showPriority: true } ) ).to.equal( destinationAfterMove ); expect( stringify( srcView, null, { showType: true, showPriority: true } ) ).to.equal( sourceAfterMove ); } describe( 'move', () => { it( 'should move single text node', () => { test( '[foobar]', '[]', '', '[foobar]' ); } ); it( 'should not leave empty text nodes', () => { test( '{foobar}', '[]', '', '[foobar]' ); } ); it( 'should move part of the text node', () => { test( 'f{oob}ar', '[]', 'far', '[oob]' ); } ); it( 'should support unicode', () => { test( 'நி{லை}க்கு', 'நி{}கு', 'நிக்கு', 'நி{லை}கு' ); } ); it( 'should move parts of nodes', () => { test( 'f{ooba}r', '[]qux', 'fr', '[ooba}qux' ); } ); it( 'should merge after moving #1', () => { test( '' + 'foo[bar]bazqux' + '', 'foo{}bazqux', 'foobazqux', '' + 'foo[bar]bazqux' + '' ); } ); it( 'should merge after moving #2', () => { test( '' + 'fo{obarba}zqux' + '', 'fo{}zqux', 'fozqux', '' + 'fo{obarba}zqux' + '' ); } ); it( 'should move part of the text node in document fragment', () => { test( 'fo{ob}ar', 'fo{}ar', 'foar', 'fo{ob}ar' ); } ); it( 'should correctly move text nodes inside same parent', () => { const { view, selection } = parse( '[a]bc' ); const newRange = move( selection.getFirstRange(), Position.createAt( view, 2 ) ); const expectedView = 'b[a}c'; expect( stringify( view, newRange, { showType: true } ) ).to.equal( expectedView ); } ); it( 'should correctly move text nodes inside same container', () => { const { view, selection } = parse( 'a{bxxc}dyy' ); const viewText = view.getChild( 3 ); const newRange = move( selection.getFirstRange(), Position.createAt( viewText, 1 ) ); expect( stringify( view, newRange, { showType: true } ) ).to.equal( 'ady[bxxc]y' ); } ); it( 'should move EmptyElement', () => { test( 'foo[]bar', 'baz{}quix', 'foobar', 'baz[]quix' ); } ); it( 'should throw if trying to move to EmptyElement', () => { const srcAttribute = new AttributeElement( 'b' ); const srcContainer = new ContainerElement( 'p', null, srcAttribute ); const srcRange = Range.createFromParentsAndOffsets( srcContainer, 0, srcContainer, 1 ); const dstEmpty = new EmptyElement( 'img' ); new ContainerElement( 'p', null, dstEmpty ); // eslint-disable-line no-new const dstPosition = new Position( dstEmpty, 0 ); expect( () => { move( srcRange, dstPosition ); } ).to.throw( CKEditorError, 'view-writer-cannot-break-empty-element' ); } ); it( 'should move UIElement', () => { test( 'foo[]bar', 'baz{}quix', 'foobar', 'baz[]quix' ); } ); it( 'should throw if trying to move to UIElement', () => { const srcAttribute = new AttributeElement( 'b' ); const srcContainer = new ContainerElement( 'p', null, srcAttribute ); const srcRange = Range.createFromParentsAndOffsets( srcContainer, 0, srcContainer, 1 ); const dstUI = new UIElement( 'span' ); new ContainerElement( 'p', null, dstUI ); // eslint-disable-line no-new const dstPosition = new Position( dstUI, 0 ); expect( () => { move( srcRange, dstPosition ); } ).to.throw( CKEditorError, 'view-writer-cannot-break-ui-element' ); } ); } ); } );