documentfragment.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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( 'is', () => {
  46. let frag;
  47. before( () => {
  48. frag = new DocumentFragment();
  49. } );
  50. it( 'should return true for documentFragment', () => {
  51. expect( frag.is( 'documentFragment' ) ).to.be.true;
  52. } );
  53. it( 'should return false for other accept values', () => {
  54. expect( frag.is( 'node' ) ).to.be.false;
  55. expect( frag.is( 'text' ) ).to.be.false;
  56. expect( frag.is( 'textProxy' ) ).to.be.false;
  57. expect( frag.is( 'element' ) ).to.be.false;
  58. expect( frag.is( 'rootElement' ) ).to.be.false;
  59. } );
  60. } );
  61. describe( 'isEmpty', () => {
  62. it( 'should return true if document fragment has no children', () => {
  63. let frag = new DocumentFragment();
  64. expect( frag.isEmpty ).to.be.true;
  65. } );
  66. it( 'should return false if document fragment has children', () => {
  67. let frag = new DocumentFragment( new Text( 'a' ) );
  68. expect( frag.isEmpty ).to.be.false;
  69. } );
  70. } );
  71. describe( 'offsetToIndex', () => {
  72. let frag;
  73. beforeEach( () => {
  74. frag = new Element( 'elem', [], [ new Element( 'p' ), new Text( 'bar' ), new Element( 'h' ) ] );
  75. } );
  76. it( 'should return index of a node that occupies given offset in this element', () => {
  77. expect( frag.offsetToIndex( 0 ) ).to.equal( 0 );
  78. expect( frag.offsetToIndex( 1 ) ).to.equal( 1 );
  79. expect( frag.offsetToIndex( 2 ) ).to.equal( 1 );
  80. expect( frag.offsetToIndex( 3 ) ).to.equal( 1 );
  81. expect( frag.offsetToIndex( 4 ) ).to.equal( 2 );
  82. } );
  83. it( 'should throw if given offset is too high or too low', () => {
  84. expect( () => {
  85. frag.offsetToIndex( -1 );
  86. } ).to.throw( CKEditorError, /nodelist-offset-out-of-bounds/ );
  87. expect( () => {
  88. frag.offsetToIndex( 55 );
  89. } ).to.throw( CKEditorError, /nodelist-offset-out-of-bounds/ );
  90. } );
  91. it( 'should return length if given offset is equal to maxOffset', () => {
  92. expect( frag.offsetToIndex( 5 ) ).to.equal( 3 );
  93. } );
  94. } );
  95. describe( 'insertChildren', () => {
  96. it( 'should add children to the document fragment', () => {
  97. let frag = new DocumentFragment( new Text( 'xy' ) );
  98. frag.insertChildren( 1, new Text( 'foo' ) );
  99. expect( frag.childCount ).to.equal( 2 );
  100. expect( frag.maxOffset ).to.equal( 5 );
  101. expect( frag.getChild( 0 ) ).to.have.property( 'data' ).that.equals( 'xy' );
  102. expect( frag.getChild( 1 ) ).to.have.property( 'data' ).that.equals( 'foo' );
  103. } );
  104. it( 'should accept strings and arrays', () => {
  105. let frag = new DocumentFragment();
  106. frag.insertChildren( 0, 'abc' );
  107. expect( frag.childCount ).to.equal( 1 );
  108. expect( frag.maxOffset ).to.equal( 3 );
  109. expect( frag.getChild( 0 ) ).to.have.property( 'data' ).that.equals( 'abc' );
  110. frag.removeChildren( 0, 1 );
  111. frag.insertChildren( 0, [ new Element( 'p' ), 'abc' ] );
  112. expect( frag.childCount ).to.equal( 2 );
  113. expect( frag.maxOffset ).to.equal( 4 );
  114. expect( frag.getChild( 0 ) ).to.have.property( 'name' ).that.equals( 'p' );
  115. expect( frag.getChild( 1 ) ).to.have.property( 'data' ).that.equals( 'abc' );
  116. } );
  117. } );
  118. describe( 'appendChildren', () => {
  119. it( 'should add children to the end of the element', () => {
  120. let frag = new DocumentFragment( new Text( 'xy' ) );
  121. frag.appendChildren( new Text( 'foo' ) );
  122. expect( frag.childCount ).to.equal( 2 );
  123. expect( frag.maxOffset ).to.equal( 5 );
  124. expect( frag.getChild( 0 ) ).to.have.property( 'data' ).that.equals( 'xy' );
  125. expect( frag.getChild( 1 ) ).to.have.property( 'data' ).that.equals( 'foo' );
  126. } );
  127. } );
  128. describe( 'removeChildren', () => {
  129. it( 'should remove children from the element and return them as an array', () => {
  130. let frag = new DocumentFragment( [ new Text( 'foobar' ), new Element( 'image' ) ] );
  131. let removed = frag.removeChildren( 1, 1 );
  132. expect( frag.childCount ).to.equal( 1 );
  133. expect( frag.maxOffset ).to.equal( 6 );
  134. expect( frag.getChild( 0 ) ).to.have.property( 'data' ).that.equals( 'foobar' );
  135. expect( removed.length ).to.equal( 1 );
  136. expect( removed[ 0 ].name ).to.equal( 'image' );
  137. } );
  138. it( 'should remove one child when second parameter is not specified', () => {
  139. let frag = new DocumentFragment( [ new Text( 'foo' ), new Element( 'image' ) ] );
  140. let removed = frag.removeChildren( 0 );
  141. expect( frag.childCount ).to.equal( 1 );
  142. expect( frag.maxOffset ).to.equal( 1 );
  143. expect( frag.getChild( 0 ).name ).to.equal( 'image' );
  144. expect( removed.length ).to.equal( 1 );
  145. expect( removed[ 0 ].data ).to.equal( 'foo' );
  146. } );
  147. } );
  148. describe( 'getChildIndex', () => {
  149. it( 'should return child index', () => {
  150. let frag = new DocumentFragment( [ new Element( 'p' ), new Text( 'bar' ), new Element( 'h' ) ] );
  151. let p = frag.getChild( 0 );
  152. let textBAR = frag.getChild( 1 );
  153. let h = frag.getChild( 2 );
  154. expect( frag.getChildIndex( p ) ).to.equal( 0 );
  155. expect( frag.getChildIndex( textBAR ) ).to.equal( 1 );
  156. expect( frag.getChildIndex( h ) ).to.equal( 2 );
  157. } );
  158. } );
  159. describe( 'getChildStartOffset', () => {
  160. it( 'should return child start offset', () => {
  161. let frag = new DocumentFragment( [ new Element( 'p' ), new Text( 'bar' ), new Element( 'h' ) ] );
  162. let p = frag.getChild( 0 );
  163. let textBAR = frag.getChild( 1 );
  164. let h = frag.getChild( 2 );
  165. expect( frag.getChildStartOffset( p ) ).to.equal( 0 );
  166. expect( frag.getChildStartOffset( textBAR ) ).to.equal( 1 );
  167. expect( frag.getChildStartOffset( h ) ).to.equal( 4 );
  168. } );
  169. it( 'should return null if node is not a child of that document fragment', () => {
  170. let frag = new DocumentFragment( [ new Element( 'p' ), new Text( 'bar' ), new Element( 'h' ) ] );
  171. let p = new Element( 'p' );
  172. expect( frag.getChildStartOffset( p ) ).to.equal( null );
  173. } );
  174. } );
  175. describe( 'getChildCount', () => {
  176. it( 'should return number of children nodes', () => {
  177. let frag = new DocumentFragment( new Text( 'bar' ) );
  178. expect( frag.childCount ).to.equal( 1 );
  179. } );
  180. } );
  181. describe( 'getMaxOffset', () => {
  182. it( 'should return offset after the last children', () => {
  183. let frag = new DocumentFragment( new Text( 'bar' ) );
  184. expect( frag.maxOffset ).to.equal( 3 );
  185. } );
  186. } );
  187. describe( 'toJSON', () => {
  188. it( 'should serialize empty document fragment', () => {
  189. let frag = new DocumentFragment();
  190. expect( jsonParseStringify( frag ) ).to.deep.equal( [] );
  191. } );
  192. it( 'should serialize document fragment with children', () => {
  193. let img = new Element( 'img' );
  194. let one = new Element( 'one' );
  195. let two = new Element( 'two', null, [ new Text( 'ba' ), img, new Text( 'r' ) ] );
  196. let three = new Element( 'three' );
  197. let frag = new DocumentFragment( [ one, two, three ] );
  198. expect( jsonParseStringify( frag ) ).to.deep.equal( [
  199. { name: 'one' },
  200. {
  201. name: 'two',
  202. children: [
  203. { data: 'ba' },
  204. { name: 'img' },
  205. { data: 'r' }
  206. ]
  207. },
  208. { name: 'three' }
  209. ] );
  210. } );
  211. } );
  212. describe( 'fromJSON', () => {
  213. it( 'should create document fragment without children', () => {
  214. const frag = new DocumentFragment();
  215. let serialized = jsonParseStringify( frag );
  216. let deserialized = DocumentFragment.fromJSON( serialized );
  217. expect( deserialized.isEmpty ).to.be.true;
  218. } );
  219. it( 'should create element with children', () => {
  220. const p = new Element( 'p' );
  221. const foo = new Text( 'foo' );
  222. const frag = new DocumentFragment( [ p, foo ] );
  223. let serialized = jsonParseStringify( frag );
  224. let deserialized = DocumentFragment.fromJSON( serialized );
  225. expect( deserialized.childCount ).to.equal( 2 );
  226. expect( deserialized.getChild( 0 ).name ).to.equal( 'p' );
  227. expect( deserialized.getChild( 0 ).parent ).to.equal( deserialized );
  228. expect( deserialized.getChild( 1 ).data ).to.equal( 'foo' );
  229. expect( deserialized.getChild( 1 ).parent ).to.equal( deserialized );
  230. } );
  231. } );
  232. } );