writer.js 68 KB

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