documentfragment.js 11 KB

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