documentfragment.js 8.5 KB

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