documentfragment.js 13 KB

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