writer.js 88 KB

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