documentfragment.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Element from '../../src/model/element';
  6. import Text from '../../src/model/text';
  7. import DocumentFragment from '../../src/model/documentfragment';
  8. import { jsonParseStringify } from '../../tests/model/_utils/utils';
  9. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  10. describe( 'DocumentFragment', () => {
  11. describe( 'constructor()', () => {
  12. it( 'should create empty document fragment', () => {
  13. let frag = new DocumentFragment();
  14. expect( frag.childCount ).to.equal( 0 );
  15. expect( frag.maxOffset ).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.childCount ).to.equal( 3 );
  20. expect( frag.maxOffset ).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 throw if given offset is too high or too low', () => {
  68. expect( () => {
  69. frag.offsetToIndex( -1 );
  70. } ).to.throw( CKEditorError, /nodelist-offset-out-of-bounds/ );
  71. expect( () => {
  72. frag.offsetToIndex( 55 );
  73. } ).to.throw( CKEditorError, /nodelist-offset-out-of-bounds/ );
  74. } );
  75. it( 'should return length if given offset is equal to maxOffset', () => {
  76. expect( frag.offsetToIndex( 5 ) ).to.equal( 3 );
  77. } );
  78. } );
  79. describe( 'insertChildren', () => {
  80. it( 'should add children to the document fragment', () => {
  81. let frag = new DocumentFragment( new Text( 'xy' ) );
  82. frag.insertChildren( 1, new Text( 'foo' ) );
  83. expect( frag.childCount ).to.equal( 2 );
  84. expect( frag.maxOffset ).to.equal( 5 );
  85. expect( frag.getChild( 0 ) ).to.have.property( 'data' ).that.equals( 'xy' );
  86. expect( frag.getChild( 1 ) ).to.have.property( 'data' ).that.equals( 'foo' );
  87. } );
  88. it( 'should accept strings and arrays', () => {
  89. let frag = new DocumentFragment();
  90. frag.insertChildren( 0, 'abc' );
  91. expect( frag.childCount ).to.equal( 1 );
  92. expect( frag.maxOffset ).to.equal( 3 );
  93. expect( frag.getChild( 0 ) ).to.have.property( 'data' ).that.equals( 'abc' );
  94. frag.removeChildren( 0, 1 );
  95. frag.insertChildren( 0, [ new Element( 'p' ), 'abc' ] );
  96. expect( frag.childCount ).to.equal( 2 );
  97. expect( frag.maxOffset ).to.equal( 4 );
  98. expect( frag.getChild( 0 ) ).to.have.property( 'name' ).that.equals( 'p' );
  99. expect( frag.getChild( 1 ) ).to.have.property( 'data' ).that.equals( 'abc' );
  100. } );
  101. } );
  102. describe( 'appendChildren', () => {
  103. it( 'should add children to the end of the element', () => {
  104. let frag = new DocumentFragment( new Text( 'xy' ) );
  105. frag.appendChildren( new Text( 'foo' ) );
  106. expect( frag.childCount ).to.equal( 2 );
  107. expect( frag.maxOffset ).to.equal( 5 );
  108. expect( frag.getChild( 0 ) ).to.have.property( 'data' ).that.equals( 'xy' );
  109. expect( frag.getChild( 1 ) ).to.have.property( 'data' ).that.equals( 'foo' );
  110. } );
  111. } );
  112. describe( 'removeChildren', () => {
  113. it( 'should remove children from the element and return them as an array', () => {
  114. let frag = new DocumentFragment( [ new Text( 'foobar' ), new Element( 'image' ) ] );
  115. let removed = frag.removeChildren( 1, 1 );
  116. expect( frag.childCount ).to.equal( 1 );
  117. expect( frag.maxOffset ).to.equal( 6 );
  118. expect( frag.getChild( 0 ) ).to.have.property( 'data' ).that.equals( 'foobar' );
  119. expect( removed.length ).to.equal( 1 );
  120. expect( removed[ 0 ].name ).to.equal( 'image' );
  121. } );
  122. it( 'should remove one child when second parameter is not specified', () => {
  123. let frag = new DocumentFragment( [ new Text( 'foo' ), new Element( 'image' ) ] );
  124. let removed = frag.removeChildren( 0 );
  125. expect( frag.childCount ).to.equal( 1 );
  126. expect( frag.maxOffset ).to.equal( 1 );
  127. expect( frag.getChild( 0 ).name ).to.equal( 'image' );
  128. expect( removed.length ).to.equal( 1 );
  129. expect( removed[ 0 ].data ).to.equal( 'foo' );
  130. } );
  131. } );
  132. describe( 'getChildIndex', () => {
  133. it( 'should return child index', () => {
  134. let frag = new DocumentFragment( [ new Element( 'p' ), new Text( 'bar' ), new Element( 'h' ) ] );
  135. let p = frag.getChild( 0 );
  136. let textBAR = frag.getChild( 1 );
  137. let h = frag.getChild( 2 );
  138. expect( frag.getChildIndex( p ) ).to.equal( 0 );
  139. expect( frag.getChildIndex( textBAR ) ).to.equal( 1 );
  140. expect( frag.getChildIndex( h ) ).to.equal( 2 );
  141. } );
  142. } );
  143. describe( 'getChildStartOffset', () => {
  144. it( 'should return child start offset', () => {
  145. let frag = new DocumentFragment( [ new Element( 'p' ), new Text( 'bar' ), new Element( 'h' ) ] );
  146. let p = frag.getChild( 0 );
  147. let textBAR = frag.getChild( 1 );
  148. let h = frag.getChild( 2 );
  149. expect( frag.getChildStartOffset( p ) ).to.equal( 0 );
  150. expect( frag.getChildStartOffset( textBAR ) ).to.equal( 1 );
  151. expect( frag.getChildStartOffset( h ) ).to.equal( 4 );
  152. } );
  153. it( 'should return null if node is not a child of that document fragment', () => {
  154. let frag = new DocumentFragment( [ new Element( 'p' ), new Text( 'bar' ), new Element( 'h' ) ] );
  155. let p = new Element( 'p' );
  156. expect( frag.getChildStartOffset( p ) ).to.equal( null );
  157. } );
  158. } );
  159. describe( 'getChildCount', () => {
  160. it( 'should return number of children nodes', () => {
  161. let frag = new DocumentFragment( new Text( 'bar' ) );
  162. expect( frag.childCount ).to.equal( 1 );
  163. } );
  164. } );
  165. describe( 'getMaxOffset', () => {
  166. it( 'should return offset after the last children', () => {
  167. let frag = new DocumentFragment( new Text( 'bar' ) );
  168. expect( frag.maxOffset ).to.equal( 3 );
  169. } );
  170. } );
  171. describe( 'toJSON', () => {
  172. it( 'should serialize empty document fragment', () => {
  173. let frag = new DocumentFragment();
  174. expect( jsonParseStringify( frag ) ).to.deep.equal( [] );
  175. } );
  176. it( 'should serialize document fragment with children', () => {
  177. let img = new Element( 'img' );
  178. let one = new Element( 'one' );
  179. let two = new Element( 'two', null, [ new Text( 'ba' ), img, new Text( 'r' ) ] );
  180. let three = new Element( 'three' );
  181. let frag = new DocumentFragment( [ one, two, three ] );
  182. expect( jsonParseStringify( frag ) ).to.deep.equal( [
  183. { name: 'one' },
  184. {
  185. name: 'two',
  186. children: [
  187. { data: 'ba' },
  188. { name: 'img' },
  189. { data: 'r' }
  190. ]
  191. },
  192. { name: 'three' }
  193. ] );
  194. } );
  195. } );
  196. describe( 'fromJSON', () => {
  197. it( 'should create document fragment without children', () => {
  198. const frag = new DocumentFragment();
  199. let serialized = jsonParseStringify( frag );
  200. let deserialized = DocumentFragment.fromJSON( serialized );
  201. expect( deserialized.isEmpty ).to.be.true;
  202. } );
  203. it( 'should create element with children', () => {
  204. const p = new Element( 'p' );
  205. const foo = new Text( 'foo' );
  206. const frag = new DocumentFragment( [ p, foo ] );
  207. let serialized = jsonParseStringify( frag );
  208. let deserialized = DocumentFragment.fromJSON( serialized );
  209. expect( deserialized.childCount ).to.equal( 2 );
  210. expect( deserialized.getChild( 0 ).name ).to.equal( 'p' );
  211. expect( deserialized.getChild( 0 ).parent ).to.equal( deserialized );
  212. expect( deserialized.getChild( 1 ).data ).to.equal( 'foo' );
  213. expect( deserialized.getChild( 1 ).parent ).to.equal( deserialized );
  214. } );
  215. } );
  216. } );