8
0

batch.js 58 KB

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