documentfragment.js 7.8 KB

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