| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177 |
- /**
- * @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 Document from '../../src/view/document';
- import DocumentFragment from '../../src/view/documentfragment';
- import AttributeElement from '../../src/view/attributeelement';
- import ContainerElement from '../../src/view/containerelement';
- import Text from '../../src/view/text';
- import TreeWalker from '../../src/view/treewalker';
- import Position from '../../src/view/position';
- import Range from '../../src/view/range';
- import createViewRoot from './_utils/createroot';
- import { expectToThrowCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
- import { StylesProcessor } from '../../src/view/stylesmap';
- describe( 'TreeWalker', () => {
- let doc, root, img1, paragraph, bold, textAbcd, charY, img2, charX, rootBeginning, rootEnding;
- before( () => {
- doc = new Document( new StylesProcessor() );
- root = createViewRoot( doc );
- // root
- // |- img1
- // |- p
- // |- b
- // | |- abcd
- // |
- // |- Y
- // |
- // |- img2
- // |
- // |- X
- textAbcd = new Text( doc, 'abcd' );
- bold = new AttributeElement( doc, 'b', null, [ textAbcd ] );
- charY = new Text( doc, 'y' );
- img2 = new ContainerElement( doc, 'img2' );
- charX = new Text( doc, 'x' );
- paragraph = new ContainerElement( doc, 'p', null, [ bold, charY, img2, charX ] );
- img1 = new ContainerElement( doc, 'img1' );
- root._insertChild( 0, [ img1, paragraph ] );
- rootBeginning = new Position( root, 0 );
- rootEnding = new Position( root, 2 );
- } );
- describe( 'constructor()', () => {
- it( 'should throw if neither boundaries nor starting position is set', () => {
- expectToThrowCKEditorError( () => {
- new TreeWalker(); // eslint-disable-line no-new
- }, /^view-tree-walker-no-start-position/, null );
- expectToThrowCKEditorError( () => {
- new TreeWalker( {} ); // eslint-disable-line no-new
- }, /^view-tree-walker-no-start-position/, null );
- expectToThrowCKEditorError( () => {
- new TreeWalker( { singleCharacters: true } ); // eslint-disable-line no-new
- }, /^view-tree-walker-no-start-position/, null );
- } );
- it( 'should throw if walking direction is unknown', () => {
- expectToThrowCKEditorError( () => {
- new TreeWalker( { startPosition: rootBeginning, direction: 'unknown' } ); // eslint-disable-line no-new
- }, /^view-tree-walker-unknown-direction/, doc );
- } );
- } );
- describe( 'iterate from start position `startPosition`', () => {
- let expected;
- beforeEach( () => {
- expected = [
- {
- type: 'elementStart',
- item: img1,
- previousPosition: new Position( root, 0 ),
- nextPosition: new Position( img1, 0 )
- },
- {
- type: 'elementEnd',
- item: img1,
- previousPosition: new Position( img1, 0 ),
- nextPosition: new Position( root, 1 )
- },
- {
- type: 'elementStart',
- item: paragraph,
- previousPosition: new Position( root, 1 ),
- nextPosition: new Position( paragraph, 0 )
- },
- {
- type: 'elementStart',
- item: bold,
- previousPosition: new Position( paragraph, 0 ),
- nextPosition: new Position( bold, 0 )
- },
- {
- type: 'text',
- text: 'abcd',
- previousPosition: new Position( bold, 0 ),
- nextPosition: new Position( bold, 1 )
- },
- {
- type: 'elementEnd',
- item: bold,
- previousPosition: new Position( bold, 1 ),
- nextPosition: new Position( paragraph, 1 )
- },
- {
- type: 'text',
- text: 'y',
- previousPosition: new Position( paragraph, 1 ),
- nextPosition: new Position( paragraph, 2 )
- },
- {
- type: 'elementStart',
- item: img2,
- previousPosition: new Position( paragraph, 2 ),
- nextPosition: new Position( img2, 0 )
- },
- {
- type: 'elementEnd',
- item: img2,
- previousPosition: new Position( img2, 0 ),
- nextPosition: new Position( paragraph, 3 )
- },
- {
- type: 'text',
- text: 'x',
- previousPosition: new Position( paragraph, 3 ),
- nextPosition: new Position( paragraph, 4 )
- },
- {
- type: 'elementEnd',
- item: paragraph,
- previousPosition: new Position( paragraph, 4 ),
- nextPosition: new Position( root, 2 )
- }
- ];
- } );
- it( 'should provide iterator interface with default forward direction', () => {
- const iterator = new TreeWalker( { startPosition: rootBeginning } );
- let i = 0;
- for ( const value of iterator ) {
- expectValue( value, expected[ i++ ] );
- }
- expect( i ).to.equal( expected.length );
- } );
- it( 'should provide iterator interface with forward direction', () => {
- const iterator = new TreeWalker( { startPosition: rootBeginning, direction: 'forward' } );
- let i = 0;
- for ( const value of iterator ) {
- expectValue( value, expected[ i++ ] );
- }
- expect( i ).to.equal( expected.length );
- } );
- it( 'should provide iterator interface which backward direction', () => {
- const iterator = new TreeWalker( { startPosition: rootEnding, direction: 'backward' } );
- let i = expected.length;
- for ( const value of iterator ) {
- expectValue( value, expected[ --i ], { direction: 'backward' } );
- }
- expect( i ).to.equal( 0 );
- } );
- it( 'should start iterating at the startPosition witch is not a root bound', () => {
- const iterator = new TreeWalker( { startPosition: new Position( root, 1 ) } );
- let i = 2;
- for ( const value of iterator ) {
- expectValue( value, expected[ i++ ] );
- }
- expect( i ).to.equal( expected.length );
- } );
- it( 'should start iterating at the startPosition witch is not a root bound, going backward', () => {
- const expected = [
- {
- type: 'elementStart',
- item: img1,
- previousPosition: new Position( root, 0 ),
- nextPosition: new Position( img1, 0 )
- },
- {
- type: 'elementEnd',
- item: img1,
- previousPosition: new Position( img1, 0 ),
- nextPosition: new Position( root, 1 )
- }
- ];
- const iterator = new TreeWalker( { startPosition: new Position( root, 1 ), direction: 'backward' } );
- let i = expected.length;
- for ( const value of iterator ) {
- expectValue( value, expected[ --i ], { direction: 'backward' } );
- }
- expect( i ).to.equal( 0 );
- } );
- } );
- describe( 'iterate trough the range `boundary`', () => {
- describe( 'range starts between elements', () => {
- let expected, range;
- before( () => {
- expected = [
- {
- type: 'elementStart',
- item: paragraph,
- previousPosition: new Position( root, 1 ),
- nextPosition: new Position( paragraph, 0 )
- },
- {
- type: 'elementStart',
- item: bold,
- previousPosition: new Position( paragraph, 0 ),
- nextPosition: new Position( bold, 0 )
- },
- {
- type: 'text',
- text: 'abcd',
- previousPosition: new Position( bold, 0 ),
- nextPosition: new Position( bold, 1 )
- },
- {
- type: 'elementEnd',
- item: bold,
- previousPosition: new Position( bold, 1 ),
- nextPosition: new Position( paragraph, 1 )
- },
- {
- type: 'text',
- text: 'y',
- previousPosition: new Position( paragraph, 1 ),
- nextPosition: new Position( paragraph, 2 )
- },
- {
- type: 'elementStart',
- item: img2,
- previousPosition: new Position( paragraph, 2 ),
- nextPosition: new Position( img2, 0 )
- },
- {
- type: 'elementEnd',
- item: img2,
- previousPosition: new Position( img2, 0 ),
- nextPosition: new Position( paragraph, 3 )
- }
- ];
- range = Range._createFromParentsAndOffsets( root, 1, paragraph, 3 );
- } );
- it( 'should iterating over the range', () => {
- const iterator = new TreeWalker( { boundaries: range } );
- let i = 0;
- for ( const value of iterator ) {
- expectValue( value, expected[ i++ ] );
- }
- expect( i ).to.equal( expected.length );
- } );
- it( 'should iterating over the range going backward', () => {
- const iterator = new TreeWalker( { boundaries: range, direction: 'backward' } );
- let i = expected.length;
- for ( const value of iterator ) {
- expectValue( value, expected[ --i ], { direction: 'backward' } );
- }
- expect( i ).to.equal( 0 );
- } );
- } );
- describe( 'range starts inside the text', () => {
- let expected, range;
- before( () => {
- expected = [
- {
- type: 'text',
- text: 'bcd',
- previousPosition: new Position( textAbcd, 1 ),
- nextPosition: new Position( bold, 1 )
- },
- {
- type: 'elementEnd',
- item: bold,
- previousPosition: new Position( bold, 1 ),
- nextPosition: new Position( paragraph, 1 )
- },
- {
- type: 'text',
- text: 'y',
- previousPosition: new Position( paragraph, 1 ),
- nextPosition: new Position( paragraph, 2 )
- },
- {
- type: 'elementStart',
- item: img2,
- previousPosition: new Position( paragraph, 2 ),
- nextPosition: new Position( img2, 0 )
- },
- {
- type: 'elementEnd',
- item: img2,
- previousPosition: new Position( img2, 0 ),
- nextPosition: new Position( paragraph, 3 )
- }
- ];
- range = Range._createFromParentsAndOffsets( textAbcd, 1, paragraph, 3 );
- } );
- it( 'should return part of the text', () => {
- const iterator = new TreeWalker( { boundaries: range } );
- let i = 0;
- for ( const value of iterator ) {
- expectValue( value, expected[ i++ ] );
- }
- expect( i ).to.equal( expected.length );
- } );
- it( 'should return part of the text going backward', () => {
- const iterator = new TreeWalker( {
- boundaries: range,
- direction: 'backward'
- }
- );
- let i = expected.length;
- for ( const value of iterator ) {
- expectValue( value, expected[ --i ], { direction: 'backward' } );
- }
- expect( i ).to.equal( 0 );
- } );
- } );
- describe( 'range ends inside the text', () => {
- let expected, range;
- before( () => {
- expected = [
- {
- type: 'elementStart',
- item: img1,
- previousPosition: new Position( root, 0 ),
- nextPosition: new Position( img1, 0 )
- },
- {
- type: 'elementEnd',
- item: img1,
- previousPosition: new Position( img1, 0 ),
- nextPosition: new Position( root, 1 )
- },
- {
- type: 'elementStart',
- item: paragraph,
- previousPosition: new Position( root, 1 ),
- nextPosition: new Position( paragraph, 0 )
- },
- {
- type: 'elementStart',
- item: bold,
- previousPosition: new Position( paragraph, 0 ),
- nextPosition: new Position( bold, 0 )
- },
- {
- type: 'text',
- text: 'ab',
- previousPosition: new Position( bold, 0 ),
- nextPosition: new Position( textAbcd, 2 )
- }
- ];
- range = new Range( rootBeginning, new Position( textAbcd, 2 ) );
- } );
- it( 'should return part of the text', () => {
- const iterator = new TreeWalker( { boundaries: range } );
- let i = 0;
- for ( const value of iterator ) {
- expectValue( value, expected[ i++ ] );
- }
- expect( i ).to.equal( expected.length );
- } );
- it( 'should return part of the text going backward', () => {
- const iterator = new TreeWalker( {
- boundaries: range,
- startPosition: range.end,
- direction: 'backward'
- } );
- let i = expected.length;
- for ( const value of iterator ) {
- expectValue( value, expected[ --i ], { direction: 'backward' } );
- }
- expect( i ).to.equal( 0 );
- } );
- } );
- describe( 'range starts and ends inside the same text', () => {
- let expected, range;
- before( () => {
- expected = [
- {
- type: 'text',
- text: 'bc',
- previousPosition: new Position( textAbcd, 1 ),
- nextPosition: new Position( textAbcd, 3 )
- }
- ];
- range = new Range( new Position( textAbcd, 1 ), new Position( textAbcd, 3 ) );
- } );
- it( 'should return part of the text', () => {
- const iterator = new TreeWalker( { boundaries: range } );
- let i = 0;
- for ( const value of iterator ) {
- expectValue( value, expected[ i++ ] );
- }
- expect( i ).to.equal( expected.length );
- } );
- it( 'should return part of the text going backward', () => {
- const iterator = new TreeWalker( {
- boundaries: range,
- startPosition: range.end,
- direction: 'backward'
- } );
- let i = expected.length;
- for ( const value of iterator ) {
- expectValue( value, expected[ --i ], { direction: 'backward' } );
- }
- expect( i ).to.equal( 0 );
- } );
- } );
- describe( 'custom start position', () => {
- it( 'should iterating from the start position', () => {
- const expected = [
- {
- type: 'text',
- text: 'y',
- previousPosition: new Position( paragraph, 1 ),
- nextPosition: new Position( paragraph, 2 )
- },
- {
- type: 'elementStart',
- item: img2,
- previousPosition: new Position( paragraph, 2 ),
- nextPosition: new Position( img2, 0 )
- },
- {
- type: 'elementEnd',
- item: img2,
- previousPosition: new Position( img2, 0 ),
- nextPosition: new Position( paragraph, 3 )
- }
- ];
- const range = Range._createFromParentsAndOffsets( bold, 1, paragraph, 3 );
- const iterator = new TreeWalker( {
- boundaries: range,
- startPosition: new Position( paragraph, 1 )
- } );
- let i = 0;
- for ( const value of iterator ) {
- expectValue( value, expected[ i++ ] );
- }
- expect( i ).to.equal( expected.length );
- } );
- it( 'should iterating from the start position going backward', () => {
- const expected = [
- {
- type: 'text',
- text: 'bcd',
- previousPosition: new Position( textAbcd, 1 ),
- nextPosition: new Position( bold, 1 )
- },
- {
- type: 'elementEnd',
- item: bold,
- previousPosition: new Position( bold, 1 ),
- nextPosition: new Position( paragraph, 1 )
- },
- {
- type: 'text',
- text: 'y',
- previousPosition: new Position( paragraph, 1 ),
- nextPosition: new Position( paragraph, 2 )
- }
- ];
- const range = new Range( new Position( textAbcd, 1 ), new Position( paragraph, 3 ) );
- const iterator = new TreeWalker( {
- boundaries: range,
- startPosition: new Position( paragraph, 2 ),
- direction: 'backward'
- } );
- let i = expected.length;
- for ( const value of iterator ) {
- expectValue( value, expected[ --i ], { direction: 'backward' } );
- }
- expect( i ).to.equal( 0 );
- } );
- } );
- } );
- describe( 'iterate by every single characters `singleCharacter`', () => {
- describe( 'whole root', () => {
- let expected;
- before( () => {
- expected = [
- {
- type: 'elementStart',
- item: img1,
- previousPosition: new Position( root, 0 ),
- nextPosition: new Position( img1, 0 )
- },
- {
- type: 'elementEnd',
- item: img1,
- previousPosition: new Position( img1, 0 ),
- nextPosition: new Position( root, 1 )
- },
- {
- type: 'elementStart',
- item: paragraph,
- previousPosition: new Position( root, 1 ),
- nextPosition: new Position( paragraph, 0 )
- },
- {
- type: 'elementStart',
- item: bold,
- previousPosition: new Position( paragraph, 0 ),
- nextPosition: new Position( bold, 0 )
- },
- {
- type: 'text',
- text: 'a',
- previousPosition: new Position( bold, 0 ),
- nextPosition: new Position( textAbcd, 1 )
- },
- {
- type: 'text',
- text: 'b',
- previousPosition: new Position( textAbcd, 1 ),
- nextPosition: new Position( textAbcd, 2 )
- },
- {
- type: 'text',
- text: 'c',
- previousPosition: new Position( textAbcd, 2 ),
- nextPosition: new Position( textAbcd, 3 )
- },
- {
- type: 'text',
- text: 'd',
- previousPosition: new Position( textAbcd, 3 ),
- nextPosition: new Position( bold, 1 )
- },
- {
- type: 'elementEnd',
- item: bold,
- previousPosition: new Position( bold, 1 ),
- nextPosition: new Position( paragraph, 1 )
- },
- {
- type: 'text',
- text: 'y',
- previousPosition: new Position( paragraph, 1 ),
- nextPosition: new Position( paragraph, 2 )
- },
- {
- type: 'elementStart',
- item: img2,
- previousPosition: new Position( paragraph, 2 ),
- nextPosition: new Position( img2, 0 )
- },
- {
- type: 'elementEnd',
- item: img2,
- previousPosition: new Position( img2, 0 ),
- nextPosition: new Position( paragraph, 3 )
- },
- {
- type: 'text',
- text: 'x',
- previousPosition: new Position( paragraph, 3 ),
- nextPosition: new Position( paragraph, 4 )
- },
- {
- type: 'elementEnd',
- item: paragraph,
- previousPosition: new Position( paragraph, 4 ),
- nextPosition: new Position( root, 2 )
- }
- ];
- } );
- it( 'should return single characters', () => {
- const iterator = new TreeWalker( { startPosition: rootBeginning, singleCharacters: true } );
- let i = 0;
- for ( const value of iterator ) {
- expectValue( value, expected[ i++ ] );
- }
- expect( i ).to.equal( expected.length );
- } );
- it( 'should return single characters going backward', () => {
- const iterator = new TreeWalker( {
- startPosition: rootEnding,
- singleCharacters: true,
- direction: 'backward'
- } );
- let i = expected.length;
- for ( const value of iterator ) {
- expectValue( value, expected[ --i ], { direction: 'backward' } );
- }
- expect( i ).to.equal( 0 );
- } );
- } );
- describe( 'range', () => {
- let range, expected;
- before( () => {
- expected = [
- {
- type: 'text',
- text: 'a',
- previousPosition: new Position( bold, 0 ),
- nextPosition: new Position( textAbcd, 1 )
- },
- {
- type: 'text',
- text: 'b',
- previousPosition: new Position( textAbcd, 1 ),
- nextPosition: new Position( textAbcd, 2 )
- },
- {
- type: 'text',
- text: 'c',
- previousPosition: new Position( textAbcd, 2 ),
- nextPosition: new Position( textAbcd, 3 )
- },
- {
- type: 'text',
- text: 'd',
- previousPosition: new Position( textAbcd, 3 ),
- nextPosition: new Position( bold, 1 )
- },
- {
- type: 'elementEnd',
- item: bold,
- previousPosition: new Position( bold, 1 ),
- nextPosition: new Position( paragraph, 1 )
- },
- {
- type: 'text',
- text: 'y',
- previousPosition: new Position( paragraph, 1 ),
- nextPosition: new Position( paragraph, 2 )
- },
- {
- type: 'elementStart',
- item: img2,
- previousPosition: new Position( paragraph, 2 ),
- nextPosition: new Position( img2, 0 )
- }
- ];
- range = new Range( new Position( bold, 0 ), new Position( img2, 0 ) );
- } );
- it( 'should respect boundaries', () => {
- const iterator = new TreeWalker( { boundaries: range, singleCharacters: true } );
- let i = 0;
- for ( const value of iterator ) {
- expectValue( value, expected[ i++ ] );
- }
- expect( i ).to.equal( expected.length );
- } );
- it( 'should respect boundaries going backward', () => {
- const iterator = new TreeWalker( {
- boundaries: range,
- singleCharacters: true,
- direction: 'backward'
- } );
- let i = expected.length;
- for ( const value of iterator ) {
- expectValue( value, expected[ --i ], { direction: 'backward' } );
- }
- expect( i ).to.equal( 0 );
- } );
- } );
- } );
- describe( 'iterate omitting child nodes and elementEnd `shallow`', () => {
- let expected;
- before( () => {
- expected = [
- {
- type: 'elementStart',
- item: img1,
- previousPosition: new Position( root, 0 ),
- nextPosition: new Position( root, 1 )
- },
- {
- type: 'elementStart',
- item: paragraph,
- previousPosition: new Position( root, 1 ),
- nextPosition: new Position( root, 2 )
- }
- ];
- } );
- it( 'should not enter elements', () => {
- const iterator = new TreeWalker( { startPosition: rootBeginning, shallow: true } );
- let i = 0;
- for ( const value of iterator ) {
- expectValue( value, expected[ i++ ] );
- }
- expect( i ).to.equal( expected.length );
- } );
- it( 'should not enter elements going backward', () => {
- const iterator = new TreeWalker( { startPosition: rootEnding, shallow: true, direction: 'backward' } );
- let i = expected.length;
- for ( const value of iterator ) {
- expectValue( value, expected[ --i ], { direction: 'backward' } );
- }
- expect( i ).to.equal( 0 );
- } );
- } );
- describe( 'iterate omitting elementEnd `ignoreElementEnd`', () => {
- describe( 'merged text', () => {
- let expected;
- before( () => {
- expected = [
- {
- type: 'elementStart',
- item: img1,
- previousPosition: new Position( root, 0 ),
- nextPosition: new Position( img1, 0 )
- },
- {
- type: 'elementStart',
- item: paragraph,
- previousPosition: new Position( root, 1 ),
- nextPosition: new Position( paragraph, 0 )
- },
- {
- type: 'elementStart',
- item: bold,
- previousPosition: new Position( paragraph, 0 ),
- nextPosition: new Position( bold, 0 )
- },
- {
- type: 'text',
- text: 'abcd',
- previousPosition: new Position( bold, 0 ),
- nextPosition: new Position( bold, 1 )
- },
- {
- type: 'text',
- text: 'y',
- previousPosition: new Position( paragraph, 1 ),
- nextPosition: new Position( paragraph, 2 )
- },
- {
- type: 'elementStart',
- item: img2,
- previousPosition: new Position( paragraph, 2 ),
- nextPosition: new Position( img2, 0 )
- },
- {
- type: 'text',
- text: 'x',
- previousPosition: new Position( paragraph, 3 ),
- nextPosition: new Position( paragraph, 4 )
- }
- ];
- } );
- it( 'should iterate ignoring elementEnd', () => {
- const iterator = new TreeWalker( { startPosition: rootBeginning, ignoreElementEnd: true } );
- let i = 0;
- for ( const value of iterator ) {
- expectValue( value, expected[ i++ ] );
- }
- expect( i ).to.equal( expected.length );
- } );
- it( 'should iterate ignoring elementEnd going backward', () => {
- const iterator = new TreeWalker( {
- startPosition: rootEnding,
- ignoreElementEnd: true,
- direction: 'backward'
- } );
- let i = expected.length;
- for ( const value of iterator ) {
- expectValue( value, expected[ --i ], { direction: 'backward' } );
- }
- expect( i ).to.equal( 0 );
- } );
- } );
- describe( 'single character', () => {
- let expected;
- before( () => {
- expected = [
- {
- type: 'elementStart',
- item: img1,
- previousPosition: new Position( root, 0 ),
- nextPosition: new Position( img1, 0 )
- },
- {
- type: 'elementStart',
- item: paragraph,
- previousPosition: new Position( root, 1 ),
- nextPosition: new Position( paragraph, 0 )
- },
- {
- type: 'elementStart',
- item: bold,
- previousPosition: new Position( paragraph, 0 ),
- nextPosition: new Position( bold, 0 )
- },
- {
- type: 'text',
- text: 'a',
- previousPosition: new Position( bold, 0 ),
- nextPosition: new Position( textAbcd, 1 )
- },
- {
- type: 'text',
- text: 'b',
- previousPosition: new Position( textAbcd, 1 ),
- nextPosition: new Position( textAbcd, 2 )
- },
- {
- type: 'text',
- text: 'c',
- previousPosition: new Position( textAbcd, 2 ),
- nextPosition: new Position( textAbcd, 3 )
- },
- {
- type: 'text',
- text: 'd',
- previousPosition: new Position( textAbcd, 3 ),
- nextPosition: new Position( bold, 1 )
- },
- {
- type: 'text',
- text: 'y',
- previousPosition: new Position( paragraph, 1 ),
- nextPosition: new Position( paragraph, 2 )
- },
- {
- type: 'elementStart',
- item: img2,
- previousPosition: new Position( paragraph, 2 ),
- nextPosition: new Position( img2, 0 )
- },
- {
- type: 'text',
- text: 'x',
- previousPosition: new Position( paragraph, 3 ),
- nextPosition: new Position( paragraph, 4 )
- }
- ];
- } );
- it( 'should return single characters ignoring elementEnd', () => {
- const iterator = new TreeWalker( {
- startPosition: rootBeginning,
- singleCharacters: true,
- ignoreElementEnd: true
- } );
- let i = 0;
- for ( const value of iterator ) {
- expectValue( value, expected[ i++ ] );
- }
- expect( i ).to.equal( expected.length );
- } );
- it( 'should return single characters ignoring elementEnd going backward', () => {
- const iterator = new TreeWalker( {
- startPosition: rootEnding,
- singleCharacters: true,
- ignoreElementEnd: true,
- direction: 'backward'
- } );
- let i = expected.length;
- for ( const value of iterator ) {
- expectValue( value, expected[ --i ], { direction: 'backward' } );
- }
- expect( i ).to.equal( 0 );
- } );
- } );
- } );
- it( 'should not return elementEnd for a text node when iteration begins at the end of that text node', () => {
- const iterator = new TreeWalker( {
- startPosition: Position._createAt( textAbcd, 'end' )
- } );
- const step = iterator.next();
- expect( step.value.type ).to.equal( 'elementEnd' );
- expect( step.value.item ).to.equal( bold );
- } );
- it( 'should not return elementStart for a text node when iteration begins at the start of that text node', () => {
- const iterator = new TreeWalker( {
- startPosition: Position._createAt( textAbcd, 0 ),
- direction: 'backward'
- } );
- const step = iterator.next();
- expect( step.value.type ).to.equal( 'elementStart' );
- expect( step.value.item ).to.equal( bold );
- } );
- it( 'should iterate over document fragment', () => {
- const foo = new Text( doc, 'foo' );
- const bar = new Text( doc, 'bar' );
- const p = new ContainerElement( doc, 'p', null, foo );
- const b = new AttributeElement( doc, 'b', null, bar );
- const docFrag = new DocumentFragment( doc, [ p, b ] );
- const expected = [
- {
- type: 'elementStart',
- item: p,
- previousPosition: new Position( docFrag, 0 ),
- nextPosition: new Position( p, 0 )
- },
- {
- type: 'text',
- text: 'foo',
- previousPosition: new Position( p, 0 ),
- nextPosition: new Position( p, 1 )
- },
- {
- type: 'elementEnd',
- item: p,
- previousPosition: new Position( p, 1 ),
- nextPosition: new Position( docFrag, 1 )
- },
- {
- type: 'elementStart',
- item: b,
- previousPosition: new Position( docFrag, 1 ),
- nextPosition: new Position( b, 0 )
- },
- {
- type: 'text',
- text: 'bar',
- previousPosition: new Position( b, 0 ),
- nextPosition: new Position( b, 1 )
- },
- {
- type: 'elementEnd',
- item: b,
- previousPosition: new Position( b, 1 ),
- nextPosition: new Position( docFrag, 2 )
- }
- ];
- const iterator = new TreeWalker( { boundaries: Range._createIn( docFrag ) } );
- let i = 0;
- for ( const value of iterator ) {
- expectValue( value, expected[ i++ ] );
- }
- expect( i ).to.equal( expected.length );
- } );
- describe( 'skip', () => {
- describe( 'forward treewalker', () => {
- it( 'should jump over all text nodes', () => {
- const walker = new TreeWalker( {
- startPosition: new Position( paragraph, 0 )
- } );
- walker.skip( value => value.type == 'text' || value.item.name == 'b' );
- expect( walker.position.parent ).to.equal( paragraph );
- expect( walker.position.offset ).to.equal( 2 );
- } );
- it( 'should do not move if the condition is false', () => {
- const walker = new TreeWalker( {
- startPosition: new Position( bold, 0 )
- } );
- walker.skip( () => false );
- expect( walker.position.parent ).to.equal( bold );
- expect( walker.position.offset ).to.equal( 0 );
- } );
- it( 'should do not move if the condition is false and the position is in text node', () => {
- const walker = new TreeWalker( {
- startPosition: new Position( bold.getChild( 0 ), 2 )
- } );
- walker.skip( () => false );
- expect( walker.position.parent ).to.equal( bold.getChild( 0 ) );
- expect( walker.position.offset ).to.equal( 2 );
- } );
- it( 'should move to the end if the condition is true', () => {
- const walker = new TreeWalker( {
- startPosition: new Position( bold, 0 )
- } );
- walker.skip( () => true );
- expect( walker.position.parent ).to.equal( rootEnding.parent );
- expect( walker.position.offset ).to.equal( rootEnding.offset );
- } );
- } );
- describe( 'backward treewalker', () => {
- it( 'should jump over all text nodes', () => {
- const walker = new TreeWalker( {
- startPosition: new Position( bold.getChild( 0 ), 2 ),
- direction: 'backward'
- } );
- walker.skip( value => value.type == 'text' || value.item.name == 'b' );
- expect( walker.position.parent ).to.equal( paragraph );
- expect( walker.position.offset ).to.equal( 0 );
- } );
- it( 'should do not move if the condition is false', () => {
- const walker = new TreeWalker( {
- startPosition: new Position( bold, 0 ),
- direction: 'backward'
- } );
- walker.skip( () => false );
- expect( walker.position.parent ).to.equal( bold );
- expect( walker.position.offset ).to.equal( 0 );
- } );
- it( 'should move to the end if the condition is true', () => {
- const walker = new TreeWalker( {
- startPosition: new Position( bold, 0 ),
- direction: 'backward'
- } );
- walker.skip( () => true );
- expect( walker.position.parent ).to.equal( rootBeginning.parent );
- expect( walker.position.offset ).to.equal( rootBeginning.offset );
- } );
- } );
- } );
- } );
- function expectValue( value, expected, options = {} ) {
- let expectedPreviousPosition, expectedNextPosition;
- if ( options.direction == 'backward' ) {
- expectedNextPosition = expected.previousPosition;
- expectedPreviousPosition = expected.nextPosition;
- } else {
- expectedNextPosition = expected.nextPosition;
- expectedPreviousPosition = expected.previousPosition;
- }
- expect( value.type ).to.equal( expected.type );
- expect( value.previousPosition ).to.deep.equal( expectedPreviousPosition );
- expect( value.nextPosition ).to.deep.equal( expectedNextPosition );
- if ( value.type == 'text' ) {
- expectText( value, expected );
- } else if ( value.type == 'elementStart' ) {
- expectStart( value, expected );
- } else if ( value.type == 'elementEnd' ) {
- expectEnd( value, expected );
- }
- }
- function expectText( value, expected ) {
- expect( value.item.data ).to.equal( expected.text );
- expect( value.length ).to.equal( value.item.data.length );
- }
- function expectStart( value, expected ) {
- expect( value.item ).to.equal( expected.item );
- expect( value.length ).to.equal( 1 );
- }
- function expectEnd( value, expected ) {
- expect( value.item ).to.equal( expected.item );
- expect( value.length ).to.be.undefined;
- }
|