documentfragment.js 13 KB

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