8
0

batch.js 63 KB

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