/**
* @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.
*/
/* bender-tags: view, browser-only */
import { unwrap } from 'ckeditor5/engine/view/writer.js';
import Element from 'ckeditor5/engine/view/element.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 Position from 'ckeditor5/engine/view/position.js';
import Range from 'ckeditor5/engine/view/range.js';
import Text from 'ckeditor5/engine/view/text.js';
import CKEditorError from 'ckeditor5/utils/ckeditorerror.js';
import { stringify, parse } from 'ckeditor5/engine/dev-utils/view.js';
describe( 'writer', () => {
/**
* Executes test using `parse` and `stringify` utils functions.
*
* @param {String} input
* @param {String} unwrapAttribute
* @param {String} expected
*/
function test( input, unwrapAttribute, expected ) {
let { view, selection } = parse( input );
const newRange = unwrap( selection.getFirstRange(), parse( unwrapAttribute ) );
expect( stringify( view.root, newRange, { showType: true, showPriority: true } ) ).to.equal( expected );
}
describe( 'unwrap', () => {
it( 'should do nothing on collapsed ranges', () => {
test(
'f{}oo',
'',
'f{}oo'
);
} );
it( 'should do nothing on single text node', () => {
test(
'[foobar]',
'',
'[foobar]'
);
} );
it( 'should throw error when element is not instance of AttributeElement', () => {
const container = new ContainerElement( 'p', null, new AttributeElement( 'b', null, new Text( 'foo' ) ) );
const range = new Range(
new Position( container, 0 ),
new Position( container, 1 )
);
const b = new Element( 'b' );
expect( () => {
unwrap( range, b );
} ).to.throw( CKEditorError, 'view-writer-unwrap-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( () => {
unwrap( 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( () => {
unwrap( Range.createFromParentsAndOffsets( el, 0, el, 0 ), b );
} ).to.throw( CKEditorError, 'view-writer-invalid-range-container' );
} );
it( 'should unwrap single node', () => {
test(
'[foobar]',
'',
'[foobar]'
);
} );
it( 'should not unwrap attributes with different priorities #1', () => {
test(
'[foobar]',
'',
'[foobar]'
);
} );
it( 'should not unwrap attributes with different priorities #2', () => {
test(
'' +
'[' +
'foo' +
'bar' +
'baz' +
']' +
'',
'',
'[foobarbaz]'
);
} );
it( 'should unwrap part of the node', () => {
test(
'[bazfoo}bar',
'',
'[bazfoo]bar'
);
} );
it( 'should support unicode', () => {
test(
'[நிலைக்}கு',
'',
'[நிலைக்]கு'
);
} );
it( 'should unwrap nested attributes', () => {
test(
'[foobar]',
'',
'[foobar]'
);
} );
it( 'should merge unwrapped nodes #1', () => {
test(
'foo[bar]baz',
'',
'foo{bar}baz'
);
} );
it( 'should merge unwrapped nodes #2', () => {
const input = '' +
'foo' +
'bar' +
'[' +
'' +
'bazqux' +
'' +
']' +
'';
const attribute = '';
const result = 'foobar{bazqux]';
test( input, attribute, result );
} );
it( 'should merge unwrapped nodes #3', () => {
const input = '' +
'foo' +
'bar' +
'[' +
'' +
'baz}qux' +
'' +
'';
const attribute = '';
const result = '' +
'foo' +
'bar{baz]' +
'' +
'qux' +
'' +
'';
test( input, attribute, result );
} );
it( 'should merge unwrapped nodes #4', () => {
const input = '' +
'foo' +
'bar' +
'[' +
'' +
'baz' +
'' +
']' +
'qux' +
'';
const attribute = '';
const result = '' +
'foo' +
'bar{baz}qux' +
'';
test( input, attribute, result );
} );
it( 'should merge unwrapped nodes #5', () => {
const input = '' +
'[' +
'foo' +
'bar' +
'baz' +
']' +
'';
const attribute = '';
const result = '[foobarbaz]';
test( input, attribute, result );
} );
it( 'should unwrap mixed ranges #1', () => {
const input = '' +
'[' +
'' +
'foo]' +
'' +
'';
const attribute = '';
const result = '[foo]';
test( input, attribute, result );
} );
it( 'should unwrap mixed ranges #2', () => {
test(
'[foo}',
'',
'[foo]'
);
} );
it( 'should unwrap single element by removing matching attributes', () => {
test(
'[test]',
'',
'[test]'
);
} );
it( 'should not unwrap single element when attributes are different', () => {
test(
'[test]',
'',
'[test]'
);
} );
it( 'should unwrap single element by removing matching classes', () => {
test(
'[test]',
'',
'[test]'
);
} );
it( 'should not unwrap single element when classes are different', () => {
test(
'[test]',
'',
'[test]'
);
} );
it( 'should unwrap single element by removing matching styles', () => {
test(
'' +
'[test]' +
'',
'',
'[test]'
);
} );
it( 'should not unwrap single element when styles are different', () => {
test(
'' +
'[test]' +
'',
'',
'' +
'[test]' +
''
);
} );
it( 'should unwrap single node in document fragment', () => {
test(
'[foobar]',
'',
'[foobar]'
);
} );
it( 'should unwrap EmptyElement', () => {
test(
'[]',
'',
'[]'
);
} );
it( 'should throw if range is inside EmptyElement', () => {
const empty = new EmptyElement( 'img' );
const attribute = new AttributeElement( 'b' );
const container = new ContainerElement( 'p', null, [ empty, attribute ] );
const range = Range.createFromParentsAndOffsets( empty, 0, container, 2 );
expect( () => {
unwrap( range, attribute );
} ).to.throw( CKEditorError, 'view-writer-cannot-break-empty-element' );
} );
} );
} );