documentfragment.js 12 KB

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