writer.js 99 KB

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