8
0

documentfragment.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: treeview */
  6. 'use strict';
  7. import DocumentFragment from '/ckeditor5/engine/treeview/documentfragment.js';
  8. import Element from '/ckeditor5/engine/treeview/element.js';
  9. import Node from '/ckeditor5/engine/treeview/node.js';
  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.getChildCount() ).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.getChildCount() ).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.getChildCount() ).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( 'children manipulation methods', () => {
  32. let fragment, el1, el2, el3, el4;
  33. beforeEach( () => {
  34. fragment = new DocumentFragment();
  35. el1 = new Element( 'el1' );
  36. el2 = new Element( 'el2' );
  37. el3 = new Element( 'el3' );
  38. el4 = new Element( 'el4' );
  39. } );
  40. describe( 'insertion', () => {
  41. it( 'should insert children', () => {
  42. const count1 = fragment.insertChildren( 0, [ el1, el3 ] );
  43. const count2 = fragment.insertChildren( 1, el2 );
  44. expect( fragment.getChildCount() ).to.equal( 3 );
  45. expect( fragment.getChild( 0 ) ).to.have.property( 'name' ).that.equals( 'el1' );
  46. expect( fragment.getChild( 1 ) ).to.have.property( 'name' ).that.equals( 'el2' );
  47. expect( fragment.getChild( 2 ) ).to.have.property( 'name' ).that.equals( 'el3' );
  48. expect( count1 ).to.equal( 2 );
  49. expect( count2 ).to.equal( 1 );
  50. } );
  51. it( 'should append children', () => {
  52. const count1 = fragment.insertChildren( 0, el1 );
  53. const count2 = fragment.appendChildren( el2 );
  54. const count3 = fragment.appendChildren( el3 );
  55. expect( fragment.getChildCount() ).to.equal( 3 );
  56. expect( fragment.getChild( 0 ) ).to.have.property( 'name' ).that.equals( 'el1' );
  57. expect( fragment.getChild( 1 ) ).to.have.property( 'name' ).that.equals( 'el2' );
  58. expect( fragment.getChild( 2 ) ).to.have.property( 'name' ).that.equals( 'el3' );
  59. expect( count1 ).to.equal( 1 );
  60. expect( count2 ).to.equal( 1 );
  61. expect( count3 ).to.equal( 1 );
  62. } );
  63. } );
  64. describe( 'getChildIndex', () => {
  65. it( 'should return child index', () => {
  66. fragment.appendChildren( el1 );
  67. fragment.appendChildren( el2 );
  68. fragment.appendChildren( el3 );
  69. expect( fragment.getChildCount() ).to.equal( 3 );
  70. expect( fragment.getChildIndex( el1 ) ).to.equal( 0 );
  71. expect( fragment.getChildIndex( el2 ) ).to.equal( 1 );
  72. expect( fragment.getChildIndex( el3 ) ).to.equal( 2 );
  73. } );
  74. } );
  75. describe( 'getChildren', () => {
  76. it( 'should renturn children iterator', () => {
  77. fragment.appendChildren( el1 );
  78. fragment.appendChildren( el2 );
  79. fragment.appendChildren( el3 );
  80. const expected = [ el1, el2, el3 ];
  81. let i = 0;
  82. for ( let child of fragment.getChildren() ) {
  83. expect( child ).to.equal( expected[ i ] );
  84. i++;
  85. }
  86. expect( i ).to.equal( 3 );
  87. } );
  88. } );
  89. describe( 'removeChildren', () => {
  90. it( 'should remove children', () => {
  91. fragment.appendChildren( el1 );
  92. fragment.appendChildren( el2 );
  93. fragment.appendChildren( el3 );
  94. fragment.appendChildren( el4 );
  95. fragment.removeChildren( 1, 2 );
  96. expect( fragment.getChildCount() ).to.equal( 2 );
  97. expect( fragment.getChild( 0 ) ).to.have.property( 'name' ).that.equals( 'el1' );
  98. expect( fragment.getChild( 1 ) ).to.have.property( 'name' ).that.equals( 'el4' );
  99. expect( el1.parent ).to.equal( fragment );
  100. expect( el2.parent ).to.be.null;
  101. expect( el3.parent ).to.be.null;
  102. expect( el4.parent ).equal( fragment );
  103. } );
  104. it( 'should remove one child when second parameter is not specified', () => {
  105. fragment.appendChildren( el1 );
  106. fragment.appendChildren( el2 );
  107. fragment.appendChildren( el3 );
  108. const removed = fragment.removeChildren( 1 );
  109. expect( fragment.getChildCount() ).to.equal( 2 );
  110. expect( fragment.getChild( 0 ) ).to.have.property( 'name' ).that.equals( 'el1' );
  111. expect( fragment.getChild( 1 ) ).to.have.property( 'name' ).that.equals( 'el3' );
  112. expect( removed.length ).to.equal( 1 );
  113. expect( removed[ 0 ] ).to.have.property( 'name' ).that.equals( 'el2' );
  114. } );
  115. } );
  116. } );
  117. describe( 'node methods when inserted to fragment', () => {
  118. it( 'getIndex() should return proper value', () => {
  119. const node1 = new Node();
  120. const node2 = new Node();
  121. const node3 = new Node();
  122. const fragment = new DocumentFragment( [ node1, node2, node3 ] );
  123. expect( node1.getIndex() ).to.equal( 0 );
  124. expect( node2.getIndex() ).to.equal( 1 );
  125. expect( node3.getIndex() ).to.equal( 2 );
  126. expect( node1.parent ).to.equal( fragment );
  127. expect( node2.parent ).to.equal( fragment );
  128. expect( node3.parent ).to.equal( fragment );
  129. } );
  130. it( 'getNextSibling() should return proper node', () => {
  131. const node1 = new Node();
  132. const node2 = new Node();
  133. const node3 = new Node();
  134. new DocumentFragment( [ node1, node2, node3 ] );
  135. expect( node1.getNextSibling() ).to.equal( node2 );
  136. expect( node2.getNextSibling() ).to.equal( node3 );
  137. expect( node3.getNextSibling() ).to.be.null;
  138. } );
  139. it( 'getPreviousSibling() should return proper node', () => {
  140. const node1 = new Node();
  141. const node2 = new Node();
  142. const node3 = new Node();
  143. new DocumentFragment( [ node1, node2, node3 ] );
  144. expect( node1.getPreviousSibling() ).to.be.null;
  145. expect( node2.getPreviousSibling() ).to.equal( node1 );
  146. expect( node3.getPreviousSibling() ).to.equal( node2 );
  147. } );
  148. it( 'remove() should remove node from fragment', () => {
  149. const node1 = new Node();
  150. const node2 = new Node();
  151. const node3 = new Node();
  152. const fragment = new DocumentFragment( [ node1, node2, node3 ] );
  153. node1.remove();
  154. node3.remove();
  155. expect( fragment.getChildCount() ).to.equal( 1 );
  156. expect( node1.parent ).to.be.null;
  157. expect( node3.parent ).to.be.null;
  158. expect( fragment.getChild( 0 ) ).to.equal( node2 );
  159. } );
  160. } );
  161. } );