batch.js 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Batch from '../../src/model/batch';
  6. import Delta from '../../src/model/delta/delta';
  7. import InsertDelta from '../../src/model/delta/insertdelta';
  8. import WeakInsertDelta from '../../src/model/delta/weakinsertdelta';
  9. import Operation from '../../src/model/operation/operation';
  10. import Document from '../../src/model/document';
  11. import DocumentFragment from '../../src/model/documentfragment';
  12. import Element from '../../src/model/element';
  13. import Text from '../../src/model/text';
  14. import Position from '../../src/model/position';
  15. import Range from '../../src/model/range';
  16. import count from '@ckeditor/ckeditor5-utils/src/count';
  17. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  18. import { getNodesAndText } from '../../tests/model/_utils/utils';
  19. describe( 'Batch', () => {
  20. describe( 'type', () => {
  21. it( 'should default to "default"', () => {
  22. const batch = new Batch( new Document() );
  23. expect( batch.type ).to.equal( 'default' );
  24. } );
  25. it( 'should be set to the value set in constructor', () => {
  26. const batch = new Batch( new Document(), 'ignore' );
  27. expect( batch.type ).to.equal( 'ignore' );
  28. } );
  29. } );
  30. describe( 'baseVersion', () => {
  31. it( 'should return base version of first delta from the batch', () => {
  32. const batch = new Batch( new Document() );
  33. const delta = new Delta();
  34. const operation = new Operation( 2 );
  35. delta.addOperation( operation );
  36. batch.addDelta( delta );
  37. expect( batch.baseVersion ).to.equal( 2 );
  38. } );
  39. it( 'should return null if there are no deltas in batch', () => {
  40. const batch = new Batch( new Document() );
  41. expect( batch.baseVersion ).to.be.null;
  42. } );
  43. } );
  44. describe( 'addDelta()', () => {
  45. it( 'should add delta to the batch', () => {
  46. const batch = new Batch( new Document() );
  47. const deltaA = new Delta();
  48. const deltaB = new Delta();
  49. batch.addDelta( deltaA );
  50. batch.addDelta( deltaB );
  51. expect( batch.deltas.length ).to.equal( 2 );
  52. expect( batch.deltas[ 0 ] ).to.equal( deltaA );
  53. expect( batch.deltas[ 1 ] ).to.equal( deltaB );
  54. } );
  55. } );
  56. describe( 'getOperations()', () => {
  57. it( 'should return collection of operations from all deltas', () => {
  58. const doc = new Document();
  59. const batch = new Batch( doc );
  60. const deltaA = new Delta();
  61. const deltaB = new Delta();
  62. const ops = [
  63. new Operation( doc.version ),
  64. new Operation( doc.version + 1 ),
  65. new Operation( doc.version + 2 )
  66. ];
  67. batch.addDelta( deltaA );
  68. deltaA.addOperation( ops[ 0 ] );
  69. batch.addDelta( deltaB );
  70. deltaA.addOperation( ops[ 1 ] );
  71. deltaA.addOperation( ops[ 2 ] );
  72. expect( Array.from( batch.getOperations() ) ).to.deep.equal( ops );
  73. expect( batch.getOperations() ).to.have.property( 'next' );
  74. } );
  75. } );
  76. describe( 'createText()', () => {
  77. let doc, batch;
  78. beforeEach( () => {
  79. doc = new Document();
  80. batch = doc.batch();
  81. } );
  82. it( 'should create text node', () => {
  83. const text = batch.createText( 'foo' );
  84. expect( text ).to.instanceof( Text );
  85. expect( text.data ).to.equal( 'foo' );
  86. expect( Array.from( text.getAttributes() ) ).to.length( 0 );
  87. } );
  88. it( 'should create text with attributes', () => {
  89. const text = batch.createText( 'foo', { foo: 'bar', biz: 'baz' } );
  90. expect( Array.from( text.getAttributes() ) ).to.deep.equal( [ [ 'foo', 'bar' ], [ 'biz', 'baz' ] ] );
  91. } );
  92. } );
  93. describe( 'createElement()', () => {
  94. let doc, batch;
  95. beforeEach( () => {
  96. doc = new Document();
  97. batch = doc.batch();
  98. } );
  99. it( 'should create element', () => {
  100. const element = batch.createElement( 'foo' );
  101. expect( element ).to.instanceof( Element );
  102. expect( element.name ).to.equal( 'foo' );
  103. expect( Array.from( element.getAttributes() ) ).to.length( 0 );
  104. } );
  105. it( 'should create element with attributes', () => {
  106. const element = batch.createText( 'foo', { foo: 'bar', biz: 'baz' } );
  107. expect( Array.from( element.getAttributes() ) ).to.deep.equal( [ [ 'foo', 'bar' ], [ 'biz', 'baz' ] ] );
  108. } );
  109. } );
  110. describe( 'createDocumentFragment()', () => {
  111. let doc, batch;
  112. beforeEach( () => {
  113. doc = new Document();
  114. batch = doc.batch();
  115. } );
  116. it( 'should create element', () => {
  117. const element = batch.createDocumentFragment();
  118. expect( element ).to.instanceof( DocumentFragment );
  119. } );
  120. } );
  121. describe( 'insert()', () => {
  122. let doc, batch;
  123. beforeEach( () => {
  124. doc = new Document();
  125. batch = doc.batch();
  126. } );
  127. it( 'should insert node at given position', () => {
  128. const parent = batch.createDocumentFragment();
  129. const child = batch.createElement( 'child' );
  130. const textChild = batch.createText( 'textChild' );
  131. batch.insert( child, new Position( parent, [ 0 ] ) );
  132. batch.insert( textChild, new Position( parent, [ 1 ] ) );
  133. expect( Array.from( parent ) ).to.deep.equal( [ child, textChild ] );
  134. } );
  135. it( 'should insert node at the beginning of given element', () => {
  136. const parent = batch.createDocumentFragment();
  137. const child1 = batch.createElement( 'child' );
  138. const child2 = batch.createElement( 'child' );
  139. batch.insert( child1, parent );
  140. batch.insert( child2, parent );
  141. expect( Array.from( parent.getChildren() ) ).to.deep.equal( [ child2, child1 ] );
  142. } );
  143. it( 'should insert node at the end of given element', () => {
  144. const parent = batch.createDocumentFragment();
  145. const child1 = batch.createElement( 'child' );
  146. const child2 = batch.createElement( 'child' );
  147. batch.insert( child1, parent );
  148. batch.insert( child2, parent, 'end' );
  149. expect( Array.from( parent.getChildren() ) ).to.deep.equal( [ child1, child2 ] );
  150. } );
  151. it( 'should insert node at the given offset of given element', () => {
  152. const parent = batch.createDocumentFragment();
  153. const child1 = batch.createElement( 'child' );
  154. const child2 = batch.createElement( 'child' );
  155. const child3 = batch.createElement( 'child' );
  156. batch.insert( child3, parent );
  157. batch.insert( child1, parent );
  158. batch.insert( child2, parent, 1 );
  159. expect( Array.from( parent.getChildren() ) ).to.deep.equal( [ child1, child2, child3 ] );
  160. } );
  161. it( 'should insert node before the given node', () => {
  162. const parent = batch.createDocumentFragment();
  163. const child1 = batch.createElement( 'child' );
  164. const child2 = batch.createElement( 'child' );
  165. const child3 = batch.createElement( 'child' );
  166. batch.insert( child3, parent );
  167. batch.insert( child1, parent );
  168. batch.insert( child2, child3, 'before' );
  169. expect( Array.from( parent.getChildren() ) ).to.deep.equal( [ child1, child2, child3 ] );
  170. } );
  171. it( 'should insert node after the given node', () => {
  172. const parent = batch.createDocumentFragment();
  173. const child1 = batch.createElement( 'child' );
  174. const child2 = batch.createElement( 'child' );
  175. const child3 = batch.createElement( 'child' );
  176. batch.insert( child3, parent );
  177. batch.insert( child1, parent );
  178. batch.insert( child2, child1, 'after' );
  179. expect( Array.from( parent.getChildren() ) ).to.deep.equal( [ child1, child2, child3 ] );
  180. } );
  181. it( 'should create proper delta for inserting element', () => {
  182. const parent = batch.createDocumentFragment();
  183. const element = batch.createElement( 'child' );
  184. const spy = sinon.spy( doc, 'applyOperation' );
  185. batch.insert( element, parent );
  186. sinon.assert.calledOnce( spy );
  187. expect( spy.lastCall.args[ 0 ].type ).to.equal( 'insert' );
  188. expect( spy.lastCall.args[ 0 ].delta ).to.instanceof( InsertDelta );
  189. expect( spy.lastCall.args[ 0 ].delta ).to.not.instanceof( WeakInsertDelta );
  190. expect( spy.lastCall.args[ 0 ].delta.batch ).to.equal( batch );
  191. } );
  192. it( 'should create proper delta for inserting text', () => {
  193. const parent = batch.createDocumentFragment();
  194. const text = batch.createText( 'child' );
  195. const spy = sinon.spy( doc, 'applyOperation' );
  196. batch.insert( text, parent );
  197. sinon.assert.calledOnce( spy );
  198. expect( spy.lastCall.args[ 0 ].type ).to.equal( 'insert' );
  199. expect( spy.lastCall.args[ 0 ].delta ).to.instanceof( WeakInsertDelta );
  200. expect( spy.lastCall.args[ 0 ].delta.batch ).to.equal( batch );
  201. } );
  202. it( 'should move element from one parent to the other within the same document (documentA -> documentA)', () => {
  203. const rootA = doc.createRoot();
  204. const parent1 = batch.createElement( 'parent' );
  205. const parent2 = batch.createElement( 'parent' );
  206. const node = batch.createText( 'foo' );
  207. batch.insert( node, parent1 );
  208. batch.insert( parent1, rootA );
  209. batch.insert( parent2, rootA );
  210. const spy = sinon.spy( doc, 'applyOperation' );
  211. batch.insert( node, parent2 );
  212. // Verify result.
  213. expect( Array.from( parent1.getChildren() ) ).to.deep.equal( [] );
  214. expect( Array.from( parent2.getChildren() ) ).to.deep.equal( [ node ] );
  215. // Verify deltas and operations.
  216. sinon.assert.calledOnce( spy );
  217. expect( spy.firstCall.args[ 0 ].type ).to.equal( 'move' );
  218. expect( spy.firstCall.args[ 0 ].delta.batch ).to.equal( batch );
  219. } );
  220. it( 'should move element from one parent to the other within the same document (documentA -> documentB)', () => {
  221. const rootA = doc.createRoot( '$root', 'A' );
  222. const rootB = doc.createRoot( '$root', 'B' );
  223. const node = batch.createText( 'foo' );
  224. batch.insert( node, rootA );
  225. const spy = sinon.spy( doc, 'applyOperation' );
  226. batch.insert( node, rootB );
  227. // Verify result.
  228. expect( Array.from( rootA.getChildren() ) ).to.deep.equal( [] );
  229. expect( Array.from( rootB.getChildren() ) ).to.deep.equal( [ node ] );
  230. // Verify deltas and operations.
  231. sinon.assert.calledOnce( spy );
  232. expect( spy.firstCall.args[ 0 ].type ).to.equal( 'move' );
  233. expect( spy.firstCall.args[ 0 ].delta.batch ).to.equal( batch );
  234. } );
  235. it( 'should move element from one parent to the other within the same document (docFragA -> docFragA)', () => {
  236. const docFragA = batch.createDocumentFragment();
  237. const parent1 = batch.createElement( 'parent' );
  238. const parent2 = batch.createElement( 'parent' );
  239. const node = batch.createText( 'foo' );
  240. batch.insert( node, parent1 );
  241. batch.insert( parent1, docFragA );
  242. batch.insert( parent2, docFragA );
  243. const spy = sinon.spy( doc, 'applyOperation' );
  244. batch.insert( node, parent2 );
  245. // Verify result.
  246. expect( Array.from( parent1.getChildren() ) ).to.deep.equal( [] );
  247. expect( Array.from( parent2.getChildren() ) ).to.deep.equal( [ node ] );
  248. // Verify deltas and operations.
  249. sinon.assert.calledOnce( spy );
  250. expect( spy.firstCall.args[ 0 ].type ).to.equal( 'move' );
  251. expect( spy.firstCall.args[ 0 ].delta.batch ).to.equal( batch );
  252. } );
  253. it( 'should move element from one parent to the other within different document (document -> docFrag)', () => {
  254. const root = doc.createRoot();
  255. const docFrag = batch.createDocumentFragment();
  256. const node = batch.createText( 'foo' );
  257. batch.insert( node, root );
  258. const spy = sinon.spy( doc, 'applyOperation' );
  259. batch.insert( node, docFrag );
  260. // Verify result.
  261. expect( Array.from( root.getChildren() ) ).to.deep.equal( [] );
  262. expect( Array.from( docFrag.getChildren() ) ).to.deep.equal( [ node ] );
  263. // Verify deltas and operations.
  264. sinon.assert.calledTwice( spy );
  265. expect( spy.firstCall.args[ 0 ].type ).to.equal( 'remove' );
  266. expect( spy.firstCall.args[ 0 ].delta.batch ).to.equal( batch );
  267. expect( spy.secondCall.args[ 0 ].type ).to.equal( 'insert' );
  268. expect( spy.secondCall.args[ 0 ].delta.batch ).to.equal( batch );
  269. } );
  270. it( 'should move element from one parent to the other within different document (docFragA -> docFragB)', () => {
  271. const docFragA = batch.createDocumentFragment();
  272. const docFragB = batch.createDocumentFragment();
  273. const node = batch.createText( 'foo' );
  274. batch.insert( node, docFragA );
  275. const spy = sinon.spy( doc, 'applyOperation' );
  276. batch.insert( node, docFragB );
  277. // Verify result.
  278. expect( Array.from( docFragA ) ).to.deep.equal( [] );
  279. expect( Array.from( docFragB ) ).to.deep.equal( [ node ] );
  280. // Verify deltas and operations.
  281. sinon.assert.calledTwice( spy );
  282. expect( spy.firstCall.args[ 0 ].type ).to.equal( 'detach' );
  283. expect( spy.firstCall.args[ 0 ].delta.batch ).to.equal( batch );
  284. expect( spy.secondCall.args[ 0 ].type ).to.equal( 'insert' );
  285. expect( spy.secondCall.args[ 0 ].delta.batch ).to.equal( batch );
  286. } );
  287. it( 'should transfer markers from given DocumentFragment', () => {
  288. const root = doc.createRoot();
  289. const docFrag = batch.createDocumentFragment();
  290. batch.appendText( 'abcd', root );
  291. batch.appendElement( 'p', docFrag );
  292. batch.insertText( 'foo bar', new Position( docFrag, [ 0, 0 ] ) );
  293. const marker = new Range( new Position( docFrag, [ 0, 1 ] ), new Position( docFrag, [ 0, 5 ] ) );
  294. docFrag.markers.set( 'marker', marker );
  295. batch.insert( docFrag, new Position( root, [ 2 ] ) );
  296. expect( Array.from( doc.markers ).length ).to.equal( 1 );
  297. const range = doc.markers.get( 'marker' ).getRange();
  298. expect( range.root ).to.equal( root );
  299. expect( range.start.path ).to.deep.equal( [ 2, 1 ] );
  300. expect( range.end.path ).to.deep.equal( [ 2, 5 ] );
  301. } );
  302. it( 'should set each marker as a separate operation', () => {
  303. const spy = sinon.spy();
  304. const root = doc.createRoot();
  305. const docFrag = batch.createDocumentFragment();
  306. batch.appendText( 'abcd', root );
  307. batch.appendElement( 'p', docFrag );
  308. batch.insertText( 'foo bar', new Position( docFrag, [ 0, 0 ] ) );
  309. const marker1 = new Range( new Position( docFrag, [ 0, 1 ] ), new Position( docFrag, [ 0, 2 ] ) );
  310. const marker2 = new Range( new Position( docFrag, [ 0, 5 ] ), new Position( docFrag, [ 0, 6 ] ) );
  311. docFrag.markers.set( 'marker1', marker1 );
  312. docFrag.markers.set( 'marker2', marker2 );
  313. doc.on( 'change', spy );
  314. batch.insert( docFrag, new Position( root, [ 2 ] ) );
  315. sinon.assert.calledThrice( spy );
  316. expect( spy.firstCall.args[ 1 ] ).to.equal( 'insert' );
  317. expect( spy.secondCall.args[ 1 ] ).to.equal( 'marker' );
  318. expect( spy.thirdCall.args[ 1 ] ).to.equal( 'marker' );
  319. } );
  320. } );
  321. describe( 'insertText()', () => {
  322. let doc, batch;
  323. beforeEach( () => {
  324. doc = new Document();
  325. batch = doc.batch();
  326. } );
  327. it( 'should create and insert text node with attributes at given position', () => {
  328. const parent = batch.createDocumentFragment();
  329. batch.insertText( 'foo', { bar: 'biz' }, new Position( parent, [ 0 ] ) );
  330. expect( parent.childCount ).to.equal( 1 );
  331. expect( parent.getChild( 0 ) ).to.instanceof( Text );
  332. expect( parent.getChild( 0 ).data ).to.equal( 'foo' );
  333. expect( Array.from( parent.getChild( 0 ).getAttributes() ) ).to.deep.equal( [ [ 'bar', 'biz' ] ] );
  334. } );
  335. it( 'should create and insert text node with no attributes at given position', () => {
  336. const parent = batch.createDocumentFragment();
  337. batch.insertText( 'foo', null, new Position( parent, [ 0 ] ) );
  338. expect( parent.childCount ).to.equal( 1 );
  339. expect( parent.getChild( 0 ) ).to.instanceof( Text );
  340. expect( parent.getChild( 0 ).data ).to.equal( 'foo' );
  341. expect( Array.from( parent.getChild( 0 ).getAttributes() ) ).to.deep.equal( [] );
  342. } );
  343. it( 'should create and insert text node omitting attributes param', () => {
  344. const parent = batch.createDocumentFragment();
  345. batch.insertText( 'foo', new Position( parent, [ 0 ] ) );
  346. expect( parent.childCount ).to.equal( 1 );
  347. expect( parent.getChild( 0 ) ).to.instanceof( Text );
  348. expect( parent.getChild( 0 ).data ).to.equal( 'foo' );
  349. expect( Array.from( parent.getChild( 0 ).getAttributes() ) ).to.deep.equal( [] );
  350. } );
  351. it( 'should create and insert text node at the beginning of given element', () => {
  352. const parent = batch.createDocumentFragment();
  353. batch.insert( batch.createElement( 'child' ), parent );
  354. batch.insertText( 'foo', parent );
  355. expect( parent.childCount ).to.equal( 2 );
  356. expect( parent.getChild( 0 ) ).to.instanceof( Text );
  357. expect( parent.getChild( 1 ) ).to.instanceof( Element );
  358. } );
  359. it( 'should create and insert text node at the end of given element', () => {
  360. const parent = batch.createDocumentFragment();
  361. batch.insert( batch.createElement( 'child' ), parent );
  362. batch.insertText( 'foo', parent, 'end' );
  363. expect( parent.childCount ).to.equal( 2 );
  364. expect( parent.getChild( 0 ) ).to.instanceof( Element );
  365. expect( parent.getChild( 1 ) ).to.instanceof( Text );
  366. } );
  367. it( 'should create and insert text node at the given offset of given element', () => {
  368. const parent = batch.createDocumentFragment();
  369. batch.insert( batch.createElement( 'child' ), parent );
  370. batch.insert( batch.createElement( 'child' ), parent );
  371. batch.insertText( 'foo', parent, 1 );
  372. expect( parent.childCount ).to.equal( 3 );
  373. expect( parent.getChild( 0 ) ).to.instanceof( Element );
  374. expect( parent.getChild( 1 ) ).to.instanceof( Text );
  375. expect( parent.getChild( 2 ) ).to.instanceof( Element );
  376. } );
  377. it( 'should create and insert text node before the given node', () => {
  378. const parent = batch.createDocumentFragment();
  379. const child1 = batch.createElement( 'child' );
  380. const child2 = batch.createElement( 'child' );
  381. batch.insert( child1, parent );
  382. batch.insert( child2, parent, 'end' );
  383. batch.insertText( 'foo', child2, 'before' );
  384. expect( parent.childCount ).to.equal( 3 );
  385. expect( parent.getChild( 0 ) ).to.instanceof( Element );
  386. expect( parent.getChild( 1 ) ).to.instanceof( Text );
  387. expect( parent.getChild( 2 ) ).to.instanceof( Element );
  388. } );
  389. it( 'should create and insert text node after the given node', () => {
  390. const parent = batch.createDocumentFragment();
  391. const child1 = batch.createElement( 'child' );
  392. const child2 = batch.createElement( 'child' );
  393. batch.insert( child1, parent );
  394. batch.insert( child2, parent, 'end' );
  395. batch.insertText( 'foo', child1, 'after' );
  396. expect( parent.childCount ).to.equal( 3 );
  397. expect( parent.getChild( 0 ) ).to.instanceof( Element );
  398. expect( parent.getChild( 1 ) ).to.instanceof( Text );
  399. expect( parent.getChild( 2 ) ).to.instanceof( Element );
  400. } );
  401. it( 'should create proper delta', () => {
  402. const parent = batch.createDocumentFragment();
  403. const spy = sinon.spy( doc, 'applyOperation' );
  404. batch.insertText( 'foo', parent );
  405. sinon.assert.calledOnce( spy );
  406. expect( spy.lastCall.args[ 0 ].type ).to.equal( 'insert' );
  407. expect( spy.lastCall.args[ 0 ].delta ).to.instanceof( WeakInsertDelta );
  408. expect( spy.lastCall.args[ 0 ].delta.batch ).to.equal( batch );
  409. } );
  410. } );
  411. describe( 'insertElement()', () => {
  412. let doc, batch;
  413. beforeEach( () => {
  414. doc = new Document();
  415. batch = doc.batch();
  416. } );
  417. it( 'should create and insert element with attributes at given position', () => {
  418. const parent = batch.createDocumentFragment();
  419. batch.insertElement( 'foo', { bar: 'biz' }, new Position( parent, [ 0 ] ) );
  420. expect( parent.childCount ).to.equal( 1 );
  421. expect( parent.getChild( 0 ) ).to.instanceof( Element );
  422. expect( parent.getChild( 0 ).name ).to.equal( 'foo' );
  423. expect( Array.from( parent.getChild( 0 ).getAttributes() ) ).to.deep.equal( [ [ 'bar', 'biz' ] ] );
  424. } );
  425. it( 'should create and insert element with no attributes at given position', () => {
  426. const parent = batch.createDocumentFragment();
  427. batch.insertElement( 'foo', null, new Position( parent, [ 0 ] ) );
  428. expect( parent.childCount ).to.equal( 1 );
  429. expect( parent.getChild( 0 ) ).to.instanceof( Element );
  430. expect( parent.getChild( 0 ).name ).to.equal( 'foo' );
  431. expect( Array.from( parent.getChild( 0 ).getAttributes() ) ).to.deep.equal( [] );
  432. } );
  433. it( 'should create and insert element with no attributes omitting attributes param', () => {
  434. const parent = batch.createDocumentFragment();
  435. batch.insertElement( 'foo', new Position( parent, [ 0 ] ) );
  436. expect( parent.childCount ).to.equal( 1 );
  437. expect( parent.getChild( 0 ) ).to.instanceof( Element );
  438. expect( parent.getChild( 0 ).name ).to.equal( 'foo' );
  439. expect( Array.from( parent.getChild( 0 ).getAttributes() ) ).to.deep.equal( [] );
  440. } );
  441. it( 'should create and insert element at the beginning of given element', () => {
  442. const parent = batch.createDocumentFragment();
  443. batch.insert( batch.createElement( 'child' ), parent );
  444. batch.insertElement( 'foo', parent );
  445. expect( parent.childCount ).to.equal( 2 );
  446. expect( parent.getChild( 0 ).name ).to.equal( 'foo' );
  447. expect( parent.getChild( 1 ).name ).to.equal( 'child' );
  448. } );
  449. it( 'should create and insert element at the end of given element', () => {
  450. const parent = batch.createDocumentFragment();
  451. batch.insert( batch.createElement( 'child' ), parent );
  452. batch.insertElement( 'foo', parent, 'end' );
  453. expect( parent.childCount ).to.equal( 2 );
  454. expect( parent.getChild( 0 ).name ).to.equal( 'child' );
  455. expect( parent.getChild( 1 ).name ).to.equal( 'foo' );
  456. } );
  457. it( 'should create and insert element at the given offset of given element', () => {
  458. const parent = batch.createDocumentFragment();
  459. batch.insert( batch.createElement( 'child1' ), parent );
  460. batch.insert( batch.createElement( 'child2' ), parent, 'end' );
  461. batch.insertElement( 'foo', parent, 1 );
  462. expect( parent.childCount ).to.equal( 3 );
  463. expect( parent.getChild( 0 ).name ).to.equal( 'child1' );
  464. expect( parent.getChild( 1 ).name ).to.equal( 'foo' );
  465. expect( parent.getChild( 2 ).name ).to.equal( 'child2' );
  466. } );
  467. it( 'should create and insert element before the given node', () => {
  468. const parent = batch.createDocumentFragment();
  469. const child1 = batch.createElement( 'child1' );
  470. const child2 = batch.createElement( 'child2' );
  471. batch.insert( child1, parent );
  472. batch.insert( child2, parent, 'end' );
  473. batch.insertElement( 'foo', child2, 'before' );
  474. expect( parent.childCount ).to.equal( 3 );
  475. expect( parent.getChild( 0 ).name ).to.equal( 'child1' );
  476. expect( parent.getChild( 1 ).name ).to.equal( 'foo' );
  477. expect( parent.getChild( 2 ).name ).to.equal( 'child2' );
  478. } );
  479. it( 'should create and insert element after the given node', () => {
  480. const parent = batch.createDocumentFragment();
  481. const child1 = batch.createElement( 'child1' );
  482. const child2 = batch.createElement( 'child2' );
  483. batch.insert( child1, parent );
  484. batch.insert( child2, parent, 'end' );
  485. batch.insertElement( 'foo', child1, 'after' );
  486. expect( parent.childCount ).to.equal( 3 );
  487. expect( parent.getChild( 0 ).name ).to.equal( 'child1' );
  488. expect( parent.getChild( 1 ).name ).to.equal( 'foo' );
  489. expect( parent.getChild( 2 ).name ).to.equal( 'child2' );
  490. } );
  491. it( 'should create proper delta', () => {
  492. const parent = batch.createDocumentFragment();
  493. const spy = sinon.spy( doc, 'applyOperation' );
  494. batch.insertText( 'foo', parent );
  495. sinon.assert.calledOnce( spy );
  496. expect( spy.lastCall.args[ 0 ].type ).to.equal( 'insert' );
  497. expect( spy.lastCall.args[ 0 ].delta ).to.instanceof( InsertDelta );
  498. expect( spy.lastCall.args[ 0 ].delta.batch ).to.equal( batch );
  499. } );
  500. } );
  501. describe( 'append()', () => {
  502. let doc, batch;
  503. beforeEach( () => {
  504. doc = new Document();
  505. batch = doc.batch();
  506. } );
  507. it( 'should insert element at the end of the parent', () => {
  508. const parent = doc.batch().createDocumentFragment();
  509. const childText = doc.batch().createText( 'foo' );
  510. const childElement = doc.batch().createElement( 'foo' );
  511. batch.append( childText, parent );
  512. batch.append( childElement, parent );
  513. expect( Array.from( parent ) ).to.deep.equal( [ childText, childElement ] );
  514. } );
  515. it( 'should create proper delta', () => {
  516. const parent = batch.createDocumentFragment();
  517. const text = batch.createText( 'foo' );
  518. const spy = sinon.spy( doc, 'applyOperation' );
  519. batch.append( text, parent );
  520. sinon.assert.calledOnce( spy );
  521. expect( spy.lastCall.args[ 0 ].type ).to.equal( 'insert' );
  522. expect( spy.lastCall.args[ 0 ].delta.batch ).to.equal( batch );
  523. } );
  524. it( 'should move element from one parent to the other within the same document (documentA -> documentA)', () => {
  525. const rootA = doc.createRoot();
  526. const parent1 = batch.createElement( 'parent' );
  527. const parent2 = batch.createElement( 'parent' );
  528. const node = batch.createText( 'foo' );
  529. batch.insert( node, parent1 );
  530. batch.insert( parent1, rootA );
  531. batch.insert( parent2, rootA );
  532. const spy = sinon.spy( doc, 'applyOperation' );
  533. batch.append( node, parent2 );
  534. // Verify result.
  535. expect( Array.from( parent1.getChildren() ) ).to.deep.equal( [] );
  536. expect( Array.from( parent2.getChildren() ) ).to.deep.equal( [ node ] );
  537. // Verify deltas and operations.
  538. sinon.assert.calledOnce( spy );
  539. expect( spy.firstCall.args[ 0 ].type ).to.equal( 'move' );
  540. expect( spy.firstCall.args[ 0 ].delta.batch ).to.equal( batch );
  541. } );
  542. it( 'should move element from one parent to the other within the same document (documentA -> documentB)', () => {
  543. const rootA = doc.createRoot( '$root', 'A' );
  544. const rootB = doc.createRoot( '$root', 'B' );
  545. const node = batch.createText( 'foo' );
  546. batch.insert( node, rootA );
  547. const spy = sinon.spy( doc, 'applyOperation' );
  548. batch.append( node, rootB );
  549. // Verify result.
  550. expect( Array.from( rootA.getChildren() ) ).to.deep.equal( [] );
  551. expect( Array.from( rootB.getChildren() ) ).to.deep.equal( [ node ] );
  552. // Verify deltas and operations.
  553. sinon.assert.calledOnce( spy );
  554. expect( spy.firstCall.args[ 0 ].type ).to.equal( 'move' );
  555. expect( spy.firstCall.args[ 0 ].delta.batch ).to.equal( batch );
  556. } );
  557. it( 'should move element from one parent to the other within the same document (docFragA -> docFragA)', () => {
  558. const docFragA = batch.createDocumentFragment();
  559. const parent1 = batch.createElement( 'parent' );
  560. const parent2 = batch.createElement( 'parent' );
  561. const node = batch.createText( 'foo' );
  562. batch.insert( node, parent1 );
  563. batch.insert( parent1, docFragA );
  564. batch.insert( parent2, docFragA );
  565. const spy = sinon.spy( doc, 'applyOperation' );
  566. batch.append( node, parent2 );
  567. // Verify result.
  568. expect( Array.from( parent1.getChildren() ) ).to.deep.equal( [] );
  569. expect( Array.from( parent2.getChildren() ) ).to.deep.equal( [ node ] );
  570. // Verify deltas and operations.
  571. sinon.assert.calledOnce( spy );
  572. expect( spy.firstCall.args[ 0 ].type ).to.equal( 'move' );
  573. expect( spy.firstCall.args[ 0 ].delta.batch ).to.equal( batch );
  574. } );
  575. it( 'should move element from one parent to the other within different document (document -> docFrag)', () => {
  576. const root = doc.createRoot();
  577. const docFrag = batch.createDocumentFragment();
  578. const node = batch.createText( 'foo' );
  579. batch.insert( node, root );
  580. const spy = sinon.spy( doc, 'applyOperation' );
  581. batch.append( node, docFrag );
  582. // Verify result.
  583. expect( Array.from( root.getChildren() ) ).to.deep.equal( [] );
  584. expect( Array.from( docFrag.getChildren() ) ).to.deep.equal( [ node ] );
  585. // Verify deltas and operations.
  586. sinon.assert.calledTwice( spy );
  587. expect( spy.firstCall.args[ 0 ].type ).to.equal( 'remove' );
  588. expect( spy.firstCall.args[ 0 ].delta.batch ).to.equal( batch );
  589. expect( spy.secondCall.args[ 0 ].type ).to.equal( 'insert' );
  590. expect( spy.secondCall.args[ 0 ].delta.batch ).to.equal( batch );
  591. } );
  592. it( 'should move element from one parent to the other within different document (docFragA -> docFragB)', () => {
  593. const docFragA = batch.createDocumentFragment();
  594. const docFragB = batch.createDocumentFragment();
  595. const node = batch.createText( 'foo' );
  596. batch.insert( node, docFragA );
  597. const spy = sinon.spy( doc, 'applyOperation' );
  598. batch.append( node, docFragB );
  599. // Verify result.
  600. expect( Array.from( docFragA ) ).to.deep.equal( [] );
  601. expect( Array.from( docFragB ) ).to.deep.equal( [ node ] );
  602. // Verify deltas and operations.
  603. sinon.assert.calledTwice( spy );
  604. expect( spy.firstCall.args[ 0 ].type ).to.equal( 'detach' );
  605. expect( spy.firstCall.args[ 0 ].delta.batch ).to.equal( batch );
  606. expect( spy.secondCall.args[ 0 ].type ).to.equal( 'insert' );
  607. expect( spy.secondCall.args[ 0 ].delta.batch ).to.equal( batch );
  608. } );
  609. } );
  610. describe( 'appendText()', () => {
  611. let doc, batch;
  612. beforeEach( () => {
  613. doc = new Document();
  614. batch = doc.batch();
  615. } );
  616. it( 'should create and insert text node with attributes at the end of the parent', () => {
  617. const parent = batch.createDocumentFragment();
  618. batch.appendText( 'foo', { bar: 'biz' }, parent );
  619. batch.appendText( 'bar', { biz: 'bar' }, parent );
  620. expect( parent.childCount ).to.equal( 2 );
  621. expect( parent.getChild( 0 ).data ).to.equal( 'foo' );
  622. expect( Array.from( parent.getChild( 0 ).getAttributes() ) ).to.deep.equal( [ [ 'bar', 'biz' ] ] );
  623. expect( parent.getChild( 1 ).data ).to.equal( 'bar' );
  624. expect( Array.from( parent.getChild( 1 ).getAttributes() ) ).to.deep.equal( [ [ 'biz', 'bar' ] ] );
  625. } );
  626. it( 'should create and insert text node with no attributes at the end of the parent', () => {
  627. const parent = batch.createDocumentFragment();
  628. batch.appendText( 'foo', null, parent );
  629. expect( parent.childCount ).to.equal( 1 );
  630. expect( parent.getChild( 0 ) ).to.instanceof( Text );
  631. expect( parent.getChild( 0 ).data ).to.equal( 'foo' );
  632. expect( Array.from( parent.getChild( 0 ).getAttributes() ) ).to.deep.equal( [] );
  633. } );
  634. it( 'should create and insert text node with no attributes omitting attributes param', () => {
  635. const parent = batch.createDocumentFragment();
  636. batch.appendText( 'foo', parent );
  637. expect( parent.childCount ).to.equal( 1 );
  638. expect( parent.getChild( 0 ) ).to.instanceof( Text );
  639. expect( parent.getChild( 0 ).data ).to.equal( 'foo' );
  640. expect( Array.from( parent.getChild( 0 ).getAttributes() ) ).to.deep.equal( [] );
  641. } );
  642. it( 'should create proper delta and operations', () => {
  643. const parent = batch.createDocumentFragment();
  644. const spy = sinon.spy( doc, 'applyOperation' );
  645. batch.appendText( 'foo', parent );
  646. sinon.assert.calledOnce( spy );
  647. expect( spy.firstCall.args[ 0 ].type ).to.equal( 'insert' );
  648. expect( spy.firstCall.args[ 0 ].delta ).to.instanceof( WeakInsertDelta );
  649. expect( spy.firstCall.args[ 0 ].delta.batch ).to.equal( batch );
  650. } );
  651. } );
  652. describe( 'appendElement()', () => {
  653. let doc, batch;
  654. beforeEach( () => {
  655. doc = new Document();
  656. batch = doc.batch();
  657. } );
  658. it( 'should create and insert element with attributes at the end of the parent', () => {
  659. const parent = batch.createDocumentFragment();
  660. batch.appendElement( 'foo', { bar: 'biz' }, parent );
  661. batch.appendElement( 'bar', { biz: 'bar' }, parent );
  662. expect( parent.childCount ).to.equal( 2 );
  663. expect( parent.getChild( 0 ).name ).to.equal( 'foo' );
  664. expect( Array.from( parent.getChild( 0 ).getAttributes() ) ).to.deep.equal( [ [ 'bar', 'biz' ] ] );
  665. expect( parent.getChild( 1 ).name ).to.equal( 'bar' );
  666. expect( Array.from( parent.getChild( 1 ).getAttributes() ) ).to.deep.equal( [ [ 'biz', 'bar' ] ] );
  667. } );
  668. it( 'should create and insert element with no attributes at the end of the parent', () => {
  669. const parent = batch.createDocumentFragment();
  670. batch.appendElement( 'foo', null, parent );
  671. expect( parent.childCount ).to.equal( 1 );
  672. expect( parent.getChild( 0 ).name ).to.equal( 'foo' );
  673. expect( Array.from( parent.getChild( 0 ).getAttributes() ) ).to.deep.equal( [] );
  674. } );
  675. it( 'should create and insert element with no attributes omitting attributes param', () => {
  676. const parent = batch.createDocumentFragment();
  677. batch.appendElement( 'foo', parent );
  678. expect( parent.childCount ).to.equal( 1 );
  679. expect( parent.getChild( 0 ).name ).to.equal( 'foo' );
  680. expect( Array.from( parent.getChild( 0 ).getAttributes() ) ).to.deep.equal( [] );
  681. } );
  682. it( 'should create proper delta and operation', () => {
  683. const parent = batch.createDocumentFragment();
  684. const spy = sinon.spy( doc, 'applyOperation' );
  685. batch.appendElement( 'foo', parent );
  686. sinon.assert.calledOnce( spy );
  687. expect( spy.firstCall.args[ 0 ].type ).to.equal( 'insert' );
  688. expect( spy.firstCall.args[ 0 ].delta ).to.instanceof( InsertDelta ).to.not.instanceof( WeakInsertDelta );
  689. expect( spy.firstCall.args[ 0 ].delta.batch ).to.equal( batch );
  690. } );
  691. } );
  692. describe( 'setAttribute() / removeAttribute()', () => {
  693. let batch, doc, root, spy;
  694. beforeEach( () => {
  695. doc = new Document();
  696. root = doc.createRoot();
  697. batch = doc.batch();
  698. } );
  699. describe( 'change attribute on node', () => {
  700. let node, text;
  701. beforeEach( () => {
  702. node = batch.createElement( 'p', { a: 1 } );
  703. text = batch.createText( 'c', { a: 1 } );
  704. batch.append( node, root );
  705. batch.append( text, root );
  706. spy = sinon.spy( doc, 'applyOperation' );
  707. } );
  708. describe( 'setAttribute', () => {
  709. it( 'should create the attribute on element', () => {
  710. batch.setAttribute( 'b', 2, node );
  711. expect( spy.callCount ).to.equal( 1 );
  712. expect( node.getAttribute( 'b' ) ).to.equal( 2 );
  713. } );
  714. it( 'should change the attribute of element', () => {
  715. batch.setAttribute( 'a', 2, node );
  716. expect( spy.callCount ).to.equal( 1 );
  717. expect( node.getAttribute( 'a' ) ).to.equal( 2 );
  718. } );
  719. it( 'should create the attribute on text node', () => {
  720. batch.setAttribute( 'b', 2, text );
  721. expect( spy.callCount ).to.equal( 1 );
  722. expect( root.getChild( 1 ).getAttribute( 'b' ) ).to.equal( 2 );
  723. } );
  724. it( 'should change the attribute of text node', () => {
  725. batch.setAttribute( 'a', 2, text );
  726. expect( spy.callCount ).to.equal( 1 );
  727. expect( root.getChild( 1 ).getAttribute( 'a' ) ).to.equal( 2 );
  728. } );
  729. it( 'should do nothing if the attribute value is the same', () => {
  730. batch.setAttribute( 'a', 1, node );
  731. expect( spy.callCount ).to.equal( 0 );
  732. expect( node.getAttribute( 'a' ) ).to.equal( 1 );
  733. } );
  734. } );
  735. describe( 'removeAttribute', () => {
  736. it( 'should remove the attribute from element', () => {
  737. batch.removeAttribute( 'a', node );
  738. expect( spy.callCount ).to.equal( 1 );
  739. expect( node.getAttribute( 'a' ) ).to.be.undefined;
  740. } );
  741. it( 'should remove the attribute from character', () => {
  742. batch.removeAttribute( 'a', text );
  743. expect( spy.callCount ).to.equal( 1 );
  744. expect( root.getChild( 1 ).getAttribute( 'a' ) ).to.be.undefined;
  745. } );
  746. it( 'should do nothing if the attribute is not set', () => {
  747. batch.removeAttribute( 'b', node );
  748. expect( spy.callCount ).to.equal( 0 );
  749. } );
  750. } );
  751. } );
  752. describe( 'change attribute on range', () => {
  753. beforeEach( () => {
  754. const element = batch.createElement( 'e', { a: 2 } );
  755. batch.appendText( 'xxx', { a: 1 }, root );
  756. batch.appendText( 'xxx', root );
  757. batch.appendText( 'xxx', { a: 1 }, root );
  758. batch.appendText( 'xxx', { a: 2 }, root );
  759. batch.appendText( 'xxx', root );
  760. batch.appendText( 'xxx', { a: 1 }, root );
  761. batch.appendText( 'xxx', element );
  762. batch.append( element, root );
  763. batch.appendText( 'xxx', root );
  764. spy = sinon.spy( doc, 'applyOperation' );
  765. } );
  766. function getRange( startIndex, endIndex ) {
  767. return new Range(
  768. Position.createFromParentAndOffset( root, startIndex ),
  769. Position.createFromParentAndOffset( root, endIndex )
  770. );
  771. }
  772. function getChangesAttrsCount() {
  773. let totalNumber = 0;
  774. for ( const delta of batch.deltas ) {
  775. for ( const operation of delta.operations ) {
  776. if ( operation.range ) {
  777. totalNumber += count( operation.range.getItems( { singleCharacters: true } ) );
  778. }
  779. }
  780. }
  781. return totalNumber;
  782. }
  783. function getCompressedAttrs() {
  784. // default: 111---111222---1112------
  785. const range = Range.createIn( root );
  786. return Array.from( range.getItems( { singleCharacters: true } ) )
  787. .map( item => item.getAttribute( 'a' ) || '-' )
  788. .join( '' );
  789. }
  790. describe( 'setAttribute', () => {
  791. it( 'should set the attribute on the range', () => {
  792. batch.setAttribute( 'a', 3, getRange( 3, 6 ) );
  793. expect( spy.callCount ).to.equal( 1 );
  794. expect( getChangesAttrsCount() ).to.equal( 3 );
  795. expect( getCompressedAttrs() ).to.equal( '111333111222---1112------' );
  796. } );
  797. it( 'should split the operations if parts of the range have different attributes', () => {
  798. batch.setAttribute( 'a', 3, getRange( 4, 14 ) );
  799. expect( spy.callCount ).to.equal( 4 );
  800. expect( getChangesAttrsCount() ).to.equal( 10 );
  801. expect( getCompressedAttrs() ).to.equal( '111-3333333333-1112------' );
  802. } );
  803. it( 'should split the operations if parts of the part of the range have the attribute', () => {
  804. batch.setAttribute( 'a', 2, getRange( 4, 14 ) );
  805. expect( spy.callCount ).to.equal( 3 );
  806. expect( getChangesAttrsCount() ).to.equal( 7 );
  807. expect( getCompressedAttrs() ).to.equal( '111-2222222222-1112------' );
  808. } );
  809. it( 'should strip the range if the beginning have the attribute', () => {
  810. batch.setAttribute( 'a', 1, getRange( 1, 5 ) );
  811. expect( spy.callCount ).to.equal( 1 );
  812. expect( getChangesAttrsCount() ).to.equal( 2 );
  813. expect( getCompressedAttrs() ).to.equal( '11111-111222---1112------' );
  814. } );
  815. it( 'should strip the range if the ending have the attribute', () => {
  816. batch.setAttribute( 'a', 1, getRange( 13, 17 ) );
  817. expect( spy.callCount ).to.equal( 1 );
  818. expect( getChangesAttrsCount() ).to.equal( 2 );
  819. expect( getCompressedAttrs() ).to.equal( '111---111222-111112------' );
  820. } );
  821. it( 'should do nothing if the range has attribute', () => {
  822. batch.setAttribute( 'a', 1, getRange( 0, 3 ) );
  823. expect( spy.callCount ).to.equal( 0 );
  824. expect( getCompressedAttrs() ).to.equal( '111---111222---1112------' );
  825. } );
  826. it( 'should not check range\'s start position node when creating operations', () => {
  827. const range = new Range(
  828. new Position( root, [ 18, 1 ] ),
  829. new Position( root, [ 19 ] )
  830. );
  831. batch.setAttribute( 'a', 1, range );
  832. expect( spy.callCount ).to.equal( 1 );
  833. expect( getChangesAttrsCount() ).to.equal( 2 );
  834. expect( getCompressedAttrs() ).to.equal( '111---111222---1112-11---' );
  835. } );
  836. it( 'should not change elements attribute if range contains closing tag', () => {
  837. const range = new Range(
  838. new Position( root, [ 18, 1 ] ),
  839. new Position( root, [ 21 ] )
  840. );
  841. batch.setAttribute( 'a', 1, range );
  842. expect( spy.callCount ).to.equal( 1 );
  843. expect( getChangesAttrsCount() ).to.equal( 4 );
  844. expect( getCompressedAttrs() ).to.equal( '111---111222---1112-1111-' );
  845. } );
  846. it( 'should not create an operation if the range contains only closing tag', () => {
  847. const range = new Range(
  848. new Position( root, [ 18, 3 ] ),
  849. new Position( root, [ 19 ] )
  850. );
  851. batch.setAttribute( 'a', 3, range );
  852. expect( spy.callCount ).to.equal( 0 );
  853. expect( getCompressedAttrs() ).to.equal( '111---111222---1112------' );
  854. } );
  855. it( 'should not create an operation if is collapsed', () => {
  856. batch.setAttribute( 'a', 1, getRange( 3, 3 ) );
  857. expect( spy.callCount ).to.equal( 0 );
  858. expect( getCompressedAttrs() ).to.equal( '111---111222---1112------' );
  859. } );
  860. it( 'should create a proper operations for the mixed range', () => {
  861. batch.setAttribute( 'a', 1, getRange( 0, 20 ) );
  862. expect( spy.callCount ).to.equal( 5 );
  863. expect( getChangesAttrsCount() ).to.equal( 14 );
  864. expect( getCompressedAttrs() ).to.equal( '11111111111111111111111--' );
  865. } );
  866. } );
  867. describe( 'removeAttribute', () => {
  868. it( 'should remove the attribute on the range', () => {
  869. batch.removeAttribute( 'a', getRange( 0, 2 ) );
  870. expect( spy.callCount ).to.equal( 1 );
  871. expect( getChangesAttrsCount() ).to.equal( 2 );
  872. expect( getCompressedAttrs() ).to.equal( '--1---111222---1112------' );
  873. } );
  874. it( 'should split the operations if parts of the range have different attributes', () => {
  875. batch.removeAttribute( 'a', getRange( 7, 11 ) );
  876. expect( spy.callCount ).to.equal( 2 );
  877. expect( getChangesAttrsCount() ).to.equal( 4 );
  878. expect( getCompressedAttrs() ).to.equal( '111---1----2---1112------' );
  879. } );
  880. it( 'should split the operations if parts of the part of the range have no attribute', () => {
  881. batch.removeAttribute( 'a', getRange( 1, 7 ) );
  882. expect( spy.callCount ).to.equal( 2 );
  883. expect( getChangesAttrsCount() ).to.equal( 3 );
  884. expect( getCompressedAttrs() ).to.equal( '1------11222---1112------' );
  885. } );
  886. it( 'should strip the range if the beginning have no attribute', () => {
  887. batch.removeAttribute( 'a', getRange( 4, 12 ) );
  888. expect( spy.callCount ).to.equal( 2 );
  889. expect( getChangesAttrsCount() ).to.equal( 6 );
  890. expect( getCompressedAttrs() ).to.equal( '111------------1112------' );
  891. } );
  892. it( 'should strip the range if the ending have no attribute', () => {
  893. batch.removeAttribute( 'a', getRange( 7, 15 ) );
  894. expect( spy.callCount ).to.equal( 2 );
  895. expect( getChangesAttrsCount() ).to.equal( 5 );
  896. expect( getCompressedAttrs() ).to.equal( '111---1--------1112------' );
  897. } );
  898. it( 'should do nothing if the range has no attribute', () => {
  899. batch.removeAttribute( 'a', getRange( 4, 5 ) );
  900. expect( spy.callCount ).to.equal( 0 );
  901. expect( getCompressedAttrs() ).to.equal( '111---111222---1112------' );
  902. } );
  903. it( 'should not check range\'s start position node when creating operations', () => {
  904. const range = new Range(
  905. new Position( root, [ 18, 3 ] ),
  906. new Position( root, [ 19 ] )
  907. );
  908. batch.removeAttribute( 'a', range );
  909. expect( spy.callCount ).to.equal( 0 );
  910. expect( getChangesAttrsCount() ).to.equal( 0 );
  911. expect( getCompressedAttrs() ).to.equal( '111---111222---1112------' );
  912. } );
  913. it( 'should not apply operation twice in the range contains opening and closing tags', () => {
  914. batch.removeAttribute( 'a', getRange( 18, 22 ) );
  915. expect( spy.callCount ).to.equal( 1 );
  916. expect( getChangesAttrsCount() ).to.equal( 1 );
  917. expect( getCompressedAttrs() ).to.equal( '111---111222---111-------' );
  918. } );
  919. it( 'should not create an operation if range is collapsed', () => {
  920. batch.removeAttribute( 'a', getRange( 3, 3 ) );
  921. expect( spy.callCount ).to.equal( 0 );
  922. expect( getCompressedAttrs() ).to.equal( '111---111222---1112------' );
  923. } );
  924. it( 'should create a proper operations for the mixed range', () => {
  925. batch.removeAttribute( 'a', getRange( 3, 15 ) );
  926. expect( spy.callCount ).to.equal( 2 );
  927. expect( getChangesAttrsCount() ).to.equal( 6 );
  928. expect( getCompressedAttrs() ).to.equal( '111------------1112------' );
  929. } );
  930. } );
  931. } );
  932. describe( 'change attribute on root element', () => {
  933. let p;
  934. beforeEach( () => {
  935. p = batch.createElement( 'p', { a: 3 } );
  936. spy = sinon.spy( doc, 'applyOperation' );
  937. } );
  938. describe( 'setAttribute', () => {
  939. it( 'should create the attribute on root', () => {
  940. batch.setAttribute( 'b', 2, root );
  941. expect( spy.callCount ).to.equal( 1 );
  942. expect( root.getAttribute( 'b' ) ).to.equal( 2 );
  943. } );
  944. it( 'should create the attribute on detached root', () => {
  945. batch.setAttribute( 'b', 2, p );
  946. expect( spy.callCount ).to.equal( 1 );
  947. expect( p.getAttribute( 'b' ) ).to.equal( 2 );
  948. } );
  949. it( 'should change the attribute of root', () => {
  950. batch.setAttribute( 'a', 2, root );
  951. expect( spy.callCount ).to.equal( 1 );
  952. expect( root.getAttribute( 'a' ) ).to.equal( 2 );
  953. } );
  954. it( 'should change the attribute of detached root', () => {
  955. batch.setAttribute( 'a', 2, p );
  956. expect( spy.callCount ).to.equal( 1 );
  957. expect( p.getAttribute( 'a' ) ).to.equal( 2 );
  958. } );
  959. it( 'should do nothing if the attribute value is the same', () => {
  960. batch.setAttribute( 'a', 1, root );
  961. expect( spy.callCount ).to.equal( 1 );
  962. batch.setAttribute( 'a', 1, root );
  963. expect( spy.callCount ).to.equal( 1 );
  964. expect( root.getAttribute( 'a' ) ).to.equal( 1 );
  965. } );
  966. it( 'should do nothing if the attribute value is the same on detached root', () => {
  967. batch.setAttribute( 'a', 1, p );
  968. expect( spy.callCount ).to.equal( 1 );
  969. batch.setAttribute( 'a', 1, p );
  970. expect( spy.callCount ).to.equal( 1 );
  971. expect( p.getAttribute( 'a' ) ).to.equal( 1 );
  972. } );
  973. } );
  974. describe( 'removeAttribute', () => {
  975. it( 'should remove the attribute from root', () => {
  976. batch.setAttribute( 'a', 1, root );
  977. batch.removeAttribute( 'a', root );
  978. expect( spy.callCount ).to.equal( 2 );
  979. expect( root.getAttribute( 'a' ) ).to.be.undefined;
  980. } );
  981. it( 'should do nothing if the attribute is not set', () => {
  982. batch.removeAttribute( 'b', root );
  983. expect( spy.callCount ).to.equal( 0 );
  984. } );
  985. } );
  986. describe( 'clearAttributes', () => {
  987. it( 'should clear attributes from range', () => {
  988. batch.appendText( 'xxx', { a: 1, b: 2, c: 3 }, root );
  989. batch.appendText( 'xxx', root );
  990. batch.appendText( 'xxx', { a: 1 }, root );
  991. batch.appendText( 'xxx', { b: 2 }, root );
  992. batch.appendText( 'xxx', root );
  993. batch.appendElement( 'e', { a: 1 }, root );
  994. batch.appendText( 'xxx', root );
  995. const range = Range.createIn( root );
  996. batch.clearAttributes( range );
  997. let itemsCount = 0;
  998. for ( const item of range.getItems() ) {
  999. itemsCount++;
  1000. expect( Array.from( item.getAttributeKeys() ).length ).to.equal( 0 );
  1001. }
  1002. expect( itemsCount ).to.equal( 3 );
  1003. } );
  1004. it( 'should clear attributes on element', () => {
  1005. const element = batch.createElement( 'x', { a: 1, b: 2, c: 3 }, root );
  1006. expect( Array.from( element.getAttributeKeys() ).length ).to.equal( 3 );
  1007. batch.clearAttributes( element );
  1008. expect( Array.from( element.getAttributeKeys() ).length ).to.equal( 0 );
  1009. } );
  1010. it( 'should clear attributes on root element', () => {
  1011. batch.setAttributes( { a: 1, b: 2, c: 3 }, root );
  1012. expect( Array.from( root.getAttributeKeys() ).length ).to.equal( 3 );
  1013. batch.clearAttributes( root );
  1014. expect( Array.from( root.getAttributeKeys() ).length ).to.equal( 0 );
  1015. } );
  1016. it( 'should do nothing if there are no attributes', () => {
  1017. const element = batch.createElement( 'x' );
  1018. expect( Array.from( element.getAttributeKeys() ).length ).to.equal( 0 );
  1019. batch.clearAttributes( element );
  1020. expect( Array.from( element.getAttributeKeys() ).length ).to.equal( 0 );
  1021. } );
  1022. } );
  1023. } );
  1024. it( 'should not add empty delta to the batch', () => {
  1025. const nodeA = new Element( 'p', { a: 1 } );
  1026. const nodeB = new Element( 'p', { b: 2 } );
  1027. root.insertChildren( 0, [ nodeA, nodeB ] );
  1028. batch.setAttribute( 'a', 1, nodeA );
  1029. expect( batch.deltas.length ).to.equal( 0 );
  1030. batch.removeAttribute( 'x', Range.createIn( root ) );
  1031. expect( batch.deltas.length ).to.equal( 0 );
  1032. } );
  1033. } );
  1034. describe( 'setAttributes()', () => {
  1035. let doc, batch, frag, item;
  1036. beforeEach( () => {
  1037. doc = new Document();
  1038. batch = doc.batch();
  1039. frag = batch.createDocumentFragment();
  1040. item = batch.createText( 'xxx', { b: 2, c: 3 } );
  1041. batch.appendText( 'xxx', { a: 1 }, frag );
  1042. batch.append( item, frag );
  1043. } );
  1044. it( 'should set attributes one by one on range', () => {
  1045. const range = Range.createIn( frag );
  1046. // `setAttribute` is a not trivial operation and is deeply tested above, there is no point to duplicate
  1047. // such a big amount of the same tests, so let's use a spy here.
  1048. const spy = sinon.spy( batch, 'setAttribute' );
  1049. batch.setAttributes( { a: 3, c: null }, range );
  1050. // Verify result.
  1051. expect( Array.from( frag.getChild( 0 ).getAttributes() ) ).to.deep.equal( [ [ 'a', 3 ] ] );
  1052. expect( Array.from( frag.getChild( 1 ).getAttributes() ) ).to.deep.equal( [ [ 'b', 2 ], [ 'a', 3 ] ] );
  1053. // Verify operations
  1054. sinon.assert.calledTwice( spy );
  1055. sinon.assert.calledWith( spy.firstCall, 'a', 3, range );
  1056. sinon.assert.calledWith( spy.secondCall, 'c', null, range );
  1057. } );
  1058. it( 'should set attributes one by one on range for map as attributes list', () => {
  1059. const range = Range.createIn( frag );
  1060. // `setAttribute` is a not trivial operation and is deeply tested above, there is no point to duplicate
  1061. // such a big amount of the same tests, so let's use a spy here.
  1062. const spy = sinon.spy( batch, 'setAttribute' );
  1063. batch.setAttributes( new Map( [ [ 'a', 3 ], [ 'c', null ] ] ), range );
  1064. // Verify result.
  1065. expect( Array.from( frag.getChild( 0 ).getAttributes() ) ).to.deep.equal( [ [ 'a', 3 ] ] );
  1066. expect( Array.from( frag.getChild( 1 ).getAttributes() ) ).to.deep.equal( [ [ 'b', 2 ], [ 'a', 3 ] ] );
  1067. // Verify operations
  1068. sinon.assert.calledTwice( spy );
  1069. sinon.assert.calledWith( spy.firstCall, 'a', 3, range );
  1070. sinon.assert.calledWith( spy.secondCall, 'c', null, range );
  1071. } );
  1072. it( 'should set attributes one by one on item', () => {
  1073. // `setAttribute` is a not trivial operation and is deeply tested above, there is no point to duplicate
  1074. // such a big amount of the same tests, so let's use a spy here.
  1075. const spy = sinon.spy( batch, 'setAttribute' );
  1076. batch.setAttributes( { a: 3, c: null }, item );
  1077. // Verify result.
  1078. expect( Array.from( item.getAttributes() ) ).to.deep.equal( [ [ 'b', 2 ], [ 'a', 3 ] ] );
  1079. // Verify operations
  1080. sinon.assert.calledTwice( spy );
  1081. sinon.assert.calledWith( spy.firstCall, 'a', 3, item );
  1082. sinon.assert.calledWith( spy.secondCall, 'c', null, item );
  1083. } );
  1084. it( 'should set attributes one by one on item for maps as attributes list', () => {
  1085. // `setAttribute` is a not trivial operation and is deeply tested above, there is no point to duplicate
  1086. // such a big amount of the same tests, so let's use a spy here.
  1087. const spy = sinon.spy( batch, 'setAttribute' );
  1088. batch.setAttributes( new Map( [ [ 'a', 3 ], [ 'c', null ] ] ), item );
  1089. // Verify result.
  1090. expect( Array.from( item.getAttributes() ) ).to.deep.equal( [ [ 'b', 2 ], [ 'a', 3 ] ] );
  1091. // Verify operations
  1092. sinon.assert.calledTwice( spy );
  1093. sinon.assert.calledWith( spy.firstCall, 'a', 3, item );
  1094. sinon.assert.calledWith( spy.secondCall, 'c', null, item );
  1095. } );
  1096. } );
  1097. describe( 'merge()', () => {
  1098. let doc, root, p1, p2;
  1099. beforeEach( () => {
  1100. doc = new Document();
  1101. root = doc.createRoot();
  1102. p1 = new Element( 'p', { key1: 'value1' }, new Text( 'foo' ) );
  1103. p2 = new Element( 'p', { key2: 'value2' }, new Text( 'bar' ) );
  1104. root.insertChildren( 0, [ p1, p2 ] );
  1105. } );
  1106. it( 'should merge foo and bar into foobar', () => {
  1107. doc.batch().merge( new Position( root, [ 1 ] ) );
  1108. expect( root.maxOffset ).to.equal( 1 );
  1109. expect( root.getChild( 0 ).name ).to.equal( 'p' );
  1110. expect( root.getChild( 0 ).maxOffset ).to.equal( 6 );
  1111. expect( count( root.getChild( 0 ).getAttributes() ) ).to.equal( 1 );
  1112. expect( root.getChild( 0 ).getAttribute( 'key1' ) ).to.equal( 'value1' );
  1113. expect( root.getChild( 0 ).getChild( 0 ).data ).to.equal( 'foobar' );
  1114. } );
  1115. it( 'should throw if there is no element after', () => {
  1116. expect( () => {
  1117. doc.batch().merge( new Position( root, [ 2 ] ) );
  1118. } ).to.throw( CKEditorError, /^batch-merge-no-element-after/ );
  1119. } );
  1120. it( 'should throw if there is no element before', () => {
  1121. expect( () => {
  1122. doc.batch().merge( new Position( root, [ 0, 2 ] ) );
  1123. } ).to.throw( CKEditorError, /^batch-merge-no-element-before/ );
  1124. } );
  1125. } );
  1126. describe( 'move()', () => {
  1127. let doc, root, range, div, p, batch;
  1128. beforeEach( () => {
  1129. doc = new Document();
  1130. root = doc.createRoot();
  1131. div = new Element( 'div', [], new Text( 'foobar' ) );
  1132. p = new Element( 'p', [], new Text( 'abcxyz' ) );
  1133. div.insertChildren( 0, [ new Element( 'p', [], new Text( 'gggg' ) ) ] );
  1134. div.insertChildren( 2, [ new Element( 'p', [], new Text( 'hhhh' ) ) ] );
  1135. root.insertChildren( 0, [ div, p ] );
  1136. range = new Range( new Position( root, [ 0, 3 ] ), new Position( root, [ 0, 7 ] ) );
  1137. batch = doc.batch();
  1138. } );
  1139. it( 'should move flat range of nodes', () => {
  1140. batch.move( range, new Position( root, [ 1, 3 ] ) );
  1141. expect( getNodesAndText( Range.createIn( root.getChild( 0 ) ) ) ).to.equal( 'PggggPfoPhhhhP' );
  1142. expect( getNodesAndText( Range.createIn( root.getChild( 1 ) ) ) ).to.equal( 'abcobarxyz' );
  1143. } );
  1144. it( 'should throw if object to move is not a range', () => {
  1145. expect( () => {
  1146. doc.batch().move( root.getChild( 0 ), new Position( root, [ 1, 3 ] ) );
  1147. } ).to.throw( CKEditorError, /^batch-move-invalid-range/ );
  1148. } );
  1149. it( 'should throw if given range is not flat', () => {
  1150. const notFlatRange = new Range( new Position( root, [ 0, 2, 2 ] ), new Position( root, [ 0, 6 ] ) );
  1151. expect( () => {
  1152. doc.batch().move( notFlatRange, new Position( root, [ 1, 3 ] ) );
  1153. } ).to.throw( CKEditorError, /^batch-move-range-not-flat/ );
  1154. } );
  1155. it( 'should throw if range is going to be moved to the other document', () => {
  1156. const docFrag = batch.createDocumentFragment();
  1157. expect( () => {
  1158. doc.batch().move( range, docFrag );
  1159. } ).to.throw( CKEditorError, /^batch-move-different-document/ );
  1160. } );
  1161. } );
  1162. describe( 'remove()', () => {
  1163. let doc, batch, div, p, range;
  1164. beforeEach( () => {
  1165. doc = new Document();
  1166. batch = doc.batch();
  1167. div = batch.createElement( 'div' );
  1168. batch.appendText( 'foobar', div );
  1169. p = batch.createElement( 'p' );
  1170. batch.appendText( 'abcxyz', p );
  1171. batch.insertElement( 'p', div );
  1172. batch.appendElement( 'p', div );
  1173. batch.insertText( 'gggg', new Position( div, [ 0, 0 ] ) );
  1174. batch.insertText( 'hhhh', new Position( div, [ 7, 0 ] ) );
  1175. } );
  1176. describe( 'remove from document', () => {
  1177. let root;
  1178. beforeEach( () => {
  1179. root = doc.createRoot();
  1180. batch.append( div, root );
  1181. batch.append( p, root );
  1182. // Reset batch.
  1183. batch = doc.batch();
  1184. // Range starts in ROOT > DIV > P > gg|gg.
  1185. // Range ends in ROOT > DIV > ...|ar.
  1186. range = new Range( new Position( root, [ 0, 0, 2 ] ), new Position( root, [ 0, 5 ] ) );
  1187. } );
  1188. it( 'should remove specified node', () => {
  1189. batch.remove( div );
  1190. expect( root.maxOffset ).to.equal( 1 );
  1191. expect( root.childCount ).to.equal( 1 );
  1192. expect( getNodesAndText( Range.createIn( root.getChild( 0 ) ) ) ).to.equal( 'abcxyz' );
  1193. } );
  1194. it( 'should remove specified text node', () => {
  1195. batch.remove( p.getChild( 0 ) );
  1196. expect( getNodesAndText( Range.createOn( p ) ) ).to.equal( 'PP' );
  1197. } );
  1198. it( 'should remove any range of nodes', () => {
  1199. batch.remove( range );
  1200. expect( getNodesAndText( Range.createIn( root.getChild( 0 ) ) ) ).to.equal( 'PggParPhhhhP' );
  1201. expect( getNodesAndText( Range.createIn( root.getChild( 1 ) ) ) ).to.equal( 'abcxyz' );
  1202. } );
  1203. it( 'should create minimal number of remove deltas, each with only one operation', () => {
  1204. batch.remove( range );
  1205. expect( batch.deltas.length ).to.equal( 2 );
  1206. expect( batch.deltas[ 0 ].operations.length ).to.equal( 1 );
  1207. expect( batch.deltas[ 1 ].operations.length ).to.equal( 1 );
  1208. } );
  1209. it( 'should use RemoveOperation', () => {
  1210. batch.remove( div );
  1211. expect( batch.deltas[ 0 ].operations[ 0 ].type ).to.equal( 'remove' );
  1212. } );
  1213. } );
  1214. describe( 'remove from document fragment', () => {
  1215. let frag;
  1216. beforeEach( () => {
  1217. frag = batch.createDocumentFragment();
  1218. batch.append( div, frag );
  1219. batch.append( p, frag );
  1220. // Reset batch.
  1221. batch = doc.batch();
  1222. // Range starts in FRAG > DIV > P > gg|gg.
  1223. // Range ends in FRAG > DIV > ...|ar.
  1224. range = new Range( new Position( frag, [ 0, 0, 2 ] ), new Position( frag, [ 0, 5 ] ) );
  1225. } );
  1226. it( 'should remove specified node', () => {
  1227. batch.remove( div );
  1228. expect( frag.maxOffset ).to.equal( 1 );
  1229. expect( frag.childCount ).to.equal( 1 );
  1230. expect( getNodesAndText( Range.createIn( frag.getChild( 0 ) ) ) ).to.equal( 'abcxyz' );
  1231. } );
  1232. it( 'should remove specified text node', () => {
  1233. batch.remove( p.getChild( 0 ) );
  1234. expect( getNodesAndText( Range.createOn( p ) ) ).to.equal( 'PP' );
  1235. } );
  1236. it( 'should remove any range of nodes', () => {
  1237. batch.remove( range );
  1238. expect( getNodesAndText( Range.createIn( frag.getChild( 0 ) ) ) ).to.equal( 'PggParPhhhhP' );
  1239. expect( getNodesAndText( Range.createIn( frag.getChild( 1 ) ) ) ).to.equal( 'abcxyz' );
  1240. } );
  1241. it( 'should create minimal number of remove deltas, each with only one operation', () => {
  1242. batch.remove( range );
  1243. expect( batch.deltas.length ).to.equal( 2 );
  1244. expect( batch.deltas[ 0 ].operations.length ).to.equal( 1 );
  1245. expect( batch.deltas[ 1 ].operations.length ).to.equal( 1 );
  1246. } );
  1247. it( 'should use DetachOperation', () => {
  1248. batch.remove( div );
  1249. expect( batch.deltas[ 0 ].operations[ 0 ].type ).to.equal( 'detach' );
  1250. } );
  1251. } );
  1252. } );
  1253. describe( 'rename()', () => {
  1254. let doc, root, batch;
  1255. beforeEach( () => {
  1256. doc = new Document();
  1257. root = doc.createRoot();
  1258. const p = new Element( 'p', null, new Text( 'abc' ) );
  1259. root.appendChildren( p );
  1260. batch = doc.batch();
  1261. batch.rename( p, 'h' );
  1262. } );
  1263. it( 'should rename given element', () => {
  1264. expect( root.maxOffset ).to.equal( 1 );
  1265. expect( root.getChild( 0 ) ).to.have.property( 'name', 'h' );
  1266. } );
  1267. it( 'should throw if not an Element instance is passed', () => {
  1268. expect( () => {
  1269. batch.rename( new Text( 'abc' ), 'h' );
  1270. } ).to.throw( CKEditorError, /^batch-rename-not-element-instance/ );
  1271. } );
  1272. } );
  1273. describe( 'split()', () => {
  1274. let doc, root, p;
  1275. beforeEach( () => {
  1276. doc = new Document();
  1277. root = doc.createRoot();
  1278. p = new Element( 'p', { key: 'value' }, new Text( 'foobar' ) );
  1279. root.insertChildren( 0, p );
  1280. } );
  1281. it( 'should split foobar to foo and bar', () => {
  1282. doc.batch().split( new Position( root, [ 0, 3 ] ) );
  1283. expect( root.maxOffset ).to.equal( 2 );
  1284. expect( root.getChild( 0 ).name ).to.equal( 'p' );
  1285. expect( root.getChild( 0 ).maxOffset ).to.equal( 3 );
  1286. expect( count( root.getChild( 0 ).getAttributes() ) ).to.equal( 1 );
  1287. expect( root.getChild( 0 ).getAttribute( 'key' ) ).to.equal( 'value' );
  1288. expect( root.getChild( 0 ).getChild( 0 ).data ).to.equal( 'foo' );
  1289. expect( root.getChild( 1 ).name ).to.equal( 'p' );
  1290. expect( root.getChild( 1 ).maxOffset ).to.equal( 3 );
  1291. expect( count( root.getChild( 1 ).getAttributes() ) ).to.equal( 1 );
  1292. expect( root.getChild( 1 ).getAttribute( 'key' ) ).to.equal( 'value' );
  1293. expect( root.getChild( 1 ).getChild( 0 ).data ).to.equal( 'bar' );
  1294. } );
  1295. it( 'should create an empty paragraph if we split at the end', () => {
  1296. doc.batch().split( new Position( root, [ 0, 6 ] ) );
  1297. expect( root.maxOffset ).to.equal( 2 );
  1298. expect( root.getChild( 0 ).name ).to.equal( 'p' );
  1299. expect( root.getChild( 0 ).maxOffset ).to.equal( 6 );
  1300. expect( count( root.getChild( 0 ).getAttributes() ) ).to.equal( 1 );
  1301. expect( root.getChild( 0 ).getAttribute( 'key' ) ).to.equal( 'value' );
  1302. expect( root.getChild( 0 ).getChild( 0 ).data ).to.equal( 'foobar' );
  1303. expect( root.getChild( 1 ).name ).to.equal( 'p' );
  1304. expect( root.getChild( 1 ).maxOffset ).to.equal( 0 );
  1305. expect( count( root.getChild( 1 ).getAttributes() ) ).to.equal( 1 );
  1306. expect( root.getChild( 1 ).getAttribute( 'key' ) ).to.equal( 'value' );
  1307. } );
  1308. it( 'should throw if we try to split a root', () => {
  1309. expect( () => {
  1310. doc.batch().split( new Position( root, [ 0 ] ) );
  1311. } ).to.throw( CKEditorError, /^batch-split-element-no-parent/ );
  1312. } );
  1313. it( 'should throw if we try to split an element with no parent', () => {
  1314. const batch = doc.batch();
  1315. expect( () => {
  1316. const element = batch.createElement( 'p' );
  1317. batch.split( new Position( element, [ 0 ] ) );
  1318. } ).to.throw( CKEditorError, /^batch-split-element-no-parent/ );
  1319. } );
  1320. it( 'should throw if we try to split a document fragment', () => {
  1321. const batch = doc.batch();
  1322. expect( () => {
  1323. const documentFragment = batch.createDocumentFragment();
  1324. batch.split( new Position( documentFragment, [ 0 ] ) );
  1325. } ).to.throw( CKEditorError, /^batch-split-element-no-parent/ );
  1326. } );
  1327. } );
  1328. describe( 'wrap()', () => {
  1329. let doc, root, range;
  1330. beforeEach( () => {
  1331. doc = new Document();
  1332. root = doc.createRoot();
  1333. root.insertChildren( 0, new Text( 'foobar' ) );
  1334. range = new Range( new Position( root, [ 2 ] ), new Position( root, [ 4 ] ) );
  1335. } );
  1336. it( 'should wrap flat range with given element', () => {
  1337. const p = new Element( 'p' );
  1338. doc.batch().wrap( range, p );
  1339. expect( root.maxOffset ).to.equal( 5 );
  1340. expect( root.getChild( 0 ).data ).to.equal( 'fo' );
  1341. expect( root.getChild( 1 ) ).to.equal( p );
  1342. expect( p.getChild( 0 ).data ).to.equal( 'ob' );
  1343. expect( root.getChild( 2 ).data ).to.equal( 'ar' );
  1344. } );
  1345. it( 'should wrap flat range with an element of given name', () => {
  1346. doc.batch().wrap( range, 'p' );
  1347. expect( root.maxOffset ).to.equal( 5 );
  1348. expect( root.getChild( 0 ).data ).to.equal( 'fo' );
  1349. expect( root.getChild( 1 ).name ).to.equal( 'p' );
  1350. expect( root.getChild( 1 ).getChild( 0 ).data ).to.equal( 'ob' );
  1351. expect( root.getChild( 2 ).data ).to.equal( 'ar' );
  1352. } );
  1353. it( 'should throw if range to wrap is not flat', () => {
  1354. root.insertChildren( 1, [ new Element( 'p', [], new Text( 'xyz' ) ) ] );
  1355. const notFlatRange = new Range( new Position( root, [ 3 ] ), new Position( root, [ 6, 2 ] ) );
  1356. expect( () => {
  1357. doc.batch().wrap( notFlatRange, 'p' );
  1358. } ).to.throw( CKEditorError, /^batch-wrap-range-not-flat/ );
  1359. } );
  1360. it( 'should throw if element to wrap with has children #1', () => {
  1361. const p = new Element( 'p', [], new Text( 'a' ) );
  1362. expect( () => {
  1363. doc.batch().wrap( range, p );
  1364. } ).to.throw( CKEditorError, /^batch-wrap-element-not-empty/ );
  1365. } );
  1366. it( 'should throw if element to wrap with has children #2', () => {
  1367. const p = new Element( 'p' );
  1368. root.insertChildren( 0, p );
  1369. expect( () => {
  1370. doc.batch().wrap( range, p );
  1371. } ).to.throw( CKEditorError, /^batch-wrap-element-attached/ );
  1372. } );
  1373. } );
  1374. describe( 'unwrap()', () => {
  1375. let doc, root, p;
  1376. beforeEach( () => {
  1377. doc = new Document();
  1378. root = doc.createRoot();
  1379. p = new Element( 'p', [], new Text( 'xyz' ) );
  1380. root.insertChildren( 0, [ new Text( 'a' ), p, new Text( 'b' ) ] );
  1381. } );
  1382. it( 'should unwrap given element', () => {
  1383. doc.batch().unwrap( p );
  1384. expect( root.maxOffset ).to.equal( 5 );
  1385. expect( root.getChild( 0 ).data ).to.equal( 'axyzb' );
  1386. } );
  1387. it( 'should throw if element to unwrap has no parent', () => {
  1388. const element = new Element( 'p' );
  1389. expect( () => {
  1390. doc.batch().unwrap( element );
  1391. } ).to.throw( CKEditorError, /^batch-unwrap-element-no-parent/ );
  1392. } );
  1393. } );
  1394. describe( 'setMarker()', () => {
  1395. let doc, root, range;
  1396. beforeEach( () => {
  1397. doc = new Document();
  1398. root = doc.createRoot();
  1399. root.appendChildren( new Text( 'foo' ) );
  1400. range = Range.createIn( root );
  1401. } );
  1402. it( 'should add marker to the document marker collection', () => {
  1403. doc.batch().setMarker( 'name', range );
  1404. expect( doc.markers.get( 'name' ).getRange().isEqual( range ) ).to.be.true;
  1405. } );
  1406. it( 'should update marker in the document marker collection', () => {
  1407. doc.batch().setMarker( 'name', range );
  1408. const range2 = Range.createFromParentsAndOffsets( root, 0, root, 0 );
  1409. doc.batch().setMarker( 'name', range2 );
  1410. expect( doc.markers.get( 'name' ).getRange().isEqual( range2 ) ).to.be.true;
  1411. } );
  1412. it( 'should accept marker instance', () => {
  1413. doc.batch().setMarker( 'name', range );
  1414. const marker = doc.markers.get( 'name' );
  1415. const range2 = Range.createFromParentsAndOffsets( root, 0, root, 0 );
  1416. const batch = doc.batch();
  1417. batch.setMarker( marker, range2 );
  1418. const op = batch.deltas[ 0 ].operations[ 0 ];
  1419. expect( doc.markers.get( 'name' ).getRange().isEqual( range2 ) ).to.be.true;
  1420. expect( op.oldRange.isEqual( range ) ).to.be.true;
  1421. expect( op.newRange.isEqual( range2 ) ).to.be.true;
  1422. } );
  1423. it( 'should accept empty range parameter if marker instance is passed', () => {
  1424. const marker = doc.markers.set( 'name', range );
  1425. sinon.spy( doc, 'fire' );
  1426. doc.on( 'change', ( evt, type, changes ) => {
  1427. if ( type == 'marker' ) {
  1428. expect( changes.type ).to.equal( 'set' );
  1429. expect( changes.name ).to.equal( 'name' );
  1430. }
  1431. } );
  1432. const batch = doc.batch();
  1433. batch.setMarker( marker );
  1434. const op = batch.deltas[ 0 ].operations[ 0 ];
  1435. expect( doc.fire.calledWith( 'change', 'marker' ) ).to.be.true;
  1436. expect( op.oldRange ).to.be.null;
  1437. expect( op.newRange.isEqual( range ) ).to.be.true;
  1438. } );
  1439. it( 'should throw if marker with given name does not exist and range is not passed', () => {
  1440. expect( () => {
  1441. doc.batch().setMarker( 'name' );
  1442. } ).to.throw( CKEditorError, /^batch-setMarker-no-range/ );
  1443. } );
  1444. } );
  1445. describe( 'removeMarker()', () => {
  1446. let doc, root, range;
  1447. beforeEach( () => {
  1448. doc = new Document();
  1449. root = doc.createRoot();
  1450. root.appendChildren( new Text( 'foo' ) );
  1451. range = Range.createIn( root );
  1452. } );
  1453. it( 'should remove marker from the document marker collection', () => {
  1454. doc.batch().setMarker( 'name', range );
  1455. doc.batch().removeMarker( 'name' );
  1456. expect( doc.markers.get( 'name' ) ).to.be.null;
  1457. } );
  1458. it( 'should throw when trying to remove non existing marker', () => {
  1459. expect( () => {
  1460. doc.batch().removeMarker( 'name' );
  1461. } ).to.throw( CKEditorError, /^batch-removeMarker-no-marker/ );
  1462. } );
  1463. it( 'should accept marker instance', () => {
  1464. doc.batch().setMarker( 'name', range );
  1465. const marker = doc.markers.get( 'name' );
  1466. doc.batch().removeMarker( marker );
  1467. expect( doc.markers.get( 'name' ) ).to.be.null;
  1468. } );
  1469. } );
  1470. } );