treewalker.js 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import Document from '../../src/view/document';
  6. import DocumentFragment from '../../src/view/documentfragment';
  7. import AttributeElement from '../../src/view/attributeelement';
  8. import ContainerElement from '../../src/view/containerelement';
  9. import Text from '../../src/view/text';
  10. import TreeWalker from '../../src/view/treewalker';
  11. import Position from '../../src/view/position';
  12. import Range from '../../src/view/range';
  13. import createViewRoot from './_utils/createroot';
  14. import { expectToThrowCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  15. describe( 'TreeWalker', () => {
  16. let doc, root, img1, paragraph, bold, textAbcd, charY, img2, charX, rootBeginning, rootEnding;
  17. before( () => {
  18. doc = new Document();
  19. root = createViewRoot( doc );
  20. // root
  21. // |- img1
  22. // |- p
  23. // |- b
  24. // | |- abcd
  25. // |
  26. // |- Y
  27. // |
  28. // |- img2
  29. // |
  30. // |- X
  31. textAbcd = new Text( 'abcd' );
  32. bold = new AttributeElement( 'b', null, [ textAbcd ] );
  33. charY = new Text( 'y' );
  34. img2 = new ContainerElement( 'img2' );
  35. charX = new Text( 'x' );
  36. paragraph = new ContainerElement( 'p', null, [ bold, charY, img2, charX ] );
  37. img1 = new ContainerElement( 'img1' );
  38. root._insertChild( 0, [ img1, paragraph ] );
  39. rootBeginning = new Position( root, 0 );
  40. rootEnding = new Position( root, 2 );
  41. } );
  42. describe( 'constructor()', () => {
  43. it( 'should throw if neither boundaries nor starting position is set', () => {
  44. expectToThrowCKEditorError( () => {
  45. new TreeWalker(); // eslint-disable-line no-new
  46. }, /^view-tree-walker-no-start-position/, null );
  47. expectToThrowCKEditorError( () => {
  48. new TreeWalker( {} ); // eslint-disable-line no-new
  49. }, /^view-tree-walker-no-start-position/, null );
  50. expectToThrowCKEditorError( () => {
  51. new TreeWalker( { singleCharacters: true } ); // eslint-disable-line no-new
  52. }, /^view-tree-walker-no-start-position/, null );
  53. } );
  54. it( 'should throw if walking direction is unknown', () => {
  55. expectToThrowCKEditorError( () => {
  56. new TreeWalker( { startPosition: rootBeginning, direction: 'unknown' } ); // eslint-disable-line no-new
  57. }, /^view-tree-walker-unknown-direction/, doc );
  58. } );
  59. } );
  60. describe( 'is()', () => {
  61. let treeWalker;
  62. beforeEach( () => {
  63. treeWalker = new TreeWalker( { startPosition: rootBeginning } );
  64. } );
  65. it( 'should return true for "treeWalker"', () => {
  66. expect( treeWalker.is( 'treeWalker' ) ).to.be.true;
  67. expect( treeWalker.is( 'view:treeWalker' ) ).to.be.true;
  68. } );
  69. it( 'should return false for other accept values', () => {
  70. expect( treeWalker.is( 'rootElement' ) ).to.be.false;
  71. expect( treeWalker.is( 'containerElement' ) ).to.be.false;
  72. expect( treeWalker.is( 'element' ) ).to.be.false;
  73. expect( treeWalker.is( 'p' ) ).to.be.false;
  74. expect( treeWalker.is( 'text' ) ).to.be.false;
  75. expect( treeWalker.is( 'textProxy' ) ).to.be.false;
  76. expect( treeWalker.is( 'attributeElement' ) ).to.be.false;
  77. expect( treeWalker.is( 'uiElement' ) ).to.be.false;
  78. expect( treeWalker.is( 'emptyElement' ) ).to.be.false;
  79. expect( treeWalker.is( 'documentFragment' ) ).to.be.false;
  80. expect( treeWalker.is( 'model:treeWalker' ) ).to.be.false;
  81. } );
  82. } );
  83. describe( 'iterate from start position `startPosition`', () => {
  84. let expected;
  85. beforeEach( () => {
  86. expected = [
  87. {
  88. type: 'elementStart',
  89. item: img1,
  90. previousPosition: new Position( root, 0 ),
  91. nextPosition: new Position( img1, 0 )
  92. },
  93. {
  94. type: 'elementEnd',
  95. item: img1,
  96. previousPosition: new Position( img1, 0 ),
  97. nextPosition: new Position( root, 1 )
  98. },
  99. {
  100. type: 'elementStart',
  101. item: paragraph,
  102. previousPosition: new Position( root, 1 ),
  103. nextPosition: new Position( paragraph, 0 )
  104. },
  105. {
  106. type: 'elementStart',
  107. item: bold,
  108. previousPosition: new Position( paragraph, 0 ),
  109. nextPosition: new Position( bold, 0 )
  110. },
  111. {
  112. type: 'text',
  113. text: 'abcd',
  114. previousPosition: new Position( bold, 0 ),
  115. nextPosition: new Position( bold, 1 )
  116. },
  117. {
  118. type: 'elementEnd',
  119. item: bold,
  120. previousPosition: new Position( bold, 1 ),
  121. nextPosition: new Position( paragraph, 1 )
  122. },
  123. {
  124. type: 'text',
  125. text: 'y',
  126. previousPosition: new Position( paragraph, 1 ),
  127. nextPosition: new Position( paragraph, 2 )
  128. },
  129. {
  130. type: 'elementStart',
  131. item: img2,
  132. previousPosition: new Position( paragraph, 2 ),
  133. nextPosition: new Position( img2, 0 )
  134. },
  135. {
  136. type: 'elementEnd',
  137. item: img2,
  138. previousPosition: new Position( img2, 0 ),
  139. nextPosition: new Position( paragraph, 3 )
  140. },
  141. {
  142. type: 'text',
  143. text: 'x',
  144. previousPosition: new Position( paragraph, 3 ),
  145. nextPosition: new Position( paragraph, 4 )
  146. },
  147. {
  148. type: 'elementEnd',
  149. item: paragraph,
  150. previousPosition: new Position( paragraph, 4 ),
  151. nextPosition: new Position( root, 2 )
  152. }
  153. ];
  154. } );
  155. it( 'should provide iterator interface with default forward direction', () => {
  156. const iterator = new TreeWalker( { startPosition: rootBeginning } );
  157. let i = 0;
  158. for ( const value of iterator ) {
  159. expectValue( value, expected[ i++ ] );
  160. }
  161. expect( i ).to.equal( expected.length );
  162. } );
  163. it( 'should provide iterator interface with forward direction', () => {
  164. const iterator = new TreeWalker( { startPosition: rootBeginning, direction: 'forward' } );
  165. let i = 0;
  166. for ( const value of iterator ) {
  167. expectValue( value, expected[ i++ ] );
  168. }
  169. expect( i ).to.equal( expected.length );
  170. } );
  171. it( 'should provide iterator interface which backward direction', () => {
  172. const iterator = new TreeWalker( { startPosition: rootEnding, direction: 'backward' } );
  173. let i = expected.length;
  174. for ( const value of iterator ) {
  175. expectValue( value, expected[ --i ], { direction: 'backward' } );
  176. }
  177. expect( i ).to.equal( 0 );
  178. } );
  179. it( 'should start iterating at the startPosition witch is not a root bound', () => {
  180. const iterator = new TreeWalker( { startPosition: new Position( root, 1 ) } );
  181. let i = 2;
  182. for ( const value of iterator ) {
  183. expectValue( value, expected[ i++ ] );
  184. }
  185. expect( i ).to.equal( expected.length );
  186. } );
  187. it( 'should start iterating at the startPosition witch is not a root bound, going backward', () => {
  188. const expected = [
  189. {
  190. type: 'elementStart',
  191. item: img1,
  192. previousPosition: new Position( root, 0 ),
  193. nextPosition: new Position( img1, 0 )
  194. },
  195. {
  196. type: 'elementEnd',
  197. item: img1,
  198. previousPosition: new Position( img1, 0 ),
  199. nextPosition: new Position( root, 1 )
  200. }
  201. ];
  202. const iterator = new TreeWalker( { startPosition: new Position( root, 1 ), direction: 'backward' } );
  203. let i = expected.length;
  204. for ( const value of iterator ) {
  205. expectValue( value, expected[ --i ], { direction: 'backward' } );
  206. }
  207. expect( i ).to.equal( 0 );
  208. } );
  209. } );
  210. describe( 'iterate trough the range `boundary`', () => {
  211. describe( 'range starts between elements', () => {
  212. let expected, range;
  213. before( () => {
  214. expected = [
  215. {
  216. type: 'elementStart',
  217. item: paragraph,
  218. previousPosition: new Position( root, 1 ),
  219. nextPosition: new Position( paragraph, 0 )
  220. },
  221. {
  222. type: 'elementStart',
  223. item: bold,
  224. previousPosition: new Position( paragraph, 0 ),
  225. nextPosition: new Position( bold, 0 )
  226. },
  227. {
  228. type: 'text',
  229. text: 'abcd',
  230. previousPosition: new Position( bold, 0 ),
  231. nextPosition: new Position( bold, 1 )
  232. },
  233. {
  234. type: 'elementEnd',
  235. item: bold,
  236. previousPosition: new Position( bold, 1 ),
  237. nextPosition: new Position( paragraph, 1 )
  238. },
  239. {
  240. type: 'text',
  241. text: 'y',
  242. previousPosition: new Position( paragraph, 1 ),
  243. nextPosition: new Position( paragraph, 2 )
  244. },
  245. {
  246. type: 'elementStart',
  247. item: img2,
  248. previousPosition: new Position( paragraph, 2 ),
  249. nextPosition: new Position( img2, 0 )
  250. },
  251. {
  252. type: 'elementEnd',
  253. item: img2,
  254. previousPosition: new Position( img2, 0 ),
  255. nextPosition: new Position( paragraph, 3 )
  256. }
  257. ];
  258. range = Range._createFromParentsAndOffsets( root, 1, paragraph, 3 );
  259. } );
  260. it( 'should iterating over the range', () => {
  261. const iterator = new TreeWalker( { boundaries: range } );
  262. let i = 0;
  263. for ( const value of iterator ) {
  264. expectValue( value, expected[ i++ ] );
  265. }
  266. expect( i ).to.equal( expected.length );
  267. } );
  268. it( 'should iterating over the range going backward', () => {
  269. const iterator = new TreeWalker( { boundaries: range, direction: 'backward' } );
  270. let i = expected.length;
  271. for ( const value of iterator ) {
  272. expectValue( value, expected[ --i ], { direction: 'backward' } );
  273. }
  274. expect( i ).to.equal( 0 );
  275. } );
  276. } );
  277. describe( 'range starts inside the text', () => {
  278. let expected, range;
  279. before( () => {
  280. expected = [
  281. {
  282. type: 'text',
  283. text: 'bcd',
  284. previousPosition: new Position( textAbcd, 1 ),
  285. nextPosition: new Position( bold, 1 )
  286. },
  287. {
  288. type: 'elementEnd',
  289. item: bold,
  290. previousPosition: new Position( bold, 1 ),
  291. nextPosition: new Position( paragraph, 1 )
  292. },
  293. {
  294. type: 'text',
  295. text: 'y',
  296. previousPosition: new Position( paragraph, 1 ),
  297. nextPosition: new Position( paragraph, 2 )
  298. },
  299. {
  300. type: 'elementStart',
  301. item: img2,
  302. previousPosition: new Position( paragraph, 2 ),
  303. nextPosition: new Position( img2, 0 )
  304. },
  305. {
  306. type: 'elementEnd',
  307. item: img2,
  308. previousPosition: new Position( img2, 0 ),
  309. nextPosition: new Position( paragraph, 3 )
  310. }
  311. ];
  312. range = Range._createFromParentsAndOffsets( textAbcd, 1, paragraph, 3 );
  313. } );
  314. it( 'should return part of the text', () => {
  315. const iterator = new TreeWalker( { boundaries: range } );
  316. let i = 0;
  317. for ( const value of iterator ) {
  318. expectValue( value, expected[ i++ ] );
  319. }
  320. expect( i ).to.equal( expected.length );
  321. } );
  322. it( 'should return part of the text going backward', () => {
  323. const iterator = new TreeWalker( {
  324. boundaries: range,
  325. direction: 'backward'
  326. }
  327. );
  328. let i = expected.length;
  329. for ( const value of iterator ) {
  330. expectValue( value, expected[ --i ], { direction: 'backward' } );
  331. }
  332. expect( i ).to.equal( 0 );
  333. } );
  334. } );
  335. describe( 'range ends inside the text', () => {
  336. let expected, range;
  337. before( () => {
  338. expected = [
  339. {
  340. type: 'elementStart',
  341. item: img1,
  342. previousPosition: new Position( root, 0 ),
  343. nextPosition: new Position( img1, 0 )
  344. },
  345. {
  346. type: 'elementEnd',
  347. item: img1,
  348. previousPosition: new Position( img1, 0 ),
  349. nextPosition: new Position( root, 1 )
  350. },
  351. {
  352. type: 'elementStart',
  353. item: paragraph,
  354. previousPosition: new Position( root, 1 ),
  355. nextPosition: new Position( paragraph, 0 )
  356. },
  357. {
  358. type: 'elementStart',
  359. item: bold,
  360. previousPosition: new Position( paragraph, 0 ),
  361. nextPosition: new Position( bold, 0 )
  362. },
  363. {
  364. type: 'text',
  365. text: 'ab',
  366. previousPosition: new Position( bold, 0 ),
  367. nextPosition: new Position( textAbcd, 2 )
  368. }
  369. ];
  370. range = new Range( rootBeginning, new Position( textAbcd, 2 ) );
  371. } );
  372. it( 'should return part of the text', () => {
  373. const iterator = new TreeWalker( { boundaries: range } );
  374. let i = 0;
  375. for ( const value of iterator ) {
  376. expectValue( value, expected[ i++ ] );
  377. }
  378. expect( i ).to.equal( expected.length );
  379. } );
  380. it( 'should return part of the text going backward', () => {
  381. const iterator = new TreeWalker( {
  382. boundaries: range,
  383. startPosition: range.end,
  384. direction: 'backward'
  385. } );
  386. let i = expected.length;
  387. for ( const value of iterator ) {
  388. expectValue( value, expected[ --i ], { direction: 'backward' } );
  389. }
  390. expect( i ).to.equal( 0 );
  391. } );
  392. } );
  393. describe( 'range starts and ends inside the same text', () => {
  394. let expected, range;
  395. before( () => {
  396. expected = [
  397. {
  398. type: 'text',
  399. text: 'bc',
  400. previousPosition: new Position( textAbcd, 1 ),
  401. nextPosition: new Position( textAbcd, 3 )
  402. }
  403. ];
  404. range = new Range( new Position( textAbcd, 1 ), new Position( textAbcd, 3 ) );
  405. } );
  406. it( 'should return part of the text', () => {
  407. const iterator = new TreeWalker( { boundaries: range } );
  408. let i = 0;
  409. for ( const value of iterator ) {
  410. expectValue( value, expected[ i++ ] );
  411. }
  412. expect( i ).to.equal( expected.length );
  413. } );
  414. it( 'should return part of the text going backward', () => {
  415. const iterator = new TreeWalker( {
  416. boundaries: range,
  417. startPosition: range.end,
  418. direction: 'backward'
  419. } );
  420. let i = expected.length;
  421. for ( const value of iterator ) {
  422. expectValue( value, expected[ --i ], { direction: 'backward' } );
  423. }
  424. expect( i ).to.equal( 0 );
  425. } );
  426. } );
  427. describe( 'custom start position', () => {
  428. it( 'should iterating from the start position', () => {
  429. const expected = [
  430. {
  431. type: 'text',
  432. text: 'y',
  433. previousPosition: new Position( paragraph, 1 ),
  434. nextPosition: new Position( paragraph, 2 )
  435. },
  436. {
  437. type: 'elementStart',
  438. item: img2,
  439. previousPosition: new Position( paragraph, 2 ),
  440. nextPosition: new Position( img2, 0 )
  441. },
  442. {
  443. type: 'elementEnd',
  444. item: img2,
  445. previousPosition: new Position( img2, 0 ),
  446. nextPosition: new Position( paragraph, 3 )
  447. }
  448. ];
  449. const range = Range._createFromParentsAndOffsets( bold, 1, paragraph, 3 );
  450. const iterator = new TreeWalker( {
  451. boundaries: range,
  452. startPosition: new Position( paragraph, 1 )
  453. } );
  454. let i = 0;
  455. for ( const value of iterator ) {
  456. expectValue( value, expected[ i++ ] );
  457. }
  458. expect( i ).to.equal( expected.length );
  459. } );
  460. it( 'should iterating from the start position going backward', () => {
  461. const expected = [
  462. {
  463. type: 'text',
  464. text: 'bcd',
  465. previousPosition: new Position( textAbcd, 1 ),
  466. nextPosition: new Position( bold, 1 )
  467. },
  468. {
  469. type: 'elementEnd',
  470. item: bold,
  471. previousPosition: new Position( bold, 1 ),
  472. nextPosition: new Position( paragraph, 1 )
  473. },
  474. {
  475. type: 'text',
  476. text: 'y',
  477. previousPosition: new Position( paragraph, 1 ),
  478. nextPosition: new Position( paragraph, 2 )
  479. }
  480. ];
  481. const range = new Range( new Position( textAbcd, 1 ), new Position( paragraph, 3 ) );
  482. const iterator = new TreeWalker( {
  483. boundaries: range,
  484. startPosition: new Position( paragraph, 2 ),
  485. direction: 'backward'
  486. } );
  487. let i = expected.length;
  488. for ( const value of iterator ) {
  489. expectValue( value, expected[ --i ], { direction: 'backward' } );
  490. }
  491. expect( i ).to.equal( 0 );
  492. } );
  493. } );
  494. } );
  495. describe( 'iterate by every single characters `singleCharacter`', () => {
  496. describe( 'whole root', () => {
  497. let expected;
  498. before( () => {
  499. expected = [
  500. {
  501. type: 'elementStart',
  502. item: img1,
  503. previousPosition: new Position( root, 0 ),
  504. nextPosition: new Position( img1, 0 )
  505. },
  506. {
  507. type: 'elementEnd',
  508. item: img1,
  509. previousPosition: new Position( img1, 0 ),
  510. nextPosition: new Position( root, 1 )
  511. },
  512. {
  513. type: 'elementStart',
  514. item: paragraph,
  515. previousPosition: new Position( root, 1 ),
  516. nextPosition: new Position( paragraph, 0 )
  517. },
  518. {
  519. type: 'elementStart',
  520. item: bold,
  521. previousPosition: new Position( paragraph, 0 ),
  522. nextPosition: new Position( bold, 0 )
  523. },
  524. {
  525. type: 'text',
  526. text: 'a',
  527. previousPosition: new Position( bold, 0 ),
  528. nextPosition: new Position( textAbcd, 1 )
  529. },
  530. {
  531. type: 'text',
  532. text: 'b',
  533. previousPosition: new Position( textAbcd, 1 ),
  534. nextPosition: new Position( textAbcd, 2 )
  535. },
  536. {
  537. type: 'text',
  538. text: 'c',
  539. previousPosition: new Position( textAbcd, 2 ),
  540. nextPosition: new Position( textAbcd, 3 )
  541. },
  542. {
  543. type: 'text',
  544. text: 'd',
  545. previousPosition: new Position( textAbcd, 3 ),
  546. nextPosition: new Position( bold, 1 )
  547. },
  548. {
  549. type: 'elementEnd',
  550. item: bold,
  551. previousPosition: new Position( bold, 1 ),
  552. nextPosition: new Position( paragraph, 1 )
  553. },
  554. {
  555. type: 'text',
  556. text: 'y',
  557. previousPosition: new Position( paragraph, 1 ),
  558. nextPosition: new Position( paragraph, 2 )
  559. },
  560. {
  561. type: 'elementStart',
  562. item: img2,
  563. previousPosition: new Position( paragraph, 2 ),
  564. nextPosition: new Position( img2, 0 )
  565. },
  566. {
  567. type: 'elementEnd',
  568. item: img2,
  569. previousPosition: new Position( img2, 0 ),
  570. nextPosition: new Position( paragraph, 3 )
  571. },
  572. {
  573. type: 'text',
  574. text: 'x',
  575. previousPosition: new Position( paragraph, 3 ),
  576. nextPosition: new Position( paragraph, 4 )
  577. },
  578. {
  579. type: 'elementEnd',
  580. item: paragraph,
  581. previousPosition: new Position( paragraph, 4 ),
  582. nextPosition: new Position( root, 2 )
  583. }
  584. ];
  585. } );
  586. it( 'should return single characters', () => {
  587. const iterator = new TreeWalker( { startPosition: rootBeginning, singleCharacters: true } );
  588. let i = 0;
  589. for ( const value of iterator ) {
  590. expectValue( value, expected[ i++ ] );
  591. }
  592. expect( i ).to.equal( expected.length );
  593. } );
  594. it( 'should return single characters going backward', () => {
  595. const iterator = new TreeWalker( {
  596. startPosition: rootEnding,
  597. singleCharacters: true,
  598. direction: 'backward'
  599. } );
  600. let i = expected.length;
  601. for ( const value of iterator ) {
  602. expectValue( value, expected[ --i ], { direction: 'backward' } );
  603. }
  604. expect( i ).to.equal( 0 );
  605. } );
  606. } );
  607. describe( 'range', () => {
  608. let range, expected;
  609. before( () => {
  610. expected = [
  611. {
  612. type: 'text',
  613. text: 'a',
  614. previousPosition: new Position( bold, 0 ),
  615. nextPosition: new Position( textAbcd, 1 )
  616. },
  617. {
  618. type: 'text',
  619. text: 'b',
  620. previousPosition: new Position( textAbcd, 1 ),
  621. nextPosition: new Position( textAbcd, 2 )
  622. },
  623. {
  624. type: 'text',
  625. text: 'c',
  626. previousPosition: new Position( textAbcd, 2 ),
  627. nextPosition: new Position( textAbcd, 3 )
  628. },
  629. {
  630. type: 'text',
  631. text: 'd',
  632. previousPosition: new Position( textAbcd, 3 ),
  633. nextPosition: new Position( bold, 1 )
  634. },
  635. {
  636. type: 'elementEnd',
  637. item: bold,
  638. previousPosition: new Position( bold, 1 ),
  639. nextPosition: new Position( paragraph, 1 )
  640. },
  641. {
  642. type: 'text',
  643. text: 'y',
  644. previousPosition: new Position( paragraph, 1 ),
  645. nextPosition: new Position( paragraph, 2 )
  646. },
  647. {
  648. type: 'elementStart',
  649. item: img2,
  650. previousPosition: new Position( paragraph, 2 ),
  651. nextPosition: new Position( img2, 0 )
  652. }
  653. ];
  654. range = new Range( new Position( bold, 0 ), new Position( img2, 0 ) );
  655. } );
  656. it( 'should respect boundaries', () => {
  657. const iterator = new TreeWalker( { boundaries: range, singleCharacters: true } );
  658. let i = 0;
  659. for ( const value of iterator ) {
  660. expectValue( value, expected[ i++ ] );
  661. }
  662. expect( i ).to.equal( expected.length );
  663. } );
  664. it( 'should respect boundaries going backward', () => {
  665. const iterator = new TreeWalker( {
  666. boundaries: range,
  667. singleCharacters: true,
  668. direction: 'backward'
  669. } );
  670. let i = expected.length;
  671. for ( const value of iterator ) {
  672. expectValue( value, expected[ --i ], { direction: 'backward' } );
  673. }
  674. expect( i ).to.equal( 0 );
  675. } );
  676. } );
  677. } );
  678. describe( 'iterate omitting child nodes and elementEnd `shallow`', () => {
  679. let expected;
  680. before( () => {
  681. expected = [
  682. {
  683. type: 'elementStart',
  684. item: img1,
  685. previousPosition: new Position( root, 0 ),
  686. nextPosition: new Position( root, 1 )
  687. },
  688. {
  689. type: 'elementStart',
  690. item: paragraph,
  691. previousPosition: new Position( root, 1 ),
  692. nextPosition: new Position( root, 2 )
  693. }
  694. ];
  695. } );
  696. it( 'should not enter elements', () => {
  697. const iterator = new TreeWalker( { startPosition: rootBeginning, shallow: true } );
  698. let i = 0;
  699. for ( const value of iterator ) {
  700. expectValue( value, expected[ i++ ] );
  701. }
  702. expect( i ).to.equal( expected.length );
  703. } );
  704. it( 'should not enter elements going backward', () => {
  705. const iterator = new TreeWalker( { startPosition: rootEnding, shallow: true, direction: 'backward' } );
  706. let i = expected.length;
  707. for ( const value of iterator ) {
  708. expectValue( value, expected[ --i ], { direction: 'backward' } );
  709. }
  710. expect( i ).to.equal( 0 );
  711. } );
  712. } );
  713. describe( 'iterate omitting elementEnd `ignoreElementEnd`', () => {
  714. describe( 'merged text', () => {
  715. let expected;
  716. before( () => {
  717. expected = [
  718. {
  719. type: 'elementStart',
  720. item: img1,
  721. previousPosition: new Position( root, 0 ),
  722. nextPosition: new Position( img1, 0 )
  723. },
  724. {
  725. type: 'elementStart',
  726. item: paragraph,
  727. previousPosition: new Position( root, 1 ),
  728. nextPosition: new Position( paragraph, 0 )
  729. },
  730. {
  731. type: 'elementStart',
  732. item: bold,
  733. previousPosition: new Position( paragraph, 0 ),
  734. nextPosition: new Position( bold, 0 )
  735. },
  736. {
  737. type: 'text',
  738. text: 'abcd',
  739. previousPosition: new Position( bold, 0 ),
  740. nextPosition: new Position( bold, 1 )
  741. },
  742. {
  743. type: 'text',
  744. text: 'y',
  745. previousPosition: new Position( paragraph, 1 ),
  746. nextPosition: new Position( paragraph, 2 )
  747. },
  748. {
  749. type: 'elementStart',
  750. item: img2,
  751. previousPosition: new Position( paragraph, 2 ),
  752. nextPosition: new Position( img2, 0 )
  753. },
  754. {
  755. type: 'text',
  756. text: 'x',
  757. previousPosition: new Position( paragraph, 3 ),
  758. nextPosition: new Position( paragraph, 4 )
  759. }
  760. ];
  761. } );
  762. it( 'should iterate ignoring elementEnd', () => {
  763. const iterator = new TreeWalker( { startPosition: rootBeginning, ignoreElementEnd: true } );
  764. let i = 0;
  765. for ( const value of iterator ) {
  766. expectValue( value, expected[ i++ ] );
  767. }
  768. expect( i ).to.equal( expected.length );
  769. } );
  770. it( 'should iterate ignoring elementEnd going backward', () => {
  771. const iterator = new TreeWalker( {
  772. startPosition: rootEnding,
  773. ignoreElementEnd: true,
  774. direction: 'backward'
  775. } );
  776. let i = expected.length;
  777. for ( const value of iterator ) {
  778. expectValue( value, expected[ --i ], { direction: 'backward' } );
  779. }
  780. expect( i ).to.equal( 0 );
  781. } );
  782. } );
  783. describe( 'single character', () => {
  784. let expected;
  785. before( () => {
  786. expected = [
  787. {
  788. type: 'elementStart',
  789. item: img1,
  790. previousPosition: new Position( root, 0 ),
  791. nextPosition: new Position( img1, 0 )
  792. },
  793. {
  794. type: 'elementStart',
  795. item: paragraph,
  796. previousPosition: new Position( root, 1 ),
  797. nextPosition: new Position( paragraph, 0 )
  798. },
  799. {
  800. type: 'elementStart',
  801. item: bold,
  802. previousPosition: new Position( paragraph, 0 ),
  803. nextPosition: new Position( bold, 0 )
  804. },
  805. {
  806. type: 'text',
  807. text: 'a',
  808. previousPosition: new Position( bold, 0 ),
  809. nextPosition: new Position( textAbcd, 1 )
  810. },
  811. {
  812. type: 'text',
  813. text: 'b',
  814. previousPosition: new Position( textAbcd, 1 ),
  815. nextPosition: new Position( textAbcd, 2 )
  816. },
  817. {
  818. type: 'text',
  819. text: 'c',
  820. previousPosition: new Position( textAbcd, 2 ),
  821. nextPosition: new Position( textAbcd, 3 )
  822. },
  823. {
  824. type: 'text',
  825. text: 'd',
  826. previousPosition: new Position( textAbcd, 3 ),
  827. nextPosition: new Position( bold, 1 )
  828. },
  829. {
  830. type: 'text',
  831. text: 'y',
  832. previousPosition: new Position( paragraph, 1 ),
  833. nextPosition: new Position( paragraph, 2 )
  834. },
  835. {
  836. type: 'elementStart',
  837. item: img2,
  838. previousPosition: new Position( paragraph, 2 ),
  839. nextPosition: new Position( img2, 0 )
  840. },
  841. {
  842. type: 'text',
  843. text: 'x',
  844. previousPosition: new Position( paragraph, 3 ),
  845. nextPosition: new Position( paragraph, 4 )
  846. }
  847. ];
  848. } );
  849. it( 'should return single characters ignoring elementEnd', () => {
  850. const iterator = new TreeWalker( {
  851. startPosition: rootBeginning,
  852. singleCharacters: true,
  853. ignoreElementEnd: true
  854. } );
  855. let i = 0;
  856. for ( const value of iterator ) {
  857. expectValue( value, expected[ i++ ] );
  858. }
  859. expect( i ).to.equal( expected.length );
  860. } );
  861. it( 'should return single characters ignoring elementEnd going backward', () => {
  862. const iterator = new TreeWalker( {
  863. startPosition: rootEnding,
  864. singleCharacters: true,
  865. ignoreElementEnd: true,
  866. direction: 'backward'
  867. } );
  868. let i = expected.length;
  869. for ( const value of iterator ) {
  870. expectValue( value, expected[ --i ], { direction: 'backward' } );
  871. }
  872. expect( i ).to.equal( 0 );
  873. } );
  874. } );
  875. } );
  876. it( 'should not return elementEnd for a text node when iteration begins at the end of that text node', () => {
  877. const iterator = new TreeWalker( {
  878. startPosition: Position._createAt( textAbcd, 'end' )
  879. } );
  880. const step = iterator.next();
  881. expect( step.value.type ).to.equal( 'elementEnd' );
  882. expect( step.value.item ).to.equal( bold );
  883. } );
  884. it( 'should not return elementStart for a text node when iteration begins at the start of that text node', () => {
  885. const iterator = new TreeWalker( {
  886. startPosition: Position._createAt( textAbcd, 0 ),
  887. direction: 'backward'
  888. } );
  889. const step = iterator.next();
  890. expect( step.value.type ).to.equal( 'elementStart' );
  891. expect( step.value.item ).to.equal( bold );
  892. } );
  893. it( 'should iterate over document fragment', () => {
  894. const foo = new Text( 'foo' );
  895. const bar = new Text( 'bar' );
  896. const p = new ContainerElement( 'p', null, foo );
  897. const b = new AttributeElement( 'b', null, bar );
  898. const docFrag = new DocumentFragment( [ p, b ] );
  899. const expected = [
  900. {
  901. type: 'elementStart',
  902. item: p,
  903. previousPosition: new Position( docFrag, 0 ),
  904. nextPosition: new Position( p, 0 )
  905. },
  906. {
  907. type: 'text',
  908. text: 'foo',
  909. previousPosition: new Position( p, 0 ),
  910. nextPosition: new Position( p, 1 )
  911. },
  912. {
  913. type: 'elementEnd',
  914. item: p,
  915. previousPosition: new Position( p, 1 ),
  916. nextPosition: new Position( docFrag, 1 )
  917. },
  918. {
  919. type: 'elementStart',
  920. item: b,
  921. previousPosition: new Position( docFrag, 1 ),
  922. nextPosition: new Position( b, 0 )
  923. },
  924. {
  925. type: 'text',
  926. text: 'bar',
  927. previousPosition: new Position( b, 0 ),
  928. nextPosition: new Position( b, 1 )
  929. },
  930. {
  931. type: 'elementEnd',
  932. item: b,
  933. previousPosition: new Position( b, 1 ),
  934. nextPosition: new Position( docFrag, 2 )
  935. }
  936. ];
  937. const iterator = new TreeWalker( { boundaries: Range._createIn( docFrag ) } );
  938. let i = 0;
  939. for ( const value of iterator ) {
  940. expectValue( value, expected[ i++ ] );
  941. }
  942. expect( i ).to.equal( expected.length );
  943. } );
  944. describe( 'skip', () => {
  945. describe( 'forward treewalker', () => {
  946. it( 'should jump over all text nodes', () => {
  947. const walker = new TreeWalker( {
  948. startPosition: new Position( paragraph, 0 )
  949. } );
  950. walker.skip( value => value.type == 'text' || value.item.name == 'b' );
  951. expect( walker.position.parent ).to.equal( paragraph );
  952. expect( walker.position.offset ).to.equal( 2 );
  953. } );
  954. it( 'should do not move if the condition is false', () => {
  955. const walker = new TreeWalker( {
  956. startPosition: new Position( bold, 0 )
  957. } );
  958. walker.skip( () => false );
  959. expect( walker.position.parent ).to.equal( bold );
  960. expect( walker.position.offset ).to.equal( 0 );
  961. } );
  962. it( 'should do not move if the condition is false and the position is in text node', () => {
  963. const walker = new TreeWalker( {
  964. startPosition: new Position( bold.getChild( 0 ), 2 )
  965. } );
  966. walker.skip( () => false );
  967. expect( walker.position.parent ).to.equal( bold.getChild( 0 ) );
  968. expect( walker.position.offset ).to.equal( 2 );
  969. } );
  970. it( 'should move to the end if the condition is true', () => {
  971. const walker = new TreeWalker( {
  972. startPosition: new Position( bold, 0 )
  973. } );
  974. walker.skip( () => true );
  975. expect( walker.position.parent ).to.equal( rootEnding.parent );
  976. expect( walker.position.offset ).to.equal( rootEnding.offset );
  977. } );
  978. } );
  979. describe( 'backward treewalker', () => {
  980. it( 'should jump over all text nodes', () => {
  981. const walker = new TreeWalker( {
  982. startPosition: new Position( bold.getChild( 0 ), 2 ),
  983. direction: 'backward'
  984. } );
  985. walker.skip( value => value.type == 'text' || value.item.name == 'b' );
  986. expect( walker.position.parent ).to.equal( paragraph );
  987. expect( walker.position.offset ).to.equal( 0 );
  988. } );
  989. it( 'should do not move if the condition is false', () => {
  990. const walker = new TreeWalker( {
  991. startPosition: new Position( bold, 0 ),
  992. direction: 'backward'
  993. } );
  994. walker.skip( () => false );
  995. expect( walker.position.parent ).to.equal( bold );
  996. expect( walker.position.offset ).to.equal( 0 );
  997. } );
  998. it( 'should move to the end if the condition is true', () => {
  999. const walker = new TreeWalker( {
  1000. startPosition: new Position( bold, 0 ),
  1001. direction: 'backward'
  1002. } );
  1003. walker.skip( () => true );
  1004. expect( walker.position.parent ).to.equal( rootBeginning.parent );
  1005. expect( walker.position.offset ).to.equal( rootBeginning.offset );
  1006. } );
  1007. } );
  1008. } );
  1009. } );
  1010. function expectValue( value, expected, options = {} ) {
  1011. let expectedPreviousPosition, expectedNextPosition;
  1012. if ( options.direction == 'backward' ) {
  1013. expectedNextPosition = expected.previousPosition;
  1014. expectedPreviousPosition = expected.nextPosition;
  1015. } else {
  1016. expectedNextPosition = expected.nextPosition;
  1017. expectedPreviousPosition = expected.previousPosition;
  1018. }
  1019. expect( value.type ).to.equal( expected.type );
  1020. expect( value.previousPosition ).to.deep.equal( expectedPreviousPosition );
  1021. expect( value.nextPosition ).to.deep.equal( expectedNextPosition );
  1022. if ( value.type == 'text' ) {
  1023. expectText( value, expected );
  1024. } else if ( value.type == 'elementStart' ) {
  1025. expectStart( value, expected );
  1026. } else if ( value.type == 'elementEnd' ) {
  1027. expectEnd( value, expected );
  1028. }
  1029. }
  1030. function expectText( value, expected ) {
  1031. expect( value.item.data ).to.equal( expected.text );
  1032. expect( value.length ).to.equal( value.item.data.length );
  1033. }
  1034. function expectStart( value, expected ) {
  1035. expect( value.item ).to.equal( expected.item );
  1036. expect( value.length ).to.equal( 1 );
  1037. }
  1038. function expectEnd( value, expected ) {
  1039. expect( value.item ).to.equal( expected.item );
  1040. expect( value.length ).to.be.undefined;
  1041. }