| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015 |
- /**
- * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
- /* bender-tags: view */
- import Document from '/ckeditor5/engine/view/document.js';
- import AttributeElement from '/ckeditor5/engine/view/attributeelement.js';
- import ContainerElement from '/ckeditor5/engine/view/containerelement.js';
- import Text from '/ckeditor5/engine/view/text.js';
- import TreeWalker from '/ckeditor5/engine/view/treewalker.js';
- import Position from '/ckeditor5/engine/view/position.js';
- import Range from '/ckeditor5/engine/view/range.js';
- import CKEditorError from '/ckeditor5/utils/ckeditorerror.js';
- describe( 'TreeWalker', () => {
- let doc, root, img1, paragraph, bold, textAbcd, charY, img2, charX;
- let rootBeginning, rootEnding;
- before( () => {
- doc = new Document();
- root = doc.createRoot( document.createElement( 'div' ) );
- // root
- // |- img1
- // |- p
- // |- b
- // | |- A
- // | |- B
- // | |- C
- // | |- D
- // |
- // |- Y
- // |
- // |- img2
- // |
- // |- X
- textAbcd = new Text( 'abcd' );
- bold = new AttributeElement( 'b', null, [ textAbcd ] );
- charY = new Text( 'y' );
- img2 = new ContainerElement( 'img2' );
- charX = new Text( 'x' );
- paragraph = new ContainerElement( 'p', null, [ bold, charY, img2, charX ] );
- img1 = new ContainerElement( 'img1' );
- root.insertChildren( 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', () => {
- expect( () => {
- new TreeWalker();
- } ).to.throw( CKEditorError, /^tree-walker-no-start-position/ );
- expect( () => {
- new TreeWalker( {} );
- } ).to.throw( CKEditorError, /^tree-walker-no-start-position/ );
- expect( () => {
- new TreeWalker( { singleCharacters: true } );
- } ).to.throw( CKEditorError, /^tree-walker-no-start-position/ );
- } );
- it( 'should throw if walking direction is unknown', () => {
- expect( () => {
- new TreeWalker( { startPosition: rootBeginning, direction: 'unknown' } );
- } ).to.throw( CKEditorError, /^tree-walker-unknown-direction/ );
- } );
- } );
- 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', () => {
- let iterator = new TreeWalker( { startPosition: rootBeginning } );
- let i = 0;
- for ( let value of iterator ) {
- expectValue( value, expected[ i++ ] );
- }
- expect( i ).to.equal( expected.length );
- } );
- it( 'should provide iterator interface with forward direction', () => {
- let iterator = new TreeWalker( { startPosition: rootBeginning, direction: 'forward' } );
- let i = 0;
- for ( let value of iterator ) {
- expectValue( value, expected[ i++ ] );
- }
- expect( i ).to.equal( expected.length );
- } );
- it( 'should provide iterator interface which backward direction', () => {
- let iterator = new TreeWalker( { startPosition: rootEnding, direction: 'backward' } );
- let i = expected.length;
- for ( let 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', () => {
- let iterator = new TreeWalker( { startPosition: new Position( root, 1 ) } );
- let i = 2;
- for ( let 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', () => {
- let 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 )
- }
- ];
- let iterator = new TreeWalker( { startPosition: new Position( root, 1 ), direction: 'backward' } );
- let i = expected.length;
- for ( let 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', () => {
- let iterator = new TreeWalker( { boundaries: range } );
- let i = 0;
- for ( let value of iterator ) {
- expectValue( value, expected[ i++ ] );
- }
- expect( i ).to.equal( expected.length );
- } );
- it( 'should iterating over the range going backward', () => {
- let iterator = new TreeWalker( { boundaries: range, direction: 'backward' } );
- let i = expected.length;
- for ( let 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', () => {
- let iterator = new TreeWalker( { boundaries: range } );
- let i = 0;
- for ( let value of iterator ) {
- expectValue( value, expected[ i++ ] );
- }
- expect( i ).to.equal( expected.length );
- } );
- it( 'should return part of the text going backward', () => {
- let iterator = new TreeWalker( {
- boundaries: range,
- direction: 'backward'
- }
- );
- let i = expected.length;
- for ( let 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', () => {
- let iterator = new TreeWalker( { boundaries: range } );
- let i = 0;
- for ( let value of iterator ) {
- expectValue( value, expected[ i++ ] );
- }
- expect( i ).to.equal( expected.length );
- } );
- it( 'should return part of the text going backward', () => {
- let iterator = new TreeWalker( {
- boundaries: range,
- startPosition: range.end,
- direction: 'backward'
- } );
- let i = expected.length;
- for ( let 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', () => {
- let iterator = new TreeWalker( { boundaries: range } );
- let i = 0;
- for ( let value of iterator ) {
- expectValue( value, expected[ i++ ] );
- }
- expect( i ).to.equal( expected.length );
- } );
- it( 'should return part of the text going backward', () => {
- let iterator = new TreeWalker( {
- boundaries: range,
- startPosition: range.end,
- direction: 'backward'
- } );
- let i = expected.length;
- for ( let 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', () => {
- let 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 )
- }
- ];
- let range = Range.createFromParentsAndOffsets( bold, 1, paragraph, 3 );
- let iterator = new TreeWalker( {
- boundaries: range,
- startPosition: new Position( paragraph, 1 )
- } );
- let i = 0;
- for ( let value of iterator ) {
- expectValue( value, expected[ i++ ] );
- }
- expect( i ).to.equal( expected.length );
- } );
- it( 'should iterating from the start position going backward', () => {
- let 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 )
- }
- ];
- let range = new Range( new Position( textAbcd, 1 ), new Position( paragraph, 3 ) );
- let iterator = new TreeWalker( {
- boundaries: range,
- startPosition: new Position( paragraph, 2 ),
- direction: 'backward'
- } );
- let i = expected.length;
- for ( let 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', () => {
- let iterator = new TreeWalker( { startPosition: rootBeginning, singleCharacters: true } );
- let i = 0;
- for ( let value of iterator ) {
- expectValue( value, expected[ i++ ] );
- }
- expect( i ).to.equal( expected.length );
- } );
- it( 'should return single characters going backward', () => {
- let iterator = new TreeWalker( {
- startPosition: rootEnding,
- singleCharacters: true,
- direction: 'backward'
- } );
- let i = expected.length;
- for ( let 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', () => {
- let iterator = new TreeWalker( { boundaries: range, singleCharacters: true } );
- let i = 0;
- for ( let value of iterator ) {
- expectValue( value, expected[ i++ ] );
- }
- expect( i ).to.equal( expected.length );
- } );
- it( 'should respect boundaries going backward', () => {
- let iterator = new TreeWalker( {
- boundaries: range,
- singleCharacters: true,
- direction: 'backward'
- } );
- let i = expected.length;
- for ( let 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', () => {
- let iterator = new TreeWalker( { startPosition: rootBeginning, shallow: true } );
- let i = 0;
- for ( let value of iterator ) {
- expectValue( value, expected[ i++ ] );
- }
- expect( i ).to.equal( expected.length );
- } );
- it( 'should not enter elements going backward', () => {
- let iterator = new TreeWalker( { startPosition: rootEnding, shallow: true, direction: 'backward' } );
- let i = expected.length;
- for ( let 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', () => {
- let iterator = new TreeWalker( { startPosition: rootBeginning, ignoreElementEnd: true } );
- let i = 0;
- for ( let value of iterator ) {
- expectValue( value, expected[ i++ ] );
- }
- expect( i ).to.equal( expected.length );
- } );
- it( 'should iterate ignoring elementEnd going backward', () => {
- let iterator = new TreeWalker( {
- startPosition: rootEnding,
- ignoreElementEnd: true,
- direction: 'backward'
- } );
- let i = expected.length;
- for ( let 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', () => {
- let iterator = new TreeWalker( {
- startPosition: rootBeginning,
- singleCharacters: true,
- ignoreElementEnd: true
- } );
- let i = 0;
- for ( let value of iterator ) {
- expectValue( value, expected[ i++ ] );
- }
- expect( i ).to.equal( expected.length );
- } );
- it( 'should return single characters ignoring elementEnd going backward', () => {
- let iterator = new TreeWalker( {
- startPosition: rootEnding,
- singleCharacters: true,
- ignoreElementEnd: true,
- direction: 'backward'
- } );
- let i = expected.length;
- for ( let value of iterator ) {
- expectValue( value, expected[ --i ], { direction: 'backward' } );
- }
- expect( i ).to.equal( 0 );
- } );
- } );
- } );
- } );
- 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;
- }
|