8
0

documentfragment.js 10 KB

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