/** * @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 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 Document from '../../../src/view/document'; import { expectToThrowCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils'; import { StylesProcessor } from '../../../src/view/stylesmap'; describe( 'DowncastWriter', () => { describe( 'unwrap()', () => { let writer, document; // Executes test using `parse` and `stringify` utils functions. // // @param {String} input // @param {String} unwrapAttribute // @param {String} expected function testUnwrap( input, unwrapAttribute, expected ) { const { view, selection } = parse( input ); const newRange = writer.unwrap( selection.getFirstRange(), parse( unwrapAttribute ) ); expect( stringify( view.root, newRange, { showType: true, showPriority: true } ) ).to.equal( expected ); } beforeEach( () => { document = new Document( new StylesProcessor() ); writer = new DowncastWriter( document ); } ); it( 'should do nothing on collapsed ranges', () => { testUnwrap( 'f{}oo', '', 'f{}oo' ); } ); it( 'should do nothing on single text node', () => { testUnwrap( '[foobar]', '', '[foobar]' ); } ); it( 'should throw error when element is not instance of AttributeElement', () => { const container = new ContainerElement( document, 'p', null, new AttributeElement( document, 'b', null, new Text( 'foo' ) ) ); const range = new Range( new Position( container, 0 ), new Position( container, 1 ) ); const b = new Element( document, 'b' ); expectToThrowCKEditorError( () => { writer.unwrap( range, b ); }, 'view-writer-unwrap-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.unwrap( 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.unwrap( Range._createFromParentsAndOffsets( el, 0, el, 0 ), b ); }, 'view-writer-invalid-range-container', document ); } ); it( 'should unwrap single node', () => { testUnwrap( '[foobar]', '', '[foobar]' ); } ); it( 'should not unwrap attributes with different priorities #1', () => { testUnwrap( '[foobar]', '', '[foobar]' ); } ); it( 'should not unwrap attributes with different priorities #2', () => { testUnwrap( '' + '[' + 'foo' + 'bar' + 'baz' + ']' + '', '', '[foobarbaz]' ); } ); it( 'should unwrap part of the node', () => { testUnwrap( '[bazfoo}bar', '', '[bazfoo]bar' ); } ); it( 'should support unicode', () => { testUnwrap( '[நிலைக்}கு', '', '[நிலைக்]கு' ); } ); it( 'should unwrap nested attributes', () => { testUnwrap( '' + '[foobar]' + '', '', '[foobar]' ); } ); it( 'should unwrap a part of a nested attribute', () => { testUnwrap( '' + 'fo{ob}ar' + '', '', '' + '' + 'fo' + '[ob]' + 'ar' + '' + '' ); } ); it( 'should merge unwrapped nodes #1', () => { testUnwrap( 'foo[bar]baz', '', 'foo{bar}baz' ); } ); it( 'should merge unwrapped nodes #2', () => { const input = '' + 'foo' + 'bar' + '[' + '' + 'bazqux' + '' + ']' + ''; const attribute = ''; const result = 'foobar{bazqux]'; testUnwrap( input, attribute, result ); } ); it( 'should merge unwrapped nodes #3', () => { const input = '' + 'foo' + 'bar' + '[' + '' + 'baz}qux' + '' + ''; const attribute = ''; const result = '' + 'foo' + 'bar{baz]' + '' + 'qux' + '' + ''; testUnwrap( input, attribute, result ); } ); it( 'should merge unwrapped nodes #4', () => { const input = '' + 'foo' + 'bar' + '[' + '' + 'baz' + '' + ']' + 'qux' + ''; const attribute = ''; const result = '' + 'foo' + 'bar{baz}qux' + ''; testUnwrap( input, attribute, result ); } ); it( 'should merge unwrapped nodes #5', () => { const input = '' + '[' + 'foo' + 'bar' + 'baz' + ']' + ''; const attribute = ''; const result = '[foobarbaz]'; testUnwrap( input, attribute, result ); } ); it( 'should unwrap mixed ranges #1', () => { const input = '' + '[' + '' + 'foo]' + '' + ''; const attribute = ''; const result = '[foo]'; testUnwrap( input, attribute, result ); } ); it( 'should unwrap mixed ranges #2', () => { testUnwrap( '' + '[foo}' + '', '', '[foo]' ); } ); it( 'should unwrap single element by removing matching attributes', () => { testUnwrap( '[test]', '', '[test]' ); } ); it( 'should not unwrap single element when attributes are different', () => { testUnwrap( '[test]', '', '[test]' ); } ); it( 'should unwrap single element by removing matching classes', () => { testUnwrap( '[test]', '', '[test]' ); } ); it( 'should not unwrap single element when classes are different', () => { testUnwrap( '[test]', '', '[test]' ); } ); it( 'should unwrap single element by removing matching styles', () => { testUnwrap( '' + '[test]' + '', '', '[test]' ); } ); it( 'should not unwrap single element when styles are different', () => { testUnwrap( '' + '[test]' + '', '', '' + '[test]' + '' ); } ); it( 'should partially unwrap part of a node', () => { testUnwrap( '' + '[foo}bar' + '', '', '' + '[foo]' + 'bar' + '' ); } ); it( 'should partially unwrap a nested attribute', () => { testUnwrap( '' + '[' + 'test' + ']' + '', '', '' + '[' + 'test' + ']' + '' ); } ); it( 'should partially unwrap a part of a nested attribute', () => { testUnwrap( '' + '' + 't{es}t' + '' + '', '', '' + '' + 't' + '[es]' + 't' + '' + '' ); } ); it( 'should be merged after being partially unwrapped', () => { testUnwrap( '' + 'xyz' + '[foo}bar' + '', '', '' + 'xyz{foo]' + 'bar' + '' ); } ); it( 'should unwrap single node in document fragment', () => { testUnwrap( '[foobar]', '', '[foobar]' ); } ); it( 'should unwrap EmptyElement', () => { testUnwrap( '[]', '', '[]' ); } ); it( 'should throw if range is inside EmptyElement', () => { const empty = new EmptyElement( document, 'img' ); const attribute = new AttributeElement( document, 'b' ); const container = new ContainerElement( document, 'p', null, [ empty, attribute ] ); const range = Range._createFromParentsAndOffsets( empty, 0, container, 2 ); expectToThrowCKEditorError( () => { writer.unwrap( range, attribute ); }, 'view-writer-cannot-break-empty-element', document ); } ); it( 'should unwrap UIElement', () => { testUnwrap( '[]', '', '[]' ); } ); it( 'should throw if range is placed inside UIElement', () => { const uiElement = new UIElement( document, 'span' ); const attribute = new AttributeElement( document, 'b' ); const container = new ContainerElement( document, 'p', null, [ uiElement, attribute ] ); const range = Range._createFromParentsAndOffsets( uiElement, 0, container, 2 ); expectToThrowCKEditorError( () => { writer.unwrap( range, attribute ); }, 'view-writer-cannot-break-ui-element', document ); } ); it( 'should unwrap if both elements have same id', () => { const unwrapper = writer.createAttributeElement( 'span', null, { id: 'foo' } ); const attribute = writer.createAttributeElement( 'span', null, { id: 'foo' } ); const container = writer.createContainerElement( 'div' ); writer.insert( writer.createPositionAt( container, 0 ), attribute ); writer.unwrap( Range._createOn( attribute ), unwrapper ); expect( stringify( container, null, { showType: false, showPriority: false } ) ).to.equal( '
' ); } ); it( 'should always unwrap whole element if both elements have same id', () => { const unwrapper = writer.createAttributeElement( 'b', null, { id: 'foo' } ); const attribute = writer.createAttributeElement( 'span', { foo: 'foo' }, { id: 'foo' } ); const container = writer.createContainerElement( 'div' ); writer.insert( writer.createPositionAt( container, 0 ), attribute ); writer.unwrap( writer.createRangeOn( attribute ), unwrapper ); expect( stringify( container, null, { showType: false, showPriority: false } ) ).to.equal( '
' ); } ); // Below are tests for elements with different ids. // Only partial unwrapping is tested because elements with different ids are never similar. // This means that unwrapping of whole element is not possible in this case. it( 'should not unwrap matching attributes if the element to unwrap has id', () => { const unwrapper = writer.createAttributeElement( 'span', { foo: 'foo' } ); const attribute = writer.createAttributeElement( 'span', { foo: 'foo', bar: 'bar' }, { id: 'id' } ); const container = writer.createContainerElement( 'div' ); writer.insert( writer.createPositionAt( container, 0 ), attribute ); writer.unwrap( writer.createRangeOn( attribute ), unwrapper ); const view = stringify( container, null, { showType: false, showPriority: false } ); expect( view ).to.equal( '
' ); } ); it( 'should not unwrap matching attributes if the unwrapping element has id', () => { const unwrapper = writer.createAttributeElement( 'span', { foo: 'foo' }, { id: 'id' } ); const attribute = writer.createAttributeElement( 'span', { foo: 'foo', bar: 'bar' } ); const container = writer.createContainerElement( 'div' ); writer.insert( writer.createPositionAt( container, 0 ), attribute ); writer.unwrap( writer.createRangeOn( attribute ), unwrapper ); const view = stringify( container, null, { showType: false, showPriority: false } ); expect( view ).to.equal( '
' ); } ); it( 'should not unwrap matching attributes if the elements have different id', () => { const unwrapper = writer.createAttributeElement( 'span', { foo: 'foo' }, { id: 'a' } ); const attribute = writer.createAttributeElement( 'span', { foo: 'foo', bar: 'bar' }, { id: 'b' } ); const container = writer.createContainerElement( 'div' ); writer.insert( writer.createPositionAt( container, 0 ), attribute ); writer.unwrap( writer.createRangeOn( attribute ), unwrapper ); const view = stringify( container, null, { showType: false, showPriority: false } ); expect( view ).to.equal( '
' ); } ); } ); } );