8
0

writer.js 63 KB

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