8
0

writer.js 62 KB

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