documentfragment.js 13 KB

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