| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- /**
- * @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 DocumentFragment from '/ckeditor5/engine/treeview/documentfragment.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( '<p><b>foobar</b></p>' );
- } );
- 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( '<p class="short wide" baz="qux" bar="taz"><b foo="bar">foobar</b></p>' );
- } );
- 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( '<p><b>foobar</b>[<b>bazqux</b>]</p>' );
- } );
- 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( '<p>[]foobar</p>' );
- } );
- 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( '<p><b>f{ooba}r</b><b>bazqux</b></p>' );
- } );
- 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( '<p>[<b>foobar</b><b>bazqu}x</b></p>' );
- } );
- 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, { showType: true } ) )
- .to.equal( '<container:p><attribute:b>foobar</attribute:b></container:p>' );
- } );
- it( 'should write elements priorities 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, { showType: true, showPriority: true } ) )
- .to.equal( '<container:p><attribute:b priority=10>foobar</attribute:b></container:p>' );
- } );
- it( 'should parse DocumentFragment as root', () => {
- 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 fragment = new DocumentFragment( [ b1, b2 ] );
- expect( getData( fragment, null ) ).to.equal( '<b>foobar</b><b>bazqux</b>' );
- } );
- // TODO: test when range is outside element/text
- } );
- } );
|