writer.js 97 KB

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