/** * @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 ContainerElement from '../../../src/view/containerelement'; import Text from '../../../src/view/text'; import Position from '../../../src/view/position'; import { stringify, parse } from '../../../src/dev-utils/view'; import Document from '../../../src/view/document'; import { StylesProcessor } from '../../../src/view/stylesmap'; describe( 'DowncastWriter', () => { describe( 'mergeAttributes', () => { let writer, document; // Executes test using `parse` and `stringify` utils functions. Uses range delimiters `[]{}` to create and // test merge position. // // @param {String} input // @param {String} expected function testMerge( input, expected ) { const { view, selection } = parse( input ); const newPosition = writer.mergeAttributes( selection.getFirstPosition() ); expect( stringify( view, newPosition, { showType: true, showPriority: true } ) ).to.equal( expected ); } before( () => { document = new Document( new StylesProcessor() ); writer = new DowncastWriter( document ); } ); it( 'should not merge if inside text node', () => { testMerge( 'fo{}bar', 'fo{}bar' ); } ); it( 'should not merge if between containers', () => { testMerge( 'foo[]bar', 'foo[]bar' ); } ); it( 'should return same position when inside empty container', () => { testMerge( '[]', '[]' ); } ); it( 'should not merge when position is placed at the beginning of the container', () => { testMerge( '[]', '[]' ); } ); it( 'should not merge when position is placed at the end of the container', () => { testMerge( '[]', '[]' ); } ); it( 'should merge when placed between two text nodes', () => { //

foobar

->

foo|bar

const t1 = new Text( document, 'foo' ); const t2 = new Text( document, 'bar' ); const p = new ContainerElement( document, 'p', null, [ t1, t2 ] ); const position = new Position( p, 1 ); const newPosition = writer.mergeAttributes( position ); expect( stringify( p, newPosition ) ).to.equal( '

foo{}bar

' ); } ); it( 'should merge when placed between similar attribute nodes', () => { testMerge( '' + 'baz[]' + 'qux' + '', 'baz{}qux' ); } ); it( 'should not merge when placed between non-similar attribute nodes', () => { testMerge( '' + '[]' + '', '' + '[]' + '' ); } ); it( 'should not merge when placed between similar attribute nodes with different priority', () => { testMerge( '' + '[]' + '', '' + '[]' + '' ); } ); it( 'should merge attribute nodes and their contents if possible', () => { testMerge( '' + 'foo[]' + 'bar' + '', 'foo{}bar' ); } ); it( 'should remove empty attributes after merge #1', () => { testMerge( '[]', '[]' ); } ); it( 'should remove empty attributes after merge #2', () => { testMerge( 'foo[]bar', 'foo{}bar' ); } ); it( 'should remove empty attributes after merge #3', () => { testMerge( '[]', '[]' ); } ); it( 'should not merge when placed between EmptyElements', () => { testMerge( '[]', '[]' ); } ); it( 'should not merge when placed between UIElements', () => { testMerge( '[]', '[]' ); } ); it( 'should not merge when placed between RawElements', () => { testMerge( '[]', '[]' ); } ); } ); } );