writer.js 96 KB

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