/** * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved. * For licensing, see LICENSE.md. */ /* bender-tags: view, browser-only */ import { breakAttributes } from 'ckeditor5/engine/view/writer.js'; import { stringify, parse } from 'ckeditor5/engine/dev-utils/view.js'; import ContainerElement from 'ckeditor5/engine/view/containerelement.js'; import AttributeElement from 'ckeditor5/engine/view/attributeelement.js'; import EmptyElement from 'ckeditor5/engine/view/emptyelement.js'; import Range from 'ckeditor5/engine/view/range.js'; import Position from 'ckeditor5/engine/view/position.js'; import CKEditorError from 'ckeditor5/utils/ckeditorerror.js'; describe( 'writer', () => { describe( 'breakAttributes', () => { describe( 'break position', () => { /** * 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 = breakAttributes( selection.getFirstPosition() ); expect( stringify( view.root, newPosition, { showType: true, showPriority: true } ) ).to.equal( expected ); } it( 'should not break text nodes if they are not in attribute elements - middle', () => { test( 'foo{}bar', 'foo{}bar' ); } ); it( 'should not break text nodes if they are not in attribute elements - beginning', () => { test( '{}foobar', '{}foobar' ); } ); it( 'should not break text nodes if they are not in attribute elements #2 - end', () => { test( 'foobar{}', 'foobar{}' ); } ); it( 'should split attribute element', () => { test( 'foo{}bar', '' + 'foo[]bar' + '' ); } ); it( 'should move from beginning of the nested text node to the container', () => { test( '' + '{}foobar' + '', '' + '[]foobar' + '' ); } ); it( 'should stick selection in text node if it is in container', () => { test( 'foo{}bar', 'foo{}bar' ); } ); it( 'should split nested attributes', () => { test( '' + 'foo{}bar' + '', '' + '' + '' + 'foo' + '' + '' + '[]' + '' + '' + 'bar' + '' + '' + '' ); } ); it( 'should move from end of the nested text node to the container', () => { test( '' + 'foobar{}' + '', '' + 'foobar[]' + '' ); } ); it( 'should split attribute element directly in document fragment', () => { test( 'foo{}bar', 'foo[]bar' ); } ); it( 'should not split text directly in document fragment', () => { test( 'foo{}bar', 'foo{}bar' ); } ); } ); describe( 'break range', () => { /** * Executes test using `parse` and `stringify` utils functions. * * @param {String} input * @param {String} expected */ function test( input, expected ) { let { view, selection } = parse( input ); const newRange = breakAttributes( selection.getFirstRange() ); expect( stringify( view.root, newRange, { showType: true } ) ).to.equal( expected ); } it( 'should throw when range placed in two containers', () => { const p1 = new ContainerElement( 'p' ); const p2 = new ContainerElement( 'p' ); expect( () => { breakAttributes( Range.createFromParentsAndOffsets( p1, 0, p2, 0 ) ); } ).to.throw( CKEditorError, 'view-writer-invalid-range-container' ); } ); it( 'should throw when range has no parent container', () => { const el = new AttributeElement( 'b' ); expect( () => { breakAttributes( Range.createFromParentsAndOffsets( el, 0, el, 0 ) ); } ).to.throw( CKEditorError, 'view-writer-invalid-range-container' ); } ); it( 'should not break text nodes if they are not in attribute elements', () => { test( 'foo{}bar', 'foo{}bar' ); } ); it( 'should break at collapsed range and return collapsed one', () => { test( 'foo{}bar', 'foo[]bar' ); } ); it( 'should break inside text node #1', () => { test( 'foo{bar}baz', 'foo[bar]baz' ); } ); it( 'should break inside text node #2', () => { test( 'foo{barbaz}', 'foo[barbaz]' ); } ); it( 'should break inside text node #3', () => { test( 'foo{barbaz]', 'foo[barbaz]' ); } ); it( 'should break inside text node #4', () => { test( '{foo}barbaz', '[foo]barbaz' ); } ); it( 'should break inside text node #5', () => { test( '[foo}barbaz', '[foo]barbaz' ); } ); it( 'should break placed inside different nodes', () => { test( 'foo{barbaz}qux', 'foo{barbaz]qux' ); } ); it( 'should split attribute element directly in document fragment', () => { test( 'fo{ob}ar', 'fo[ob]ar' ); } ); it( 'should not split text directly in document fragment', () => { test( 'foo{}bar', 'foo{}bar' ); } ); it( 'should throw if breaking inside EmptyElement #1', () => { const img = new EmptyElement( 'img' ); new ContainerElement( 'p', null, img ); const position = new Position( img, 0 ); expect( () => { breakAttributes( position ); } ).to.throw( CKEditorError, 'view-writer-cannot-break-empty-element' ); } ); it( 'should throw if breaking inside EmptyElement #2', () => { const img = new EmptyElement( 'img' ); const b = new AttributeElement( 'b' ); new ContainerElement( 'p', null, [ img, b ] ); const range = Range.createFromParentsAndOffsets( img, 0, b, 0 ); expect( () => { breakAttributes( range ); } ).to.throw( CKEditorError, 'view-writer-cannot-break-empty-element' ); } ); } ); } ); } );