/** * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved. * For licensing, see LICENSE.md. */ import Writer from '../../../src/view/writer'; import View from '../../../src/view/view'; import DocumentFragment from '../../../src/view/documentfragment'; import Element from '../../../src/view/element'; 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 Position from '../../../src/view/position'; import Range from '../../../src/view/range'; import Text from '../../../src/view/text'; import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror'; import { stringify, parse } from '../../../src/dev-utils/view'; import createViewRoot from '../_utils/createroot'; import Document from '../../../src/view/document'; describe( 'Writer', () => { describe( 'wrap()', () => { let writer; before( () => { writer = new Writer( new Document() ); } ); describe( 'non-collapsed range', () => { /** * Executes test using `parse` and `stringify` utils functions. * * @param {String} input * @param {String} wrapAttribute * @param {String} expected */ function test( input, wrapAttribute, expected ) { const { view, selection } = parse( input ); const newRange = writer.wrap( selection.getFirstRange(), parse( wrapAttribute ) ); expect( stringify( view.root, newRange, { showType: true, showPriority: true } ) ).to.equal( expected ); } it( 'wraps single text node', () => { test( '[foobar]', '', '[foobar]' ); } ); it( 'wraps single text node in document fragment', () => { test( '{foobar}', '', '[foobar]' ); } ); it( 'should throw error when element is not instance of AttributeElement', () => { const container = new ContainerElement( 'p', null, new Text( 'foo' ) ); const range = new Range( new Position( container, 0 ), new Position( container, 1 ) ); const b = new Element( 'b' ); expect( () => { writer.wrap( range, b ); } ).to.throw( CKEditorError, 'view-writer-wrap-invalid-attribute' ); } ); it( 'should throw error when range placed in two containers', () => { const container1 = new ContainerElement( 'p' ); const container2 = new ContainerElement( 'p' ); const range = new Range( new Position( container1, 0 ), new Position( container2, 1 ) ); const b = new AttributeElement( 'b' ); expect( () => { writer.wrap( range, b ); } ).to.throw( CKEditorError, 'view-writer-invalid-range-container' ); } ); it( 'should throw when range has no parent container', () => { const el = new AttributeElement( 'b' ); const b = new AttributeElement( 'b' ); expect( () => { writer.wrap( Range.createFromParentsAndOffsets( el, 0, el, 0 ), b ); } ).to.throw( CKEditorError, 'view-writer-invalid-range-container' ); } ); it( 'wraps part of a single text node #1', () => { test( '[foo}bar', '', '[foo]bar' ); } ); it( 'wraps part of a single text node #2', () => { test( '{foo}bar', '', '[foo]bar' ); } ); it( 'should support unicode', () => { test( 'நி{லை}க்கு', '', 'நி[லை]க்கு' ); } ); it( 'wraps part of a single text node #3', () => { test( 'foo{bar}', '', 'foo[bar]' ); } ); it( 'should not wrap inside nested containers', () => { test( '[foobarbaz]', '', '[foobarbaz]' ); } ); it( 'wraps according to priorities', () => { test( '[foobar]', '', '' + '[foobar]' + '' ); } ); it( 'merges wrapped nodes #1', () => { test( '' + '[foobarbaz]' + '', '', '[foobarbaz]' ); } ); it( 'merges wrapped nodes #2', () => { test( 'foo[bar}baz', '', 'foo{bar]baz' ); } ); it( 'merges wrapped nodes #3', () => { test( 'foobar[baz]', '', 'foobar{baz]' ); } ); it( 'merges wrapped nodes #4', () => { test( '[foobar]baz', '', '' + '[foobar]baz' + '' ); } ); it( 'merges wrapped nodes #5', () => { test( '[foobarbaz]', '', '' + '[' + 'foo' + '' + 'bar' + '' + 'baz' + ']' + '' ); } ); it( 'merges wrapped nodes #6', () => { test( 'f{ooba}r', '', 'f[' + 'o' + 'ob' + 'a' + ']r' ); } ); it( 'should wrap single element by merging attributes', () => { test( '[]', '', '[]' ); } ); it( 'should not merge attributes when they differ', () => { test( '[text]', '', '' + '[' + 'text' + ']' + '' ); } ); it( 'should wrap single element by merging classes', () => { test( '[]', '', '[]' ); } ); it( 'should wrap single element by merging styles', () => { test( '' + '[]' + '', '', '' + '[]' + '' ); } ); it( 'should not merge styles when they differ', () => { test( '[]', '', '' + '[' + '' + '' + '' + ']' + '' ); } ); it( 'should not merge single elements when they have different priority', () => { test( '[]', '', '' + '[' + '' + '' + '' + ']' + '' ); } ); it( 'should be merged with outside element when wrapping all children', () => { test( '' + '[foobarbaz]' + '', '', '' + '[' + '' + 'foobar' + 'baz' + '' + ']' + '' ); } ); it( 'should be merged with broken element', () => { test( '' + '[foo}bar' + '', '', '' + '[foo]' + 'bar' + '' ); } ); it( 'should be merged with broken element and merged with siblings', () => { test( '' + 'xyz' + '[foo}bar' + '', '', '' + 'xyz{foo]' + 'bar' + '' ); } ); it( 'should wrap EmptyElement', () => { test( '[]', '', '[]' ); } ); it( 'should throw if range is inside EmptyElement', () => { const emptyElement = new EmptyElement( 'img' ); const container = new ContainerElement( 'p', null, emptyElement ); const range = Range.createFromParentsAndOffsets( emptyElement, 0, container, 1 ); expect( () => { writer.wrap( range, new AttributeElement( 'b' ) ); } ).to.throw( CKEditorError, 'view-writer-cannot-break-empty-element' ); } ); it( 'should wrap UIElement', () => { test( '[]', '', '[]' ); } ); it( 'should throw if range is inside UIElement', () => { const uiElement = new UIElement( 'span' ); const container = new ContainerElement( 'p', null, uiElement ); const range = Range.createFromParentsAndOffsets( uiElement, 0, container, 1 ); expect( () => { writer.wrap( range, new AttributeElement( 'b' ) ); } ).to.throw( CKEditorError, 'view-writer-cannot-break-ui-element' ); } ); it( 'should keep stable hierarchy when wrapping with attribute with same priority', () => { test( '[foo]', '', '' + '[' + 'foo' + ']' + '' ); test( '[foo]', '', '' + '[' + 'foo' + ']' + '' ); } ); it( 'should keep stable hierarchy when wrapping with attribute with same priority that can\'t be merged', () => { test( '[foo]', '', '' + '[' + 'foo' + ']' + '' ); test( '[foo]', '', '' + '[' + 'foo' + ']' + '' ); } ); } ); describe( 'collapsed range', () => { let view, viewDocument, viewRoot; beforeEach( () => { view = new View(); viewDocument = view.document; viewRoot = createViewRoot( viewDocument ); } ); afterEach( () => { view.destroy(); } ); /** * Executes test using `parse` and `stringify` utils functions. * * @param {String} input * @param {String} wrapAttribute * @param {String} expected */ function test( input, wrapAttribute, expected ) { const { view, selection } = parse( input, { rootElement: viewRoot } ); viewDocument.selection._setTo( selection ); const newPosition = writer.wrap( selection.getFirstRange(), parse( wrapAttribute ) ); // Moving parsed elements to a document fragment so the view root is not shown in `stringify`. const viewChildren = new DocumentFragment( view.getChildren() ); expect( stringify( viewChildren, newPosition, { showType: true, showPriority: true } ) ).to.equal( expected ); } it( 'should throw error when element is not instance of AttributeElement', () => { const container = new ContainerElement( 'p', null, new Text( 'foo' ) ); const position = new Position( container, 0 ); const b = new Element( 'b' ); expect( () => { writer.wrap( new Range( position ), b ); } ).to.throw( CKEditorError, 'view-writer-wrap-invalid-attribute' ); } ); it( 'should wrap position at the beginning of text node', () => { test( '{}foobar', '', '[]foobar' ); } ); it( 'should wrap position inside text node', () => { test( 'foo{}bar', '', 'foo[]bar' ); } ); it( 'should support unicode', () => { test( 'நிலை{}க்கு', '', 'நிலை[]க்கு' ); } ); it( 'should wrap position inside document fragment', () => { test( 'foo[]bar', '', 'foo' + '[]' + 'bar' ); } ); it( 'should wrap position at the end of text node', () => { test( 'foobar{}', '', 'foobar[]' ); } ); it( 'should merge with existing attributes #1', () => { test( 'foo[]', '', 'foo{}' ); } ); it( 'should merge with existing attributes #2', () => { test( '[]foo', '', '{}foo' ); } ); it( 'should wrap when inside nested attributes', () => { test( 'foo{}bar', '', '' + '' + 'foo' + '[]' + 'bar' + '' + '' ); } ); it( 'should merge when wrapping between same attribute', () => { test( '' + 'foo[]bar' + '', '', 'foo{}bar' ); } ); it( 'should move position to text node if in same attribute', () => { test( 'foobar[]', '', 'foobar{}' ); } ); } ); } ); } );