8
0

documentfragment.js 9.4 KB

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