8
0

documentfragment.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import Element from '../../src/model/element';
  6. import Text from '../../src/model/text';
  7. import TextProxy from '../../src/model/textproxy';
  8. import DocumentFragment from '../../src/model/documentfragment';
  9. import { expectToThrowCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  10. describe( 'DocumentFragment', () => {
  11. describe( 'constructor()', () => {
  12. it( 'should create empty document fragment', () => {
  13. const 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. const 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 markers list', () => {
  26. const frag = new DocumentFragment();
  27. expect( frag ).to.have.property( 'markers' ).to.instanceof( Map );
  28. } );
  29. it( 'should have root property, equal to itself', () => {
  30. const frag = new DocumentFragment();
  31. expect( frag ).to.have.property( 'root' ).that.equals( frag );
  32. } );
  33. } );
  34. describe( 'iterator', () => {
  35. it( 'should iterate over document fragment\'s children', () => {
  36. const xx = new Text( 'xx' );
  37. const p = new Element( 'p' );
  38. const yy = new Text( 'yy' );
  39. const frag = new DocumentFragment( [ xx, p, yy ] );
  40. expect( Array.from( frag ) ).to.deep.equal( [ xx, p, yy ] );
  41. } );
  42. } );
  43. describe( 'getPath', () => {
  44. it( 'should return empty array', () => {
  45. const frag = new DocumentFragment( [ new Text( 'x' ), new Element( 'p' ), new Text( 'y' ) ] );
  46. expect( frag.getPath() ).to.deep.equal( [] );
  47. } );
  48. } );
  49. describe( 'is', () => {
  50. let frag;
  51. before( () => {
  52. frag = new DocumentFragment();
  53. } );
  54. it( 'should return true for documentFragment', () => {
  55. expect( frag.is( 'documentFragment' ) ).to.be.true;
  56. } );
  57. it( 'should return false for other accept values', () => {
  58. expect( frag.is( 'node' ) ).to.be.false;
  59. expect( frag.is( 'text' ) ).to.be.false;
  60. expect( frag.is( 'textProxy' ) ).to.be.false;
  61. expect( frag.is( 'element' ) ).to.be.false;
  62. expect( frag.is( 'rootElement' ) ).to.be.false;
  63. } );
  64. } );
  65. describe( 'isEmpty', () => {
  66. it( 'should return true if document fragment has no children', () => {
  67. const frag = new DocumentFragment();
  68. expect( frag.isEmpty ).to.be.true;
  69. } );
  70. it( 'should return false if document fragment has children', () => {
  71. const frag = new DocumentFragment( new Text( 'a' ) );
  72. expect( frag.isEmpty ).to.be.false;
  73. } );
  74. } );
  75. describe( 'offsetToIndex', () => {
  76. let frag;
  77. beforeEach( () => {
  78. frag = new Element( 'elem', [], [ new Element( 'p' ), new Text( 'bar' ), new Element( 'h' ) ] );
  79. } );
  80. it( 'should return index of a node that occupies given offset in this element', () => {
  81. expect( frag.offsetToIndex( 0 ) ).to.equal( 0 );
  82. expect( frag.offsetToIndex( 1 ) ).to.equal( 1 );
  83. expect( frag.offsetToIndex( 2 ) ).to.equal( 1 );
  84. expect( frag.offsetToIndex( 3 ) ).to.equal( 1 );
  85. expect( frag.offsetToIndex( 4 ) ).to.equal( 2 );
  86. } );
  87. it( 'should throw if given offset is too high or too low', () => {
  88. expectToThrowCKEditorError( () => {
  89. frag.offsetToIndex( -1 );
  90. }, /nodelist-offset-out-of-bounds/, frag );
  91. expectToThrowCKEditorError( () => {
  92. frag.offsetToIndex( 55 );
  93. }, /nodelist-offset-out-of-bounds/, frag );
  94. } );
  95. it( 'should return length if given offset is equal to maxOffset', () => {
  96. expect( frag.offsetToIndex( 5 ) ).to.equal( 3 );
  97. } );
  98. } );
  99. describe( '_insertChild', () => {
  100. it( 'should add children to the document fragment', () => {
  101. const frag = new DocumentFragment( new Text( 'xy' ) );
  102. frag._insertChild( 1, new Text( 'foo' ) );
  103. expect( frag.childCount ).to.equal( 2 );
  104. expect( frag.maxOffset ).to.equal( 5 );
  105. expect( frag.getChild( 0 ) ).to.have.property( 'data' ).that.equals( 'xy' );
  106. expect( frag.getChild( 1 ) ).to.have.property( 'data' ).that.equals( 'foo' );
  107. } );
  108. it( 'should accept strings and arrays', () => {
  109. const frag = new DocumentFragment();
  110. frag._insertChild( 0, 'abc' );
  111. expect( frag.childCount ).to.equal( 1 );
  112. expect( frag.maxOffset ).to.equal( 3 );
  113. expect( frag.getChild( 0 ) ).to.have.property( 'data' ).that.equals( 'abc' );
  114. frag._removeChildren( 0, 1 );
  115. frag._insertChild( 0, [ new Element( 'p' ), 'abc' ] );
  116. expect( frag.childCount ).to.equal( 2 );
  117. expect( frag.maxOffset ).to.equal( 4 );
  118. expect( frag.getChild( 0 ) ).to.have.property( 'name' ).that.equals( 'p' );
  119. expect( frag.getChild( 1 ) ).to.have.property( 'data' ).that.equals( 'abc' );
  120. } );
  121. it( 'should accept and correctly handle text proxies', () => {
  122. const frag = new DocumentFragment();
  123. const text = new Text( 'abcxyz', { bold: true } );
  124. const textProxy = new TextProxy( text, 2, 3 );
  125. frag._insertChild( 0, textProxy );
  126. expect( frag.childCount ).to.equal( 1 );
  127. expect( frag.maxOffset ).to.equal( 3 );
  128. expect( frag.getChild( 0 ) ).to.be.instanceof( Text );
  129. expect( frag.getChild( 0 ).data ).to.equal( 'cxy' );
  130. expect( frag.getChild( 0 ).getAttribute( 'bold' ) ).to.equal( true );
  131. } );
  132. } );
  133. describe( '_appendChild', () => {
  134. it( 'should add children to the end of the element', () => {
  135. const frag = new DocumentFragment( new Text( 'xy' ) );
  136. frag._appendChild( new Text( 'foo' ) );
  137. expect( frag.childCount ).to.equal( 2 );
  138. expect( frag.maxOffset ).to.equal( 5 );
  139. expect( frag.getChild( 0 ) ).to.have.property( 'data' ).that.equals( 'xy' );
  140. expect( frag.getChild( 1 ) ).to.have.property( 'data' ).that.equals( 'foo' );
  141. } );
  142. } );
  143. describe( '_removeChildren', () => {
  144. it( 'should remove children from the element and return them as an array', () => {
  145. const frag = new DocumentFragment( [ new Text( 'foobar' ), new Element( 'image' ) ] );
  146. const removed = frag._removeChildren( 1, 1 );
  147. expect( frag.childCount ).to.equal( 1 );
  148. expect( frag.maxOffset ).to.equal( 6 );
  149. expect( frag.getChild( 0 ) ).to.have.property( 'data' ).that.equals( 'foobar' );
  150. expect( removed.length ).to.equal( 1 );
  151. expect( removed[ 0 ].name ).to.equal( 'image' );
  152. } );
  153. it( 'should remove one child when second parameter is not specified', () => {
  154. const frag = new DocumentFragment( [ new Text( 'foo' ), new Element( 'image' ) ] );
  155. const removed = frag._removeChildren( 0 );
  156. expect( frag.childCount ).to.equal( 1 );
  157. expect( frag.maxOffset ).to.equal( 1 );
  158. expect( frag.getChild( 0 ).name ).to.equal( 'image' );
  159. expect( removed.length ).to.equal( 1 );
  160. expect( removed[ 0 ].data ).to.equal( 'foo' );
  161. } );
  162. } );
  163. describe( 'getChildIndex', () => {
  164. it( 'should return child index', () => {
  165. const frag = new DocumentFragment( [ new Element( 'p' ), new Text( 'bar' ), new Element( 'h' ) ] );
  166. const p = frag.getChild( 0 );
  167. const textBAR = frag.getChild( 1 );
  168. const h = frag.getChild( 2 );
  169. expect( frag.getChildIndex( p ) ).to.equal( 0 );
  170. expect( frag.getChildIndex( textBAR ) ).to.equal( 1 );
  171. expect( frag.getChildIndex( h ) ).to.equal( 2 );
  172. } );
  173. } );
  174. describe( 'getChildStartOffset', () => {
  175. it( 'should return child start offset', () => {
  176. const frag = new DocumentFragment( [ new Element( 'p' ), new Text( 'bar' ), new Element( 'h' ) ] );
  177. const p = frag.getChild( 0 );
  178. const textBAR = frag.getChild( 1 );
  179. const h = frag.getChild( 2 );
  180. expect( frag.getChildStartOffset( p ) ).to.equal( 0 );
  181. expect( frag.getChildStartOffset( textBAR ) ).to.equal( 1 );
  182. expect( frag.getChildStartOffset( h ) ).to.equal( 4 );
  183. } );
  184. it( 'should return null if node is not a child of that document fragment', () => {
  185. const frag = new DocumentFragment( [ new Element( 'p' ), new Text( 'bar' ), new Element( 'h' ) ] );
  186. const p = new Element( 'p' );
  187. expect( frag.getChildStartOffset( p ) ).to.equal( null );
  188. } );
  189. } );
  190. describe( 'getChildCount', () => {
  191. it( 'should return number of children nodes', () => {
  192. const frag = new DocumentFragment( new Text( 'bar' ) );
  193. expect( frag.childCount ).to.equal( 1 );
  194. } );
  195. } );
  196. describe( 'getMaxOffset', () => {
  197. it( 'should return offset after the last children', () => {
  198. const frag = new DocumentFragment( new Text( 'bar' ) );
  199. expect( frag.maxOffset ).to.equal( 3 );
  200. } );
  201. } );
  202. describe( 'toJSON', () => {
  203. it( 'should serialize empty document fragment', () => {
  204. const frag = new DocumentFragment();
  205. expect( frag.toJSON() ).to.deep.equal( [] );
  206. } );
  207. it( 'should serialize document fragment with children', () => {
  208. const img = new Element( 'img' );
  209. const one = new Element( 'one' );
  210. const two = new Element( 'two', null, [ new Text( 'ba' ), img, new Text( 'r' ) ] );
  211. const three = new Element( 'three' );
  212. const frag = new DocumentFragment( [ one, two, three ] );
  213. expect( frag.toJSON() ).to.deep.equal( [
  214. { name: 'one' },
  215. {
  216. name: 'two',
  217. children: [
  218. { data: 'ba' },
  219. { name: 'img' },
  220. { data: 'r' }
  221. ]
  222. },
  223. { name: 'three' }
  224. ] );
  225. } );
  226. } );
  227. describe( 'fromJSON', () => {
  228. it( 'should create document fragment without children', () => {
  229. const frag = new DocumentFragment();
  230. const serialized = frag.toJSON();
  231. const deserialized = DocumentFragment.fromJSON( serialized );
  232. expect( deserialized.isEmpty ).to.be.true;
  233. } );
  234. it( 'should create element with children', () => {
  235. const p = new Element( 'p' );
  236. const foo = new Text( 'foo' );
  237. const frag = new DocumentFragment( [ p, foo ] );
  238. const serialized = frag.toJSON();
  239. const deserialized = DocumentFragment.fromJSON( serialized );
  240. expect( deserialized.childCount ).to.equal( 2 );
  241. expect( deserialized.getChild( 0 ).name ).to.equal( 'p' );
  242. expect( deserialized.getChild( 0 ).parent ).to.equal( deserialized );
  243. expect( deserialized.getChild( 1 ).data ).to.equal( 'foo' );
  244. expect( deserialized.getChild( 1 ).parent ).to.equal( deserialized );
  245. } );
  246. } );
  247. describe( 'getNodeByPath', () => {
  248. it( 'should return the whole document fragment if path is empty', () => {
  249. const frag = new DocumentFragment();
  250. expect( frag.getNodeByPath( [] ) ).to.equal( frag );
  251. } );
  252. it( 'should return a descendant of this node', () => {
  253. const foo = new Text( 'foo' );
  254. const image = new Element( 'image' );
  255. const element = new Element( 'elem', [], [
  256. new Element( 'elem', [], [
  257. foo,
  258. image
  259. ] )
  260. ] );
  261. const frag = new DocumentFragment( element );
  262. expect( frag.getNodeByPath( [ 0, 0, 0 ] ) ).to.equal( foo );
  263. expect( frag.getNodeByPath( [ 0, 0, 1 ] ) ).to.equal( foo );
  264. expect( frag.getNodeByPath( [ 0, 0, 2 ] ) ).to.equal( foo );
  265. expect( frag.getNodeByPath( [ 0, 0, 3 ] ) ).to.equal( image );
  266. } );
  267. it( 'works fine with offsets', () => {
  268. const abc = new Text( 'abc' );
  269. const xyz = new Text( 'xyz' );
  270. const bar = new Text( 'bar' );
  271. const foo = new Text( 'foo' );
  272. const bom = new Text( 'bom' );
  273. const bold = new Element( 'b', [], [
  274. bar
  275. ] );
  276. const paragraph = new Element( 'paragraph', [], [
  277. foo,
  278. bold,
  279. bom
  280. ] );
  281. const frag = new DocumentFragment( [
  282. abc,
  283. paragraph,
  284. xyz
  285. ] );
  286. // abc<paragraph>foo<bold>bar</bold>bom</paragraph>xyz
  287. expect( frag.getNodeByPath( [ 0 ] ), 1 ).to.equal( abc );
  288. expect( frag.getNodeByPath( [ 1 ] ), 2 ).to.equal( abc );
  289. expect( frag.getNodeByPath( [ 2 ] ), 3 ).to.equal( abc );
  290. expect( frag.getNodeByPath( [ 3 ] ), 4 ).to.equal( paragraph );
  291. expect( frag.getNodeByPath( [ 3, 0 ] ), 5 ).to.equal( foo );
  292. expect( frag.getNodeByPath( [ 3, 1 ] ), 6 ).to.equal( foo );
  293. expect( frag.getNodeByPath( [ 3, 2 ] ), 7 ).to.equal( foo );
  294. expect( frag.getNodeByPath( [ 3, 3 ] ), 8 ).to.equal( bold );
  295. expect( frag.getNodeByPath( [ 3, 3, 0 ] ), 9 ).to.equal( bar );
  296. expect( frag.getNodeByPath( [ 3, 3, 1 ] ), 10 ).to.equal( bar );
  297. expect( frag.getNodeByPath( [ 3, 3, 2 ] ), 11 ).to.equal( bar );
  298. expect( frag.getNodeByPath( [ 3, 3, 3 ] ), 12 ).to.equal( null );
  299. expect( frag.getNodeByPath( [ 3, 4 ] ), 13 ).to.equal( bom );
  300. expect( frag.getNodeByPath( [ 3, 5 ] ), 14 ).to.equal( bom );
  301. expect( frag.getNodeByPath( [ 3, 6 ] ), 15 ).to.equal( bom );
  302. expect( frag.getNodeByPath( [ 3, 7 ] ), 16 ).to.equal( null );
  303. expect( frag.getNodeByPath( [ 4 ] ), 17 ).to.equal( xyz );
  304. expect( frag.getNodeByPath( [ 5 ] ), 18 ).to.equal( xyz );
  305. expect( frag.getNodeByPath( [ 6 ] ), 19 ).to.equal( xyz );
  306. expect( frag.getNodeByPath( [ 7 ] ), 20 ).to.equal( null );
  307. } );
  308. } );
  309. } );