8
0

documentfragment.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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.getChildCount() ).to.equal( 0 );
  16. expect( frag.getMaxOffset() ).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.getChildCount() ).to.equal( 3 );
  21. expect( frag.getMaxOffset() ).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 getMaxOffset()', () => {
  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.getChildCount() ).to.equal( 2 );
  85. expect( frag.getMaxOffset() ).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, [ new Element( 'p' ), 'abc' ] );
  92. expect( frag.getChildCount() ).to.equal( 2 );
  93. expect( frag.getMaxOffset() ).to.equal( 4 );
  94. expect( frag.getChild( 0 ) ).to.have.property( 'name' ).that.equals( 'p' );
  95. expect( frag.getChild( 1 ) ).to.have.property( 'data' ).that.equals( 'abc' );
  96. } );
  97. } );
  98. describe( 'appendChildren', () => {
  99. it( 'should add children to the end of the element', () => {
  100. let frag = new DocumentFragment( new Text( 'xy' ) );
  101. frag.appendChildren( new Text( 'foo' ) );
  102. expect( frag.getChildCount() ).to.equal( 2 );
  103. expect( frag.getMaxOffset() ).to.equal( 5 );
  104. expect( frag.getChild( 0 ) ).to.have.property( 'data' ).that.equals( 'xy' );
  105. expect( frag.getChild( 1 ) ).to.have.property( 'data' ).that.equals( 'foo' );
  106. } );
  107. } );
  108. describe( 'removeChildren', () => {
  109. it( 'should remove children from the element and return them as an array', () => {
  110. let frag = new DocumentFragment( [ new Text( 'foobar' ), new Element( 'image' ) ] );
  111. let removed = frag.removeChildren( 1, 1 );
  112. expect( frag.getChildCount() ).to.equal( 1 );
  113. expect( frag.getMaxOffset() ).to.equal( 6 );
  114. expect( frag.getChild( 0 ) ).to.have.property( 'data' ).that.equals( 'foobar' );
  115. expect( removed.length ).to.equal( 1 );
  116. expect( removed[ 0 ].name ).to.equal( 'image' );
  117. } );
  118. it( 'should remove one child when second parameter is not specified', () => {
  119. let frag = new DocumentFragment( [ new Text( 'foo' ), new Element( 'image' ) ] );
  120. let removed = frag.removeChildren( 0 );
  121. expect( frag.getChildCount() ).to.equal( 1 );
  122. expect( frag.getMaxOffset() ).to.equal( 1 );
  123. expect( frag.getChild( 0 ).name ).to.equal( 'image' );
  124. expect( removed.length ).to.equal( 1 );
  125. expect( removed[ 0 ].data ).to.equal( 'foo' );
  126. } );
  127. } );
  128. describe( 'getChildIndex', () => {
  129. it( 'should return child index', () => {
  130. let frag = new DocumentFragment( [ new Element( 'p' ), new Text( 'bar' ), new Element( 'h' ) ] );
  131. let p = frag.getChild( 0 );
  132. let textBAR = frag.getChild( 1 );
  133. let h = frag.getChild( 2 );
  134. expect( frag.getChildIndex( p ) ).to.equal( 0 );
  135. expect( frag.getChildIndex( textBAR ) ).to.equal( 1 );
  136. expect( frag.getChildIndex( h ) ).to.equal( 2 );
  137. } );
  138. } );
  139. describe( 'getChildStartOffset', () => {
  140. it( 'should return child start offset', () => {
  141. let frag = new DocumentFragment( [ new Element( 'p' ), new Text( 'bar' ), new Element( 'h' ) ] );
  142. let p = frag.getChild( 0 );
  143. let textBAR = frag.getChild( 1 );
  144. let h = frag.getChild( 2 );
  145. expect( frag.getChildStartOffset( p ) ).to.equal( 0 );
  146. expect( frag.getChildStartOffset( textBAR ) ).to.equal( 1 );
  147. expect( frag.getChildStartOffset( h ) ).to.equal( 4 );
  148. } );
  149. it( 'should return null if node is not a child of that document fragment', () => {
  150. let frag = new DocumentFragment( [ new Element( 'p' ), new Text( 'bar' ), new Element( 'h' ) ] );
  151. let p = new Element( 'p' );
  152. expect( frag.getChildStartOffset( p ) ).to.equal( null );
  153. } );
  154. } );
  155. describe( 'getChildCount', () => {
  156. it( 'should return number of children nodes', () => {
  157. let frag = new DocumentFragment( new Text( 'bar' ) );
  158. expect( frag.getChildCount() ).to.equal( 1 );
  159. } );
  160. } );
  161. describe( 'getMaxOffset', () => {
  162. it( 'should return offset after the last children', () => {
  163. let frag = new DocumentFragment( new Text( 'bar' ) );
  164. expect( frag.getMaxOffset() ).to.equal( 3 );
  165. } );
  166. } );
  167. describe( 'toJSON', () => {
  168. it( 'should serialize empty document fragment', () => {
  169. let frag = new DocumentFragment();
  170. expect( jsonParseStringify( frag ) ).to.deep.equal( [] );
  171. } );
  172. it( 'should serialize document fragment with children', () => {
  173. let img = new Element( 'img' );
  174. let one = new Element( 'one' );
  175. let two = new Element( 'two', null, [ new Text( 'ba' ), img, new Text( 'r' ) ] );
  176. let three = new Element( 'three' );
  177. let frag = new DocumentFragment( [ one, two, three ] );
  178. expect( jsonParseStringify( frag ) ).to.deep.equal( [
  179. { name: 'one' },
  180. {
  181. name: 'two',
  182. children: [
  183. { data: 'ba' },
  184. { name: 'img' },
  185. { data: 'r' }
  186. ]
  187. },
  188. { name: 'three' }
  189. ] );
  190. } );
  191. } );
  192. describe( 'fromJSON', () => {
  193. it( 'should create document fragment without children', () => {
  194. const frag = new DocumentFragment();
  195. let serialized = jsonParseStringify( frag );
  196. let deserialized = DocumentFragment.fromJSON( serialized );
  197. expect( deserialized.isEmpty() ).to.be.true;
  198. } );
  199. it( 'should create element with children', () => {
  200. const p = new Element( 'p' );
  201. const foo = new Text( 'foo' );
  202. const frag = new DocumentFragment( [ p, foo ] );
  203. let serialized = jsonParseStringify( frag );
  204. let deserialized = DocumentFragment.fromJSON( serialized );
  205. expect( deserialized.getChildCount() ).to.equal( 2 );
  206. expect( deserialized.getChild( 0 ).name ).to.equal( 'p' );
  207. expect( deserialized.getChild( 0 ).parent ).to.equal( deserialized );
  208. expect( deserialized.getChild( 1 ).data ).to.equal( 'foo' );
  209. expect( deserialized.getChild( 1 ).parent ).to.equal( deserialized );
  210. } );
  211. } );
  212. } );