documentfragment.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 accept strings', () => {
  83. fragment.insertChildren( 0, 'abc' );
  84. expect( fragment.getChildCount() ).to.equal( 1 );
  85. expect( fragment.getChild( 0 ) ).to.have.property( 'data' ).that.equals( 'abc' );
  86. } );
  87. it( 'should append children', () => {
  88. const count1 = fragment.insertChildren( 0, el1 );
  89. const count2 = fragment.appendChildren( el2 );
  90. const count3 = fragment.appendChildren( el3 );
  91. expect( fragment.getChildCount() ).to.equal( 3 );
  92. expect( fragment.getChild( 0 ) ).to.have.property( 'name' ).that.equals( 'el1' );
  93. expect( fragment.getChild( 1 ) ).to.have.property( 'name' ).that.equals( 'el2' );
  94. expect( fragment.getChild( 2 ) ).to.have.property( 'name' ).that.equals( 'el3' );
  95. expect( count1 ).to.equal( 1 );
  96. expect( count2 ).to.equal( 1 );
  97. expect( count3 ).to.equal( 1 );
  98. } );
  99. it( 'should fire change event when inserting', ( done ) => {
  100. fragment.once( 'change:children', ( event, node ) => {
  101. expect( node ).to.equal( fragment );
  102. done();
  103. } );
  104. fragment.insertChildren( 0, el1 );
  105. } );
  106. it( 'should fire change event when appending', ( done ) => {
  107. fragment.once( 'change:children', ( event, node ) => {
  108. expect( node ).to.equal( fragment );
  109. done();
  110. } );
  111. fragment.appendChildren( el1 );
  112. } );
  113. } );
  114. describe( 'getChildIndex', () => {
  115. it( 'should return child index', () => {
  116. fragment.appendChildren( el1 );
  117. fragment.appendChildren( el2 );
  118. fragment.appendChildren( el3 );
  119. expect( fragment.getChildCount() ).to.equal( 3 );
  120. expect( fragment.getChildIndex( el1 ) ).to.equal( 0 );
  121. expect( fragment.getChildIndex( el2 ) ).to.equal( 1 );
  122. expect( fragment.getChildIndex( el3 ) ).to.equal( 2 );
  123. } );
  124. } );
  125. describe( 'getChildren', () => {
  126. it( 'should renturn children iterator', () => {
  127. fragment.appendChildren( el1 );
  128. fragment.appendChildren( el2 );
  129. fragment.appendChildren( el3 );
  130. const expected = [ el1, el2, el3 ];
  131. let i = 0;
  132. for ( let child of fragment.getChildren() ) {
  133. expect( child ).to.equal( expected[ i ] );
  134. i++;
  135. }
  136. expect( i ).to.equal( 3 );
  137. } );
  138. } );
  139. describe( 'removeChildren', () => {
  140. it( 'should remove children', () => {
  141. fragment.appendChildren( el1 );
  142. fragment.appendChildren( el2 );
  143. fragment.appendChildren( el3 );
  144. fragment.appendChildren( el4 );
  145. fragment.removeChildren( 1, 2 );
  146. expect( fragment.getChildCount() ).to.equal( 2 );
  147. expect( fragment.getChild( 0 ) ).to.have.property( 'name' ).that.equals( 'el1' );
  148. expect( fragment.getChild( 1 ) ).to.have.property( 'name' ).that.equals( 'el4' );
  149. expect( el1.parent ).to.equal( fragment );
  150. expect( el2.parent ).to.be.null;
  151. expect( el3.parent ).to.be.null;
  152. expect( el4.parent ).equal( fragment );
  153. } );
  154. it( 'should remove one child when second parameter is not specified', () => {
  155. fragment.appendChildren( el1 );
  156. fragment.appendChildren( el2 );
  157. fragment.appendChildren( el3 );
  158. const removed = fragment.removeChildren( 1 );
  159. expect( fragment.getChildCount() ).to.equal( 2 );
  160. expect( fragment.getChild( 0 ) ).to.have.property( 'name' ).that.equals( 'el1' );
  161. expect( fragment.getChild( 1 ) ).to.have.property( 'name' ).that.equals( 'el3' );
  162. expect( removed.length ).to.equal( 1 );
  163. expect( removed[ 0 ] ).to.have.property( 'name' ).that.equals( 'el2' );
  164. } );
  165. it( 'should fire change event', ( done ) => {
  166. fragment.appendChildren( el1 );
  167. fragment.once( 'change:children', ( event, node ) => {
  168. expect( node ).to.equal( fragment );
  169. done();
  170. } );
  171. fragment.removeChildren( 0 );
  172. } );
  173. } );
  174. } );
  175. describe( 'node methods when inserted to fragment', () => {
  176. it( 'getIndex() should return proper value', () => {
  177. const node1 = new Node();
  178. const node2 = new Node();
  179. const node3 = new Node();
  180. const fragment = new DocumentFragment( [ node1, node2, node3 ] );
  181. expect( node1.getIndex() ).to.equal( 0 );
  182. expect( node2.getIndex() ).to.equal( 1 );
  183. expect( node3.getIndex() ).to.equal( 2 );
  184. expect( node1.parent ).to.equal( fragment );
  185. expect( node2.parent ).to.equal( fragment );
  186. expect( node3.parent ).to.equal( fragment );
  187. } );
  188. it( 'getNextSibling() should return proper node', () => {
  189. const node1 = new Node();
  190. const node2 = new Node();
  191. const node3 = new Node();
  192. new DocumentFragment( [ node1, node2, node3 ] );
  193. expect( node1.getNextSibling() ).to.equal( node2 );
  194. expect( node2.getNextSibling() ).to.equal( node3 );
  195. expect( node3.getNextSibling() ).to.be.null;
  196. } );
  197. it( 'getPreviousSibling() should return proper node', () => {
  198. const node1 = new Node();
  199. const node2 = new Node();
  200. const node3 = new Node();
  201. new DocumentFragment( [ node1, node2, node3 ] );
  202. expect( node1.getPreviousSibling() ).to.be.null;
  203. expect( node2.getPreviousSibling() ).to.equal( node1 );
  204. expect( node3.getPreviousSibling() ).to.equal( node2 );
  205. } );
  206. it( 'remove() should remove node from fragment', () => {
  207. const node1 = new Node();
  208. const node2 = new Node();
  209. const node3 = new Node();
  210. const fragment = new DocumentFragment( [ node1, node2, node3 ] );
  211. node1.remove();
  212. node3.remove();
  213. expect( fragment.getChildCount() ).to.equal( 1 );
  214. expect( node1.parent ).to.be.null;
  215. expect( node3.parent ).to.be.null;
  216. expect( fragment.getChild( 0 ) ).to.equal( node2 );
  217. } );
  218. } );
  219. } );