documentfragment.js 8.6 KB

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