documentfragment.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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( 'is', () => {
  56. let frag;
  57. before( () => {
  58. frag = new DocumentFragment();
  59. } );
  60. it( 'should return true for documentFragment', () => {
  61. expect( frag.is( 'documentFragment' ) ).to.be.true;
  62. } );
  63. it( 'should return false for other accept values', () => {
  64. expect( frag.is( 'text' ) ).to.be.false;
  65. expect( frag.is( 'textProxy' ) ).to.be.false;
  66. expect( frag.is( 'element' ) ).to.be.false;
  67. expect( frag.is( 'containerElement' ) ).to.be.false;
  68. expect( frag.is( 'attributeElement' ) ).to.be.false;
  69. expect( frag.is( 'uiElement' ) ).to.be.false;
  70. expect( frag.is( 'emptyElement' ) ).to.be.false;
  71. expect( frag.is( 'rootElement' ) ).to.be.false;
  72. } );
  73. } );
  74. describe( 'children manipulation methods', () => {
  75. let fragment, el1, el2, el3, el4;
  76. beforeEach( () => {
  77. fragment = new DocumentFragment();
  78. el1 = new Element( 'el1' );
  79. el2 = new Element( 'el2' );
  80. el3 = new Element( 'el3' );
  81. el4 = new Element( 'el4' );
  82. } );
  83. describe( 'insertion', () => {
  84. it( 'should insert children', () => {
  85. const count1 = fragment.insertChildren( 0, [ el1, el3 ] );
  86. const count2 = fragment.insertChildren( 1, el2 );
  87. expect( fragment.childCount ).to.equal( 3 );
  88. expect( fragment.getChild( 0 ) ).to.have.property( 'name' ).that.equals( 'el1' );
  89. expect( fragment.getChild( 1 ) ).to.have.property( 'name' ).that.equals( 'el2' );
  90. expect( fragment.getChild( 2 ) ).to.have.property( 'name' ).that.equals( 'el3' );
  91. expect( count1 ).to.equal( 2 );
  92. expect( count2 ).to.equal( 1 );
  93. } );
  94. it( 'should accept strings', () => {
  95. fragment.insertChildren( 0, 'abc' );
  96. expect( fragment.childCount ).to.equal( 1 );
  97. expect( fragment.getChild( 0 ) ).to.have.property( 'data' ).that.equals( 'abc' );
  98. fragment.removeChildren( 0, 1 );
  99. fragment.insertChildren( 0, [ new Element( 'p' ), 'abc' ] );
  100. expect( fragment.childCount ).to.equal( 2 );
  101. expect( fragment.getChild( 1 ) ).to.have.property( 'data' ).that.equals( 'abc' );
  102. } );
  103. it( 'should append children', () => {
  104. const count1 = fragment.insertChildren( 0, el1 );
  105. const count2 = fragment.appendChildren( el2 );
  106. const count3 = fragment.appendChildren( el3 );
  107. expect( fragment.childCount ).to.equal( 3 );
  108. expect( fragment.getChild( 0 ) ).to.have.property( 'name' ).that.equals( 'el1' );
  109. expect( fragment.getChild( 1 ) ).to.have.property( 'name' ).that.equals( 'el2' );
  110. expect( fragment.getChild( 2 ) ).to.have.property( 'name' ).that.equals( 'el3' );
  111. expect( count1 ).to.equal( 1 );
  112. expect( count2 ).to.equal( 1 );
  113. expect( count3 ).to.equal( 1 );
  114. } );
  115. it( 'should fire change event when inserting', done => {
  116. fragment.once( 'change:children', ( event, node ) => {
  117. expect( node ).to.equal( fragment );
  118. done();
  119. } );
  120. fragment.insertChildren( 0, el1 );
  121. } );
  122. it( 'should fire change event when appending', done => {
  123. fragment.once( 'change:children', ( event, node ) => {
  124. expect( node ).to.equal( fragment );
  125. done();
  126. } );
  127. fragment.appendChildren( el1 );
  128. } );
  129. } );
  130. describe( 'getChildIndex', () => {
  131. it( 'should return child index', () => {
  132. fragment.appendChildren( el1 );
  133. fragment.appendChildren( el2 );
  134. fragment.appendChildren( el3 );
  135. expect( fragment.childCount ).to.equal( 3 );
  136. expect( fragment.getChildIndex( el1 ) ).to.equal( 0 );
  137. expect( fragment.getChildIndex( el2 ) ).to.equal( 1 );
  138. expect( fragment.getChildIndex( el3 ) ).to.equal( 2 );
  139. } );
  140. } );
  141. describe( 'getChildren', () => {
  142. it( 'should renturn children iterator', () => {
  143. fragment.appendChildren( el1 );
  144. fragment.appendChildren( el2 );
  145. fragment.appendChildren( el3 );
  146. const expected = [ el1, el2, el3 ];
  147. let i = 0;
  148. for ( const child of fragment.getChildren() ) {
  149. expect( child ).to.equal( expected[ i ] );
  150. i++;
  151. }
  152. expect( i ).to.equal( 3 );
  153. } );
  154. } );
  155. describe( 'removeChildren', () => {
  156. it( 'should remove children', () => {
  157. fragment.appendChildren( el1 );
  158. fragment.appendChildren( el2 );
  159. fragment.appendChildren( el3 );
  160. fragment.appendChildren( el4 );
  161. fragment.removeChildren( 1, 2 );
  162. expect( fragment.childCount ).to.equal( 2 );
  163. expect( fragment.getChild( 0 ) ).to.have.property( 'name' ).that.equals( 'el1' );
  164. expect( fragment.getChild( 1 ) ).to.have.property( 'name' ).that.equals( 'el4' );
  165. expect( el1.parent ).to.equal( fragment );
  166. expect( el2.parent ).to.be.null;
  167. expect( el3.parent ).to.be.null;
  168. expect( el4.parent ).equal( fragment );
  169. } );
  170. it( 'should remove one child when second parameter is not specified', () => {
  171. fragment.appendChildren( el1 );
  172. fragment.appendChildren( el2 );
  173. fragment.appendChildren( el3 );
  174. const removed = fragment.removeChildren( 1 );
  175. expect( fragment.childCount ).to.equal( 2 );
  176. expect( fragment.getChild( 0 ) ).to.have.property( 'name' ).that.equals( 'el1' );
  177. expect( fragment.getChild( 1 ) ).to.have.property( 'name' ).that.equals( 'el3' );
  178. expect( removed.length ).to.equal( 1 );
  179. expect( removed[ 0 ] ).to.have.property( 'name' ).that.equals( 'el2' );
  180. } );
  181. it( 'should fire change event', done => {
  182. fragment.appendChildren( el1 );
  183. fragment.once( 'change:children', ( event, node ) => {
  184. expect( node ).to.equal( fragment );
  185. done();
  186. } );
  187. fragment.removeChildren( 0 );
  188. } );
  189. } );
  190. } );
  191. describe( 'node methods when inserted to fragment', () => {
  192. it( 'index should return proper value', () => {
  193. const node1 = new Node();
  194. const node2 = new Node();
  195. const node3 = new Node();
  196. const fragment = new DocumentFragment( [ node1, node2, node3 ] );
  197. expect( node1.index ).to.equal( 0 );
  198. expect( node2.index ).to.equal( 1 );
  199. expect( node3.index ).to.equal( 2 );
  200. expect( node1.parent ).to.equal( fragment );
  201. expect( node2.parent ).to.equal( fragment );
  202. expect( node3.parent ).to.equal( fragment );
  203. } );
  204. it( 'nextSibling should return proper node', () => {
  205. const node1 = new Node();
  206. const node2 = new Node();
  207. const node3 = new Node();
  208. new DocumentFragment( [ node1, node2, node3 ] ); // eslint-disable-line no-new
  209. expect( node1.nextSibling ).to.equal( node2 );
  210. expect( node2.nextSibling ).to.equal( node3 );
  211. expect( node3.nextSibling ).to.be.null;
  212. } );
  213. it( 'previousSibling should return proper node', () => {
  214. const node1 = new Node();
  215. const node2 = new Node();
  216. const node3 = new Node();
  217. new DocumentFragment( [ node1, node2, node3 ] ); // eslint-disable-line no-new
  218. expect( node1.previousSibling ).to.be.null;
  219. expect( node2.previousSibling ).to.equal( node1 );
  220. expect( node3.previousSibling ).to.equal( node2 );
  221. } );
  222. it( 'remove() should remove node from fragment', () => {
  223. const node1 = new Node();
  224. const node2 = new Node();
  225. const node3 = new Node();
  226. const fragment = new DocumentFragment( [ node1, node2, node3 ] );
  227. node1.remove();
  228. node3.remove();
  229. expect( fragment.childCount ).to.equal( 1 );
  230. expect( node1.parent ).to.be.null;
  231. expect( node3.parent ).to.be.null;
  232. expect( fragment.getChild( 0 ) ).to.equal( node2 );
  233. } );
  234. } );
  235. } );