8
0

documentfragment.js 8.9 KB

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