documentfragment.js 9.9 KB

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