writer.js 99 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import Model from '../../src/model/model';
  6. import Writer from '../../src/model/writer';
  7. import Batch from '../../src/model/batch';
  8. import InsertOperation from '../../src/model/operation/insertoperation';
  9. import DocumentFragment from '../../src/model/documentfragment';
  10. import Element from '../../src/model/element';
  11. import Text from '../../src/model/text';
  12. import Position from '../../src/model/position';
  13. import Range from '../../src/model/range';
  14. import count from '@ckeditor/ckeditor5-utils/src/count';
  15. import { getNodesAndText } from '../../tests/model/_utils/utils';
  16. import DocumentSelection from '../../src/model/documentselection';
  17. import { expectToThrowCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_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 do nothing if empty text node is being inserted', () => {
  123. const parent = createDocumentFragment();
  124. model.enqueueChange( batch, writer => {
  125. const text = writer.createText( '' );
  126. writer.insert( text, parent );
  127. } );
  128. expect( parent.childCount ).to.equal( 0 );
  129. } );
  130. it( 'should create proper operation for inserting element', () => {
  131. const parent = createDocumentFragment();
  132. const element = createElement( 'child' );
  133. const spy = sinon.spy( model, 'applyOperation' );
  134. insert( element, parent );
  135. sinon.assert.calledOnce( spy );
  136. expect( spy.lastCall.args[ 0 ].type ).to.equal( 'insert' );
  137. expect( spy.lastCall.args[ 0 ] ).to.instanceof( InsertOperation );
  138. expect( spy.lastCall.args[ 0 ].shouldReceiveAttributes ).to.be.false;
  139. expect( spy.lastCall.args[ 0 ].batch ).to.equal( batch );
  140. } );
  141. it( 'should create proper operation for inserting text', () => {
  142. const parent = createDocumentFragment();
  143. const text = createText( 'child' );
  144. const spy = sinon.spy( model, 'applyOperation' );
  145. insert( text, parent );
  146. sinon.assert.calledOnce( spy );
  147. expect( spy.lastCall.args[ 0 ].type ).to.equal( 'insert' );
  148. expect( spy.lastCall.args[ 0 ] ).to.instanceof( InsertOperation );
  149. expect( spy.lastCall.args[ 0 ].shouldReceiveAttributes ).to.be.true;
  150. expect( spy.lastCall.args[ 0 ].batch ).to.equal( batch );
  151. } );
  152. it( 'should move element from one parent to the other within the same document (rootA -> rootA)', () => {
  153. const root = doc.createRoot();
  154. const parent1 = createElement( 'parent' );
  155. const parent2 = createElement( 'parent' );
  156. const node = createText( 'foo' );
  157. insert( node, parent1 );
  158. insert( parent1, root );
  159. insert( parent2, root );
  160. const spy = sinon.spy( model, 'applyOperation' );
  161. insert( node, parent2 );
  162. // Verify result.
  163. expect( Array.from( parent1.getChildren() ) ).to.deep.equal( [] );
  164. expect( Array.from( parent2.getChildren() ) ).to.deep.equal( [ node ] );
  165. // Verify operations.
  166. sinon.assert.calledOnce( spy );
  167. expect( spy.firstCall.args[ 0 ].type ).to.equal( 'move' );
  168. expect( spy.firstCall.args[ 0 ].batch ).to.equal( batch );
  169. } );
  170. it( 'should move element from one parent to the other within the same document (rootA -> rootB)', () => {
  171. const rootA = doc.createRoot( '$root', 'A' );
  172. const rootB = doc.createRoot( '$root', 'B' );
  173. const node = createText( 'foo' );
  174. insert( node, rootA );
  175. const spy = sinon.spy( model, 'applyOperation' );
  176. insert( node, rootB );
  177. // Verify result.
  178. expect( Array.from( rootA.getChildren() ) ).to.deep.equal( [] );
  179. expect( Array.from( rootB.getChildren() ) ).to.deep.equal( [ node ] );
  180. // Verify operations.
  181. sinon.assert.calledOnce( spy );
  182. expect( spy.firstCall.args[ 0 ].type ).to.equal( 'move' );
  183. expect( spy.firstCall.args[ 0 ].batch ).to.equal( batch );
  184. } );
  185. it( 'should move element from one parent to the other within the same document (docFragA -> docFragA)', () => {
  186. const docFragA = createDocumentFragment();
  187. const parent1 = createElement( 'parent' );
  188. const parent2 = createElement( 'parent' );
  189. const node = createText( 'foo' );
  190. insert( node, parent1 );
  191. insert( parent1, docFragA );
  192. insert( parent2, docFragA );
  193. const spy = sinon.spy( model, 'applyOperation' );
  194. insert( node, parent2 );
  195. // Verify result.
  196. expect( Array.from( parent1.getChildren() ) ).to.deep.equal( [] );
  197. expect( Array.from( parent2.getChildren() ) ).to.deep.equal( [ node ] );
  198. // Verify operations.
  199. sinon.assert.calledOnce( spy );
  200. expect( spy.firstCall.args[ 0 ].type ).to.equal( 'move' );
  201. expect( spy.firstCall.args[ 0 ].batch ).to.equal( batch );
  202. } );
  203. it( 'should move element from one parent to the other within different document (docFragA -> docFragB)', () => {
  204. const docFragA = createDocumentFragment();
  205. const docFragB = createDocumentFragment();
  206. const node = createText( 'foo' );
  207. insert( node, docFragA );
  208. const spy = sinon.spy( model, 'applyOperation' );
  209. insert( node, docFragB );
  210. // Verify result.
  211. expect( Array.from( docFragA ) ).to.deep.equal( [] );
  212. expect( Array.from( docFragB ) ).to.deep.equal( [ node ] );
  213. // Verify operations.
  214. sinon.assert.calledTwice( spy );
  215. expect( spy.firstCall.args[ 0 ].type ).to.equal( 'detach' );
  216. expect( spy.firstCall.args[ 0 ].batch ).to.equal( batch );
  217. expect( spy.secondCall.args[ 0 ].type ).to.equal( 'insert' );
  218. expect( spy.secondCall.args[ 0 ].batch ).to.equal( batch );
  219. } );
  220. it( 'should throw when moving element from document to document fragment', () => {
  221. const root = doc.createRoot();
  222. const docFrag = createDocumentFragment();
  223. const node = createText( 'foo' );
  224. insert( node, root );
  225. expectToThrowCKEditorError( () => {
  226. insert( node, docFrag );
  227. }, /^model-writer-insert-forbidden-move/, model );
  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 modelMarker = model.markers.get( 'marker' );
  240. const range = modelMarker.getRange();
  241. expect( range.root ).to.equal( root );
  242. expect( range.start.path ).to.deep.equal( [ 2, 1 ] );
  243. expect( range.end.path ).to.deep.equal( [ 2, 5 ] );
  244. expect( modelMarker.managedUsingOperations ).to.be.true;
  245. expect( modelMarker.affectsData ).to.be.true;
  246. } );
  247. // https://github.com/ckeditor/ckeditor5-engine/issues/1721.
  248. it( 'should update a marker if DocumentFragment has a marker that is already in the model (markers have the same name)', () => {
  249. const root = doc.createRoot();
  250. const docFrag = createDocumentFragment();
  251. // <root><p></p><p>[ab]cd</p></root>.
  252. appendText( 'abcd', root );
  253. // <docFrag><p>f[oo b]ar</p></docFrag>.
  254. appendElement( 'p', docFrag );
  255. insertText( 'foo bar', new Position( docFrag, [ 0, 0 ] ) );
  256. model.change( writer => {
  257. const range = new Range( new Position( root, [ 1, 0 ] ), new Position( root, [ 1, 2 ] ) );
  258. writer.addMarker( 'marker', { range, usingOperation: true } );
  259. } );
  260. docFrag.markers.set( 'marker', new Range( new Position( docFrag, [ 0, 1 ] ), new Position( docFrag, [ 0, 5 ] ) ) );
  261. insert( docFrag, new Position( root, [ 2 ] ) );
  262. expect( Array.from( model.markers ).length ).to.equal( 1 );
  263. const modelMarker = model.markers.get( 'marker' );
  264. const range = modelMarker.getRange();
  265. expect( range.root ).to.equal( root );
  266. expect( range.start.path ).to.deep.equal( [ 2, 1 ] );
  267. expect( range.end.path ).to.deep.equal( [ 2, 5 ] );
  268. expect( modelMarker.managedUsingOperations ).to.be.true;
  269. expect( modelMarker.affectsData ).to.be.true;
  270. } );
  271. it( 'should throw when trying to use detached writer', () => {
  272. const writer = new Writer( model, batch );
  273. const root = doc.createRoot();
  274. const node = createText( 'foo' );
  275. expectToThrowCKEditorError( () => {
  276. writer.insert( node, root );
  277. }, /^writer-incorrect-use/, model );
  278. } );
  279. } );
  280. describe( 'insertText()', () => {
  281. it( 'should create and insert text node with attributes at given position', () => {
  282. const parent = createDocumentFragment();
  283. insertText( 'foo', { bar: 'biz' }, new Position( parent, [ 0 ] ) );
  284. expect( parent.childCount ).to.equal( 1 );
  285. expect( parent.getChild( 0 ) ).to.instanceof( Text );
  286. expect( parent.getChild( 0 ).data ).to.equal( 'foo' );
  287. expect( Array.from( parent.getChild( 0 ).getAttributes() ) ).to.deep.equal( [ [ 'bar', 'biz' ] ] );
  288. } );
  289. it( 'should create and insert text node with no attributes at given position', () => {
  290. const parent = createDocumentFragment();
  291. insertText( 'foo', null, new Position( parent, [ 0 ] ) );
  292. expect( parent.childCount ).to.equal( 1 );
  293. expect( parent.getChild( 0 ) ).to.instanceof( Text );
  294. expect( parent.getChild( 0 ).data ).to.equal( 'foo' );
  295. expect( Array.from( parent.getChild( 0 ).getAttributes() ) ).to.deep.equal( [] );
  296. } );
  297. it( 'should create and insert text node omitting attributes param', () => {
  298. const parent = createDocumentFragment();
  299. insertText( 'foo', new Position( parent, [ 0 ] ) );
  300. expect( parent.childCount ).to.equal( 1 );
  301. expect( parent.getChild( 0 ) ).to.instanceof( Text );
  302. expect( parent.getChild( 0 ).data ).to.equal( 'foo' );
  303. expect( Array.from( parent.getChild( 0 ).getAttributes() ) ).to.deep.equal( [] );
  304. } );
  305. it( 'should create and insert text node at the beginning of given element', () => {
  306. const parent = createDocumentFragment();
  307. insert( createElement( 'child' ), parent );
  308. insertText( 'foo', parent );
  309. expect( parent.childCount ).to.equal( 2 );
  310. expect( parent.getChild( 0 ) ).to.instanceof( Text );
  311. expect( parent.getChild( 1 ) ).to.instanceof( Element );
  312. } );
  313. it( 'should create and insert text node at the end of given element', () => {
  314. const parent = createDocumentFragment();
  315. insert( createElement( 'child' ), parent );
  316. insertText( 'foo', parent, 'end' );
  317. expect( parent.childCount ).to.equal( 2 );
  318. expect( parent.getChild( 0 ) ).to.instanceof( Element );
  319. expect( parent.getChild( 1 ) ).to.instanceof( Text );
  320. } );
  321. it( 'should create and insert text node at the given offset of given element', () => {
  322. const parent = createDocumentFragment();
  323. insert( createElement( 'child' ), parent );
  324. insert( createElement( 'child' ), parent );
  325. insertText( 'foo', parent, 1 );
  326. expect( parent.childCount ).to.equal( 3 );
  327. expect( parent.getChild( 0 ) ).to.instanceof( Element );
  328. expect( parent.getChild( 1 ) ).to.instanceof( Text );
  329. expect( parent.getChild( 2 ) ).to.instanceof( Element );
  330. } );
  331. it( 'should create and insert text node before the given node', () => {
  332. const parent = createDocumentFragment();
  333. const child1 = createElement( 'child' );
  334. const child2 = createElement( 'child' );
  335. insert( child1, parent );
  336. insert( child2, parent, 'end' );
  337. insertText( 'foo', child2, 'before' );
  338. expect( parent.childCount ).to.equal( 3 );
  339. expect( parent.getChild( 0 ) ).to.instanceof( Element );
  340. expect( parent.getChild( 1 ) ).to.instanceof( Text );
  341. expect( parent.getChild( 2 ) ).to.instanceof( Element );
  342. } );
  343. it( 'should create and insert text node after the given node', () => {
  344. const parent = createDocumentFragment();
  345. const child1 = createElement( 'child' );
  346. const child2 = createElement( 'child' );
  347. insert( child1, parent );
  348. insert( child2, parent, 'end' );
  349. insertText( 'foo', child1, 'after' );
  350. expect( parent.childCount ).to.equal( 3 );
  351. expect( parent.getChild( 0 ) ).to.instanceof( Element );
  352. expect( parent.getChild( 1 ) ).to.instanceof( Text );
  353. expect( parent.getChild( 2 ) ).to.instanceof( Element );
  354. } );
  355. it( 'should create proper operation', () => {
  356. const parent = createDocumentFragment();
  357. const spy = sinon.spy( model, 'applyOperation' );
  358. insertText( 'foo', parent );
  359. sinon.assert.calledOnce( spy );
  360. expect( spy.lastCall.args[ 0 ].type ).to.equal( 'insert' );
  361. expect( spy.lastCall.args[ 0 ] ).to.instanceof( InsertOperation );
  362. expect( spy.lastCall.args[ 0 ].shouldReceiveAttributes ).to.be.true;
  363. expect( spy.lastCall.args[ 0 ].batch ).to.equal( batch );
  364. } );
  365. it( 'should throw when trying to use detached writer', () => {
  366. const writer = new Writer( model, batch );
  367. const parent = createDocumentFragment();
  368. expectToThrowCKEditorError( () => {
  369. writer.insertText( 'foo', parent );
  370. }, /^writer-incorrect-use/, model );
  371. } );
  372. } );
  373. describe( 'insertElement()', () => {
  374. it( 'should create and insert element with attributes at given position', () => {
  375. const parent = createDocumentFragment();
  376. insertElement( 'foo', { bar: 'biz' }, new Position( parent, [ 0 ] ) );
  377. expect( parent.childCount ).to.equal( 1 );
  378. expect( parent.getChild( 0 ) ).to.instanceof( Element );
  379. expect( parent.getChild( 0 ).name ).to.equal( 'foo' );
  380. expect( Array.from( parent.getChild( 0 ).getAttributes() ) ).to.deep.equal( [ [ 'bar', 'biz' ] ] );
  381. } );
  382. it( 'should create and insert element with no attributes at given position', () => {
  383. const parent = createDocumentFragment();
  384. insertElement( 'foo', null, new Position( parent, [ 0 ] ) );
  385. expect( parent.childCount ).to.equal( 1 );
  386. expect( parent.getChild( 0 ) ).to.instanceof( Element );
  387. expect( parent.getChild( 0 ).name ).to.equal( 'foo' );
  388. expect( Array.from( parent.getChild( 0 ).getAttributes() ) ).to.deep.equal( [] );
  389. } );
  390. it( 'should create and insert element with no attributes omitting attributes param', () => {
  391. const parent = createDocumentFragment();
  392. insertElement( 'foo', new Position( parent, [ 0 ] ) );
  393. expect( parent.childCount ).to.equal( 1 );
  394. expect( parent.getChild( 0 ) ).to.instanceof( Element );
  395. expect( parent.getChild( 0 ).name ).to.equal( 'foo' );
  396. expect( Array.from( parent.getChild( 0 ).getAttributes() ) ).to.deep.equal( [] );
  397. } );
  398. it( 'should create and insert element at the beginning of given element', () => {
  399. const parent = createDocumentFragment();
  400. insert( createElement( 'child' ), parent );
  401. insertElement( 'foo', parent );
  402. expect( parent.childCount ).to.equal( 2 );
  403. expect( parent.getChild( 0 ).name ).to.equal( 'foo' );
  404. expect( parent.getChild( 1 ).name ).to.equal( 'child' );
  405. } );
  406. it( 'should create and insert element at the end of given element', () => {
  407. const parent = createDocumentFragment();
  408. insert( createElement( 'child' ), parent );
  409. insertElement( 'foo', parent, 'end' );
  410. expect( parent.childCount ).to.equal( 2 );
  411. expect( parent.getChild( 0 ).name ).to.equal( 'child' );
  412. expect( parent.getChild( 1 ).name ).to.equal( 'foo' );
  413. } );
  414. it( 'should create and insert element at the given offset of given element', () => {
  415. const parent = createDocumentFragment();
  416. insert( createElement( 'child1' ), parent );
  417. insert( createElement( 'child2' ), parent, 'end' );
  418. insertElement( 'foo', parent, 1 );
  419. expect( parent.childCount ).to.equal( 3 );
  420. expect( parent.getChild( 0 ).name ).to.equal( 'child1' );
  421. expect( parent.getChild( 1 ).name ).to.equal( 'foo' );
  422. expect( parent.getChild( 2 ).name ).to.equal( 'child2' );
  423. } );
  424. it( 'should create and insert element before the given node', () => {
  425. const parent = createDocumentFragment();
  426. const child1 = createElement( 'child1' );
  427. const child2 = createElement( 'child2' );
  428. insert( child1, parent );
  429. insert( child2, parent, 'end' );
  430. insertElement( 'foo', child2, 'before' );
  431. expect( parent.childCount ).to.equal( 3 );
  432. expect( parent.getChild( 0 ).name ).to.equal( 'child1' );
  433. expect( parent.getChild( 1 ).name ).to.equal( 'foo' );
  434. expect( parent.getChild( 2 ).name ).to.equal( 'child2' );
  435. } );
  436. it( 'should create and insert element after the given node', () => {
  437. const parent = createDocumentFragment();
  438. const child1 = createElement( 'child1' );
  439. const child2 = createElement( 'child2' );
  440. insert( child1, parent );
  441. insert( child2, parent, 'end' );
  442. insertElement( 'foo', child1, 'after' );
  443. expect( parent.childCount ).to.equal( 3 );
  444. expect( parent.getChild( 0 ).name ).to.equal( 'child1' );
  445. expect( parent.getChild( 1 ).name ).to.equal( 'foo' );
  446. expect( parent.getChild( 2 ).name ).to.equal( 'child2' );
  447. } );
  448. it( 'should create proper operation', () => {
  449. const parent = createDocumentFragment();
  450. const spy = sinon.spy( model, 'applyOperation' );
  451. insertElement( 'foo', parent );
  452. sinon.assert.calledOnce( spy );
  453. expect( spy.lastCall.args[ 0 ].type ).to.equal( 'insert' );
  454. expect( spy.lastCall.args[ 0 ] ).to.instanceof( InsertOperation );
  455. expect( spy.lastCall.args[ 0 ].shouldReceiveAttributes ).to.be.false;
  456. expect( spy.lastCall.args[ 0 ].batch ).to.equal( batch );
  457. } );
  458. it( 'should throw when trying to use detached writer', () => {
  459. const writer = new Writer( model, batch );
  460. const child = createElement( 'child' );
  461. expectToThrowCKEditorError( () => {
  462. writer.insertElement( 'foo', child, 'after' );
  463. }, /^writer-incorrect-use/, model );
  464. } );
  465. } );
  466. describe( 'append()', () => {
  467. it( 'should insert element at the end of the parent', () => {
  468. const parent = createDocumentFragment();
  469. const childText = createText( 'foo' );
  470. const childElement = createElement( 'foo' );
  471. append( childText, parent );
  472. append( childElement, parent );
  473. expect( Array.from( parent ) ).to.deep.equal( [ childText, childElement ] );
  474. } );
  475. it( 'should create proper operation', () => {
  476. const parent = createDocumentFragment();
  477. const text = createText( 'foo' );
  478. const spy = sinon.spy( model, 'applyOperation' );
  479. append( text, parent );
  480. sinon.assert.calledOnce( spy );
  481. expect( spy.lastCall.args[ 0 ].type ).to.equal( 'insert' );
  482. expect( spy.lastCall.args[ 0 ].batch ).to.equal( batch );
  483. } );
  484. it( 'should move element from one parent to the other within the same root (rootA -> rootA)', () => {
  485. const rootA = doc.createRoot();
  486. const parent1 = createElement( 'parent' );
  487. const parent2 = createElement( 'parent' );
  488. const node = createText( 'foo' );
  489. insert( node, parent1 );
  490. insert( parent1, rootA );
  491. insert( parent2, rootA );
  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 operations.
  498. sinon.assert.calledOnce( spy );
  499. expect( spy.firstCall.args[ 0 ].type ).to.equal( 'move' );
  500. expect( spy.firstCall.args[ 0 ].batch ).to.equal( batch );
  501. } );
  502. it( 'should move element from one parent to the other within the same document (rootA -> rootB)', () => {
  503. const rootA = doc.createRoot( '$root', 'A' );
  504. const rootB = doc.createRoot( '$root', 'B' );
  505. const node = createText( 'foo' );
  506. insert( node, rootA );
  507. const spy = sinon.spy( model, 'applyOperation' );
  508. append( node, rootB );
  509. // Verify result.
  510. expect( Array.from( rootA.getChildren() ) ).to.deep.equal( [] );
  511. expect( Array.from( rootB.getChildren() ) ).to.deep.equal( [ node ] );
  512. // Verify operations.
  513. sinon.assert.calledOnce( spy );
  514. expect( spy.firstCall.args[ 0 ].type ).to.equal( 'move' );
  515. expect( spy.firstCall.args[ 0 ].batch ).to.equal( batch );
  516. } );
  517. it( 'should move element from one parent to the other within the same document fragment (docFragA -> docFragA)', () => {
  518. const docFragA = createDocumentFragment();
  519. const parent1 = createElement( 'parent' );
  520. const parent2 = createElement( 'parent' );
  521. const node = createText( 'foo' );
  522. insert( node, parent1 );
  523. insert( parent1, docFragA );
  524. insert( parent2, docFragA );
  525. const spy = sinon.spy( model, 'applyOperation' );
  526. append( node, parent2 );
  527. // Verify result.
  528. expect( Array.from( parent1.getChildren() ) ).to.deep.equal( [] );
  529. expect( Array.from( parent2.getChildren() ) ).to.deep.equal( [ node ] );
  530. // Verify operations.
  531. sinon.assert.calledOnce( spy );
  532. expect( spy.firstCall.args[ 0 ].type ).to.equal( 'move' );
  533. expect( spy.firstCall.args[ 0 ].batch ).to.equal( batch );
  534. } );
  535. it( 'should move element from one parent to the other within different document fragments (docFragA -> docFragB)', () => {
  536. const docFragA = createDocumentFragment();
  537. const docFragB = createDocumentFragment();
  538. const node = createText( 'foo' );
  539. insert( node, docFragA );
  540. const spy = sinon.spy( model, 'applyOperation' );
  541. append( node, docFragB );
  542. // Verify result.
  543. expect( Array.from( docFragA ) ).to.deep.equal( [] );
  544. expect( Array.from( docFragB ) ).to.deep.equal( [ node ] );
  545. // Verify operations.
  546. sinon.assert.calledTwice( spy );
  547. expect( spy.firstCall.args[ 0 ].type ).to.equal( 'detach' );
  548. expect( spy.firstCall.args[ 0 ].batch ).to.equal( batch );
  549. expect( spy.secondCall.args[ 0 ].type ).to.equal( 'insert' );
  550. expect( spy.secondCall.args[ 0 ].batch ).to.equal( batch );
  551. } );
  552. it( 'should throw when moving element from document to document fragment', () => {
  553. const root = doc.createRoot();
  554. const docFrag = createDocumentFragment();
  555. const node = createText( 'foo' );
  556. insert( node, root );
  557. expectToThrowCKEditorError( () => {
  558. append( node, docFrag );
  559. }, /^model-writer-insert-forbidden-move/, model );
  560. } );
  561. } );
  562. describe( 'appendText()', () => {
  563. it( 'should create and insert text node with attributes at the end of the parent', () => {
  564. const parent = createDocumentFragment();
  565. appendText( 'foo', { bar: 'biz' }, parent );
  566. appendText( 'bar', { biz: 'bar' }, parent );
  567. expect( parent.childCount ).to.equal( 2 );
  568. expect( parent.getChild( 0 ).data ).to.equal( 'foo' );
  569. expect( Array.from( parent.getChild( 0 ).getAttributes() ) ).to.deep.equal( [ [ 'bar', 'biz' ] ] );
  570. expect( parent.getChild( 1 ).data ).to.equal( 'bar' );
  571. expect( Array.from( parent.getChild( 1 ).getAttributes() ) ).to.deep.equal( [ [ 'biz', 'bar' ] ] );
  572. } );
  573. it( 'should create and insert text node with no attributes at the end of the parent', () => {
  574. const parent = createDocumentFragment();
  575. appendText( 'foo', null, parent );
  576. expect( parent.childCount ).to.equal( 1 );
  577. expect( parent.getChild( 0 ) ).to.instanceof( Text );
  578. expect( parent.getChild( 0 ).data ).to.equal( 'foo' );
  579. expect( Array.from( parent.getChild( 0 ).getAttributes() ) ).to.deep.equal( [] );
  580. } );
  581. it( 'should create and insert text node with no attributes omitting attributes param', () => {
  582. const parent = createDocumentFragment();
  583. appendText( 'foo', parent );
  584. expect( parent.childCount ).to.equal( 1 );
  585. expect( parent.getChild( 0 ) ).to.instanceof( Text );
  586. expect( parent.getChild( 0 ).data ).to.equal( 'foo' );
  587. expect( Array.from( parent.getChild( 0 ).getAttributes() ) ).to.deep.equal( [] );
  588. } );
  589. it( 'should create proper operations', () => {
  590. const parent = createDocumentFragment();
  591. const spy = sinon.spy( model, 'applyOperation' );
  592. appendText( 'foo', parent );
  593. sinon.assert.calledOnce( spy );
  594. expect( spy.firstCall.args[ 0 ].type ).to.equal( 'insert' );
  595. expect( spy.firstCall.args[ 0 ] ).to.instanceof( InsertOperation );
  596. expect( spy.firstCall.args[ 0 ].shouldReceiveAttributes ).to.be.true;
  597. expect( spy.firstCall.args[ 0 ].batch ).to.equal( batch );
  598. } );
  599. it( 'should throw when trying to use detached writer', () => {
  600. const writer = new Writer( model, batch );
  601. const parent = createDocumentFragment();
  602. expectToThrowCKEditorError( () => {
  603. writer.appendText( 'foo', parent );
  604. }, /^writer-incorrect-use/, model );
  605. } );
  606. } );
  607. describe( 'appendElement()', () => {
  608. it( 'should create and insert element with attributes at the end of the parent', () => {
  609. const parent = createDocumentFragment();
  610. appendElement( 'foo', { bar: 'biz' }, parent );
  611. appendElement( 'bar', { biz: 'bar' }, parent );
  612. expect( parent.childCount ).to.equal( 2 );
  613. expect( parent.getChild( 0 ).name ).to.equal( 'foo' );
  614. expect( Array.from( parent.getChild( 0 ).getAttributes() ) ).to.deep.equal( [ [ 'bar', 'biz' ] ] );
  615. expect( parent.getChild( 1 ).name ).to.equal( 'bar' );
  616. expect( Array.from( parent.getChild( 1 ).getAttributes() ) ).to.deep.equal( [ [ 'biz', 'bar' ] ] );
  617. } );
  618. it( 'should create and insert element with no attributes at the end of the parent', () => {
  619. const parent = createDocumentFragment();
  620. appendElement( 'foo', null, parent );
  621. expect( parent.childCount ).to.equal( 1 );
  622. expect( parent.getChild( 0 ).name ).to.equal( 'foo' );
  623. expect( Array.from( parent.getChild( 0 ).getAttributes() ) ).to.deep.equal( [] );
  624. } );
  625. it( 'should create and insert element with no attributes omitting attributes param', () => {
  626. const parent = createDocumentFragment();
  627. appendElement( 'foo', parent );
  628. expect( parent.childCount ).to.equal( 1 );
  629. expect( parent.getChild( 0 ).name ).to.equal( 'foo' );
  630. expect( Array.from( parent.getChild( 0 ).getAttributes() ) ).to.deep.equal( [] );
  631. } );
  632. it( 'should create proper operation', () => {
  633. const parent = createDocumentFragment();
  634. const spy = sinon.spy( model, 'applyOperation' );
  635. appendElement( 'foo', parent );
  636. sinon.assert.calledOnce( spy );
  637. expect( spy.firstCall.args[ 0 ].type ).to.equal( 'insert' );
  638. expect( spy.firstCall.args[ 0 ] ).to.be.instanceof( InsertOperation );
  639. expect( spy.firstCall.args[ 0 ].shouldReceiveAttributes ).to.be.false;
  640. expect( spy.firstCall.args[ 0 ].batch ).to.equal( batch );
  641. } );
  642. it( 'should throw when trying to use detached writer', () => {
  643. const writer = new Writer( model, batch );
  644. const parent = createDocumentFragment();
  645. expectToThrowCKEditorError( () => {
  646. writer.appendElement( 'foo', parent );
  647. }, /^writer-incorrect-use/, model );
  648. } );
  649. } );
  650. describe( 'setAttribute() / removeAttribute()', () => {
  651. let root, spy;
  652. beforeEach( () => {
  653. root = doc.createRoot();
  654. } );
  655. describe( 'change attribute on node', () => {
  656. let node, text;
  657. beforeEach( () => {
  658. node = createElement( 'p', { a: 1 } );
  659. text = createText( 'c', { a: 1 } );
  660. append( node, root );
  661. append( text, root );
  662. spy = sinon.spy( model, 'applyOperation' );
  663. } );
  664. describe( 'setAttribute', () => {
  665. it( 'should create the attribute on element', () => {
  666. setAttribute( 'b', 2, node );
  667. expect( spy.callCount ).to.equal( 1 );
  668. expect( node.getAttribute( 'b' ) ).to.equal( 2 );
  669. } );
  670. it( 'should change the attribute of element', () => {
  671. setAttribute( 'a', 2, node );
  672. expect( spy.callCount ).to.equal( 1 );
  673. expect( node.getAttribute( 'a' ) ).to.equal( 2 );
  674. } );
  675. it( 'should create the attribute on text node', () => {
  676. setAttribute( 'b', 2, text );
  677. expect( spy.callCount ).to.equal( 1 );
  678. expect( root.getChild( 1 ).getAttribute( 'b' ) ).to.equal( 2 );
  679. } );
  680. it( 'should change the attribute of text node', () => {
  681. setAttribute( 'a', 2, text );
  682. expect( spy.callCount ).to.equal( 1 );
  683. expect( root.getChild( 1 ).getAttribute( 'a' ) ).to.equal( 2 );
  684. } );
  685. it( 'should do nothing if the attribute value is the same', () => {
  686. setAttribute( 'a', 1, node );
  687. expect( spy.callCount ).to.equal( 0 );
  688. expect( node.getAttribute( 'a' ) ).to.equal( 1 );
  689. } );
  690. it( 'should throw when trying to use detached writer', () => {
  691. const writer = new Writer( model, batch );
  692. expectToThrowCKEditorError( () => {
  693. writer.setAttribute( 'a', 1, node );
  694. }, /^writer-incorrect-use/, model );
  695. } );
  696. } );
  697. describe( 'removeAttribute', () => {
  698. it( 'should remove the attribute from element', () => {
  699. removeAttribute( 'a', node );
  700. expect( spy.callCount ).to.equal( 1 );
  701. expect( node.getAttribute( 'a' ) ).to.be.undefined;
  702. } );
  703. it( 'should remove the attribute from character', () => {
  704. removeAttribute( 'a', text );
  705. expect( spy.callCount ).to.equal( 1 );
  706. expect( root.getChild( 1 ).getAttribute( 'a' ) ).to.be.undefined;
  707. } );
  708. it( 'should do nothing if the attribute is not set', () => {
  709. removeAttribute( 'b', node );
  710. expect( spy.callCount ).to.equal( 0 );
  711. } );
  712. it( 'should throw when trying to use detached writer', () => {
  713. const writer = new Writer( model, batch );
  714. expectToThrowCKEditorError( () => {
  715. writer.removeAttribute( 'b', node );
  716. }, /^writer-incorrect-use/, model );
  717. } );
  718. } );
  719. } );
  720. describe( 'change attribute on range', () => {
  721. beforeEach( () => {
  722. const element = createElement( 'e', { a: 2 } );
  723. appendText( 'xxx', { a: 1 }, root );
  724. appendText( 'xxx', root );
  725. appendText( 'xxx', { a: 1 }, root );
  726. appendText( 'xxx', { a: 2 }, root );
  727. appendText( 'xxx', root );
  728. appendText( 'xxx', { a: 1 }, root );
  729. appendText( 'xxx', element );
  730. append( element, root );
  731. appendText( 'xxx', root );
  732. spy = sinon.spy( model, 'applyOperation' );
  733. } );
  734. function getRange( startIndex, endIndex ) {
  735. return new Range(
  736. Position._createAt( root, startIndex ),
  737. Position._createAt( root, endIndex )
  738. );
  739. }
  740. function getChangesAttrsCount() {
  741. let totalNumber = 0;
  742. for ( const operation of batch.operations ) {
  743. if ( operation.range ) {
  744. totalNumber += count( operation.range.getItems( { singleCharacters: true } ) );
  745. }
  746. }
  747. return totalNumber;
  748. }
  749. function getCompressedAttrs() {
  750. // default: 111---111222---1112------
  751. const range = Range._createIn( root );
  752. return Array.from( range.getItems( { singleCharacters: true } ) )
  753. .map( item => item.getAttribute( 'a' ) || '-' )
  754. .join( '' );
  755. }
  756. describe( 'setAttribute', () => {
  757. it( 'should set the attribute on the range', () => {
  758. setAttribute( 'a', 3, getRange( 3, 6 ) );
  759. expect( spy.callCount ).to.equal( 1 );
  760. expect( getChangesAttrsCount() ).to.equal( 3 );
  761. expect( getCompressedAttrs() ).to.equal( '111333111222---1112------' );
  762. } );
  763. it( 'should split the operations if parts of the range have different attributes', () => {
  764. setAttribute( 'a', 3, getRange( 4, 14 ) );
  765. expect( spy.callCount ).to.equal( 4 );
  766. expect( getChangesAttrsCount() ).to.equal( 10 );
  767. expect( getCompressedAttrs() ).to.equal( '111-3333333333-1112------' );
  768. } );
  769. it( 'should split the operations if parts of the part of the range have the attribute', () => {
  770. setAttribute( 'a', 2, getRange( 4, 14 ) );
  771. expect( spy.callCount ).to.equal( 3 );
  772. expect( getChangesAttrsCount() ).to.equal( 7 );
  773. expect( getCompressedAttrs() ).to.equal( '111-2222222222-1112------' );
  774. } );
  775. it( 'should strip the range if the beginning have the attribute', () => {
  776. setAttribute( 'a', 1, getRange( 1, 5 ) );
  777. expect( spy.callCount ).to.equal( 1 );
  778. expect( getChangesAttrsCount() ).to.equal( 2 );
  779. expect( getCompressedAttrs() ).to.equal( '11111-111222---1112------' );
  780. } );
  781. it( 'should strip the range if the ending have the attribute', () => {
  782. setAttribute( 'a', 1, getRange( 13, 17 ) );
  783. expect( spy.callCount ).to.equal( 1 );
  784. expect( getChangesAttrsCount() ).to.equal( 2 );
  785. expect( getCompressedAttrs() ).to.equal( '111---111222-111112------' );
  786. } );
  787. it( 'should do nothing if the range has attribute', () => {
  788. setAttribute( 'a', 1, getRange( 0, 3 ) );
  789. expect( spy.callCount ).to.equal( 0 );
  790. expect( getCompressedAttrs() ).to.equal( '111---111222---1112------' );
  791. } );
  792. it( 'should not check range\'s start position node when creating operations', () => {
  793. const range = new Range(
  794. new Position( root, [ 18, 1 ] ),
  795. new Position( root, [ 19 ] )
  796. );
  797. setAttribute( 'a', 1, range );
  798. expect( spy.callCount ).to.equal( 1 );
  799. expect( getChangesAttrsCount() ).to.equal( 2 );
  800. expect( getCompressedAttrs() ).to.equal( '111---111222---1112-11---' );
  801. } );
  802. it( 'should not change elements attribute if range contains closing tag', () => {
  803. const range = new Range(
  804. new Position( root, [ 18, 1 ] ),
  805. new Position( root, [ 21 ] )
  806. );
  807. setAttribute( 'a', 1, range );
  808. expect( spy.callCount ).to.equal( 2 );
  809. expect( getChangesAttrsCount() ).to.equal( 4 );
  810. expect( getCompressedAttrs() ).to.equal( '111---111222---1112-1111-' );
  811. } );
  812. it( 'should not create an operation if the range contains only closing tag', () => {
  813. const range = new Range(
  814. new Position( root, [ 18, 3 ] ),
  815. new Position( root, [ 19 ] )
  816. );
  817. setAttribute( 'a', 3, range );
  818. expect( spy.callCount ).to.equal( 0 );
  819. expect( getCompressedAttrs() ).to.equal( '111---111222---1112------' );
  820. } );
  821. it( 'should not create an operation if is collapsed', () => {
  822. setAttribute( 'a', 1, getRange( 3, 3 ) );
  823. expect( spy.callCount ).to.equal( 0 );
  824. expect( getCompressedAttrs() ).to.equal( '111---111222---1112------' );
  825. } );
  826. it( 'should not change children of items in the range', () => {
  827. setAttribute( 'a', 1, getRange( 0, 20 ) );
  828. expect( spy.callCount ).to.equal( 5 );
  829. expect( getChangesAttrsCount() ).to.equal( 14 );
  830. expect( getCompressedAttrs() ).to.equal( '1111111111111111111---1--' );
  831. } );
  832. it( 'should throw when trying to use detached writer', () => {
  833. const writer = new Writer( model, batch );
  834. expectToThrowCKEditorError( () => {
  835. writer.setAttribute( 'a', 1, getRange( 0, 20 ) );
  836. }, /^writer-incorrect-use/, model );
  837. } );
  838. } );
  839. describe( 'removeAttribute', () => {
  840. it( 'should remove the attribute on the range', () => {
  841. removeAttribute( 'a', getRange( 0, 2 ) );
  842. expect( spy.callCount ).to.equal( 1 );
  843. expect( getChangesAttrsCount() ).to.equal( 2 );
  844. expect( getCompressedAttrs() ).to.equal( '--1---111222---1112------' );
  845. } );
  846. it( 'should split the operations if parts of the range have different attributes', () => {
  847. removeAttribute( 'a', getRange( 7, 11 ) );
  848. expect( spy.callCount ).to.equal( 2 );
  849. expect( getChangesAttrsCount() ).to.equal( 4 );
  850. expect( getCompressedAttrs() ).to.equal( '111---1----2---1112------' );
  851. } );
  852. it( 'should split the operations if parts of the part of the range have no attribute', () => {
  853. removeAttribute( 'a', getRange( 1, 7 ) );
  854. expect( spy.callCount ).to.equal( 2 );
  855. expect( getChangesAttrsCount() ).to.equal( 3 );
  856. expect( getCompressedAttrs() ).to.equal( '1------11222---1112------' );
  857. } );
  858. it( 'should strip the range if the beginning have no attribute', () => {
  859. removeAttribute( 'a', getRange( 4, 12 ) );
  860. expect( spy.callCount ).to.equal( 2 );
  861. expect( getChangesAttrsCount() ).to.equal( 6 );
  862. expect( getCompressedAttrs() ).to.equal( '111------------1112------' );
  863. } );
  864. it( 'should strip the range if the ending have no attribute', () => {
  865. removeAttribute( 'a', getRange( 7, 15 ) );
  866. expect( spy.callCount ).to.equal( 2 );
  867. expect( getChangesAttrsCount() ).to.equal( 5 );
  868. expect( getCompressedAttrs() ).to.equal( '111---1--------1112------' );
  869. } );
  870. it( 'should do nothing if the range has no attribute', () => {
  871. removeAttribute( 'a', getRange( 4, 5 ) );
  872. expect( spy.callCount ).to.equal( 0 );
  873. expect( getCompressedAttrs() ).to.equal( '111---111222---1112------' );
  874. } );
  875. it( 'should not check range\'s start position node when creating operations', () => {
  876. const range = new Range(
  877. new Position( root, [ 18, 3 ] ),
  878. new Position( root, [ 19 ] )
  879. );
  880. removeAttribute( 'a', range );
  881. expect( spy.callCount ).to.equal( 0 );
  882. expect( getChangesAttrsCount() ).to.equal( 0 );
  883. expect( getCompressedAttrs() ).to.equal( '111---111222---1112------' );
  884. } );
  885. it( 'should not create an operation if range is collapsed', () => {
  886. removeAttribute( 'a', getRange( 3, 3 ) );
  887. expect( spy.callCount ).to.equal( 0 );
  888. expect( getCompressedAttrs() ).to.equal( '111---111222---1112------' );
  889. } );
  890. it( 'should create a proper operations for the mixed range', () => {
  891. removeAttribute( 'a', getRange( 3, 15 ) );
  892. expect( spy.callCount ).to.equal( 2 );
  893. expect( getChangesAttrsCount() ).to.equal( 6 );
  894. expect( getCompressedAttrs() ).to.equal( '111------------1112------' );
  895. } );
  896. it( 'should throw when trying to use detached writer', () => {
  897. const writer = new Writer( model, batch );
  898. expectToThrowCKEditorError( () => {
  899. writer.removeAttribute( 'a', getRange( 3, 15 ) );
  900. }, /^writer-incorrect-use/, model );
  901. } );
  902. } );
  903. } );
  904. describe( 'change attribute on root element', () => {
  905. let p;
  906. beforeEach( () => {
  907. p = createElement( 'p', { a: 3 } );
  908. spy = sinon.spy( model, 'applyOperation' );
  909. } );
  910. describe( 'setAttribute', () => {
  911. it( 'should create the attribute on root', () => {
  912. setAttribute( 'b', 2, root );
  913. expect( spy.callCount ).to.equal( 1 );
  914. expect( root.getAttribute( 'b' ) ).to.equal( 2 );
  915. } );
  916. it( 'should create the attribute on detached root', () => {
  917. setAttribute( 'b', 2, p );
  918. expect( spy.callCount ).to.equal( 1 );
  919. expect( p.getAttribute( 'b' ) ).to.equal( 2 );
  920. } );
  921. it( 'should change the attribute of root', () => {
  922. setAttribute( 'a', 2, root );
  923. expect( spy.callCount ).to.equal( 1 );
  924. expect( root.getAttribute( 'a' ) ).to.equal( 2 );
  925. } );
  926. it( 'should change the attribute of detached root', () => {
  927. setAttribute( 'a', 2, p );
  928. expect( spy.callCount ).to.equal( 1 );
  929. expect( p.getAttribute( 'a' ) ).to.equal( 2 );
  930. } );
  931. it( 'should do nothing if the attribute value is the same', () => {
  932. setAttribute( 'a', 1, root );
  933. expect( spy.callCount ).to.equal( 1 );
  934. setAttribute( 'a', 1, root );
  935. expect( spy.callCount ).to.equal( 1 );
  936. expect( root.getAttribute( 'a' ) ).to.equal( 1 );
  937. } );
  938. it( 'should do nothing if the attribute value is the same on detached root', () => {
  939. setAttribute( 'a', 1, p );
  940. expect( spy.callCount ).to.equal( 1 );
  941. setAttribute( 'a', 1, p );
  942. expect( spy.callCount ).to.equal( 1 );
  943. expect( p.getAttribute( 'a' ) ).to.equal( 1 );
  944. } );
  945. it( 'should throw when trying to use detached writer', () => {
  946. const writer = new Writer( model, batch );
  947. expectToThrowCKEditorError( () => {
  948. writer.setAttribute( 'a', 1, p );
  949. }, /^writer-incorrect-use/, model );
  950. } );
  951. } );
  952. describe( 'removeAttribute', () => {
  953. it( 'should remove the attribute from root', () => {
  954. setAttribute( 'a', 1, root );
  955. removeAttribute( 'a', root );
  956. expect( spy.callCount ).to.equal( 2 );
  957. expect( root.getAttribute( 'a' ) ).to.be.undefined;
  958. } );
  959. it( 'should do nothing if the attribute is not set', () => {
  960. removeAttribute( 'b', root );
  961. expect( spy.callCount ).to.equal( 0 );
  962. } );
  963. it( 'should throw when trying to use detached writer', () => {
  964. const writer = new Writer( model, batch );
  965. expectToThrowCKEditorError( () => {
  966. writer.removeAttribute( 'b', root );
  967. }, /^writer-incorrect-use/, model );
  968. } );
  969. } );
  970. describe( 'clearAttributes', () => {
  971. it( 'should clear attributes from range', () => {
  972. appendText( 'xxx', { a: 1, b: 2, c: 3 }, root );
  973. appendText( 'xxx', root );
  974. appendText( 'xxx', { a: 1 }, root );
  975. appendText( 'xxx', { b: 2 }, root );
  976. appendText( 'xxx', root );
  977. appendElement( 'e', { a: 1 }, root );
  978. appendText( 'xxx', root );
  979. const range = Range._createIn( root );
  980. clearAttributes( range );
  981. let itemsCount = 0;
  982. for ( const item of range.getItems() ) {
  983. itemsCount++;
  984. expect( Array.from( item.getAttributeKeys() ).length ).to.equal( 0 );
  985. }
  986. expect( itemsCount ).to.equal( 3 );
  987. } );
  988. it( 'should clear attributes on element', () => {
  989. const element = createElement( 'x', { a: 1, b: 2, c: 3 }, root );
  990. expect( Array.from( element.getAttributeKeys() ).length ).to.equal( 3 );
  991. clearAttributes( element );
  992. expect( Array.from( element.getAttributeKeys() ).length ).to.equal( 0 );
  993. } );
  994. it( 'should clear attributes on root element', () => {
  995. setAttributes( { a: 1, b: 2, c: 3 }, root );
  996. expect( Array.from( root.getAttributeKeys() ).length ).to.equal( 3 );
  997. clearAttributes( root );
  998. expect( Array.from( root.getAttributeKeys() ).length ).to.equal( 0 );
  999. } );
  1000. it( 'should do nothing if there are no attributes', () => {
  1001. const element = createElement( 'x' );
  1002. expect( Array.from( element.getAttributeKeys() ).length ).to.equal( 0 );
  1003. clearAttributes( element );
  1004. expect( Array.from( element.getAttributeKeys() ).length ).to.equal( 0 );
  1005. } );
  1006. it( 'should throw when trying to use detached writer', () => {
  1007. const writer = new Writer( model, batch );
  1008. const element = createElement( 'x' );
  1009. expectToThrowCKEditorError( () => {
  1010. writer.clearAttributes( element );
  1011. }, /^writer-incorrect-use/, model );
  1012. } );
  1013. } );
  1014. } );
  1015. } );
  1016. describe( 'setAttributes()', () => {
  1017. let frag, item;
  1018. beforeEach( () => {
  1019. frag = createDocumentFragment();
  1020. item = createText( 'xxx', { b: 2, c: 3 } );
  1021. appendText( 'xxx', { a: 1 }, frag );
  1022. append( item, frag );
  1023. } );
  1024. it( 'should set attributes one by one on range', () => {
  1025. const range = Range._createIn( frag );
  1026. let spy;
  1027. model.change( writer => {
  1028. // `setAttribute` is a not trivial operation and is deeply tested above, there is no point to duplicate
  1029. // such a big amount of the same tests, so let's use a spy here.
  1030. spy = sinon.spy( writer, 'setAttribute' );
  1031. writer.setAttributes( { a: 3, c: null }, range );
  1032. } );
  1033. // Verify result.
  1034. expect( Array.from( frag.getChild( 0 ).getAttributes() ) ).to.deep.equal( [ [ 'a', 3 ] ] );
  1035. expect( Array.from( frag.getChild( 1 ).getAttributes() ) ).to.deep.equal( [ [ 'b', 2 ], [ 'a', 3 ] ] );
  1036. // Verify operations
  1037. sinon.assert.calledTwice( spy );
  1038. sinon.assert.calledWith( spy.firstCall, 'a', 3, range );
  1039. sinon.assert.calledWith( spy.secondCall, 'c', null, range );
  1040. } );
  1041. it( 'should set attributes one by one on range for map as attributes list', () => {
  1042. const range = Range._createIn( frag );
  1043. let spy;
  1044. model.change( writer => {
  1045. // `setAttribute` is a not trivial operation and is deeply tested above, there is no point to duplicate
  1046. // such a big amount of the same tests, so let's use a spy here.
  1047. spy = sinon.spy( writer, 'setAttribute' );
  1048. writer.setAttributes( new Map( [ [ 'a', 3 ], [ 'c', null ] ] ), range );
  1049. } );
  1050. // Verify result.
  1051. expect( Array.from( frag.getChild( 0 ).getAttributes() ) ).to.deep.equal( [ [ 'a', 3 ] ] );
  1052. expect( Array.from( frag.getChild( 1 ).getAttributes() ) ).to.deep.equal( [ [ 'b', 2 ], [ 'a', 3 ] ] );
  1053. // Verify operations
  1054. sinon.assert.calledTwice( spy );
  1055. sinon.assert.calledWith( spy.firstCall, 'a', 3, range );
  1056. sinon.assert.calledWith( spy.secondCall, 'c', null, range );
  1057. } );
  1058. it( 'should set attributes one by one on item', () => {
  1059. let spy;
  1060. model.change( writer => {
  1061. // `setAttribute` is a not trivial operation and is deeply tested above, there is no point to duplicate
  1062. // such a big amount of the same tests, so let's use a spy here.
  1063. spy = sinon.spy( writer, 'setAttribute' );
  1064. writer.setAttributes( { a: 3, c: null }, item );
  1065. } );
  1066. // Verify result.
  1067. expect( Array.from( item.getAttributes() ) ).to.deep.equal( [ [ 'b', 2 ], [ 'a', 3 ] ] );
  1068. // Verify operations
  1069. sinon.assert.calledTwice( spy );
  1070. sinon.assert.calledWith( spy.firstCall, 'a', 3, item );
  1071. sinon.assert.calledWith( spy.secondCall, 'c', null, item );
  1072. } );
  1073. it( 'should set attributes one by one on item for maps as attributes list', () => {
  1074. let spy;
  1075. model.change( writer => {
  1076. // `setAttribute` is a not trivial operation and is deeply tested above, there is no point to duplicate
  1077. // such a big amount of the same tests, so let's use a spy here.
  1078. spy = sinon.spy( writer, 'setAttribute' );
  1079. writer.setAttributes( new Map( [ [ 'a', 3 ], [ 'c', null ] ] ), item );
  1080. } );
  1081. // Verify result.
  1082. expect( Array.from( item.getAttributes() ) ).to.deep.equal( [ [ 'b', 2 ], [ 'a', 3 ] ] );
  1083. // Verify operations
  1084. sinon.assert.calledTwice( spy );
  1085. sinon.assert.calledWith( spy.firstCall, 'a', 3, item );
  1086. sinon.assert.calledWith( spy.secondCall, 'c', null, item );
  1087. } );
  1088. it( 'should throw when trying to use detached writer', () => {
  1089. const writer = new Writer( model, batch );
  1090. expectToThrowCKEditorError( () => {
  1091. writer.setAttributes( new Map( [ [ 'a', 3 ], [ 'c', null ] ] ), item );
  1092. }, /^writer-incorrect-use/, model );
  1093. } );
  1094. } );
  1095. describe( 'merge()', () => {
  1096. let root, p1, p2;
  1097. beforeEach( () => {
  1098. root = doc.createRoot();
  1099. p1 = new Element( 'p', { key1: 'value1' }, new Text( 'foo' ) );
  1100. p2 = new Element( 'p', { key2: 'value2' }, new Text( 'bar' ) );
  1101. root._insertChild( 0, [ p1, p2 ] );
  1102. } );
  1103. it( 'should merge foo and bar into foobar', () => {
  1104. merge( new Position( root, [ 1 ] ) );
  1105. expect( root.maxOffset ).to.equal( 1 );
  1106. expect( root.getChild( 0 ).name ).to.equal( 'p' );
  1107. expect( root.getChild( 0 ).maxOffset ).to.equal( 6 );
  1108. expect( count( root.getChild( 0 ).getAttributes() ) ).to.equal( 1 );
  1109. expect( root.getChild( 0 ).getAttribute( 'key1' ) ).to.equal( 'value1' );
  1110. expect( root.getChild( 0 ).getChild( 0 ).data ).to.equal( 'foobar' );
  1111. } );
  1112. it( 'should correctly merge in document fragment', () => {
  1113. const docFrag = new DocumentFragment( [
  1114. new Element( 'p', null, 'foo' ),
  1115. new Element( 'p', null, 'bar' )
  1116. ] );
  1117. merge( new Position( docFrag, [ 1 ] ) );
  1118. expect( docFrag.getChild( 0 ).name ).to.equal( 'p' );
  1119. expect( docFrag.getChild( 0 ).getChild( 0 ).data ).to.equal( 'foobar' );
  1120. } );
  1121. describe( 'should create a marker operation if a marker was affected', () => {
  1122. it( '<p>Foo[</p><p>Bar]</p>', () => {
  1123. testMerge( p1, 'end', p2, 0 );
  1124. } );
  1125. it( '<p>[Foo</p><p>]Bar</p>', () => {
  1126. testMerge( p1, 0, p2, 0 );
  1127. } );
  1128. it( '<p>[Foo</p>]<p>Bar</p>', () => {
  1129. testMerge( p1, 0, root, 1 );
  1130. } );
  1131. it( '<p>Foo</p>[<p>Bar]</p>', () => {
  1132. testMerge( root, 1, p2, 'end' );
  1133. } );
  1134. function testMerge( startElement, startOffset, endElement, endOffset ) {
  1135. const markerRange = new Range(
  1136. Position._createAt( startElement, startOffset ),
  1137. Position._createAt( endElement, endOffset )
  1138. );
  1139. addMarker( 'name', {
  1140. range: markerRange,
  1141. usingOperation: true
  1142. } );
  1143. const documentVersion = model.document.version;
  1144. merge( Position._createAfter( p1 ) );
  1145. const history = model.document.history;
  1146. const lastOperation = history._operations[ history._operations.length - 1 ];
  1147. const secondLastOperation = history._operations[ history._operations.length - 2 ];
  1148. expect( secondLastOperation.type ).to.equal( 'marker' );
  1149. expect( secondLastOperation.oldRange.isEqual( markerRange ) );
  1150. expect( secondLastOperation.newRange.isEqual( markerRange ) );
  1151. expect( lastOperation.type ).to.equal( 'merge' );
  1152. expect( model.document.version ).to.equal( documentVersion + 2 );
  1153. }
  1154. } );
  1155. it( 'should not create a marker operation if affected marker was not using operations', () => {
  1156. const markerRange = new Range( Position._createAt( p2, 0 ), Position._createAt( p2, 2 ) );
  1157. addMarker( 'name', {
  1158. range: markerRange,
  1159. usingOperation: false
  1160. } );
  1161. const documentVersion = model.document.version;
  1162. merge( Position._createAfter( p1 ) );
  1163. const history = model.document.history;
  1164. const lastOperation = history._operations[ history._operations.length - 1 ];
  1165. expect( lastOperation.type ).to.equal( 'merge' );
  1166. expect( model.document.version ).to.equal( documentVersion + 1 );
  1167. } );
  1168. it( 'should throw if there is no element after', () => {
  1169. expectToThrowCKEditorError( () => {
  1170. merge( new Position( root, [ 2 ] ) );
  1171. }, /^writer-merge-no-element-after/, model );
  1172. } );
  1173. it( 'should throw if there is no element before', () => {
  1174. expectToThrowCKEditorError( () => {
  1175. merge( new Position( root, [ 0, 2 ] ) );
  1176. }, /^writer-merge-no-element-before/, model );
  1177. } );
  1178. it( 'should throw when trying to use detached writer', () => {
  1179. const writer = new Writer( model, batch );
  1180. expectToThrowCKEditorError( () => {
  1181. writer.merge( new Position( root, [ 1 ] ) );
  1182. }, /^writer-incorrect-use/, model );
  1183. } );
  1184. } );
  1185. describe( 'move()', () => {
  1186. let root, range, div, p;
  1187. beforeEach( () => {
  1188. root = doc.createRoot();
  1189. div = new Element( 'div', [], new Text( 'foobar' ) );
  1190. p = new Element( 'p', [], new Text( 'abcxyz' ) );
  1191. div._insertChild( 0, [ new Element( 'p', [], new Text( 'gggg' ) ) ] );
  1192. div._insertChild( 2, [ new Element( 'p', [], new Text( 'hhhh' ) ) ] );
  1193. root._insertChild( 0, [ div, p ] );
  1194. range = new Range( new Position( root, [ 0, 3 ] ), new Position( root, [ 0, 7 ] ) );
  1195. } );
  1196. it( 'should move flat range of nodes', () => {
  1197. move( range, new Position( root, [ 1, 3 ] ) );
  1198. expect( getNodesAndText( Range._createIn( root.getChild( 0 ) ) ) ).to.equal( 'PggggPfoPhhhhP' );
  1199. expect( getNodesAndText( Range._createIn( root.getChild( 1 ) ) ) ).to.equal( 'abcobarxyz' );
  1200. } );
  1201. it( 'should create a marker operation if a marker was affected', () => {
  1202. const markerRange = new Range( Position._createAt( p, 1 ), Position._createAt( p, 4 ) );
  1203. addMarker( 'name', {
  1204. range: markerRange,
  1205. usingOperation: true
  1206. } );
  1207. const documentVersion = model.document.version;
  1208. move( new Range( Position._createAt( p, 0 ), Position._createAt( p, 2 ) ), Position._createAt( div, 0 ) );
  1209. const history = model.document.history;
  1210. const lastOperation = history._operations[ history._operations.length - 1 ];
  1211. const secondLastOperation = history._operations[ history._operations.length - 2 ];
  1212. expect( secondLastOperation.type ).to.equal( 'marker' );
  1213. expect( secondLastOperation.oldRange.isEqual( markerRange ) );
  1214. expect( secondLastOperation.newRange.isEqual( markerRange ) );
  1215. expect( lastOperation.type ).to.equal( 'move' );
  1216. expect( model.document.version ).to.equal( documentVersion + 2 );
  1217. } );
  1218. it( 'should not create a marker operation if affected marker was not using operations', () => {
  1219. const markerRange = new Range( Position._createAt( p, 1 ), Position._createAt( p, 4 ) );
  1220. addMarker( 'name', {
  1221. range: markerRange,
  1222. usingOperation: false
  1223. } );
  1224. const documentVersion = model.document.version;
  1225. move( new Range( Position._createAt( p, 0 ), Position._createAt( p, 2 ) ), Position._createAt( div, 0 ) );
  1226. const history = model.document.history;
  1227. const lastOperation = history._operations[ history._operations.length - 1 ];
  1228. expect( lastOperation.type ).to.equal( 'move' );
  1229. expect( model.document.version ).to.equal( documentVersion + 1 );
  1230. } );
  1231. it( 'should throw if object to move is not a range', () => {
  1232. expectToThrowCKEditorError( () => {
  1233. move( root.getChild( 0 ), new Position( root, [ 1, 3 ] ) );
  1234. }, /^writer-move-invalid-range/, model );
  1235. } );
  1236. it( 'should throw if given range is not flat', () => {
  1237. const notFlatRange = new Range( new Position( root, [ 0, 2, 2 ] ), new Position( root, [ 0, 6 ] ) );
  1238. expectToThrowCKEditorError( () => {
  1239. move( notFlatRange, new Position( root, [ 1, 3 ] ) );
  1240. }, /^writer-move-range-not-flat/, model );
  1241. } );
  1242. it( 'should throw if range is going to be moved to the other document', () => {
  1243. const docFrag = createDocumentFragment();
  1244. expectToThrowCKEditorError( () => {
  1245. move( range, docFrag, 0 );
  1246. }, /^writer-move-different-document/, model );
  1247. } );
  1248. it( 'should throw when trying to use detached writer', () => {
  1249. const writer = new Writer( model, batch );
  1250. expectToThrowCKEditorError( () => {
  1251. writer.move( range, new Position( root, [ 1, 3 ] ) );
  1252. }, /^writer-incorrect-use/, model );
  1253. } );
  1254. } );
  1255. describe( 'remove()', () => {
  1256. let div, p, range;
  1257. beforeEach( () => {
  1258. div = createElement( 'div' );
  1259. appendText( 'foobar', div );
  1260. p = createElement( 'p' );
  1261. appendText( 'abcxyz', p );
  1262. insertElement( 'p', div );
  1263. appendElement( 'p', div );
  1264. insertText( 'gggg', new Position( div, [ 0, 0 ] ) );
  1265. insertText( 'hhhh', new Position( div, [ 7, 0 ] ) );
  1266. } );
  1267. describe( 'remove from document', () => {
  1268. let root;
  1269. beforeEach( () => {
  1270. root = doc.createRoot();
  1271. append( div, root );
  1272. append( p, root );
  1273. // Range starts in ROOT > DIV > P > gg|gg.
  1274. // Range ends in ROOT > DIV > ...|ar.
  1275. range = new Range( new Position( root, [ 0, 0, 2 ] ), new Position( root, [ 0, 5 ] ) );
  1276. } );
  1277. it( 'should remove specified node', () => {
  1278. remove( div );
  1279. expect( root.maxOffset ).to.equal( 1 );
  1280. expect( root.childCount ).to.equal( 1 );
  1281. expect( getNodesAndText( Range._createIn( root.getChild( 0 ) ) ) ).to.equal( 'abcxyz' );
  1282. } );
  1283. it( 'should remove specified text node', () => {
  1284. remove( p.getChild( 0 ) );
  1285. expect( getNodesAndText( Range._createOn( p ) ) ).to.equal( 'PP' );
  1286. } );
  1287. it( 'should remove any range of nodes', () => {
  1288. remove( range );
  1289. expect( getNodesAndText( Range._createIn( root.getChild( 0 ) ) ) ).to.equal( 'PggParPhhhhP' );
  1290. expect( getNodesAndText( Range._createIn( root.getChild( 1 ) ) ) ).to.equal( 'abcxyz' );
  1291. } );
  1292. it( 'should create minimal number of remove operations, each with only one operation', () => {
  1293. batch = new Batch();
  1294. remove( range );
  1295. expect( batch.operations.length ).to.equal( 2 );
  1296. } );
  1297. it( 'should use MoveOperation to graveyard', () => {
  1298. batch = new Batch();
  1299. remove( div );
  1300. expect( batch.operations[ 0 ].type ).to.equal( 'remove' );
  1301. } );
  1302. it( 'should create a marker operation if a marker was affected', () => {
  1303. const markerRange = new Range( Position._createAt( p, 1 ), Position._createAt( p, 4 ) );
  1304. addMarker( 'name', {
  1305. range: markerRange,
  1306. usingOperation: true
  1307. } );
  1308. const documentVersion = model.document.version;
  1309. remove( new Range( Position._createAt( p, 0 ), Position._createAt( p, 2 ) ) );
  1310. const history = model.document.history;
  1311. const lastOperation = history._operations[ history._operations.length - 1 ];
  1312. const secondLastOperation = history._operations[ history._operations.length - 2 ];
  1313. expect( secondLastOperation.type ).to.equal( 'marker' );
  1314. expect( secondLastOperation.oldRange.isEqual( markerRange ) );
  1315. expect( secondLastOperation.newRange.isEqual( markerRange ) );
  1316. expect( lastOperation.type ).to.equal( 'remove' );
  1317. expect( model.document.version ).to.equal( documentVersion + 2 );
  1318. } );
  1319. it( 'should not create a marker operation if affected marker was not using operations', () => {
  1320. const markerRange = new Range( Position._createAt( p, 1 ), Position._createAt( p, 4 ) );
  1321. addMarker( 'name', {
  1322. range: markerRange,
  1323. usingOperation: false
  1324. } );
  1325. const documentVersion = model.document.version;
  1326. remove( new Range( Position._createAt( p, 0 ), Position._createAt( p, 2 ) ) );
  1327. const history = model.document.history;
  1328. const lastOperation = history._operations[ history._operations.length - 1 ];
  1329. expect( lastOperation.type ).to.equal( 'remove' );
  1330. expect( model.document.version ).to.equal( documentVersion + 1 );
  1331. } );
  1332. it( 'should throw when trying to use detached writer', () => {
  1333. const writer = new Writer( model, batch );
  1334. expectToThrowCKEditorError( () => {
  1335. writer.remove( range );
  1336. }, /^writer-incorrect-use/, model );
  1337. } );
  1338. } );
  1339. describe( 'remove from document fragment', () => {
  1340. let frag;
  1341. beforeEach( () => {
  1342. frag = createDocumentFragment();
  1343. append( div, frag );
  1344. append( p, frag );
  1345. // Range starts in FRAG > DIV > P > gg|gg.
  1346. // Range ends in FRAG > DIV > ...|ar.
  1347. range = new Range( new Position( frag, [ 0, 0, 2 ] ), new Position( frag, [ 0, 5 ] ) );
  1348. } );
  1349. it( 'should remove specified node', () => {
  1350. remove( div );
  1351. expect( frag.maxOffset ).to.equal( 1 );
  1352. expect( frag.childCount ).to.equal( 1 );
  1353. expect( getNodesAndText( Range._createIn( frag.getChild( 0 ) ) ) ).to.equal( 'abcxyz' );
  1354. } );
  1355. it( 'should remove specified text node', () => {
  1356. remove( p.getChild( 0 ) );
  1357. expect( getNodesAndText( Range._createOn( p ) ) ).to.equal( 'PP' );
  1358. } );
  1359. it( 'should remove any range of nodes', () => {
  1360. remove( range );
  1361. expect( getNodesAndText( Range._createIn( frag.getChild( 0 ) ) ) ).to.equal( 'PggParPhhhhP' );
  1362. expect( getNodesAndText( Range._createIn( frag.getChild( 1 ) ) ) ).to.equal( 'abcxyz' );
  1363. } );
  1364. it( 'should create minimal number of remove operations, each with only one operation', () => {
  1365. batch = new Batch();
  1366. remove( range );
  1367. expect( batch.operations.length ).to.equal( 2 );
  1368. } );
  1369. it( 'should use DetachOperation', () => {
  1370. batch = new Batch();
  1371. remove( div );
  1372. expect( batch.operations[ 0 ].type ).to.equal( 'detach' );
  1373. } );
  1374. it( 'should throw when trying to use detached writer', () => {
  1375. const writer = new Writer( model, batch );
  1376. expectToThrowCKEditorError( () => {
  1377. writer.remove( range );
  1378. }, /^writer-incorrect-use/, model );
  1379. } );
  1380. } );
  1381. } );
  1382. describe( 'rename()', () => {
  1383. it( 'should rename given element', () => {
  1384. const root = doc.createRoot();
  1385. const p = new Element( 'p', null, new Text( 'abc' ) );
  1386. root._appendChild( p );
  1387. rename( p, 'h' );
  1388. expect( root.maxOffset ).to.equal( 1 );
  1389. expect( root.getChild( 0 ) ).to.have.property( 'name', 'h' );
  1390. } );
  1391. it( 'should rename in document fragment', () => {
  1392. const docFrag = new DocumentFragment();
  1393. const p = new Element( 'p' );
  1394. docFrag._appendChild( p );
  1395. rename( p, 'h' );
  1396. expect( docFrag.maxOffset ).to.equal( 1 );
  1397. expect( docFrag.getChild( 0 ) ).to.have.property( 'name', 'h' );
  1398. } );
  1399. it( 'should throw if not an Element instance is passed', () => {
  1400. expectToThrowCKEditorError( () => {
  1401. rename( new Text( 'abc' ), 'h' );
  1402. }, /^writer-rename-not-element-instance/, model );
  1403. } );
  1404. it( 'should throw when trying to use detached writer', () => {
  1405. const writer = new Writer( model, batch );
  1406. const p = new Element( 'p', null, new Text( 'abc' ) );
  1407. expectToThrowCKEditorError( () => {
  1408. writer.rename( p, 'h' );
  1409. }, /^writer-incorrect-use/, model );
  1410. } );
  1411. } );
  1412. describe( 'split()', () => {
  1413. let root, p;
  1414. beforeEach( () => {
  1415. root = doc.createRoot();
  1416. p = new Element( 'p', { key: 'value' }, new Text( 'foobar' ) );
  1417. root._insertChild( 0, p );
  1418. } );
  1419. it( 'should split foobar to foo and bar', () => {
  1420. split( new Position( root, [ 0, 3 ] ) );
  1421. expect( root.maxOffset ).to.equal( 2 );
  1422. expect( root.getChild( 0 ).name ).to.equal( 'p' );
  1423. expect( root.getChild( 0 ).maxOffset ).to.equal( 3 );
  1424. expect( count( root.getChild( 0 ).getAttributes() ) ).to.equal( 1 );
  1425. expect( root.getChild( 0 ).getAttribute( 'key' ) ).to.equal( 'value' );
  1426. expect( root.getChild( 0 ).getChild( 0 ).data ).to.equal( 'foo' );
  1427. expect( root.getChild( 1 ).name ).to.equal( 'p' );
  1428. expect( root.getChild( 1 ).maxOffset ).to.equal( 3 );
  1429. expect( count( root.getChild( 1 ).getAttributes() ) ).to.equal( 1 );
  1430. expect( root.getChild( 1 ).getAttribute( 'key' ) ).to.equal( 'value' );
  1431. expect( root.getChild( 1 ).getChild( 0 ).data ).to.equal( 'bar' );
  1432. } );
  1433. it( 'should split inside document fragment', () => {
  1434. const docFrag = new DocumentFragment();
  1435. docFrag._appendChild( new Element( 'p', null, new Text( 'foobar' ) ) );
  1436. split( new Position( docFrag, [ 0, 3 ] ) );
  1437. expect( docFrag.maxOffset ).to.equal( 2 );
  1438. expect( docFrag.getChild( 0 ).name ).to.equal( 'p' );
  1439. expect( docFrag.getChild( 0 ).maxOffset ).to.equal( 3 );
  1440. expect( docFrag.getChild( 0 ).getChild( 0 ).data ).to.equal( 'foo' );
  1441. expect( docFrag.getChild( 1 ).name ).to.equal( 'p' );
  1442. expect( docFrag.getChild( 1 ).maxOffset ).to.equal( 3 );
  1443. expect( docFrag.getChild( 1 ).getChild( 0 ).data ).to.equal( 'bar' );
  1444. } );
  1445. it( 'should create an empty paragraph if we split at the end', () => {
  1446. split( new Position( root, [ 0, 6 ] ) );
  1447. expect( root.maxOffset ).to.equal( 2 );
  1448. expect( root.getChild( 0 ).name ).to.equal( 'p' );
  1449. expect( root.getChild( 0 ).maxOffset ).to.equal( 6 );
  1450. expect( count( root.getChild( 0 ).getAttributes() ) ).to.equal( 1 );
  1451. expect( root.getChild( 0 ).getAttribute( 'key' ) ).to.equal( 'value' );
  1452. expect( root.getChild( 0 ).getChild( 0 ).data ).to.equal( 'foobar' );
  1453. expect( root.getChild( 1 ).name ).to.equal( 'p' );
  1454. expect( root.getChild( 1 ).maxOffset ).to.equal( 0 );
  1455. expect( count( root.getChild( 1 ).getAttributes() ) ).to.equal( 1 );
  1456. expect( root.getChild( 1 ).getAttribute( 'key' ) ).to.equal( 'value' );
  1457. } );
  1458. it( 'should throw if we try to split a root', () => {
  1459. expectToThrowCKEditorError( () => {
  1460. split( new Position( root, [ 0 ] ) );
  1461. }, /^writer-split-element-no-parent/, model );
  1462. } );
  1463. it( 'should throw if we try to split an element with no parent', () => {
  1464. expectToThrowCKEditorError( () => {
  1465. const element = createElement( 'p' );
  1466. split( new Position( element, [ 0 ] ) );
  1467. }, /^writer-split-element-no-parent/, model );
  1468. } );
  1469. it( 'should throw if we try to split a document fragment', () => {
  1470. expectToThrowCKEditorError( () => {
  1471. const documentFragment = createDocumentFragment();
  1472. split( new Position( documentFragment, [ 0 ] ) );
  1473. }, /^writer-split-element-no-parent/, model );
  1474. } );
  1475. it( 'should split elements to limitElement', () => {
  1476. const div = new Element( 'div', null, p );
  1477. const section = new Element( 'section', null, div );
  1478. root._insertChild( 0, section );
  1479. split( new Position( p, [ 3 ] ), section );
  1480. expect( root.maxOffset ).to.equal( 1 );
  1481. expect( section.maxOffset ).to.equal( 2 );
  1482. expect( section.getChild( 0 ).name ).to.equal( 'div' );
  1483. expect( section.getChild( 0 ).getChild( 0 ).name ).to.equal( 'p' );
  1484. expect( section.getChild( 0 ).getChild( 0 ).getAttribute( 'key' ) ).to.equal( 'value' );
  1485. expect( count( section.getChild( 0 ).getChild( 0 ).getAttributes() ) ).to.equal( 1 );
  1486. expect( section.getChild( 0 ).getChild( 0 ).getChild( 0 ).data ).to.equal( 'foo' );
  1487. expect( section.getChild( 1 ).name ).to.equal( 'div' );
  1488. expect( section.getChild( 1 ).getChild( 0 ).name ).to.equal( 'p' );
  1489. expect( section.getChild( 1 ).getChild( 0 ).getAttribute( 'key' ) ).to.equal( 'value' );
  1490. expect( count( section.getChild( 1 ).getChild( 0 ).getAttributes() ) ).to.equal( 1 );
  1491. expect( section.getChild( 1 ).getChild( 0 ).getChild( 0 ).data ).to.equal( 'bar' );
  1492. } );
  1493. it( 'should throw when limitElement is not a position ancestor', () => {
  1494. const div = new Element( 'div', null, p );
  1495. const section = new Element( 'section', null, div );
  1496. root._insertChild( 0, div );
  1497. root._insertChild( 1, section );
  1498. expectToThrowCKEditorError( () => {
  1499. split( new Position( p, [ 3 ] ), section );
  1500. }, /^writer-split-invalid-limit-element/, model );
  1501. } );
  1502. it( 'should throw when trying to use detached writer', () => {
  1503. const writer = new Writer( model, batch );
  1504. expectToThrowCKEditorError( () => {
  1505. writer.split( new Position( root, [ 0, 3 ] ) );
  1506. }, /^writer-incorrect-use/, model );
  1507. } );
  1508. } );
  1509. describe( 'wrap()', () => {
  1510. let root, range;
  1511. beforeEach( () => {
  1512. root = doc.createRoot();
  1513. root._insertChild( 0, new Text( 'foobar' ) );
  1514. range = new Range( new Position( root, [ 2 ] ), new Position( root, [ 4 ] ) );
  1515. } );
  1516. it( 'should wrap flat range with given element', () => {
  1517. const p = new Element( 'p' );
  1518. wrap( range, p );
  1519. expect( root.maxOffset ).to.equal( 5 );
  1520. expect( root.getChild( 0 ).data ).to.equal( 'fo' );
  1521. expect( root.getChild( 1 ).name ).to.equal( 'p' );
  1522. expect( root.getChild( 1 ).getChild( 0 ).data ).to.equal( 'ob' );
  1523. expect( root.getChild( 2 ).data ).to.equal( 'ar' );
  1524. } );
  1525. it( 'should wrap flat range with an element of given name', () => {
  1526. wrap( range, 'p' );
  1527. expect( root.maxOffset ).to.equal( 5 );
  1528. expect( root.getChild( 0 ).data ).to.equal( 'fo' );
  1529. expect( root.getChild( 1 ).name ).to.equal( 'p' );
  1530. expect( root.getChild( 1 ).getChild( 0 ).data ).to.equal( 'ob' );
  1531. expect( root.getChild( 2 ).data ).to.equal( 'ar' );
  1532. } );
  1533. it( 'should wrap inside document fragment', () => {
  1534. const docFrag = new DocumentFragment( new Text( 'foo' ) );
  1535. wrap( Range._createIn( docFrag ), 'p' );
  1536. expect( docFrag.maxOffset ).to.equal( 1 );
  1537. expect( docFrag.getChild( 0 ).name ).to.equal( 'p' );
  1538. expect( docFrag.getChild( 0 ).getChild( 0 ).data ).to.equal( 'foo' );
  1539. } );
  1540. it( 'should throw if range to wrap is not flat', () => {
  1541. root._insertChild( 1, [ new Element( 'p', [], new Text( 'xyz' ) ) ] );
  1542. const notFlatRange = new Range( new Position( root, [ 3 ] ), new Position( root, [ 6, 2 ] ) );
  1543. expectToThrowCKEditorError( () => {
  1544. wrap( notFlatRange, 'p' );
  1545. }, /^writer-wrap-range-not-flat/, model );
  1546. } );
  1547. it( 'should throw if element to wrap with has children #1', () => {
  1548. const p = new Element( 'p', [], new Text( 'a' ) );
  1549. expectToThrowCKEditorError( () => {
  1550. wrap( range, p );
  1551. }, /^writer-wrap-element-not-empty/, model );
  1552. } );
  1553. it( 'should throw if element to wrap with has children #2', () => {
  1554. const p = new Element( 'p' );
  1555. root._insertChild( 0, p );
  1556. expectToThrowCKEditorError( () => {
  1557. wrap( range, p );
  1558. }, /^writer-wrap-element-attached/, model );
  1559. } );
  1560. it( 'should throw when trying to use detached writer', () => {
  1561. const writer = new Writer( model, batch );
  1562. expectToThrowCKEditorError( () => {
  1563. writer.wrap( range, 'p' );
  1564. }, /^writer-incorrect-use/, model );
  1565. } );
  1566. } );
  1567. describe( 'unwrap()', () => {
  1568. let root, p;
  1569. beforeEach( () => {
  1570. root = doc.createRoot();
  1571. p = new Element( 'p', [], new Text( 'xyz' ) );
  1572. root._insertChild( 0, [ new Text( 'a' ), p, new Text( 'b' ) ] );
  1573. } );
  1574. it( 'should unwrap given element', () => {
  1575. unwrap( p );
  1576. expect( root.maxOffset ).to.equal( 5 );
  1577. expect( root.getChild( 0 ).data ).to.equal( 'axyzb' );
  1578. } );
  1579. it( 'should unwrap inside document fragment', () => {
  1580. const docFrag = new DocumentFragment( new Element( 'p', null, new Text( 'foo' ) ) );
  1581. unwrap( docFrag.getChild( 0 ) );
  1582. expect( docFrag.maxOffset ).to.equal( 3 );
  1583. expect( docFrag.getChild( 0 ).data ).to.equal( 'foo' );
  1584. } );
  1585. it( 'should throw if element to unwrap has no parent', () => {
  1586. const element = new Element( 'p' );
  1587. expectToThrowCKEditorError( () => {
  1588. unwrap( element );
  1589. }, /^writer-unwrap-element-no-parent/, model );
  1590. } );
  1591. it( 'should throw when trying to use detached writer', () => {
  1592. const writer = new Writer( model, batch );
  1593. expectToThrowCKEditorError( () => {
  1594. writer.unwrap( p );
  1595. }, /^writer-incorrect-use/, model );
  1596. } );
  1597. } );
  1598. describe( 'addMarker()', () => {
  1599. let root, range;
  1600. beforeEach( () => {
  1601. root = doc.createRoot();
  1602. root._appendChild( new Text( 'foo' ) );
  1603. range = Range._createIn( root );
  1604. } );
  1605. it( 'should throw if options.usingOperation is not defined', () => {
  1606. expectToThrowCKEditorError( () => {
  1607. addMarker( 'name' );
  1608. }, /^writer-addMarker-no-usingOperation/, model );
  1609. } );
  1610. it( 'should throw if name and range is defined but options.usingOperation is not defined', () => {
  1611. expectToThrowCKEditorError( () => {
  1612. addMarker( 'name', { range } );
  1613. }, /^writer-addMarker-no-usingOperation/, model );
  1614. } );
  1615. it( 'should add marker to the document marker collection', () => {
  1616. addMarker( 'name', { range, usingOperation: false } );
  1617. expect( model.markers.get( 'name' ).getRange().isEqual( range ) ).to.be.true;
  1618. } );
  1619. it( 'should return marker if that was set directly', () => {
  1620. const marker = addMarker( 'name', { range, usingOperation: false } );
  1621. expect( model.markers.get( 'name' ) ).to.equal( marker );
  1622. } );
  1623. it( 'should return marker if that was set using operation API', () => {
  1624. const marker = addMarker( 'name', { range, usingOperation: true } );
  1625. expect( model.markers.get( 'name' ) ).to.equal( marker );
  1626. } );
  1627. it( 'should return marker with properly set managedUsingOperations (to true)', () => {
  1628. const marker = addMarker( 'name', { range, usingOperation: true } );
  1629. expect( marker.managedUsingOperations ).to.be.true;
  1630. } );
  1631. it( 'should return marker with properly set managedUsingOperations (to false)', () => {
  1632. const marker = addMarker( 'name', { range, usingOperation: false } );
  1633. expect( marker.managedUsingOperations ).to.be.false;
  1634. } );
  1635. it( 'should return marker with properly set affectsData (default to false)', () => {
  1636. const marker = addMarker( 'name', { range, usingOperation: false } );
  1637. expect( marker.affectsData ).to.be.false;
  1638. } );
  1639. it( 'should return marker with properly set affectsData (to false)', () => {
  1640. const marker = addMarker( 'name', { range, usingOperation: false, affectsData: true } );
  1641. expect( marker.affectsData ).to.be.true;
  1642. } );
  1643. it( 'should throw when trying to update existing marker in the document marker collection', () => {
  1644. addMarker( 'name', { range, usingOperation: false } );
  1645. const range2 = new Range( Position._createAt( root, 0 ), Position._createAt( root, 0 ) );
  1646. expectToThrowCKEditorError( () => {
  1647. addMarker( 'name', { range: range2, usingOperation: false } );
  1648. }, /^writer-addMarker-marker-exists/, model );
  1649. } );
  1650. it( 'should use operations when having set usingOperation to true', () => {
  1651. const spy = sinon.spy();
  1652. model.on( 'applyOperation', spy );
  1653. addMarker( 'name', { range, usingOperation: true } );
  1654. const op = batch.operations[ 0 ];
  1655. sinon.assert.calledOnce( spy );
  1656. expect( spy.firstCall.args[ 1 ][ 0 ].type ).to.equal( 'marker' );
  1657. expect( op.oldRange ).to.be.null;
  1658. expect( op.newRange.isEqual( range ) ).to.be.true;
  1659. } );
  1660. it( 'should throw if marker with given name does not exist and range is not passed', () => {
  1661. expectToThrowCKEditorError( () => {
  1662. addMarker( 'name', { usingOperation: true } );
  1663. }, /^writer-addMarker-no-range/, model );
  1664. } );
  1665. it( 'should throw if marker is set directly and range is not passed', () => {
  1666. expectToThrowCKEditorError( () => {
  1667. addMarker( 'name', { usingOperation: false } );
  1668. }, /^writer-addMarker-no-range/, model );
  1669. } );
  1670. it( 'should throw when trying to use detached writer', () => {
  1671. const marker = addMarker( 'name', { range, usingOperation: false } );
  1672. const writer = new Writer( model, batch );
  1673. expectToThrowCKEditorError( () => {
  1674. writer.addMarker( marker, null, { usingOperation: true } );
  1675. }, /^writer-incorrect-use/, model );
  1676. } );
  1677. } );
  1678. describe( 'updateMarker()', () => {
  1679. let root, range;
  1680. beforeEach( () => {
  1681. root = doc.createRoot();
  1682. root._appendChild( new Text( 'foo' ) );
  1683. range = Range._createIn( root );
  1684. } );
  1685. it( 'should update managed marker\'s range by marker instance using operations', () => {
  1686. const marker = addMarker( 'name', { range, usingOperation: true } );
  1687. const range2 = new Range( Position._createAt( root, 0 ), Position._createAt( root, 0 ) );
  1688. updateMarker( marker, { range: range2 } );
  1689. expect( batch.operations.length ).to.equal( 2 );
  1690. const op = batch.operations[ 1 ];
  1691. expect( marker.getRange().isEqual( range2 ) ).to.be.true;
  1692. expect( op.oldRange.isEqual( range ) ).to.be.true;
  1693. expect( op.newRange.isEqual( range2 ) ).to.be.true;
  1694. } );
  1695. it( 'should update managed marker\'s range by marker name using operations', () => {
  1696. const marker = addMarker( 'name', { range, usingOperation: true } );
  1697. const range2 = new Range( Position._createAt( root, 0 ), Position._createAt( root, 0 ) );
  1698. updateMarker( 'name', { range: range2 } );
  1699. expect( batch.operations.length ).to.equal( 2 );
  1700. const op = batch.operations[ 1 ];
  1701. expect( marker.getRange().isEqual( range2 ) ).to.be.true;
  1702. expect( op.oldRange.isEqual( range ) ).to.be.true;
  1703. expect( op.newRange.isEqual( range2 ) ).to.be.true;
  1704. } );
  1705. it( 'should update managed marker\'s range by marker instance using operations and usingOperation explicitly passed', () => {
  1706. const marker = addMarker( 'name', { range, usingOperation: true } );
  1707. const range2 = new Range( Position._createAt( root, 0 ), Position._createAt( root, 0 ) );
  1708. updateMarker( marker, { range: range2, usingOperation: true } );
  1709. expect( batch.operations.length ).to.equal( 2 );
  1710. const op = batch.operations[ 1 ];
  1711. expect( marker.getRange().isEqual( range2 ) ).to.be.true;
  1712. expect( op.oldRange.isEqual( range ) ).to.be.true;
  1713. expect( op.newRange.isEqual( range2 ) ).to.be.true;
  1714. } );
  1715. it( 'should update managed marker\'s range by marker name using operations and usingOperation explicitly passed', () => {
  1716. const marker = addMarker( 'name', { range, usingOperation: true } );
  1717. const range2 = new Range( Position._createAt( root, 0 ), Position._createAt( root, 0 ) );
  1718. updateMarker( 'name', { range: range2, usingOperation: true } );
  1719. expect( batch.operations.length ).to.equal( 2 );
  1720. const op = batch.operations[ 1 ];
  1721. expect( marker.getRange().isEqual( range2 ) ).to.be.true;
  1722. expect( op.oldRange.isEqual( range ) ).to.be.true;
  1723. expect( op.newRange.isEqual( range2 ) ).to.be.true;
  1724. } );
  1725. it( 'should not use operations when updating marker which does not use operations', () => {
  1726. const spy = sinon.spy();
  1727. model.on( 'applyOperation', spy );
  1728. const marker = addMarker( 'name', { range, usingOperation: false } );
  1729. const range2 = new Range( Position._createAt( root, 0 ), Position._createAt( root, 0 ) );
  1730. updateMarker( marker, { range: range2 } );
  1731. sinon.assert.notCalled( spy );
  1732. } );
  1733. it( 'should create additional operation when marker type changes to not managed using operation', () => {
  1734. const spy = sinon.spy();
  1735. model.on( 'applyOperation', spy );
  1736. addMarker( 'name', { range, usingOperation: true } );
  1737. updateMarker( 'name', { usingOperation: false } );
  1738. const marker = model.markers.get( 'name' );
  1739. const op1 = batch.operations[ 0 ];
  1740. const op2 = batch.operations[ 1 ];
  1741. sinon.assert.calledTwice( spy );
  1742. expect( spy.firstCall.args[ 1 ][ 0 ].type ).to.equal( 'marker' );
  1743. expect( spy.secondCall.args[ 1 ][ 0 ].type ).to.equal( 'marker' );
  1744. expect( op1.oldRange ).to.be.null;
  1745. expect( op1.newRange.isEqual( range ) ).to.be.true;
  1746. expect( op2.oldRange.isEqual( range ) ).to.be.true;
  1747. expect( op2.newRange ).to.be.null;
  1748. expect( marker.managedUsingOperations ).to.be.false;
  1749. } );
  1750. it( 'should create additional operation when marker type changes to not managed using operation and changing its range', () => {
  1751. const spy = sinon.spy();
  1752. model.on( 'applyOperation', spy );
  1753. const range2 = new Range( Position._createAt( root, 0 ), Position._createAt( root, 0 ) );
  1754. addMarker( 'name', { range, usingOperation: true } );
  1755. updateMarker( 'name', { range: range2, usingOperation: false } );
  1756. const marker = model.markers.get( 'name' );
  1757. const op1 = batch.operations[ 0 ];
  1758. const op2 = batch.operations[ 1 ];
  1759. sinon.assert.calledTwice( spy );
  1760. expect( spy.firstCall.args[ 1 ][ 0 ].type ).to.equal( 'marker' );
  1761. expect( spy.secondCall.args[ 1 ][ 0 ].type ).to.equal( 'marker' );
  1762. expect( op1.oldRange ).to.be.null;
  1763. expect( op1.newRange.isEqual( range ) ).to.be.true;
  1764. expect( op2.oldRange.isEqual( range ) ).to.be.true;
  1765. expect( op2.newRange ).to.be.null;
  1766. expect( marker.getRange().isEqual( range2 ) );
  1767. expect( marker.managedUsingOperations ).to.be.false;
  1768. } );
  1769. it( 'should enable changing marker to be managed using operation', () => {
  1770. const spy = sinon.spy();
  1771. model.on( 'applyOperation', spy );
  1772. addMarker( 'name', { range, usingOperation: false } );
  1773. updateMarker( 'name', { usingOperation: true } );
  1774. const marker = model.markers.get( 'name' );
  1775. sinon.assert.calledOnce( spy );
  1776. expect( spy.firstCall.args[ 1 ][ 0 ].type ).to.equal( 'marker' );
  1777. expect( marker.managedUsingOperations ).to.be.true;
  1778. } );
  1779. it( 'should enable changing marker to be managed using operation while changing range', () => {
  1780. const spy = sinon.spy();
  1781. model.on( 'applyOperation', spy );
  1782. const range2 = new Range( Position._createAt( root, 0 ), Position._createAt( root, 0 ) );
  1783. addMarker( 'name', { range, usingOperation: false } );
  1784. updateMarker( 'name', { range: range2, usingOperation: true } );
  1785. const marker = model.markers.get( 'name' );
  1786. sinon.assert.calledOnce( spy );
  1787. expect( spy.firstCall.args[ 1 ][ 0 ].type ).to.equal( 'marker' );
  1788. expect( marker.getRange().isEqual( range2 ) ).to.be.true;
  1789. expect( marker.managedUsingOperations ).to.be.true;
  1790. } );
  1791. it( 'should allow changing affectsData property not using operations', () => {
  1792. addMarker( 'name', { range, usingOperation: false } );
  1793. updateMarker( 'name', { affectsData: false } );
  1794. const marker = model.markers.get( 'name' );
  1795. expect( marker.affectsData ).to.be.false;
  1796. } );
  1797. it( 'should allow changing affectsData property using operations', () => {
  1798. addMarker( 'name', { range, usingOperation: true } );
  1799. updateMarker( 'name', { affectsData: true } );
  1800. const op1 = batch.operations[ 0 ];
  1801. const op2 = batch.operations[ 1 ];
  1802. const marker = model.markers.get( 'name' );
  1803. expect( op1.affectsData ).to.be.false;
  1804. expect( op2.affectsData ).to.be.true;
  1805. expect( marker.affectsData ).to.be.true;
  1806. } );
  1807. it( 'should not change affectsData property if not provided', () => {
  1808. const range2 = new Range( Position._createAt( root, 0 ), Position._createAt( root, 0 ) );
  1809. addMarker( 'name', { range, affectsData: false, usingOperation: false } );
  1810. updateMarker( 'name', { range: range2 } );
  1811. const marker = model.markers.get( 'name' );
  1812. expect( marker.affectsData ).to.be.false;
  1813. } );
  1814. it( 'should allow changing affectsData and usingOperation', () => {
  1815. addMarker( 'name', { range, usingOperation: true } );
  1816. updateMarker( 'name', { affectsData: true, usingOperation: false } );
  1817. const op1 = batch.operations[ 0 ];
  1818. const op2 = batch.operations[ 1 ];
  1819. const marker = model.markers.get( 'name' );
  1820. expect( op1.affectsData ).to.be.false;
  1821. expect( op2.affectsData ).to.be.true;
  1822. expect( marker.affectsData ).to.be.true;
  1823. } );
  1824. it( 'should allow changing affectsData and usingOperation #2', () => {
  1825. const spy = sinon.spy();
  1826. model.on( 'applyOperation', spy );
  1827. addMarker( 'name', { range, usingOperation: false, affectsData: true } );
  1828. updateMarker( 'name', { usingOperation: true, affectsData: false } );
  1829. const marker = model.markers.get( 'name' );
  1830. sinon.assert.calledOnce( spy );
  1831. expect( spy.firstCall.args[ 1 ][ 0 ].type ).to.equal( 'marker' );
  1832. expect( marker.affectsData ).to.be.false;
  1833. } );
  1834. it( 'should throw when range and usingOperations were not provided', () => {
  1835. expectToThrowCKEditorError( () => {
  1836. addMarker( 'name', { range, usingOperation: false } );
  1837. updateMarker( 'name', {} );
  1838. }, /^writer-updateMarker-wrong-options/, model );
  1839. } );
  1840. it( 'should throw when marker provided by name does not exists', () => {
  1841. expectToThrowCKEditorError( () => {
  1842. updateMarker( 'name', { usingOperation: false } );
  1843. }, /^writer-updateMarker-marker-not-exists/, model );
  1844. } );
  1845. it( 'should only refresh the marker when there is no provided options to update', () => {
  1846. const marker = addMarker( 'name', { range, usingOperation: true } );
  1847. const spy = sinon.spy( model.markers, '_refresh' );
  1848. updateMarker( marker );
  1849. sinon.assert.calledOnce( spy );
  1850. sinon.assert.calledWithExactly( spy, marker );
  1851. updateMarker( 'name' );
  1852. sinon.assert.calledTwice( spy );
  1853. sinon.assert.calledWithExactly( spy.secondCall, marker );
  1854. } );
  1855. it( 'should throw when trying to use detached writer', () => {
  1856. const marker = addMarker( 'name', { range, usingOperation: false } );
  1857. const writer = new Writer( model, batch );
  1858. expectToThrowCKEditorError( () => {
  1859. writer.updateMarker( marker, { usingOperation: true } );
  1860. }, /^writer-incorrect-use/, model );
  1861. } );
  1862. } );
  1863. describe( 'removeMarker()', () => {
  1864. let root, range;
  1865. beforeEach( () => {
  1866. root = doc.createRoot();
  1867. root._appendChild( new Text( 'foo' ) );
  1868. range = Range._createIn( root );
  1869. } );
  1870. it( 'should remove marker from the document marker collection', () => {
  1871. addMarker( 'name', { range, usingOperation: false } );
  1872. removeMarker( 'name' );
  1873. expect( model.markers.get( 'name' ) ).to.be.null;
  1874. } );
  1875. it( 'should throw when trying to remove non existing marker', () => {
  1876. expectToThrowCKEditorError( () => {
  1877. removeMarker( 'name' );
  1878. }, /^writer-removeMarker-no-marker/, model );
  1879. } );
  1880. it( 'should throw when trying to use detached writer', () => {
  1881. const writer = new Writer( model, batch );
  1882. expectToThrowCKEditorError( () => {
  1883. writer.removeMarker( 'name' );
  1884. }, /^writer-incorrect-use/, model );
  1885. } );
  1886. it( 'should accept marker instance', () => {
  1887. addMarker( 'name', { range, usingOperation: false } );
  1888. const marker = model.markers.get( 'name' );
  1889. removeMarker( marker );
  1890. expect( model.markers.get( 'name' ) ).to.be.null;
  1891. } );
  1892. it( 'should use MarkerOperation when marker was created using operation', () => {
  1893. addMarker( 'name', { range, usingOperation: true } );
  1894. const marker = model.markers.get( 'name' );
  1895. const spy = sinon.spy();
  1896. model.on( 'applyOperation', spy );
  1897. removeMarker( marker );
  1898. sinon.assert.calledOnce( spy );
  1899. expect( spy.firstCall.args[ 1 ][ 0 ].type ).to.equal( 'marker' );
  1900. expect( model.markers.get( 'name' ) ).to.be.null;
  1901. } );
  1902. } );
  1903. describe( 'setSelection()', () => {
  1904. let root;
  1905. beforeEach( () => {
  1906. model.schema.register( 'p', { inheritAllFrom: '$block' } );
  1907. model.schema.extend( 'p', { allowIn: '$root' } );
  1908. root = doc.createRoot();
  1909. root._appendChild( [
  1910. new Element( 'p' ),
  1911. new Element( 'p' ),
  1912. new Element( 'p', [], new Text( 'foo' ) )
  1913. ] );
  1914. } );
  1915. it( 'should use DocumentSelection#_setTo method', () => {
  1916. const firstParagraph = root.getNodeByPath( [ 1 ] );
  1917. const setToSpy = sinon.spy( DocumentSelection.prototype, '_setTo' );
  1918. setSelection( firstParagraph, 0 );
  1919. expect( setToSpy.calledOnce ).to.be.true;
  1920. setToSpy.restore();
  1921. } );
  1922. it( 'should change document selection ranges', () => {
  1923. const range = new Range( new Position( root, [ 1, 0 ] ), new Position( root, [ 2, 2 ] ) );
  1924. setSelection( range, { backward: true } );
  1925. expect( model.document.selection._ranges.length ).to.equal( 1 );
  1926. expect( model.document.selection._ranges[ 0 ].start.path ).to.deep.equal( [ 1, 0 ] );
  1927. expect( model.document.selection._ranges[ 0 ].end.path ).to.deep.equal( [ 2, 2 ] );
  1928. expect( model.document.selection.isBackward ).to.be.true;
  1929. } );
  1930. } );
  1931. describe( 'setSelectionFocus()', () => {
  1932. let root;
  1933. beforeEach( () => {
  1934. model.schema.register( 'p', { inheritAllFrom: '$block' } );
  1935. model.schema.extend( 'p', { allowIn: '$root' } );
  1936. root = doc.createRoot();
  1937. root._appendChild( [
  1938. new Element( 'p' ),
  1939. new Element( 'p' ),
  1940. new Element( 'p', [], new Text( 'foo' ) )
  1941. ] );
  1942. } );
  1943. it( 'should use DocumentSelection#_setFocus method', () => {
  1944. const firstParagraph = root.getNodeByPath( [ 1 ] );
  1945. const setFocusSpy = sinon.spy( DocumentSelection.prototype, '_setFocus' );
  1946. setSelectionFocus( firstParagraph, 0 );
  1947. expect( setFocusSpy.calledOnce ).to.be.true;
  1948. setFocusSpy.restore();
  1949. } );
  1950. it( 'should change document selection ranges', () => {
  1951. setSelection( new Position( root, [ 0, 0 ] ) );
  1952. setSelectionFocus( new Position( root, [ 2, 2 ] ) );
  1953. expect( model.document.selection._ranges.length ).to.equal( 1 );
  1954. expect( model.document.selection._ranges[ 0 ].start.path ).to.deep.equal( [ 0, 0 ] );
  1955. expect( model.document.selection._ranges[ 0 ].end.path ).to.deep.equal( [ 2, 2 ] );
  1956. } );
  1957. } );
  1958. describe( 'setSelectionAttribute()', () => {
  1959. const fooStoreAttrKey = DocumentSelection._getStoreAttributeKey( 'foo' );
  1960. let root, rangeInEmptyP, emptyP;
  1961. beforeEach( () => {
  1962. model.schema.register( 'p', { inheritAllFrom: '$block' } );
  1963. model.schema.extend( 'p', { allowIn: '$root' } );
  1964. root = doc.createRoot();
  1965. root._appendChild( [
  1966. new Element( 'p', [], [] ),
  1967. new Element( 'p' ),
  1968. new Element( 'p', [], new Text( 'foo' ) )
  1969. ] );
  1970. rangeInEmptyP = new Range( new Position( root, [ 0, 0 ] ), new Position( root, [ 0, 0 ] ) );
  1971. emptyP = root.getChild( 0 );
  1972. } );
  1973. it( 'should store attribute if the selection is in empty node', () => {
  1974. setSelection( rangeInEmptyP );
  1975. setSelectionAttribute( 'foo', 'bar' );
  1976. expect( model.document.selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  1977. expect( emptyP.getAttribute( fooStoreAttrKey ) ).to.equal( 'bar' );
  1978. } );
  1979. it( 'should be able to store attributes from the given object', () => {
  1980. setSelection( rangeInEmptyP );
  1981. setSelectionAttribute( { key1: 'foo', key2: 'bar' } );
  1982. expect( model.document.selection.getAttribute( 'key1' ) ).to.equal( 'foo' );
  1983. expect( model.document.selection.getAttribute( 'key2' ) ).to.equal( 'bar' );
  1984. } );
  1985. it( 'should be able to store attributes from the given iterable', () => {
  1986. setSelection( rangeInEmptyP );
  1987. setSelectionAttribute( new Map( [ [ 'key1', 'foo' ], [ 'key2', 'bar' ] ] ) );
  1988. expect( model.document.selection.getAttribute( 'key1' ) ).to.equal( 'foo' );
  1989. expect( model.document.selection.getAttribute( 'key2' ) ).to.equal( 'bar' );
  1990. } );
  1991. } );
  1992. describe( 'removeSelectionAttribute()', () => {
  1993. const fooStoreAttrKey = DocumentSelection._getStoreAttributeKey( 'foo' );
  1994. let root, rangeInEmptyP, emptyP;
  1995. beforeEach( () => {
  1996. model.schema.register( 'p', { inheritAllFrom: '$block' } );
  1997. model.schema.extend( 'p', { allowIn: '$root' } );
  1998. root = doc.createRoot();
  1999. root._appendChild( [
  2000. new Element( 'p', [], [] ),
  2001. new Element( 'p' ),
  2002. new Element( 'p', [], new Text( 'foo' ) )
  2003. ] );
  2004. rangeInEmptyP = new Range( new Position( root, [ 0, 0 ] ), new Position( root, [ 0, 0 ] ) );
  2005. emptyP = root.getChild( 0 );
  2006. } );
  2007. it( 'should remove stored attribute if the selection is in empty node', () => {
  2008. setSelection( rangeInEmptyP );
  2009. setSelectionAttribute( 'foo', 'bar' );
  2010. removeSelectionAttribute( 'foo' );
  2011. expect( model.document.selection.getAttribute( 'foo' ) ).to.be.undefined;
  2012. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  2013. } );
  2014. it( 'should remove all attributes from the given iterable', () => {
  2015. setSelection( rangeInEmptyP );
  2016. setSelectionAttribute( 'foo', 'bar' );
  2017. setSelectionAttribute( 'foo2', 'bar2' );
  2018. removeSelectionAttribute( [ 'foo', 'foo2' ] );
  2019. expect( model.document.selection.getAttribute( 'foo' ) ).to.be.undefined;
  2020. expect( model.document.selection.getAttribute( 'foo2' ) ).to.be.undefined;
  2021. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  2022. } );
  2023. it( 'should do nothing if attribute does not exist in the selection', () => {
  2024. setSelection( rangeInEmptyP );
  2025. setSelectionAttribute( 'foo', 'bar' );
  2026. setSelectionAttribute( 'foo2', 'bar2' );
  2027. removeSelectionAttribute( [ 'foo', 'baz' ] );
  2028. expect( model.document.selection.getAttribute( 'foo' ) ).to.be.undefined;
  2029. expect( model.document.selection.getAttribute( 'foo2' ) ).to.equal( 'bar2' );
  2030. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  2031. } );
  2032. } );
  2033. describe( 'overrideSelectionGravity()', () => {
  2034. it( 'should use DocumentSelection#_overrideGravity', () => {
  2035. const overrideGravitySpy = sinon.spy( DocumentSelection.prototype, '_overrideGravity' );
  2036. overrideSelectionGravity();
  2037. sinon.assert.calledOnce( overrideGravitySpy );
  2038. overrideGravitySpy.restore();
  2039. } );
  2040. it( 'should return the unique id', () => {
  2041. expect( overrideSelectionGravity() ).to.be.a( 'string' );
  2042. } );
  2043. it( 'should not get attributes from the node before the caret when gravity is overridden', () => {
  2044. const root = doc.createRoot();
  2045. root._appendChild( [
  2046. new Text( 'foo', { foo: true } ),
  2047. new Text( 'bar', { foo: true, bar: true } ),
  2048. new Text( 'biz', { foo: true } )
  2049. ] );
  2050. setSelection( new Position( root, [ 6 ] ) );
  2051. expect( Array.from( model.document.selection.getAttributeKeys() ) ).to.deep.equal( [ 'foo', 'bar' ] );
  2052. overrideSelectionGravity();
  2053. expect( Array.from( model.document.selection.getAttributeKeys() ) ).to.deep.equal( [ 'foo' ] );
  2054. expect( model.document.selection.isGravityOverridden ).to.true;
  2055. // Moving selection should not restore the gravity.
  2056. setSelection( new Position( root, [ 5 ] ) );
  2057. expect( Array.from( model.document.selection.getAttributeKeys() ) ).to.deep.equal( [ 'foo', 'bar' ] );
  2058. expect( model.document.selection.isGravityOverridden ).to.true;
  2059. } );
  2060. } );
  2061. describe( 'restoreSelectionGravity()', () => {
  2062. it( 'should use DocumentSelection#_restoreGravity', () => {
  2063. const overrideUid = overrideSelectionGravity();
  2064. const restoreGravitySpy = sinon.spy( DocumentSelection.prototype, '_restoreGravity' );
  2065. restoreSelectionGravity( overrideUid );
  2066. sinon.assert.calledOnce( restoreGravitySpy );
  2067. restoreGravitySpy.restore();
  2068. } );
  2069. it( 'should restore overridden gravity to default', () => {
  2070. const root = doc.createRoot();
  2071. root._appendChild( [
  2072. new Text( 'foo', { foo: true } ),
  2073. new Text( 'bar', { foo: true, bar: true } ),
  2074. new Text( 'biz', { foo: true } )
  2075. ] );
  2076. setSelection( new Position( root, [ 6 ] ) );
  2077. const overrideUid = overrideSelectionGravity();
  2078. expect( Array.from( model.document.selection.getAttributeKeys() ) ).to.deep.equal( [ 'foo' ] );
  2079. restoreSelectionGravity( overrideUid );
  2080. expect( Array.from( model.document.selection.getAttributeKeys() ) ).to.deep.equal( [ 'foo', 'bar' ] );
  2081. } );
  2082. } );
  2083. describe( 'createPositionFromPath()', () => {
  2084. it( 'should call model.createPositionFromPath()', () => {
  2085. const stub = sinon.stub( model, 'createPositionFromPath' );
  2086. model.change( writer => {
  2087. writer.createPositionFromPath();
  2088. } );
  2089. sinon.assert.calledOnce( stub );
  2090. } );
  2091. } );
  2092. describe( 'createPositionAt()', () => {
  2093. it( 'should call model.createPositionAt()', () => {
  2094. const stub = sinon.stub( model, 'createPositionAt' );
  2095. model.change( writer => {
  2096. writer.createPositionAt();
  2097. } );
  2098. sinon.assert.calledOnce( stub );
  2099. } );
  2100. } );
  2101. describe( 'createPositionAfter()', () => {
  2102. it( 'should call model.createPositionAfter()', () => {
  2103. const stub = sinon.stub( model, 'createPositionAfter' );
  2104. model.change( writer => {
  2105. writer.createPositionAfter();
  2106. } );
  2107. sinon.assert.calledOnce( stub );
  2108. } );
  2109. } );
  2110. describe( 'createPositionBefore()', () => {
  2111. it( 'should call model.createPositionBefore()', () => {
  2112. const stub = sinon.stub( model, 'createPositionBefore' );
  2113. model.change( writer => {
  2114. writer.createPositionBefore();
  2115. } );
  2116. sinon.assert.calledOnce( stub );
  2117. } );
  2118. } );
  2119. describe( 'createRange()', () => {
  2120. it( 'should call model.createRange()', () => {
  2121. const stub = sinon.stub( model, 'createRange' );
  2122. model.change( writer => {
  2123. writer.createRange();
  2124. } );
  2125. sinon.assert.calledOnce( stub );
  2126. } );
  2127. } );
  2128. describe( 'createRangeIn()', () => {
  2129. it( 'should call model.createRangeIn()', () => {
  2130. const stub = sinon.stub( model, 'createRangeIn' );
  2131. model.change( writer => {
  2132. writer.createRangeIn();
  2133. } );
  2134. sinon.assert.calledOnce( stub );
  2135. } );
  2136. } );
  2137. describe( 'createRangeOn()', () => {
  2138. it( 'should call model.createRangeOn()', () => {
  2139. const stub = sinon.stub( model, 'createRangeOn' );
  2140. model.change( writer => {
  2141. writer.createRangeOn();
  2142. } );
  2143. sinon.assert.calledOnce( stub );
  2144. } );
  2145. } );
  2146. describe( 'createSelection()', () => {
  2147. it( 'should call model.createSelection()', () => {
  2148. const stub = sinon.stub( model, 'createSelection' );
  2149. model.change( writer => {
  2150. writer.createSelection();
  2151. } );
  2152. sinon.assert.calledOnce( stub );
  2153. } );
  2154. } );
  2155. function createText( data, attributes ) {
  2156. return model.change( writer => {
  2157. return writer.createText( data, attributes );
  2158. } );
  2159. }
  2160. function createElement( name, attributes ) {
  2161. return model.change( writer => {
  2162. return writer.createElement( name, attributes );
  2163. } );
  2164. }
  2165. function createDocumentFragment() {
  2166. return model.change( writer => {
  2167. return writer.createDocumentFragment();
  2168. } );
  2169. }
  2170. function insert( item, itemOrPosition, offset ) {
  2171. model.enqueueChange( batch, writer => {
  2172. writer.insert( item, itemOrPosition, offset );
  2173. } );
  2174. }
  2175. function insertText( text, attributes, itemOrPosition, offset ) {
  2176. model.enqueueChange( batch, writer => {
  2177. writer.insertText( text, attributes, itemOrPosition, offset );
  2178. } );
  2179. }
  2180. function insertElement( name, attributes, itemOrPosition, offset ) {
  2181. model.enqueueChange( batch, writer => {
  2182. writer.insertElement( name, attributes, itemOrPosition, offset );
  2183. } );
  2184. }
  2185. function append( item, parent ) {
  2186. model.enqueueChange( batch, writer => {
  2187. writer.append( item, parent );
  2188. } );
  2189. }
  2190. function appendText( text, attributes, parent ) {
  2191. model.enqueueChange( batch, writer => {
  2192. writer.appendText( text, attributes, parent );
  2193. } );
  2194. }
  2195. function appendElement( name, attributes, parent ) {
  2196. model.enqueueChange( batch, writer => {
  2197. writer.appendElement( name, attributes, parent );
  2198. } );
  2199. }
  2200. function setAttribute( key, value, itemOrRange ) {
  2201. model.enqueueChange( batch, writer => {
  2202. writer.setAttribute( key, value, itemOrRange );
  2203. } );
  2204. }
  2205. function setAttributes( attributes, itemOrRange ) {
  2206. model.enqueueChange( batch, writer => {
  2207. writer.setAttributes( attributes, itemOrRange );
  2208. } );
  2209. }
  2210. function removeAttribute( key, itemOrRange ) {
  2211. model.enqueueChange( batch, writer => {
  2212. writer.removeAttribute( key, itemOrRange );
  2213. } );
  2214. }
  2215. function clearAttributes( itemOrRange ) {
  2216. model.enqueueChange( batch, writer => {
  2217. writer.clearAttributes( itemOrRange );
  2218. } );
  2219. }
  2220. function move( range, itemOrPosition, offset ) {
  2221. model.enqueueChange( batch, writer => {
  2222. writer.move( range, itemOrPosition, offset );
  2223. } );
  2224. }
  2225. function remove( itemOrRange ) {
  2226. model.enqueueChange( batch, writer => {
  2227. writer.remove( itemOrRange );
  2228. } );
  2229. }
  2230. function merge( position ) {
  2231. model.enqueueChange( batch, writer => {
  2232. writer.merge( position );
  2233. } );
  2234. }
  2235. function rename( element, newName ) {
  2236. model.enqueueChange( batch, writer => {
  2237. writer.rename( element, newName );
  2238. } );
  2239. }
  2240. function split( position, limitElement ) {
  2241. model.enqueueChange( batch, writer => {
  2242. writer.split( position, limitElement );
  2243. } );
  2244. }
  2245. function wrap( range, elementOrString ) {
  2246. model.enqueueChange( batch, writer => {
  2247. writer.wrap( range, elementOrString );
  2248. } );
  2249. }
  2250. function unwrap( element ) {
  2251. model.enqueueChange( batch, writer => {
  2252. writer.unwrap( element );
  2253. } );
  2254. }
  2255. function addMarker( name, options ) {
  2256. let marker;
  2257. model.enqueueChange( batch, writer => {
  2258. marker = writer.addMarker( name, options );
  2259. } );
  2260. return marker;
  2261. }
  2262. function updateMarker( markerOrName, options ) {
  2263. model.enqueueChange( batch, writer => {
  2264. writer.updateMarker( markerOrName, options );
  2265. } );
  2266. }
  2267. function removeMarker( markerOrName, options ) {
  2268. model.enqueueChange( batch, writer => {
  2269. writer.removeMarker( markerOrName, options );
  2270. } );
  2271. }
  2272. function setSelection( selectable, optionsOrPlaceOrOffset, options ) {
  2273. model.enqueueChange( batch, writer => {
  2274. writer.setSelection( selectable, optionsOrPlaceOrOffset, options );
  2275. } );
  2276. }
  2277. function setSelectionFocus( itemOrPosition, offset ) {
  2278. model.enqueueChange( batch, writer => {
  2279. writer.setSelectionFocus( itemOrPosition, offset );
  2280. } );
  2281. }
  2282. function setSelectionAttribute( key, value ) {
  2283. model.enqueueChange( batch, writer => {
  2284. writer.setSelectionAttribute( key, value );
  2285. } );
  2286. }
  2287. function removeSelectionAttribute( key ) {
  2288. model.enqueueChange( batch, writer => {
  2289. writer.removeSelectionAttribute( key );
  2290. } );
  2291. }
  2292. function overrideSelectionGravity() {
  2293. return model.change( writer => {
  2294. return writer.overrideSelectionGravity();
  2295. } );
  2296. }
  2297. function restoreSelectionGravity( overrideUid ) {
  2298. model.change( writer => {
  2299. writer.restoreSelectionGravity( overrideUid );
  2300. } );
  2301. }
  2302. } );