8
0

documentfragment.js 9.0 KB

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