8
0

batch.js 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922
  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( node, 'b', 2 );
  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( node, 'a', 2 );
  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( text, 'b', 2 );
  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( text, 'a', 2 );
  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( node, 'a', 1 );
  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( node, 'a' );
  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( text, 'a' );
  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( node, 'b' );
  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( getRange( 3, 6 ), 'a', 3 );
  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( getRange( 4, 14 ), 'a', 3 );
  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( getRange( 4, 14 ), 'a', 2 );
  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( getRange( 1, 5 ), 'a', 1 );
  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( getRange( 13, 17 ), 'a', 1 );
  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( getRange( 0, 3 ), 'a', 1 );
  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( range, 'a', 1 );
  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( range, 'a', 1 );
  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( range, 'a', 3 );
  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( getRange( 3, 3 ), 'a', 1 );
  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( getRange( 0, 20 ), 'a', 1 );
  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( getRange( 0, 2 ), 'a' );
  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( getRange( 7, 11 ), 'a' );
  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( getRange( 1, 7 ), 'a' );
  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( getRange( 4, 12 ), 'a' );
  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( getRange( 7, 15 ), 'a' );
  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( getRange( 4, 5 ), 'a' );
  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( range, 'a' );
  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( getRange( 18, 22 ), 'a' );
  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( getRange( 3, 3 ), 'a' );
  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( getRange( 3, 15 ), 'a' );
  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( root, 'b', 2 );
  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( p, 'b', 2 );
  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( root, 'a', 2 );
  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( p, 'a', 2 );
  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( root, 'a', 1 );
  961. expect( spy.callCount ).to.equal( 1 );
  962. batch.setAttribute( root, 'a', 1 );
  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( p, 'a', 1 );
  968. expect( spy.callCount ).to.equal( 1 );
  969. batch.setAttribute( p, 'a', 1 );
  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( root, 'a', 1 );
  977. batch.removeAttribute( root, 'a' );
  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( root, 'b' );
  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( root, { a: 1, b: 2, c: 3 } );
  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( nodeA, 'a', 1 );
  1029. expect( batch.deltas.length ).to.equal( 0 );
  1030. batch.removeAttribute( Range.createIn( root ), 'x' );
  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( range, { a: 3, c: null } );
  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, range, 'a', 3 );
  1056. sinon.assert.calledWith( spy.secondCall, range, 'c', null );
  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( range, new Map( [ [ 'a', 3 ], [ 'c', null ] ] ) );
  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, range, 'a', 3 );
  1070. sinon.assert.calledWith( spy.secondCall, range, 'c', null );
  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( item, { a: 3, c: null } );
  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, item, 'a', 3 );
  1082. sinon.assert.calledWith( spy.secondCall, item, 'c', null );
  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( item, new Map( [ [ 'a', 3 ], [ 'c', null ] ] ) );
  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, item, 'a', 3 );
  1094. sinon.assert.calledWith( spy.secondCall, item, 'c', null );
  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-root/ );
  1312. } );
  1313. } );
  1314. describe( 'wrap()', () => {
  1315. let doc, root, range;
  1316. beforeEach( () => {
  1317. doc = new Document();
  1318. root = doc.createRoot();
  1319. root.insertChildren( 0, new Text( 'foobar' ) );
  1320. range = new Range( new Position( root, [ 2 ] ), new Position( root, [ 4 ] ) );
  1321. } );
  1322. it( 'should wrap flat range with given element', () => {
  1323. const p = new Element( 'p' );
  1324. doc.batch().wrap( range, p );
  1325. expect( root.maxOffset ).to.equal( 5 );
  1326. expect( root.getChild( 0 ).data ).to.equal( 'fo' );
  1327. expect( root.getChild( 1 ) ).to.equal( p );
  1328. expect( p.getChild( 0 ).data ).to.equal( 'ob' );
  1329. expect( root.getChild( 2 ).data ).to.equal( 'ar' );
  1330. } );
  1331. it( 'should wrap flat range with an element of given name', () => {
  1332. doc.batch().wrap( range, 'p' );
  1333. expect( root.maxOffset ).to.equal( 5 );
  1334. expect( root.getChild( 0 ).data ).to.equal( 'fo' );
  1335. expect( root.getChild( 1 ).name ).to.equal( 'p' );
  1336. expect( root.getChild( 1 ).getChild( 0 ).data ).to.equal( 'ob' );
  1337. expect( root.getChild( 2 ).data ).to.equal( 'ar' );
  1338. } );
  1339. it( 'should throw if range to wrap is not flat', () => {
  1340. root.insertChildren( 1, [ new Element( 'p', [], new Text( 'xyz' ) ) ] );
  1341. const notFlatRange = new Range( new Position( root, [ 3 ] ), new Position( root, [ 6, 2 ] ) );
  1342. expect( () => {
  1343. doc.batch().wrap( notFlatRange, 'p' );
  1344. } ).to.throw( CKEditorError, /^batch-wrap-range-not-flat/ );
  1345. } );
  1346. it( 'should throw if element to wrap with has children #1', () => {
  1347. const p = new Element( 'p', [], new Text( 'a' ) );
  1348. expect( () => {
  1349. doc.batch().wrap( range, p );
  1350. } ).to.throw( CKEditorError, /^batch-wrap-element-not-empty/ );
  1351. } );
  1352. it( 'should throw if element to wrap with has children #2', () => {
  1353. const p = new Element( 'p' );
  1354. root.insertChildren( 0, p );
  1355. expect( () => {
  1356. doc.batch().wrap( range, p );
  1357. } ).to.throw( CKEditorError, /^batch-wrap-element-attached/ );
  1358. } );
  1359. } );
  1360. describe( 'unwrap()', () => {
  1361. let doc, root, p;
  1362. beforeEach( () => {
  1363. doc = new Document();
  1364. root = doc.createRoot();
  1365. p = new Element( 'p', [], new Text( 'xyz' ) );
  1366. root.insertChildren( 0, [ new Text( 'a' ), p, new Text( 'b' ) ] );
  1367. } );
  1368. it( 'should unwrap given element', () => {
  1369. doc.batch().unwrap( p );
  1370. expect( root.maxOffset ).to.equal( 5 );
  1371. expect( root.getChild( 0 ).data ).to.equal( 'axyzb' );
  1372. } );
  1373. it( 'should throw if element to unwrap has no parent', () => {
  1374. const element = new Element( 'p' );
  1375. expect( () => {
  1376. doc.batch().unwrap( element );
  1377. } ).to.throw( CKEditorError, /^batch-unwrap-element-no-parent/ );
  1378. } );
  1379. } );
  1380. describe( 'setMarker()', () => {
  1381. let doc, root, range;
  1382. beforeEach( () => {
  1383. doc = new Document();
  1384. root = doc.createRoot();
  1385. root.appendChildren( new Text( 'foo' ) );
  1386. range = Range.createIn( root );
  1387. } );
  1388. it( 'should add marker to the document marker collection', () => {
  1389. doc.batch().setMarker( 'name', range );
  1390. expect( doc.markers.get( 'name' ).getRange().isEqual( range ) ).to.be.true;
  1391. } );
  1392. it( 'should update marker in the document marker collection', () => {
  1393. doc.batch().setMarker( 'name', range );
  1394. const range2 = Range.createFromParentsAndOffsets( root, 0, root, 0 );
  1395. doc.batch().setMarker( 'name', range2 );
  1396. expect( doc.markers.get( 'name' ).getRange().isEqual( range2 ) ).to.be.true;
  1397. } );
  1398. it( 'should accept marker instance', () => {
  1399. doc.batch().setMarker( 'name', range );
  1400. const marker = doc.markers.get( 'name' );
  1401. const range2 = Range.createFromParentsAndOffsets( root, 0, root, 0 );
  1402. const batch = doc.batch();
  1403. batch.setMarker( marker, range2 );
  1404. const op = batch.deltas[ 0 ].operations[ 0 ];
  1405. expect( doc.markers.get( 'name' ).getRange().isEqual( range2 ) ).to.be.true;
  1406. expect( op.oldRange.isEqual( range ) ).to.be.true;
  1407. expect( op.newRange.isEqual( range2 ) ).to.be.true;
  1408. } );
  1409. it( 'should accept empty range parameter if marker instance is passed', () => {
  1410. const marker = doc.markers.set( 'name', range );
  1411. sinon.spy( doc, 'fire' );
  1412. doc.on( 'change', ( evt, type, changes ) => {
  1413. if ( type == 'marker' ) {
  1414. expect( changes.type ).to.equal( 'set' );
  1415. expect( changes.name ).to.equal( 'name' );
  1416. }
  1417. } );
  1418. const batch = doc.batch();
  1419. batch.setMarker( marker );
  1420. const op = batch.deltas[ 0 ].operations[ 0 ];
  1421. expect( doc.fire.calledWith( 'change', 'marker' ) ).to.be.true;
  1422. expect( op.oldRange ).to.be.null;
  1423. expect( op.newRange.isEqual( range ) ).to.be.true;
  1424. } );
  1425. it( 'should throw if marker with given name does not exist and range is not passed', () => {
  1426. expect( () => {
  1427. doc.batch().setMarker( 'name' );
  1428. } ).to.throw( CKEditorError, /^batch-setMarker-no-range/ );
  1429. } );
  1430. } );
  1431. describe( 'removeMarker()', () => {
  1432. let doc, root, range;
  1433. beforeEach( () => {
  1434. doc = new Document();
  1435. root = doc.createRoot();
  1436. root.appendChildren( new Text( 'foo' ) );
  1437. range = Range.createIn( root );
  1438. } );
  1439. it( 'should remove marker from the document marker collection', () => {
  1440. doc.batch().setMarker( 'name', range );
  1441. doc.batch().removeMarker( 'name' );
  1442. expect( doc.markers.get( 'name' ) ).to.be.null;
  1443. } );
  1444. it( 'should throw when trying to remove non existing marker', () => {
  1445. expect( () => {
  1446. doc.batch().removeMarker( 'name' );
  1447. } ).to.throw( CKEditorError, /^batch-removeMarker-no-marker/ );
  1448. } );
  1449. it( 'should accept marker instance', () => {
  1450. doc.batch().setMarker( 'name', range );
  1451. const marker = doc.markers.get( 'name' );
  1452. doc.batch().removeMarker( marker );
  1453. expect( doc.markers.get( 'name' ) ).to.be.null;
  1454. } );
  1455. } );
  1456. } );