/** * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved. * For licensing, see LICENSE.md. */ 'use strict'; import { getData } from '/tests/engine/_utils/view.js'; import Element from '/ckeditor5/engine/treeview/element.js'; import AttributeElement from '/ckeditor5/engine/treeview/attributeelement.js'; import ContainerElement from '/ckeditor5/engine/treeview/containerelement.js'; import Text from '/ckeditor5/engine/treeview/text.js'; import Selection from '/ckeditor5/engine/treeview/selection.js'; import Range from '/ckeditor5/engine/treeview/range.js'; describe( 'view test utils', () => { describe( 'getData', () => { it( 'should write elements and texts', () => { const text = new Text( 'foobar' ); const b = new Element( 'b', null, text ); const p = new Element( 'p', null, b ); expect( getData( p ) ).to.equal( '

foobar

' ); } ); it( 'should write elements with attributes', () => { const text = new Text( 'foobar' ); const b = new Element( 'b', { foo: 'bar' }, text ); const p = new Element( 'p', { baz: 'qux', bar: 'taz', class: 'short wide' }, b ); expect( getData( p ) ).to.equal( '

foobar

' ); } ); it( 'should write selection ranges inside elements', () => { const text1 = new Text( 'foobar' ); const text2 = new Text( 'bazqux' ); const b1 = new Element( 'b', null, text1 ); const b2 = new Element( 'b', null, text2 ); const p = new Element( 'p', null, [ b1, b2 ] ); const range = Range.createFromParentsAndOffsets( p, 1, p, 2 ); const selection = new Selection(); selection.addRange( range ); expect( getData( p, selection ) ).to.equal( '

foobar[bazqux]

' ); } ); it( 'should write collapsed selection ranges inside elements', () => { const text = new Text( 'foobar' ); const p = new Element( 'p', null, text ); const range = Range.createFromParentsAndOffsets( p, 0, p, 0 ); const selection = new Selection(); selection.addRange( range ); expect( getData( p, selection ) ).to.equal( '

[]foobar

' ); } ); it( 'should write selection ranges inside text', () => { const text1 = new Text( 'foobar' ); const text2 = new Text( 'bazqux' ); const b1 = new Element( 'b', null, text1 ); const b2 = new Element( 'b', null, text2 ); const p = new Element( 'p', null, [ b1, b2 ] ); const range = Range.createFromParentsAndOffsets( text1, 1, text1, 5 ); const selection = new Selection(); selection.addRange( range ); expect( getData( p, selection ) ).to.equal( '

f{ooba}rbazqux

' ); } ); it( 'should write ranges that start inside text end ends between elements', () => { const text1 = new Text( 'foobar' ); const text2 = new Text( 'bazqux' ); const b1 = new Element( 'b', null, text1 ); const b2 = new Element( 'b', null, text2 ); const p = new Element( 'p', null, [ b1, b2 ] ); const range = Range.createFromParentsAndOffsets( p, 0, text2, 5 ); const selection = new Selection(); selection.addRange( range ); expect( getData( p, selection ) ).to.equal( '

[foobarbazqu}x

' ); } ); it( 'should write elements types as namespaces when needed', () => { const text = new Text( 'foobar' ); const b = new AttributeElement( 'b', null, text ); const p = new ContainerElement( 'p', null, b ); expect( getData( p, null, { showTypes: true } ) ) .to.equal( 'foobar' ); } ); } ); } );