/**
* @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 Document from '../../../src/view/document';
import { stringify, parse } from '../../../src/dev-utils/view';
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 Range from '../../../src/view/range';
import Position from '../../../src/view/position';
import { expectToThrowCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
describe( 'DowncastWriter', () => {
describe( 'breakAttributes()', () => {
let writer, document;
beforeEach( () => {
document = new Document();
writer = new DowncastWriter( document );
} );
describe( 'break position', () => {
/**
* Executes test using `parse` and `stringify` utils functions. Uses range delimiters `[]{}` to create and
* test break position.
*
* @param {String} input
* @param {String} expected
*/
function testBreakAttributes( input, expected ) {
const { view, selection } = parse( input );
const newPosition = writer.breakAttributes( selection.getFirstPosition() );
expect( stringify( view.root, newPosition, {
showType: true,
showPriority: true
} ) ).to.equal( expected );
}
it( 'should not break text nodes if they are not in attribute elements - middle', () => {
testBreakAttributes(
'foo{}bar',
'foo{}bar'
);
} );
it( 'should not break text nodes if they are not in attribute elements - beginning', () => {
testBreakAttributes(
'{}foobar',
'{}foobar'
);
} );
it( 'should not break text nodes if they are not in attribute elements #2 - end', () => {
testBreakAttributes(
'foobar{}',
'foobar{}'
);
} );
it( 'should split attribute element', () => {
testBreakAttributes(
'foo{}bar',
'' +
'foo[]bar' +
''
);
} );
it( 'should move from beginning of the nested text node to the container', () => {
testBreakAttributes(
'' +
'{}foobar' +
'',
'' +
'[]foobar' +
''
);
} );
it( 'should stick selection in text node if it is in container', () => {
testBreakAttributes(
'foo{}bar',
'foo{}bar'
);
} );
it( 'should split nested attributes', () => {
testBreakAttributes(
'' +
'foo{}bar' +
'',
'' +
'' +
'' +
'foo' +
'' +
'' +
'[]' +
'' +
'' +
'bar' +
'' +
'' +
''
);
} );
it( 'should move from end of the nested text node to the container', () => {
testBreakAttributes(
'' +
'foobar{}' +
'',
'' +
'foobar[]' +
''
);
} );
it( 'should split attribute element directly in document fragment', () => {
testBreakAttributes(
'foo{}bar',
'foo[]bar'
);
} );
it( 'should not split text directly in document fragment', () => {
testBreakAttributes(
'foo{}bar',
'foo{}bar'
);
} );
} );
describe( 'break range', () => {
/**
* Executes test using `parse` and `stringify` utils functions.
*
* @param {String} input
* @param {String} expected
*/
function testBreak( input, expected ) {
const { view, selection } = parse( input );
const newRange = writer.breakAttributes( selection.getFirstRange() );
expect( stringify( view.root, newRange, { showType: true } ) ).to.equal( expected );
}
it( 'should throw when range placed in two containers', () => {
const p1 = new ContainerElement( 'p' );
const p2 = new ContainerElement( 'p' );
expectToThrowCKEditorError( () => {
writer.breakAttributes( Range._createFromParentsAndOffsets( p1, 0, p2, 0 ) );
}, 'view-writer-invalid-range-container', document );
} );
it( 'should throw when range has no parent container', () => {
const el = new AttributeElement( 'b' );
expectToThrowCKEditorError( () => {
writer.breakAttributes( Range._createFromParentsAndOffsets( el, 0, el, 0 ) );
}, 'view-writer-invalid-range-container', document );
} );
it( 'should not break text nodes if they are not in attribute elements', () => {
testBreak(
'foo{}bar',
'foo{}bar'
);
} );
it( 'should break at collapsed range and return collapsed one', () => {
testBreak(
'foo{}bar',
'foo[]bar'
);
} );
it( 'should break inside text node #1', () => {
testBreak(
'foo{bar}baz',
'' +
'foo[bar]baz' +
''
);
} );
it( 'should break inside text node #2', () => {
testBreak(
'foo{barbaz}',
'foo[barbaz]'
);
} );
it( 'should break inside text node #3', () => {
testBreak(
'foo{barbaz]',
'foo[barbaz]'
);
} );
it( 'should break inside text node #4', () => {
testBreak(
'{foo}barbaz',
'[foo]barbaz'
);
} );
it( 'should break inside text node #5', () => {
testBreak(
'[foo}barbaz',
'[foo]barbaz'
);
} );
it( 'should break placed inside different nodes', () => {
testBreak(
'foo{barbaz}qux',
'foo{barbaz]qux'
);
} );
it( 'should split attribute element directly in document fragment', () => {
testBreak(
'fo{ob}ar',
'fo[ob]ar'
);
} );
it( 'should not split text directly in document fragment', () => {
testBreak(
'foo{}bar',
'foo{}bar'
);
} );
it( 'should throw if breaking inside EmptyElement #1', () => {
const img = new EmptyElement( 'img' );
new ContainerElement( 'p', null, img ); // eslint-disable-line no-new
const position = new Position( img, 0 );
expectToThrowCKEditorError( () => {
writer.breakAttributes( position );
}, 'view-writer-cannot-break-empty-element', writer );
} );
it( 'should throw if breaking inside EmptyElement #2', () => {
const img = new EmptyElement( 'img' );
const b = new AttributeElement( 'b' );
new ContainerElement( 'p', null, [ img, b ] ); // eslint-disable-line no-new
const range = Range._createFromParentsAndOffsets( img, 0, b, 0 );
expectToThrowCKEditorError( () => {
writer.breakAttributes( range );
}, 'view-writer-cannot-break-empty-element', writer );
} );
it( 'should throw if breaking inside UIElement #1', () => {
const span = new UIElement( 'span' );
new ContainerElement( 'p', null, span ); // eslint-disable-line no-new
const position = new Position( span, 0 );
expectToThrowCKEditorError( () => {
writer.breakAttributes( position );
}, 'view-writer-cannot-break-ui-element', writer );
} );
it( 'should throw if breaking inside UIElement #2', () => {
const span = new UIElement( 'span' );
const b = new AttributeElement( 'b' );
new ContainerElement( 'p', null, [ span, b ] ); // eslint-disable-line no-new
const range = Range._createFromParentsAndOffsets( span, 0, b, 0 );
expectToThrowCKEditorError( () => {
writer.breakAttributes( range );
}, 'view-writer-cannot-break-ui-element', writer );
} );
} );
} );
} );