documentfragment.js 11 KB

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