8
0

writer.js 100 KB

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