documentfragment.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: model */
  6. import Element from '/ckeditor5/engine/model/element.js';
  7. import Text from '/ckeditor5/engine/model/text.js';
  8. import DocumentFragment from '/ckeditor5/engine/model/documentfragment.js';
  9. import { jsonParseStringify } from '/tests/engine/model/_utils/utils.js';
  10. describe( 'DocumentFragment', () => {
  11. describe( 'constructor', () => {
  12. it( 'should create empty document fragment', () => {
  13. let frag = new DocumentFragment();
  14. expect( frag.getChildCount() ).to.equal( 0 );
  15. expect( frag.getMaxOffset() ).to.equal( 0 );
  16. } );
  17. it( 'should create document fragment with children', () => {
  18. let frag = new DocumentFragment( [ new Text( 'xx' ), new Element( 'p' ), new Text( 'yy' ) ] );
  19. expect( frag.getChildCount() ).to.equal( 3 );
  20. expect( frag.getMaxOffset() ).to.equal( 5 );
  21. expect( frag.getChild( 0 ) ).to.have.property( 'data' ).that.equals( 'xx' );
  22. expect( frag.getChild( 1 ) ).to.have.property( 'name' ).that.equals( 'p' );
  23. expect( frag.getChild( 2 ) ).to.have.property( 'data' ).that.equals( 'yy' );
  24. } );
  25. it( 'should have root property, equal to itself', () => {
  26. let frag = new DocumentFragment();
  27. expect( frag ).to.have.property( 'root' ).that.equals( frag );
  28. } );
  29. } );
  30. describe( 'iterator', () => {
  31. it( 'should iterate over document fragment\'s children', () => {
  32. let xx = new Text( 'xx' );
  33. let p = new Element( 'p' );
  34. let yy = new Text( 'yy' );
  35. let frag = new DocumentFragment( [ xx, p, yy ] );
  36. expect( Array.from( frag ) ).to.deep.equal( [ xx, p, yy ] );
  37. } );
  38. } );
  39. describe( 'getPath', () => {
  40. it( 'should return empty array', () => {
  41. let frag = new DocumentFragment( [ new Text( 'x' ), new Element( 'p' ), new Text( 'y' ) ] );
  42. expect( frag.getPath() ).to.deep.equal( [] );
  43. } );
  44. } );
  45. describe( 'isEmpty', () => {
  46. it( 'should return true if document fragment has no children', () => {
  47. let frag = new DocumentFragment();
  48. expect( frag.isEmpty() ).to.be.true;
  49. } );
  50. it( 'should return false if document fragment has children', () => {
  51. let frag = new DocumentFragment( new Text( 'a' ) );
  52. expect( frag.isEmpty() ).to.be.false;
  53. } );
  54. } );
  55. describe( 'offsetToIndex', () => {
  56. let frag;
  57. beforeEach( () => {
  58. frag = new Element( 'elem', [], [ new Element( 'p' ), new Text( 'bar' ), new Element( 'h' ) ] );
  59. } );
  60. it( 'should return index of a node that occupies given offset in this element', () => {
  61. expect( frag.offsetToIndex( 0 ) ).to.equal( 0 );
  62. expect( frag.offsetToIndex( 1 ) ).to.equal( 1 );
  63. expect( frag.offsetToIndex( 2 ) ).to.equal( 1 );
  64. expect( frag.offsetToIndex( 3 ) ).to.equal( 1 );
  65. expect( frag.offsetToIndex( 4 ) ).to.equal( 2 );
  66. } );
  67. it( 'should return 0 if offset is too low', () => {
  68. expect( frag.offsetToIndex( -1 ) ).to.equal( 0 );
  69. } );
  70. it( 'should return document fragment\'s child count if offset is too high', () => {
  71. expect( frag.offsetToIndex( 5 ) ).to.equal( 3 );
  72. expect( frag.offsetToIndex( 33 ) ).to.equal( 3 );
  73. } );
  74. } );
  75. describe( 'insertChildren', () => {
  76. it( 'should add children to the document fragment', () => {
  77. let frag = new DocumentFragment( new Text( 'xy' ) );
  78. frag.insertChildren( 1, new Text( 'foo' ) );
  79. expect( frag.getChildCount() ).to.equal( 2 );
  80. expect( frag.getMaxOffset() ).to.equal( 5 );
  81. expect( frag.getChild( 0 ) ).to.have.property( 'data' ).that.equals( 'xy' );
  82. expect( frag.getChild( 1 ) ).to.have.property( 'data' ).that.equals( 'foo' );
  83. } );
  84. } );
  85. describe( 'appendChildren', () => {
  86. it( 'should add children to the end of the element', () => {
  87. let frag = new DocumentFragment( new Text( 'xy' ) );
  88. frag.appendChildren( new Text( 'foo' ) );
  89. expect( frag.getChildCount() ).to.equal( 2 );
  90. expect( frag.getMaxOffset() ).to.equal( 5 );
  91. expect( frag.getChild( 0 ) ).to.have.property( 'data' ).that.equals( 'xy' );
  92. expect( frag.getChild( 1 ) ).to.have.property( 'data' ).that.equals( 'foo' );
  93. } );
  94. } );
  95. describe( 'removeChildren', () => {
  96. it( 'should remove children from the element and return them as an array', () => {
  97. let frag = new DocumentFragment( [ new Text( 'foobar' ), new Element( 'image' ) ] );
  98. let removed = frag.removeChildren( 1, 1 );
  99. expect( frag.getChildCount() ).to.equal( 1 );
  100. expect( frag.getMaxOffset() ).to.equal( 6 );
  101. expect( frag.getChild( 0 ) ).to.have.property( 'data' ).that.equals( 'foobar' );
  102. expect( removed.length ).to.equal( 1 );
  103. expect( removed[ 0 ].name ).to.equal( 'image' );
  104. } );
  105. it( 'should remove one child when second parameter is not specified', () => {
  106. let frag = new DocumentFragment( [ new Text( 'foo' ), new Element( 'image' ) ] );
  107. let removed = frag.removeChildren( 0 );
  108. expect( frag.getChildCount() ).to.equal( 1 );
  109. expect( frag.getMaxOffset() ).to.equal( 1 );
  110. expect( frag.getChild( 0 ).name ).to.equal( 'image' );
  111. expect( removed.length ).to.equal( 1 );
  112. expect( removed[ 0 ].data ).to.equal( 'foo' );
  113. } );
  114. } );
  115. describe( 'getChildIndex', () => {
  116. it( 'should return child index', () => {
  117. let frag = new DocumentFragment( [ new Element( 'p' ), new Text( 'bar' ), new Element( 'h' ) ] );
  118. let p = frag.getChild( 0 );
  119. let textBAR = frag.getChild( 1 );
  120. let h = frag.getChild( 2 );
  121. expect( frag.getChildIndex( p ) ).to.equal( 0 );
  122. expect( frag.getChildIndex( textBAR ) ).to.equal( 1 );
  123. expect( frag.getChildIndex( h ) ).to.equal( 2 );
  124. } );
  125. } );
  126. describe( 'getChildStartOffset', () => {
  127. it( 'should return child start offset', () => {
  128. let frag = new DocumentFragment( [ new Element( 'p' ), new Text( 'bar' ), new Element( 'h' ) ] );
  129. let p = frag.getChild( 0 );
  130. let textBAR = frag.getChild( 1 );
  131. let h = frag.getChild( 2 );
  132. expect( frag.getChildStartOffset( p ) ).to.equal( 0 );
  133. expect( frag.getChildStartOffset( textBAR ) ).to.equal( 1 );
  134. expect( frag.getChildStartOffset( h ) ).to.equal( 4 );
  135. } );
  136. it( 'should return null if node is not a child of that document fragment', () => {
  137. let frag = new DocumentFragment( [ new Element( 'p' ), new Text( 'bar' ), new Element( 'h' ) ] );
  138. let p = new Element( 'p' );
  139. expect( frag.getChildStartOffset( p ) ).to.equal( null );
  140. } );
  141. } );
  142. describe( 'getChildCount', () => {
  143. it( 'should return number of children nodes', () => {
  144. let frag = new DocumentFragment( new Text( 'bar' ) );
  145. expect( frag.getChildCount() ).to.equal( 1 );
  146. } );
  147. } );
  148. describe( 'getMaxOffset', () => {
  149. it( 'should return offset after the last children', () => {
  150. let frag = new DocumentFragment( new Text( 'bar' ) );
  151. expect( frag.getMaxOffset() ).to.equal( 3 );
  152. } );
  153. } );
  154. describe( 'toJSON', () => {
  155. it( 'should serialize empty document fragment', () => {
  156. let frag = new DocumentFragment();
  157. expect( jsonParseStringify( frag ) ).to.deep.equal( [] );
  158. } );
  159. it( 'should serialize document fragment with children', () => {
  160. let img = new Element( 'img' );
  161. let one = new Element( 'one' );
  162. let two = new Element( 'two', null, [ new Text( 'ba' ), img, new Text( 'r' ) ] );
  163. let three = new Element( 'three' );
  164. let frag = new DocumentFragment( [ one, two, three ] );
  165. expect( jsonParseStringify( frag ) ).to.deep.equal( [
  166. { name: 'one' },
  167. {
  168. name: 'two',
  169. children: [
  170. { data: 'ba' },
  171. { name: 'img' },
  172. { data: 'r' }
  173. ]
  174. },
  175. { name: 'three' }
  176. ] );
  177. } );
  178. } );
  179. describe( 'fromJSON', () => {
  180. it( 'should create document fragment without children', () => {
  181. const frag = new DocumentFragment();
  182. let serialized = jsonParseStringify( frag );
  183. let deserialized = DocumentFragment.fromJSON( serialized );
  184. expect( deserialized.isEmpty() ).to.be.true;
  185. } );
  186. it( 'should create element with children', () => {
  187. const p = new Element( 'p' );
  188. const foo = new Text( 'foo' );
  189. const frag = new DocumentFragment( [ p, foo ] );
  190. let serialized = jsonParseStringify( frag );
  191. let deserialized = DocumentFragment.fromJSON( serialized );
  192. expect( deserialized.getChildCount() ).to.equal( 2 );
  193. expect( deserialized.getChild( 0 ).name ).to.equal( 'p' );
  194. expect( deserialized.getChild( 0 ).parent ).to.equal( deserialized );
  195. expect( deserialized.getChild( 1 ).data ).to.equal( 'foo' );
  196. expect( deserialized.getChild( 1 ).parent ).to.equal( deserialized );
  197. } );
  198. } );
  199. } );