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