documentfragment.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: treeview */
  6. 'use strict';
  7. import DocumentFragment from '/ckeditor5/engine/treeview/documentfragment.js';
  8. import Element from '/ckeditor5/engine/treeview/element.js';
  9. import Node from '/ckeditor5/engine/treeview/node.js';
  10. describe( 'DocumentFragment', () => {
  11. describe( 'constructor', () => {
  12. it( 'should create DocumentFragment without children', () => {
  13. const fragment = new DocumentFragment();
  14. expect( fragment ).to.be.an.instanceof( DocumentFragment );
  15. expect( fragment.getChildCount() ).to.equal( 0 );
  16. } );
  17. it( 'should create DocumentFragment with child node', () => {
  18. const child = new Element( 'p' );
  19. const fragment = new DocumentFragment( child );
  20. expect( fragment.getChildCount() ).to.equal( 1 );
  21. expect( fragment.getChild( 0 ) ).to.have.property( 'name' ).that.equals( 'p' );
  22. } );
  23. it( 'should create DocumentFragment with multiple nodes', () => {
  24. const children = [ new Element( 'p' ), new Element( 'div' ) ];
  25. const fragment = new DocumentFragment( children );
  26. expect( fragment.getChildCount() ).to.equal( 2 );
  27. expect( fragment.getChild( 0 ) ).to.have.property( 'name' ).that.equals( 'p' );
  28. expect( fragment.getChild( 1 ) ).to.have.property( 'name' ).that.equals( 'div' );
  29. } );
  30. } );
  31. describe( 'iterator', () => {
  32. it( 'should iterate over all nodes added to document fragment', () => {
  33. const children = [ new Element( 'p' ), new Element( 'div' ) ];
  34. const fragment = new DocumentFragment( children );
  35. const arr = Array.from( fragment );
  36. expect( arr.length ).to.equal( 2 );
  37. expect( arr[ 0 ] ).to.have.property( 'name' ).that.equals( 'p' );
  38. expect( arr[ 1 ] ).to.have.property( 'name' ).that.equals( 'div' );
  39. } );
  40. } );
  41. describe( 'children manipulation methods', () => {
  42. let fragment, el1, el2, el3, el4;
  43. beforeEach( () => {
  44. fragment = new DocumentFragment();
  45. el1 = new Element( 'el1' );
  46. el2 = new Element( 'el2' );
  47. el3 = new Element( 'el3' );
  48. el4 = new Element( 'el4' );
  49. } );
  50. describe( 'insertion', () => {
  51. it( 'should insert children', () => {
  52. const count1 = fragment.insertChildren( 0, [ el1, el3 ] );
  53. const count2 = fragment.insertChildren( 1, el2 );
  54. expect( fragment.getChildCount() ).to.equal( 3 );
  55. expect( fragment.getChild( 0 ) ).to.have.property( 'name' ).that.equals( 'el1' );
  56. expect( fragment.getChild( 1 ) ).to.have.property( 'name' ).that.equals( 'el2' );
  57. expect( fragment.getChild( 2 ) ).to.have.property( 'name' ).that.equals( 'el3' );
  58. expect( count1 ).to.equal( 2 );
  59. expect( count2 ).to.equal( 1 );
  60. } );
  61. it( 'should append children', () => {
  62. const count1 = fragment.insertChildren( 0, el1 );
  63. const count2 = fragment.appendChildren( el2 );
  64. const count3 = fragment.appendChildren( el3 );
  65. expect( fragment.getChildCount() ).to.equal( 3 );
  66. expect( fragment.getChild( 0 ) ).to.have.property( 'name' ).that.equals( 'el1' );
  67. expect( fragment.getChild( 1 ) ).to.have.property( 'name' ).that.equals( 'el2' );
  68. expect( fragment.getChild( 2 ) ).to.have.property( 'name' ).that.equals( 'el3' );
  69. expect( count1 ).to.equal( 1 );
  70. expect( count2 ).to.equal( 1 );
  71. expect( count3 ).to.equal( 1 );
  72. } );
  73. } );
  74. describe( 'getChildIndex', () => {
  75. it( 'should return child index', () => {
  76. fragment.appendChildren( el1 );
  77. fragment.appendChildren( el2 );
  78. fragment.appendChildren( el3 );
  79. expect( fragment.getChildCount() ).to.equal( 3 );
  80. expect( fragment.getChildIndex( el1 ) ).to.equal( 0 );
  81. expect( fragment.getChildIndex( el2 ) ).to.equal( 1 );
  82. expect( fragment.getChildIndex( el3 ) ).to.equal( 2 );
  83. } );
  84. } );
  85. describe( 'getChildren', () => {
  86. it( 'should renturn children iterator', () => {
  87. fragment.appendChildren( el1 );
  88. fragment.appendChildren( el2 );
  89. fragment.appendChildren( el3 );
  90. const expected = [ el1, el2, el3 ];
  91. let i = 0;
  92. for ( let child of fragment.getChildren() ) {
  93. expect( child ).to.equal( expected[ i ] );
  94. i++;
  95. }
  96. expect( i ).to.equal( 3 );
  97. } );
  98. } );
  99. describe( 'removeChildren', () => {
  100. it( 'should remove children', () => {
  101. fragment.appendChildren( el1 );
  102. fragment.appendChildren( el2 );
  103. fragment.appendChildren( el3 );
  104. fragment.appendChildren( el4 );
  105. fragment.removeChildren( 1, 2 );
  106. expect( fragment.getChildCount() ).to.equal( 2 );
  107. expect( fragment.getChild( 0 ) ).to.have.property( 'name' ).that.equals( 'el1' );
  108. expect( fragment.getChild( 1 ) ).to.have.property( 'name' ).that.equals( 'el4' );
  109. expect( el1.parent ).to.equal( fragment );
  110. expect( el2.parent ).to.be.null;
  111. expect( el3.parent ).to.be.null;
  112. expect( el4.parent ).equal( fragment );
  113. } );
  114. it( 'should remove one child when second parameter is not specified', () => {
  115. fragment.appendChildren( el1 );
  116. fragment.appendChildren( el2 );
  117. fragment.appendChildren( el3 );
  118. const removed = fragment.removeChildren( 1 );
  119. expect( fragment.getChildCount() ).to.equal( 2 );
  120. expect( fragment.getChild( 0 ) ).to.have.property( 'name' ).that.equals( 'el1' );
  121. expect( fragment.getChild( 1 ) ).to.have.property( 'name' ).that.equals( 'el3' );
  122. expect( removed.length ).to.equal( 1 );
  123. expect( removed[ 0 ] ).to.have.property( 'name' ).that.equals( 'el2' );
  124. } );
  125. } );
  126. } );
  127. describe( 'node methods when inserted to fragment', () => {
  128. it( 'getIndex() should return proper value', () => {
  129. const node1 = new Node();
  130. const node2 = new Node();
  131. const node3 = new Node();
  132. const fragment = new DocumentFragment( [ node1, node2, node3 ] );
  133. expect( node1.getIndex() ).to.equal( 0 );
  134. expect( node2.getIndex() ).to.equal( 1 );
  135. expect( node3.getIndex() ).to.equal( 2 );
  136. expect( node1.parent ).to.equal( fragment );
  137. expect( node2.parent ).to.equal( fragment );
  138. expect( node3.parent ).to.equal( fragment );
  139. } );
  140. it( 'getNextSibling() should return proper node', () => {
  141. const node1 = new Node();
  142. const node2 = new Node();
  143. const node3 = new Node();
  144. new DocumentFragment( [ node1, node2, node3 ] );
  145. expect( node1.getNextSibling() ).to.equal( node2 );
  146. expect( node2.getNextSibling() ).to.equal( node3 );
  147. expect( node3.getNextSibling() ).to.be.null;
  148. } );
  149. it( 'getPreviousSibling() should return proper node', () => {
  150. const node1 = new Node();
  151. const node2 = new Node();
  152. const node3 = new Node();
  153. new DocumentFragment( [ node1, node2, node3 ] );
  154. expect( node1.getPreviousSibling() ).to.be.null;
  155. expect( node2.getPreviousSibling() ).to.equal( node1 );
  156. expect( node3.getPreviousSibling() ).to.equal( node2 );
  157. } );
  158. it( 'remove() should remove node from fragment', () => {
  159. const node1 = new Node();
  160. const node2 = new Node();
  161. const node3 = new Node();
  162. const fragment = new DocumentFragment( [ node1, node2, node3 ] );
  163. node1.remove();
  164. node3.remove();
  165. expect( fragment.getChildCount() ).to.equal( 1 );
  166. expect( node1.parent ).to.be.null;
  167. expect( node3.parent ).to.be.null;
  168. expect( fragment.getChild( 0 ) ).to.equal( node2 );
  169. } );
  170. } );
  171. } );