/** * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved. * For licensing, see LICENSE.md. */ /* bender-tags: view, browser-only */ 'use strict'; import { wrapPosition } from '/ckeditor5/engine/view/writer.js'; import Text from '/ckeditor5/engine/view/text.js'; import Element from '/ckeditor5/engine/view/element.js'; import ContainerElement from '/ckeditor5/engine/view/containerelement.js'; import DocumentFragment from '/ckeditor5/engine/view/documentfragment.js'; import Position from '/ckeditor5/engine/view/position.js'; import CKEditorError from '/ckeditor5/utils/ckeditorerror.js'; import { stringify, parse } from '/tests/engine/_utils/view.js'; import AttributeElement from '/ckeditor5/engine/view/attributeelement.js'; describe( 'wrapPosition', () => { /** * 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 ); if ( view instanceof AttributeElement || view instanceof Text ) { view = new DocumentFragment( view ); } const newPosition = wrapPosition( selection.getFirstPosition(), parse( unwrapAttribute ) ); expect( stringify( view, newPosition, { showType: true, showPriority: true } ) ).to.equal( expected ); } it( 'should throw error when element is not instance of AttributeElement', () => { const container = new ContainerElement( 'p', null, new Text( 'foo' ) ); const position = new Position( container, 0 ); const b = new Element( 'b' ); expect( () => { wrapPosition( position, b ); } ).to.throw( CKEditorError, 'view-writer-wrap-invalid-attribute' ); } ); it( 'should wrap position at the beginning of text node', () => { test( '{}foobar', '', '[]foobar' ); } ); it( 'should wrap position inside text node', () => { test( 'foo{}bar', '', 'foo[]bar' ); } ); it( 'should wrap position inside document fragment', () => { test( 'foo[]bar', '', 'foo[]bar' ); } ); it( 'should wrap position at the end of text node', () => { test( 'foobar{}', '', 'foobar[]' ); } ); it( 'should merge with existing attributes #1', () => { test( 'foo[]', '', 'foo{}' ); } ); it( 'should merge with existing attributes #2', () => { test( '[]foo', '', '{}foo' ); } ); it( 'should wrap when inside nested attributes', () => { test( 'foo{}bar', '', '' + 'foo' + '[]' + 'bar' + '' ); } ); it( 'should merge when wrapping between same attribute', () => { test( 'foo[]bar', '', 'foo{}bar' ); } ); it( 'should move position to text node if in same attribute', () => { test( 'foobar[]', '', 'foobar{}' ); } ); } );