documentfragment.js 11 KB

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