8
0

writer.js 78 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391
  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 (document -> docFrag)', () => {
  196. const root = doc.createRoot();
  197. const docFrag = createDocumentFragment();
  198. const node = createText( 'foo' );
  199. insert( node, root );
  200. const spy = sinon.spy( model, 'applyOperation' );
  201. insert( node, docFrag );
  202. // Verify result.
  203. expect( Array.from( root.getChildren() ) ).to.deep.equal( [] );
  204. expect( Array.from( docFrag.getChildren() ) ).to.deep.equal( [ node ] );
  205. // Verify deltas and operations.
  206. sinon.assert.calledTwice( spy );
  207. expect( spy.firstCall.args[ 0 ].type ).to.equal( 'remove' );
  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 move element from one parent to the other within different document (docFragA -> docFragB)', () => {
  213. const docFragA = createDocumentFragment();
  214. const docFragB = createDocumentFragment();
  215. const node = createText( 'foo' );
  216. insert( node, docFragA );
  217. const spy = sinon.spy( model, 'applyOperation' );
  218. insert( node, docFragB );
  219. // Verify result.
  220. expect( Array.from( docFragA ) ).to.deep.equal( [] );
  221. expect( Array.from( docFragB ) ).to.deep.equal( [ node ] );
  222. // Verify deltas and operations.
  223. sinon.assert.calledTwice( spy );
  224. expect( spy.firstCall.args[ 0 ].type ).to.equal( 'detach' );
  225. expect( spy.firstCall.args[ 0 ].delta.batch ).to.equal( batch );
  226. expect( spy.secondCall.args[ 0 ].type ).to.equal( 'insert' );
  227. expect( spy.secondCall.args[ 0 ].delta.batch ).to.equal( batch );
  228. } );
  229. it( 'should transfer markers from given DocumentFragment', () => {
  230. const root = doc.createRoot();
  231. const docFrag = createDocumentFragment();
  232. appendText( 'abcd', root );
  233. appendElement( 'p', docFrag );
  234. insertText( 'foo bar', new Position( docFrag, [ 0, 0 ] ) );
  235. const marker = new Range( new Position( docFrag, [ 0, 1 ] ), new Position( docFrag, [ 0, 5 ] ) );
  236. docFrag.markers.set( 'marker', marker );
  237. insert( docFrag, new Position( root, [ 2 ] ) );
  238. expect( Array.from( model.markers ).length ).to.equal( 1 );
  239. const range = model.markers.get( 'marker' ).getRange();
  240. expect( range.root ).to.equal( root );
  241. expect( range.start.path ).to.deep.equal( [ 2, 1 ] );
  242. expect( range.end.path ).to.deep.equal( [ 2, 5 ] );
  243. } );
  244. it( 'should throw when trying to use detached writer', () => {
  245. const writer = new Writer( model, batch );
  246. const root = doc.createRoot();
  247. const node = createText( 'foo' );
  248. expect( () => {
  249. writer.insert( node, root );
  250. } ).to.throw( CKEditorError, /^writer-incorrect-use/ );
  251. } );
  252. } );
  253. describe( 'insertText()', () => {
  254. it( 'should create and insert text node with attributes at given position', () => {
  255. const parent = createDocumentFragment();
  256. insertText( 'foo', { bar: 'biz' }, 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( [ [ 'bar', 'biz' ] ] );
  261. } );
  262. it( 'should create and insert text node with no attributes at given position', () => {
  263. const parent = createDocumentFragment();
  264. insertText( 'foo', null, 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 omitting attributes param', () => {
  271. const parent = createDocumentFragment();
  272. insertText( 'foo', new Position( parent, [ 0 ] ) );
  273. expect( parent.childCount ).to.equal( 1 );
  274. expect( parent.getChild( 0 ) ).to.instanceof( Text );
  275. expect( parent.getChild( 0 ).data ).to.equal( 'foo' );
  276. expect( Array.from( parent.getChild( 0 ).getAttributes() ) ).to.deep.equal( [] );
  277. } );
  278. it( 'should create and insert text node at the beginning of given element', () => {
  279. const parent = createDocumentFragment();
  280. insert( createElement( 'child' ), parent );
  281. insertText( 'foo', parent );
  282. expect( parent.childCount ).to.equal( 2 );
  283. expect( parent.getChild( 0 ) ).to.instanceof( Text );
  284. expect( parent.getChild( 1 ) ).to.instanceof( Element );
  285. } );
  286. it( 'should create and insert text node at the end of given element', () => {
  287. const parent = createDocumentFragment();
  288. insert( createElement( 'child' ), parent );
  289. insertText( 'foo', parent, 'end' );
  290. expect( parent.childCount ).to.equal( 2 );
  291. expect( parent.getChild( 0 ) ).to.instanceof( Element );
  292. expect( parent.getChild( 1 ) ).to.instanceof( Text );
  293. } );
  294. it( 'should create and insert text node at the given offset of given element', () => {
  295. const parent = createDocumentFragment();
  296. insert( createElement( 'child' ), parent );
  297. insert( createElement( 'child' ), parent );
  298. insertText( 'foo', parent, 1 );
  299. expect( parent.childCount ).to.equal( 3 );
  300. expect( parent.getChild( 0 ) ).to.instanceof( Element );
  301. expect( parent.getChild( 1 ) ).to.instanceof( Text );
  302. expect( parent.getChild( 2 ) ).to.instanceof( Element );
  303. } );
  304. it( 'should create and insert text node before the given node', () => {
  305. const parent = createDocumentFragment();
  306. const child1 = createElement( 'child' );
  307. const child2 = createElement( 'child' );
  308. insert( child1, parent );
  309. insert( child2, parent, 'end' );
  310. insertText( 'foo', child2, 'before' );
  311. expect( parent.childCount ).to.equal( 3 );
  312. expect( parent.getChild( 0 ) ).to.instanceof( Element );
  313. expect( parent.getChild( 1 ) ).to.instanceof( Text );
  314. expect( parent.getChild( 2 ) ).to.instanceof( Element );
  315. } );
  316. it( 'should create and insert text node after the given node', () => {
  317. const parent = createDocumentFragment();
  318. const child1 = createElement( 'child' );
  319. const child2 = createElement( 'child' );
  320. insert( child1, parent );
  321. insert( child2, parent, 'end' );
  322. insertText( 'foo', child1, 'after' );
  323. expect( parent.childCount ).to.equal( 3 );
  324. expect( parent.getChild( 0 ) ).to.instanceof( Element );
  325. expect( parent.getChild( 1 ) ).to.instanceof( Text );
  326. expect( parent.getChild( 2 ) ).to.instanceof( Element );
  327. } );
  328. it( 'should create proper delta', () => {
  329. const parent = createDocumentFragment();
  330. const spy = sinon.spy( model, 'applyOperation' );
  331. insertText( 'foo', parent );
  332. sinon.assert.calledOnce( spy );
  333. expect( spy.lastCall.args[ 0 ].type ).to.equal( 'insert' );
  334. expect( spy.lastCall.args[ 0 ].delta ).to.instanceof( WeakInsertDelta );
  335. expect( spy.lastCall.args[ 0 ].delta.batch ).to.equal( batch );
  336. } );
  337. it( 'should throw when trying to use detached writer', () => {
  338. const writer = new Writer( model, batch );
  339. const parent = createDocumentFragment();
  340. expect( () => {
  341. writer.insertText( 'foo', parent );
  342. } ).to.throw( CKEditorError, /^writer-incorrect-use/ );
  343. } );
  344. } );
  345. describe( 'insertElement()', () => {
  346. it( 'should create and insert element with attributes at given position', () => {
  347. const parent = createDocumentFragment();
  348. insertElement( 'foo', { bar: 'biz' }, 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( [ [ 'bar', 'biz' ] ] );
  353. } );
  354. it( 'should create and insert element with no attributes at given position', () => {
  355. const parent = createDocumentFragment();
  356. insertElement( 'foo', null, 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 with no attributes omitting attributes param', () => {
  363. const parent = createDocumentFragment();
  364. insertElement( 'foo', new Position( parent, [ 0 ] ) );
  365. expect( parent.childCount ).to.equal( 1 );
  366. expect( parent.getChild( 0 ) ).to.instanceof( Element );
  367. expect( parent.getChild( 0 ).name ).to.equal( 'foo' );
  368. expect( Array.from( parent.getChild( 0 ).getAttributes() ) ).to.deep.equal( [] );
  369. } );
  370. it( 'should create and insert element at the beginning of given element', () => {
  371. const parent = createDocumentFragment();
  372. insert( createElement( 'child' ), parent );
  373. insertElement( 'foo', parent );
  374. expect( parent.childCount ).to.equal( 2 );
  375. expect( parent.getChild( 0 ).name ).to.equal( 'foo' );
  376. expect( parent.getChild( 1 ).name ).to.equal( 'child' );
  377. } );
  378. it( 'should create and insert element at the end of given element', () => {
  379. const parent = createDocumentFragment();
  380. insert( createElement( 'child' ), parent );
  381. insertElement( 'foo', parent, 'end' );
  382. expect( parent.childCount ).to.equal( 2 );
  383. expect( parent.getChild( 0 ).name ).to.equal( 'child' );
  384. expect( parent.getChild( 1 ).name ).to.equal( 'foo' );
  385. } );
  386. it( 'should create and insert element at the given offset of given element', () => {
  387. const parent = createDocumentFragment();
  388. insert( createElement( 'child1' ), parent );
  389. insert( createElement( 'child2' ), parent, 'end' );
  390. insertElement( 'foo', parent, 1 );
  391. expect( parent.childCount ).to.equal( 3 );
  392. expect( parent.getChild( 0 ).name ).to.equal( 'child1' );
  393. expect( parent.getChild( 1 ).name ).to.equal( 'foo' );
  394. expect( parent.getChild( 2 ).name ).to.equal( 'child2' );
  395. } );
  396. it( 'should create and insert element before the given node', () => {
  397. const parent = createDocumentFragment();
  398. const child1 = createElement( 'child1' );
  399. const child2 = createElement( 'child2' );
  400. insert( child1, parent );
  401. insert( child2, parent, 'end' );
  402. insertElement( 'foo', child2, 'before' );
  403. expect( parent.childCount ).to.equal( 3 );
  404. expect( parent.getChild( 0 ).name ).to.equal( 'child1' );
  405. expect( parent.getChild( 1 ).name ).to.equal( 'foo' );
  406. expect( parent.getChild( 2 ).name ).to.equal( 'child2' );
  407. } );
  408. it( 'should create and insert element after the given node', () => {
  409. const parent = createDocumentFragment();
  410. const child1 = createElement( 'child1' );
  411. const child2 = createElement( 'child2' );
  412. insert( child1, parent );
  413. insert( child2, parent, 'end' );
  414. insertElement( 'foo', child1, 'after' );
  415. expect( parent.childCount ).to.equal( 3 );
  416. expect( parent.getChild( 0 ).name ).to.equal( 'child1' );
  417. expect( parent.getChild( 1 ).name ).to.equal( 'foo' );
  418. expect( parent.getChild( 2 ).name ).to.equal( 'child2' );
  419. } );
  420. it( 'should create proper delta', () => {
  421. const parent = createDocumentFragment();
  422. const spy = sinon.spy( model, 'applyOperation' );
  423. insertText( 'foo', parent );
  424. sinon.assert.calledOnce( spy );
  425. expect( spy.lastCall.args[ 0 ].type ).to.equal( 'insert' );
  426. expect( spy.lastCall.args[ 0 ].delta ).to.instanceof( InsertDelta );
  427. expect( spy.lastCall.args[ 0 ].delta.batch ).to.equal( batch );
  428. } );
  429. it( 'should throw when trying to use detached writer', () => {
  430. const writer = new Writer( model, batch );
  431. const child = createElement( 'child' );
  432. expect( () => {
  433. writer.insertElement( 'foo', child, 'after' );
  434. } ).to.throw( CKEditorError, /^writer-incorrect-use/ );
  435. } );
  436. } );
  437. describe( 'append()', () => {
  438. it( 'should insert element at the end of the parent', () => {
  439. const parent = createDocumentFragment();
  440. const childText = createText( 'foo' );
  441. const childElement = createElement( 'foo' );
  442. append( childText, parent );
  443. append( childElement, parent );
  444. expect( Array.from( parent ) ).to.deep.equal( [ childText, childElement ] );
  445. } );
  446. it( 'should create proper delta', () => {
  447. const parent = createDocumentFragment();
  448. const text = createText( 'foo' );
  449. const spy = sinon.spy( model, 'applyOperation' );
  450. append( text, parent );
  451. sinon.assert.calledOnce( spy );
  452. expect( spy.lastCall.args[ 0 ].type ).to.equal( 'insert' );
  453. expect( spy.lastCall.args[ 0 ].delta.batch ).to.equal( batch );
  454. } );
  455. it( 'should move element from one parent to the other within the same document (documentA -> documentA)', () => {
  456. const rootA = doc.createRoot();
  457. const parent1 = createElement( 'parent' );
  458. const parent2 = createElement( 'parent' );
  459. const node = createText( 'foo' );
  460. insert( node, parent1 );
  461. insert( parent1, rootA );
  462. insert( parent2, rootA );
  463. const spy = sinon.spy( model, 'applyOperation' );
  464. append( node, parent2 );
  465. // Verify result.
  466. expect( Array.from( parent1.getChildren() ) ).to.deep.equal( [] );
  467. expect( Array.from( parent2.getChildren() ) ).to.deep.equal( [ node ] );
  468. // Verify deltas and operations.
  469. sinon.assert.calledOnce( spy );
  470. expect( spy.firstCall.args[ 0 ].type ).to.equal( 'move' );
  471. expect( spy.firstCall.args[ 0 ].delta.batch ).to.equal( batch );
  472. } );
  473. it( 'should move element from one parent to the other within the same document (documentA -> documentB)', () => {
  474. const rootA = doc.createRoot( '$root', 'A' );
  475. const rootB = doc.createRoot( '$root', 'B' );
  476. const node = createText( 'foo' );
  477. insert( node, rootA );
  478. const spy = sinon.spy( model, 'applyOperation' );
  479. append( node, rootB );
  480. // Verify result.
  481. expect( Array.from( rootA.getChildren() ) ).to.deep.equal( [] );
  482. expect( Array.from( rootB.getChildren() ) ).to.deep.equal( [ node ] );
  483. // Verify deltas and operations.
  484. sinon.assert.calledOnce( spy );
  485. expect( spy.firstCall.args[ 0 ].type ).to.equal( 'move' );
  486. expect( spy.firstCall.args[ 0 ].delta.batch ).to.equal( batch );
  487. } );
  488. it( 'should move element from one parent to the other within the same document (docFragA -> docFragA)', () => {
  489. const docFragA = createDocumentFragment();
  490. const parent1 = createElement( 'parent' );
  491. const parent2 = createElement( 'parent' );
  492. const node = createText( 'foo' );
  493. insert( node, parent1 );
  494. insert( parent1, docFragA );
  495. insert( parent2, docFragA );
  496. const spy = sinon.spy( model, 'applyOperation' );
  497. append( node, parent2 );
  498. // Verify result.
  499. expect( Array.from( parent1.getChildren() ) ).to.deep.equal( [] );
  500. expect( Array.from( parent2.getChildren() ) ).to.deep.equal( [ node ] );
  501. // Verify deltas and operations.
  502. sinon.assert.calledOnce( spy );
  503. expect( spy.firstCall.args[ 0 ].type ).to.equal( 'move' );
  504. expect( spy.firstCall.args[ 0 ].delta.batch ).to.equal( batch );
  505. } );
  506. it( 'should move element from one parent to the other within different document (document -> docFrag)', () => {
  507. const root = doc.createRoot();
  508. const docFrag = createDocumentFragment();
  509. const node = createText( 'foo' );
  510. insert( node, root );
  511. const spy = sinon.spy( model, 'applyOperation' );
  512. append( node, docFrag );
  513. // Verify result.
  514. expect( Array.from( root.getChildren() ) ).to.deep.equal( [] );
  515. expect( Array.from( docFrag.getChildren() ) ).to.deep.equal( [ node ] );
  516. // Verify deltas and operations.
  517. sinon.assert.calledTwice( spy );
  518. expect( spy.firstCall.args[ 0 ].type ).to.equal( 'remove' );
  519. expect( spy.firstCall.args[ 0 ].delta.batch ).to.equal( batch );
  520. expect( spy.secondCall.args[ 0 ].type ).to.equal( 'insert' );
  521. expect( spy.secondCall.args[ 0 ].delta.batch ).to.equal( batch );
  522. } );
  523. it( 'should move element from one parent to the other within different document (docFragA -> docFragB)', () => {
  524. const docFragA = createDocumentFragment();
  525. const docFragB = createDocumentFragment();
  526. const node = createText( 'foo' );
  527. insert( node, docFragA );
  528. const spy = sinon.spy( model, 'applyOperation' );
  529. append( node, docFragB );
  530. // Verify result.
  531. expect( Array.from( docFragA ) ).to.deep.equal( [] );
  532. expect( Array.from( docFragB ) ).to.deep.equal( [ node ] );
  533. // Verify deltas and operations.
  534. sinon.assert.calledTwice( spy );
  535. expect( spy.firstCall.args[ 0 ].type ).to.equal( 'detach' );
  536. expect( spy.firstCall.args[ 0 ].delta.batch ).to.equal( batch );
  537. expect( spy.secondCall.args[ 0 ].type ).to.equal( 'insert' );
  538. expect( spy.secondCall.args[ 0 ].delta.batch ).to.equal( batch );
  539. } );
  540. } );
  541. describe( 'appendText()', () => {
  542. it( 'should create and insert text node with attributes at the end of the parent', () => {
  543. const parent = createDocumentFragment();
  544. appendText( 'foo', { bar: 'biz' }, parent );
  545. appendText( 'bar', { biz: 'bar' }, parent );
  546. expect( parent.childCount ).to.equal( 2 );
  547. expect( parent.getChild( 0 ).data ).to.equal( 'foo' );
  548. expect( Array.from( parent.getChild( 0 ).getAttributes() ) ).to.deep.equal( [ [ 'bar', 'biz' ] ] );
  549. expect( parent.getChild( 1 ).data ).to.equal( 'bar' );
  550. expect( Array.from( parent.getChild( 1 ).getAttributes() ) ).to.deep.equal( [ [ 'biz', 'bar' ] ] );
  551. } );
  552. it( 'should create and insert text node with no attributes at the end of the parent', () => {
  553. const parent = createDocumentFragment();
  554. appendText( 'foo', null, parent );
  555. expect( parent.childCount ).to.equal( 1 );
  556. expect( parent.getChild( 0 ) ).to.instanceof( Text );
  557. expect( parent.getChild( 0 ).data ).to.equal( 'foo' );
  558. expect( Array.from( parent.getChild( 0 ).getAttributes() ) ).to.deep.equal( [] );
  559. } );
  560. it( 'should create and insert text node with no attributes omitting attributes param', () => {
  561. const parent = createDocumentFragment();
  562. appendText( 'foo', parent );
  563. expect( parent.childCount ).to.equal( 1 );
  564. expect( parent.getChild( 0 ) ).to.instanceof( Text );
  565. expect( parent.getChild( 0 ).data ).to.equal( 'foo' );
  566. expect( Array.from( parent.getChild( 0 ).getAttributes() ) ).to.deep.equal( [] );
  567. } );
  568. it( 'should create proper delta and operations', () => {
  569. const parent = createDocumentFragment();
  570. const spy = sinon.spy( model, 'applyOperation' );
  571. appendText( 'foo', parent );
  572. sinon.assert.calledOnce( spy );
  573. expect( spy.firstCall.args[ 0 ].type ).to.equal( 'insert' );
  574. expect( spy.firstCall.args[ 0 ].delta ).to.instanceof( WeakInsertDelta );
  575. expect( spy.firstCall.args[ 0 ].delta.batch ).to.equal( batch );
  576. } );
  577. it( 'should throw when trying to use detached writer', () => {
  578. const writer = new Writer( model, batch );
  579. const parent = createDocumentFragment();
  580. expect( () => {
  581. writer.appendText( 'foo', parent );
  582. } ).to.throw( CKEditorError, /^writer-incorrect-use/ );
  583. } );
  584. } );
  585. describe( 'appendElement()', () => {
  586. it( 'should create and insert element with attributes at the end of the parent', () => {
  587. const parent = createDocumentFragment();
  588. appendElement( 'foo', { bar: 'biz' }, parent );
  589. appendElement( 'bar', { biz: 'bar' }, parent );
  590. expect( parent.childCount ).to.equal( 2 );
  591. expect( parent.getChild( 0 ).name ).to.equal( 'foo' );
  592. expect( Array.from( parent.getChild( 0 ).getAttributes() ) ).to.deep.equal( [ [ 'bar', 'biz' ] ] );
  593. expect( parent.getChild( 1 ).name ).to.equal( 'bar' );
  594. expect( Array.from( parent.getChild( 1 ).getAttributes() ) ).to.deep.equal( [ [ 'biz', 'bar' ] ] );
  595. } );
  596. it( 'should create and insert element with no attributes at the end of the parent', () => {
  597. const parent = createDocumentFragment();
  598. appendElement( 'foo', null, parent );
  599. expect( parent.childCount ).to.equal( 1 );
  600. expect( parent.getChild( 0 ).name ).to.equal( 'foo' );
  601. expect( Array.from( parent.getChild( 0 ).getAttributes() ) ).to.deep.equal( [] );
  602. } );
  603. it( 'should create and insert element with no attributes omitting attributes param', () => {
  604. const parent = createDocumentFragment();
  605. appendElement( 'foo', parent );
  606. expect( parent.childCount ).to.equal( 1 );
  607. expect( parent.getChild( 0 ).name ).to.equal( 'foo' );
  608. expect( Array.from( parent.getChild( 0 ).getAttributes() ) ).to.deep.equal( [] );
  609. } );
  610. it( 'should create proper delta and operation', () => {
  611. const parent = createDocumentFragment();
  612. const spy = sinon.spy( model, 'applyOperation' );
  613. appendElement( 'foo', parent );
  614. sinon.assert.calledOnce( spy );
  615. expect( spy.firstCall.args[ 0 ].type ).to.equal( 'insert' );
  616. expect( spy.firstCall.args[ 0 ].delta ).to.instanceof( InsertDelta ).to.not.instanceof( WeakInsertDelta );
  617. expect( spy.firstCall.args[ 0 ].delta.batch ).to.equal( batch );
  618. } );
  619. it( 'should throw when trying to use detached writer', () => {
  620. const writer = new Writer( model, batch );
  621. const parent = createDocumentFragment();
  622. expect( () => {
  623. writer.appendElement( 'foo', parent );
  624. } ).to.throw( CKEditorError, /^writer-incorrect-use/ );
  625. } );
  626. } );
  627. describe( 'setAttribute() / removeAttribute()', () => {
  628. let root, spy;
  629. beforeEach( () => {
  630. root = doc.createRoot();
  631. } );
  632. describe( 'change attribute on node', () => {
  633. let node, text;
  634. beforeEach( () => {
  635. node = createElement( 'p', { a: 1 } );
  636. text = createText( 'c', { a: 1 } );
  637. append( node, root );
  638. append( text, root );
  639. spy = sinon.spy( model, 'applyOperation' );
  640. } );
  641. describe( 'setAttribute', () => {
  642. it( 'should create the attribute on element', () => {
  643. setAttribute( 'b', 2, node );
  644. expect( spy.callCount ).to.equal( 1 );
  645. expect( node.getAttribute( 'b' ) ).to.equal( 2 );
  646. } );
  647. it( 'should change the attribute of element', () => {
  648. setAttribute( 'a', 2, node );
  649. expect( spy.callCount ).to.equal( 1 );
  650. expect( node.getAttribute( 'a' ) ).to.equal( 2 );
  651. } );
  652. it( 'should create the attribute on text node', () => {
  653. setAttribute( 'b', 2, text );
  654. expect( spy.callCount ).to.equal( 1 );
  655. expect( root.getChild( 1 ).getAttribute( 'b' ) ).to.equal( 2 );
  656. } );
  657. it( 'should change the attribute of text node', () => {
  658. setAttribute( 'a', 2, text );
  659. expect( spy.callCount ).to.equal( 1 );
  660. expect( root.getChild( 1 ).getAttribute( 'a' ) ).to.equal( 2 );
  661. } );
  662. it( 'should do nothing if the attribute value is the same', () => {
  663. setAttribute( 'a', 1, node );
  664. expect( spy.callCount ).to.equal( 0 );
  665. expect( node.getAttribute( 'a' ) ).to.equal( 1 );
  666. } );
  667. it( 'should throw when trying to use detached writer', () => {
  668. const writer = new Writer( model, batch );
  669. expect( () => {
  670. writer.setAttribute( 'a', 1, node );
  671. } ).to.throw( CKEditorError, /^writer-incorrect-use/ );
  672. } );
  673. } );
  674. describe( 'removeAttribute', () => {
  675. it( 'should remove the attribute from element', () => {
  676. removeAttribute( 'a', node );
  677. expect( spy.callCount ).to.equal( 1 );
  678. expect( node.getAttribute( 'a' ) ).to.be.undefined;
  679. } );
  680. it( 'should remove the attribute from character', () => {
  681. removeAttribute( 'a', text );
  682. expect( spy.callCount ).to.equal( 1 );
  683. expect( root.getChild( 1 ).getAttribute( 'a' ) ).to.be.undefined;
  684. } );
  685. it( 'should do nothing if the attribute is not set', () => {
  686. removeAttribute( 'b', node );
  687. expect( spy.callCount ).to.equal( 0 );
  688. } );
  689. it( 'should throw when trying to use detached writer', () => {
  690. const writer = new Writer( model, batch );
  691. expect( () => {
  692. writer.removeAttribute( 'b', node );
  693. } ).to.throw( CKEditorError, /^writer-incorrect-use/ );
  694. } );
  695. } );
  696. } );
  697. describe( 'change attribute on range', () => {
  698. beforeEach( () => {
  699. const element = createElement( 'e', { a: 2 } );
  700. appendText( 'xxx', { a: 1 }, root );
  701. appendText( 'xxx', root );
  702. appendText( 'xxx', { a: 1 }, root );
  703. appendText( 'xxx', { a: 2 }, root );
  704. appendText( 'xxx', root );
  705. appendText( 'xxx', { a: 1 }, root );
  706. appendText( 'xxx', element );
  707. append( element, root );
  708. appendText( 'xxx', root );
  709. spy = sinon.spy( model, 'applyOperation' );
  710. } );
  711. function getRange( startIndex, endIndex ) {
  712. return new Range(
  713. Position.createFromParentAndOffset( root, startIndex ),
  714. Position.createFromParentAndOffset( root, endIndex )
  715. );
  716. }
  717. function getChangesAttrsCount() {
  718. let totalNumber = 0;
  719. for ( const delta of batch.deltas ) {
  720. for ( const operation of delta.operations ) {
  721. if ( operation.range ) {
  722. totalNumber += count( operation.range.getItems( { singleCharacters: true } ) );
  723. }
  724. }
  725. }
  726. return totalNumber;
  727. }
  728. function getCompressedAttrs() {
  729. // default: 111---111222---1112------
  730. const range = Range.createIn( root );
  731. return Array.from( range.getItems( { singleCharacters: true } ) )
  732. .map( item => item.getAttribute( 'a' ) || '-' )
  733. .join( '' );
  734. }
  735. describe( 'setAttribute', () => {
  736. it( 'should set the attribute on the range', () => {
  737. setAttribute( 'a', 3, getRange( 3, 6 ) );
  738. expect( spy.callCount ).to.equal( 1 );
  739. expect( getChangesAttrsCount() ).to.equal( 3 );
  740. expect( getCompressedAttrs() ).to.equal( '111333111222---1112------' );
  741. } );
  742. it( 'should split the operations if parts of the range have different attributes', () => {
  743. setAttribute( 'a', 3, getRange( 4, 14 ) );
  744. expect( spy.callCount ).to.equal( 4 );
  745. expect( getChangesAttrsCount() ).to.equal( 10 );
  746. expect( getCompressedAttrs() ).to.equal( '111-3333333333-1112------' );
  747. } );
  748. it( 'should split the operations if parts of the part of the range have the attribute', () => {
  749. setAttribute( 'a', 2, getRange( 4, 14 ) );
  750. expect( spy.callCount ).to.equal( 3 );
  751. expect( getChangesAttrsCount() ).to.equal( 7 );
  752. expect( getCompressedAttrs() ).to.equal( '111-2222222222-1112------' );
  753. } );
  754. it( 'should strip the range if the beginning have the attribute', () => {
  755. setAttribute( 'a', 1, getRange( 1, 5 ) );
  756. expect( spy.callCount ).to.equal( 1 );
  757. expect( getChangesAttrsCount() ).to.equal( 2 );
  758. expect( getCompressedAttrs() ).to.equal( '11111-111222---1112------' );
  759. } );
  760. it( 'should strip the range if the ending have the attribute', () => {
  761. setAttribute( 'a', 1, getRange( 13, 17 ) );
  762. expect( spy.callCount ).to.equal( 1 );
  763. expect( getChangesAttrsCount() ).to.equal( 2 );
  764. expect( getCompressedAttrs() ).to.equal( '111---111222-111112------' );
  765. } );
  766. it( 'should do nothing if the range has attribute', () => {
  767. setAttribute( 'a', 1, getRange( 0, 3 ) );
  768. expect( spy.callCount ).to.equal( 0 );
  769. expect( getCompressedAttrs() ).to.equal( '111---111222---1112------' );
  770. } );
  771. it( 'should not check range\'s start position node when creating operations', () => {
  772. const range = new Range(
  773. new Position( root, [ 18, 1 ] ),
  774. new Position( root, [ 19 ] )
  775. );
  776. setAttribute( 'a', 1, range );
  777. expect( spy.callCount ).to.equal( 1 );
  778. expect( getChangesAttrsCount() ).to.equal( 2 );
  779. expect( getCompressedAttrs() ).to.equal( '111---111222---1112-11---' );
  780. } );
  781. it( 'should not change elements attribute if range contains closing tag', () => {
  782. const range = new Range(
  783. new Position( root, [ 18, 1 ] ),
  784. new Position( root, [ 21 ] )
  785. );
  786. setAttribute( 'a', 1, range );
  787. expect( spy.callCount ).to.equal( 1 );
  788. expect( getChangesAttrsCount() ).to.equal( 4 );
  789. expect( getCompressedAttrs() ).to.equal( '111---111222---1112-1111-' );
  790. } );
  791. it( 'should not create an operation if the range contains only closing tag', () => {
  792. const range = new Range(
  793. new Position( root, [ 18, 3 ] ),
  794. new Position( root, [ 19 ] )
  795. );
  796. setAttribute( 'a', 3, range );
  797. expect( spy.callCount ).to.equal( 0 );
  798. expect( getCompressedAttrs() ).to.equal( '111---111222---1112------' );
  799. } );
  800. it( 'should not create an operation if is collapsed', () => {
  801. setAttribute( 'a', 1, getRange( 3, 3 ) );
  802. expect( spy.callCount ).to.equal( 0 );
  803. expect( getCompressedAttrs() ).to.equal( '111---111222---1112------' );
  804. } );
  805. it( 'should create a proper operations for the mixed range', () => {
  806. setAttribute( 'a', 1, getRange( 0, 20 ) );
  807. expect( spy.callCount ).to.equal( 5 );
  808. expect( getChangesAttrsCount() ).to.equal( 14 );
  809. expect( getCompressedAttrs() ).to.equal( '11111111111111111111111--' );
  810. } );
  811. it( 'should throw when trying to use detached writer', () => {
  812. const writer = new Writer( model, batch );
  813. expect( () => {
  814. writer.setAttribute( 'a', 1, getRange( 0, 20 ) );
  815. } ).to.throw( CKEditorError, /^writer-incorrect-use/ );
  816. } );
  817. } );
  818. describe( 'removeAttribute', () => {
  819. it( 'should remove the attribute on the range', () => {
  820. removeAttribute( 'a', getRange( 0, 2 ) );
  821. expect( spy.callCount ).to.equal( 1 );
  822. expect( getChangesAttrsCount() ).to.equal( 2 );
  823. expect( getCompressedAttrs() ).to.equal( '--1---111222---1112------' );
  824. } );
  825. it( 'should split the operations if parts of the range have different attributes', () => {
  826. removeAttribute( 'a', getRange( 7, 11 ) );
  827. expect( spy.callCount ).to.equal( 2 );
  828. expect( getChangesAttrsCount() ).to.equal( 4 );
  829. expect( getCompressedAttrs() ).to.equal( '111---1----2---1112------' );
  830. } );
  831. it( 'should split the operations if parts of the part of the range have no attribute', () => {
  832. removeAttribute( 'a', getRange( 1, 7 ) );
  833. expect( spy.callCount ).to.equal( 2 );
  834. expect( getChangesAttrsCount() ).to.equal( 3 );
  835. expect( getCompressedAttrs() ).to.equal( '1------11222---1112------' );
  836. } );
  837. it( 'should strip the range if the beginning have no attribute', () => {
  838. removeAttribute( 'a', getRange( 4, 12 ) );
  839. expect( spy.callCount ).to.equal( 2 );
  840. expect( getChangesAttrsCount() ).to.equal( 6 );
  841. expect( getCompressedAttrs() ).to.equal( '111------------1112------' );
  842. } );
  843. it( 'should strip the range if the ending have no attribute', () => {
  844. removeAttribute( 'a', getRange( 7, 15 ) );
  845. expect( spy.callCount ).to.equal( 2 );
  846. expect( getChangesAttrsCount() ).to.equal( 5 );
  847. expect( getCompressedAttrs() ).to.equal( '111---1--------1112------' );
  848. } );
  849. it( 'should do nothing if the range has no attribute', () => {
  850. removeAttribute( 'a', getRange( 4, 5 ) );
  851. expect( spy.callCount ).to.equal( 0 );
  852. expect( getCompressedAttrs() ).to.equal( '111---111222---1112------' );
  853. } );
  854. it( 'should not check range\'s start position node when creating operations', () => {
  855. const range = new Range(
  856. new Position( root, [ 18, 3 ] ),
  857. new Position( root, [ 19 ] )
  858. );
  859. removeAttribute( 'a', range );
  860. expect( spy.callCount ).to.equal( 0 );
  861. expect( getChangesAttrsCount() ).to.equal( 0 );
  862. expect( getCompressedAttrs() ).to.equal( '111---111222---1112------' );
  863. } );
  864. it( 'should not apply operation twice in the range contains opening and closing tags', () => {
  865. removeAttribute( 'a', getRange( 18, 22 ) );
  866. expect( spy.callCount ).to.equal( 1 );
  867. expect( getChangesAttrsCount() ).to.equal( 1 );
  868. expect( getCompressedAttrs() ).to.equal( '111---111222---111-------' );
  869. } );
  870. it( 'should not create an operation if range is collapsed', () => {
  871. removeAttribute( 'a', getRange( 3, 3 ) );
  872. expect( spy.callCount ).to.equal( 0 );
  873. expect( getCompressedAttrs() ).to.equal( '111---111222---1112------' );
  874. } );
  875. it( 'should create a proper operations for the mixed range', () => {
  876. removeAttribute( 'a', getRange( 3, 15 ) );
  877. expect( spy.callCount ).to.equal( 2 );
  878. expect( getChangesAttrsCount() ).to.equal( 6 );
  879. expect( getCompressedAttrs() ).to.equal( '111------------1112------' );
  880. } );
  881. it( 'should throw when trying to use detached writer', () => {
  882. const writer = new Writer( model, batch );
  883. expect( () => {
  884. writer.removeAttribute( 'a', getRange( 3, 15 ) );
  885. } ).to.throw( CKEditorError, /^writer-incorrect-use/ );
  886. } );
  887. } );
  888. } );
  889. describe( 'change attribute on root element', () => {
  890. let p;
  891. beforeEach( () => {
  892. p = createElement( 'p', { a: 3 } );
  893. spy = sinon.spy( model, 'applyOperation' );
  894. } );
  895. describe( 'setAttribute', () => {
  896. it( 'should create the attribute on root', () => {
  897. setAttribute( 'b', 2, root );
  898. expect( spy.callCount ).to.equal( 1 );
  899. expect( root.getAttribute( 'b' ) ).to.equal( 2 );
  900. } );
  901. it( 'should create the attribute on detached root', () => {
  902. setAttribute( 'b', 2, p );
  903. expect( spy.callCount ).to.equal( 1 );
  904. expect( p.getAttribute( 'b' ) ).to.equal( 2 );
  905. } );
  906. it( 'should change the attribute of root', () => {
  907. setAttribute( 'a', 2, root );
  908. expect( spy.callCount ).to.equal( 1 );
  909. expect( root.getAttribute( 'a' ) ).to.equal( 2 );
  910. } );
  911. it( 'should change the attribute of detached root', () => {
  912. setAttribute( 'a', 2, p );
  913. expect( spy.callCount ).to.equal( 1 );
  914. expect( p.getAttribute( 'a' ) ).to.equal( 2 );
  915. } );
  916. it( 'should do nothing if the attribute value is the same', () => {
  917. setAttribute( 'a', 1, root );
  918. expect( spy.callCount ).to.equal( 1 );
  919. setAttribute( 'a', 1, root );
  920. expect( spy.callCount ).to.equal( 1 );
  921. expect( root.getAttribute( 'a' ) ).to.equal( 1 );
  922. } );
  923. it( 'should do nothing if the attribute value is the same on detached root', () => {
  924. setAttribute( 'a', 1, p );
  925. expect( spy.callCount ).to.equal( 1 );
  926. setAttribute( 'a', 1, p );
  927. expect( spy.callCount ).to.equal( 1 );
  928. expect( p.getAttribute( 'a' ) ).to.equal( 1 );
  929. } );
  930. it( 'should throw when trying to use detached writer', () => {
  931. const writer = new Writer( model, batch );
  932. expect( () => {
  933. writer.setAttribute( 'a', 1, p );
  934. } ).to.throw( CKEditorError, /^writer-incorrect-use/ );
  935. } );
  936. } );
  937. describe( 'removeAttribute', () => {
  938. it( 'should remove the attribute from root', () => {
  939. setAttribute( 'a', 1, root );
  940. removeAttribute( 'a', root );
  941. expect( spy.callCount ).to.equal( 2 );
  942. expect( root.getAttribute( 'a' ) ).to.be.undefined;
  943. } );
  944. it( 'should do nothing if the attribute is not set', () => {
  945. removeAttribute( 'b', root );
  946. expect( spy.callCount ).to.equal( 0 );
  947. } );
  948. it( 'should throw when trying to use detached writer', () => {
  949. const writer = new Writer( model, batch );
  950. expect( () => {
  951. writer.removeAttribute( 'b', root );
  952. } ).to.throw( CKEditorError, /^writer-incorrect-use/ );
  953. } );
  954. } );
  955. describe( 'clearAttributes', () => {
  956. it( 'should clear attributes from range', () => {
  957. appendText( 'xxx', { a: 1, b: 2, c: 3 }, root );
  958. appendText( 'xxx', root );
  959. appendText( 'xxx', { a: 1 }, root );
  960. appendText( 'xxx', { b: 2 }, root );
  961. appendText( 'xxx', root );
  962. appendElement( 'e', { a: 1 }, root );
  963. appendText( 'xxx', root );
  964. const range = Range.createIn( root );
  965. clearAttributes( range );
  966. let itemsCount = 0;
  967. for ( const item of range.getItems() ) {
  968. itemsCount++;
  969. expect( Array.from( item.getAttributeKeys() ).length ).to.equal( 0 );
  970. }
  971. expect( itemsCount ).to.equal( 3 );
  972. } );
  973. it( 'should clear attributes on element', () => {
  974. const element = createElement( 'x', { a: 1, b: 2, c: 3 }, root );
  975. expect( Array.from( element.getAttributeKeys() ).length ).to.equal( 3 );
  976. clearAttributes( element );
  977. expect( Array.from( element.getAttributeKeys() ).length ).to.equal( 0 );
  978. } );
  979. it( 'should clear attributes on root element', () => {
  980. setAttributes( { a: 1, b: 2, c: 3 }, root );
  981. expect( Array.from( root.getAttributeKeys() ).length ).to.equal( 3 );
  982. clearAttributes( root );
  983. expect( Array.from( root.getAttributeKeys() ).length ).to.equal( 0 );
  984. } );
  985. it( 'should do nothing if there are no attributes', () => {
  986. const element = createElement( 'x' );
  987. expect( Array.from( element.getAttributeKeys() ).length ).to.equal( 0 );
  988. clearAttributes( element );
  989. expect( Array.from( element.getAttributeKeys() ).length ).to.equal( 0 );
  990. } );
  991. it( 'should throw when trying to use detached writer', () => {
  992. const writer = new Writer( model, batch );
  993. const element = createElement( 'x' );
  994. expect( () => {
  995. writer.clearAttributes( element );
  996. } ).to.throw( CKEditorError, /^writer-incorrect-use/ );
  997. } );
  998. } );
  999. } );
  1000. it( 'should not add empty delta to the batch', () => {
  1001. const nodeA = new Element( 'p', { a: 1 } );
  1002. const nodeB = new Element( 'p', { b: 2 } );
  1003. root.insertChildren( 0, [ nodeA, nodeB ] );
  1004. setAttribute( 'a', 1, nodeA );
  1005. expect( batch.deltas.length ).to.equal( 0 );
  1006. removeAttribute( 'x', Range.createIn( root ) );
  1007. expect( batch.deltas.length ).to.equal( 0 );
  1008. } );
  1009. } );
  1010. describe( 'setAttributes()', () => {
  1011. let frag, item;
  1012. beforeEach( () => {
  1013. frag = createDocumentFragment();
  1014. item = createText( 'xxx', { b: 2, c: 3 } );
  1015. appendText( 'xxx', { a: 1 }, frag );
  1016. append( item, frag );
  1017. } );
  1018. it( 'should set attributes one by one on range', () => {
  1019. const range = Range.createIn( frag );
  1020. let spy;
  1021. model.change( writer => {
  1022. // `setAttribute` is a not trivial operation and is deeply tested above, there is no point to duplicate
  1023. // such a big amount of the same tests, so let's use a spy here.
  1024. spy = sinon.spy( writer, 'setAttribute' );
  1025. writer.setAttributes( { a: 3, c: null }, range );
  1026. } );
  1027. // Verify result.
  1028. expect( Array.from( frag.getChild( 0 ).getAttributes() ) ).to.deep.equal( [ [ 'a', 3 ] ] );
  1029. expect( Array.from( frag.getChild( 1 ).getAttributes() ) ).to.deep.equal( [ [ 'b', 2 ], [ 'a', 3 ] ] );
  1030. // Verify operations
  1031. sinon.assert.calledTwice( spy );
  1032. sinon.assert.calledWith( spy.firstCall, 'a', 3, range );
  1033. sinon.assert.calledWith( spy.secondCall, 'c', null, range );
  1034. } );
  1035. it( 'should set attributes one by one on range for map as attributes list', () => {
  1036. const range = Range.createIn( frag );
  1037. let spy;
  1038. model.change( writer => {
  1039. // `setAttribute` is a not trivial operation and is deeply tested above, there is no point to duplicate
  1040. // such a big amount of the same tests, so let's use a spy here.
  1041. spy = sinon.spy( writer, 'setAttribute' );
  1042. writer.setAttributes( new Map( [ [ 'a', 3 ], [ 'c', null ] ] ), range );
  1043. } );
  1044. // Verify result.
  1045. expect( Array.from( frag.getChild( 0 ).getAttributes() ) ).to.deep.equal( [ [ 'a', 3 ] ] );
  1046. expect( Array.from( frag.getChild( 1 ).getAttributes() ) ).to.deep.equal( [ [ 'b', 2 ], [ 'a', 3 ] ] );
  1047. // Verify operations
  1048. sinon.assert.calledTwice( spy );
  1049. sinon.assert.calledWith( spy.firstCall, 'a', 3, range );
  1050. sinon.assert.calledWith( spy.secondCall, 'c', null, range );
  1051. } );
  1052. it( 'should set attributes one by one on item', () => {
  1053. let spy;
  1054. model.change( writer => {
  1055. // `setAttribute` is a not trivial operation and is deeply tested above, there is no point to duplicate
  1056. // such a big amount of the same tests, so let's use a spy here.
  1057. spy = sinon.spy( writer, 'setAttribute' );
  1058. writer.setAttributes( { a: 3, c: null }, item );
  1059. } );
  1060. // Verify result.
  1061. expect( Array.from( item.getAttributes() ) ).to.deep.equal( [ [ 'b', 2 ], [ 'a', 3 ] ] );
  1062. // Verify operations
  1063. sinon.assert.calledTwice( spy );
  1064. sinon.assert.calledWith( spy.firstCall, 'a', 3, item );
  1065. sinon.assert.calledWith( spy.secondCall, 'c', null, item );
  1066. } );
  1067. it( 'should set attributes one by one on item for maps as attributes list', () => {
  1068. let spy;
  1069. model.change( writer => {
  1070. // `setAttribute` is a not trivial operation and is deeply tested above, there is no point to duplicate
  1071. // such a big amount of the same tests, so let's use a spy here.
  1072. spy = sinon.spy( writer, 'setAttribute' );
  1073. writer.setAttributes( new Map( [ [ 'a', 3 ], [ 'c', null ] ] ), item );
  1074. } );
  1075. // Verify result.
  1076. expect( Array.from( item.getAttributes() ) ).to.deep.equal( [ [ 'b', 2 ], [ 'a', 3 ] ] );
  1077. // Verify operations
  1078. sinon.assert.calledTwice( spy );
  1079. sinon.assert.calledWith( spy.firstCall, 'a', 3, item );
  1080. sinon.assert.calledWith( spy.secondCall, 'c', null, item );
  1081. } );
  1082. it( 'should throw when trying to use detached writer', () => {
  1083. const writer = new Writer( model, batch );
  1084. expect( () => {
  1085. writer.setAttributes( new Map( [ [ 'a', 3 ], [ 'c', null ] ] ), item );
  1086. } ).to.throw( CKEditorError, /^writer-incorrect-use/ );
  1087. } );
  1088. } );
  1089. describe( 'merge()', () => {
  1090. let root, p1, p2;
  1091. beforeEach( () => {
  1092. root = doc.createRoot();
  1093. p1 = new Element( 'p', { key1: 'value1' }, new Text( 'foo' ) );
  1094. p2 = new Element( 'p', { key2: 'value2' }, new Text( 'bar' ) );
  1095. root.insertChildren( 0, [ p1, p2 ] );
  1096. } );
  1097. it( 'should merge foo and bar into foobar', () => {
  1098. merge( new Position( root, [ 1 ] ) );
  1099. expect( root.maxOffset ).to.equal( 1 );
  1100. expect( root.getChild( 0 ).name ).to.equal( 'p' );
  1101. expect( root.getChild( 0 ).maxOffset ).to.equal( 6 );
  1102. expect( count( root.getChild( 0 ).getAttributes() ) ).to.equal( 1 );
  1103. expect( root.getChild( 0 ).getAttribute( 'key1' ) ).to.equal( 'value1' );
  1104. expect( root.getChild( 0 ).getChild( 0 ).data ).to.equal( 'foobar' );
  1105. } );
  1106. it( 'should correctly merge in document fragment', () => {
  1107. const docFrag = new DocumentFragment( [
  1108. new Element( 'p', null, 'foo' ),
  1109. new Element( 'p', null, 'bar' )
  1110. ] );
  1111. merge( new Position( docFrag, [ 1 ] ) );
  1112. expect( docFrag.getChild( 0 ).name ).to.equal( 'p' );
  1113. expect( docFrag.getChild( 0 ).getChild( 0 ).data ).to.equal( 'foobar' );
  1114. } );
  1115. it( 'should throw if there is no element after', () => {
  1116. expect( () => {
  1117. merge( new Position( root, [ 2 ] ) );
  1118. } ).to.throw( CKEditorError, /^writer-merge-no-element-after/ );
  1119. } );
  1120. it( 'should throw if there is no element before', () => {
  1121. expect( () => {
  1122. merge( new Position( root, [ 0, 2 ] ) );
  1123. } ).to.throw( CKEditorError, /^writer-merge-no-element-before/ );
  1124. } );
  1125. it( 'should throw when trying to use detached writer', () => {
  1126. const writer = new Writer( model, batch );
  1127. expect( () => {
  1128. writer.merge( new Position( root, [ 1 ] ) );
  1129. } ).to.throw( CKEditorError, /^writer-incorrect-use/ );
  1130. } );
  1131. } );
  1132. describe( 'move()', () => {
  1133. let root, range, div, p;
  1134. beforeEach( () => {
  1135. root = doc.createRoot();
  1136. div = new Element( 'div', [], new Text( 'foobar' ) );
  1137. p = new Element( 'p', [], new Text( 'abcxyz' ) );
  1138. div.insertChildren( 0, [ new Element( 'p', [], new Text( 'gggg' ) ) ] );
  1139. div.insertChildren( 2, [ new Element( 'p', [], new Text( 'hhhh' ) ) ] );
  1140. root.insertChildren( 0, [ div, p ] );
  1141. range = new Range( new Position( root, [ 0, 3 ] ), new Position( root, [ 0, 7 ] ) );
  1142. } );
  1143. it( 'should move flat range of nodes', () => {
  1144. move( range, new Position( root, [ 1, 3 ] ) );
  1145. expect( getNodesAndText( Range.createIn( root.getChild( 0 ) ) ) ).to.equal( 'PggggPfoPhhhhP' );
  1146. expect( getNodesAndText( Range.createIn( root.getChild( 1 ) ) ) ).to.equal( 'abcobarxyz' );
  1147. } );
  1148. it( 'should throw if object to move is not a range', () => {
  1149. expect( () => {
  1150. move( root.getChild( 0 ), new Position( root, [ 1, 3 ] ) );
  1151. } ).to.throw( CKEditorError, /^writer-move-invalid-range/ );
  1152. } );
  1153. it( 'should throw if given range is not flat', () => {
  1154. const notFlatRange = new Range( new Position( root, [ 0, 2, 2 ] ), new Position( root, [ 0, 6 ] ) );
  1155. expect( () => {
  1156. move( notFlatRange, new Position( root, [ 1, 3 ] ) );
  1157. } ).to.throw( CKEditorError, /^writer-move-range-not-flat/ );
  1158. } );
  1159. it( 'should throw if range is going to be moved to the other document', () => {
  1160. const docFrag = createDocumentFragment();
  1161. expect( () => {
  1162. move( range, docFrag );
  1163. } ).to.throw( CKEditorError, /^writer-move-different-document/ );
  1164. } );
  1165. it( 'should throw when trying to use detached writer', () => {
  1166. const writer = new Writer( model, batch );
  1167. expect( () => {
  1168. writer.move( range, new Position( root, [ 1, 3 ] ) );
  1169. } ).to.throw( CKEditorError, /^writer-incorrect-use/ );
  1170. } );
  1171. } );
  1172. describe( 'remove()', () => {
  1173. let div, p, range;
  1174. beforeEach( () => {
  1175. div = createElement( 'div' );
  1176. appendText( 'foobar', div );
  1177. p = createElement( 'p' );
  1178. appendText( 'abcxyz', p );
  1179. insertElement( 'p', div );
  1180. appendElement( 'p', div );
  1181. insertText( 'gggg', new Position( div, [ 0, 0 ] ) );
  1182. insertText( 'hhhh', new Position( div, [ 7, 0 ] ) );
  1183. } );
  1184. describe( 'remove from document', () => {
  1185. let root;
  1186. beforeEach( () => {
  1187. root = doc.createRoot();
  1188. append( div, root );
  1189. append( p, root );
  1190. // Range starts in ROOT > DIV > P > gg|gg.
  1191. // Range ends in ROOT > DIV > ...|ar.
  1192. range = new Range( new Position( root, [ 0, 0, 2 ] ), new Position( root, [ 0, 5 ] ) );
  1193. } );
  1194. it( 'should remove specified node', () => {
  1195. remove( div );
  1196. expect( root.maxOffset ).to.equal( 1 );
  1197. expect( root.childCount ).to.equal( 1 );
  1198. expect( getNodesAndText( Range.createIn( root.getChild( 0 ) ) ) ).to.equal( 'abcxyz' );
  1199. } );
  1200. it( 'should remove specified text node', () => {
  1201. remove( p.getChild( 0 ) );
  1202. expect( getNodesAndText( Range.createOn( p ) ) ).to.equal( 'PP' );
  1203. } );
  1204. it( 'should remove any range of nodes', () => {
  1205. remove( range );
  1206. expect( getNodesAndText( Range.createIn( root.getChild( 0 ) ) ) ).to.equal( 'PggParPhhhhP' );
  1207. expect( getNodesAndText( Range.createIn( root.getChild( 1 ) ) ) ).to.equal( 'abcxyz' );
  1208. } );
  1209. it( 'should create minimal number of remove deltas, each with only one operation', () => {
  1210. batch = new Batch();
  1211. remove( range );
  1212. expect( batch.deltas.length ).to.equal( 2 );
  1213. expect( batch.deltas[ 0 ].operations.length ).to.equal( 1 );
  1214. expect( batch.deltas[ 1 ].operations.length ).to.equal( 1 );
  1215. } );
  1216. it( 'should use RemoveOperation', () => {
  1217. batch = new Batch();
  1218. remove( div );
  1219. expect( batch.deltas[ 0 ].operations[ 0 ].type ).to.equal( 'remove' );
  1220. } );
  1221. it( 'should throw when trying to use detached writer', () => {
  1222. const writer = new Writer( model, batch );
  1223. expect( () => {
  1224. writer.remove( range );
  1225. } ).to.throw( CKEditorError, /^writer-incorrect-use/ );
  1226. } );
  1227. } );
  1228. describe( 'remove from document fragment', () => {
  1229. let frag;
  1230. beforeEach( () => {
  1231. frag = createDocumentFragment();
  1232. append( div, frag );
  1233. append( p, frag );
  1234. // Range starts in FRAG > DIV > P > gg|gg.
  1235. // Range ends in FRAG > DIV > ...|ar.
  1236. range = new Range( new Position( frag, [ 0, 0, 2 ] ), new Position( frag, [ 0, 5 ] ) );
  1237. } );
  1238. it( 'should remove specified node', () => {
  1239. remove( div );
  1240. expect( frag.maxOffset ).to.equal( 1 );
  1241. expect( frag.childCount ).to.equal( 1 );
  1242. expect( getNodesAndText( Range.createIn( frag.getChild( 0 ) ) ) ).to.equal( 'abcxyz' );
  1243. } );
  1244. it( 'should remove specified text node', () => {
  1245. remove( p.getChild( 0 ) );
  1246. expect( getNodesAndText( Range.createOn( p ) ) ).to.equal( 'PP' );
  1247. } );
  1248. it( 'should remove any range of nodes', () => {
  1249. remove( range );
  1250. expect( getNodesAndText( Range.createIn( frag.getChild( 0 ) ) ) ).to.equal( 'PggParPhhhhP' );
  1251. expect( getNodesAndText( Range.createIn( frag.getChild( 1 ) ) ) ).to.equal( 'abcxyz' );
  1252. } );
  1253. it( 'should create minimal number of remove deltas, each with only one operation', () => {
  1254. batch = new Batch();
  1255. remove( range );
  1256. expect( batch.deltas.length ).to.equal( 2 );
  1257. expect( batch.deltas[ 0 ].operations.length ).to.equal( 1 );
  1258. expect( batch.deltas[ 1 ].operations.length ).to.equal( 1 );
  1259. } );
  1260. it( 'should use DetachOperation', () => {
  1261. batch = new Batch();
  1262. remove( div );
  1263. expect( batch.deltas[ 0 ].operations[ 0 ].type ).to.equal( 'detach' );
  1264. } );
  1265. it( 'should throw when trying to use detached writer', () => {
  1266. const writer = new Writer( model, batch );
  1267. expect( () => {
  1268. writer.remove( range );
  1269. } ).to.throw( CKEditorError, /^writer-incorrect-use/ );
  1270. } );
  1271. } );
  1272. } );
  1273. describe( 'rename()', () => {
  1274. it( 'should rename given element', () => {
  1275. const root = doc.createRoot();
  1276. const p = new Element( 'p', null, new Text( 'abc' ) );
  1277. root.appendChildren( p );
  1278. rename( p, 'h' );
  1279. expect( root.maxOffset ).to.equal( 1 );
  1280. expect( root.getChild( 0 ) ).to.have.property( 'name', 'h' );
  1281. } );
  1282. it( 'should rename in document fragment', () => {
  1283. const docFrag = new DocumentFragment();
  1284. const p = new Element( 'p' );
  1285. docFrag.appendChildren( p );
  1286. rename( p, 'h' );
  1287. expect( docFrag.maxOffset ).to.equal( 1 );
  1288. expect( docFrag.getChild( 0 ) ).to.have.property( 'name', 'h' );
  1289. } );
  1290. it( 'should throw if not an Element instance is passed', () => {
  1291. expect( () => {
  1292. rename( new Text( 'abc' ), 'h' );
  1293. } ).to.throw( CKEditorError, /^writer-rename-not-element-instance/ );
  1294. } );
  1295. it( 'should throw when trying to use detached writer', () => {
  1296. const writer = new Writer( model, batch );
  1297. const p = new Element( 'p', null, new Text( 'abc' ) );
  1298. expect( () => {
  1299. writer.rename( p, 'h' );
  1300. } ).to.throw( CKEditorError, /^writer-incorrect-use/ );
  1301. } );
  1302. } );
  1303. describe( 'split()', () => {
  1304. let root, p;
  1305. beforeEach( () => {
  1306. root = doc.createRoot();
  1307. p = new Element( 'p', { key: 'value' }, new Text( 'foobar' ) );
  1308. root.insertChildren( 0, p );
  1309. } );
  1310. it( 'should split foobar to foo and bar', () => {
  1311. split( new Position( root, [ 0, 3 ] ) );
  1312. expect( root.maxOffset ).to.equal( 2 );
  1313. expect( root.getChild( 0 ).name ).to.equal( 'p' );
  1314. expect( root.getChild( 0 ).maxOffset ).to.equal( 3 );
  1315. expect( count( root.getChild( 0 ).getAttributes() ) ).to.equal( 1 );
  1316. expect( root.getChild( 0 ).getAttribute( 'key' ) ).to.equal( 'value' );
  1317. expect( root.getChild( 0 ).getChild( 0 ).data ).to.equal( 'foo' );
  1318. expect( root.getChild( 1 ).name ).to.equal( 'p' );
  1319. expect( root.getChild( 1 ).maxOffset ).to.equal( 3 );
  1320. expect( count( root.getChild( 1 ).getAttributes() ) ).to.equal( 1 );
  1321. expect( root.getChild( 1 ).getAttribute( 'key' ) ).to.equal( 'value' );
  1322. expect( root.getChild( 1 ).getChild( 0 ).data ).to.equal( 'bar' );
  1323. } );
  1324. it( 'should split inside document fragment', () => {
  1325. const docFrag = new DocumentFragment();
  1326. docFrag.appendChildren( new Element( 'p', null, new Text( 'foobar' ) ) );
  1327. split( new Position( docFrag, [ 0, 3 ] ) );
  1328. expect( docFrag.maxOffset ).to.equal( 2 );
  1329. expect( docFrag.getChild( 0 ).name ).to.equal( 'p' );
  1330. expect( docFrag.getChild( 0 ).maxOffset ).to.equal( 3 );
  1331. expect( docFrag.getChild( 0 ).getChild( 0 ).data ).to.equal( 'foo' );
  1332. expect( docFrag.getChild( 1 ).name ).to.equal( 'p' );
  1333. expect( docFrag.getChild( 1 ).maxOffset ).to.equal( 3 );
  1334. expect( docFrag.getChild( 1 ).getChild( 0 ).data ).to.equal( 'bar' );
  1335. } );
  1336. it( 'should create an empty paragraph if we split at the end', () => {
  1337. split( new Position( root, [ 0, 6 ] ) );
  1338. expect( root.maxOffset ).to.equal( 2 );
  1339. expect( root.getChild( 0 ).name ).to.equal( 'p' );
  1340. expect( root.getChild( 0 ).maxOffset ).to.equal( 6 );
  1341. expect( count( root.getChild( 0 ).getAttributes() ) ).to.equal( 1 );
  1342. expect( root.getChild( 0 ).getAttribute( 'key' ) ).to.equal( 'value' );
  1343. expect( root.getChild( 0 ).getChild( 0 ).data ).to.equal( 'foobar' );
  1344. expect( root.getChild( 1 ).name ).to.equal( 'p' );
  1345. expect( root.getChild( 1 ).maxOffset ).to.equal( 0 );
  1346. expect( count( root.getChild( 1 ).getAttributes() ) ).to.equal( 1 );
  1347. expect( root.getChild( 1 ).getAttribute( 'key' ) ).to.equal( 'value' );
  1348. } );
  1349. it( 'should throw if we try to split a root', () => {
  1350. expect( () => {
  1351. split( new Position( root, [ 0 ] ) );
  1352. } ).to.throw( CKEditorError, /^writer-split-element-no-parent/ );
  1353. } );
  1354. it( 'should throw if we try to split an element with no parent', () => {
  1355. expect( () => {
  1356. const element = createElement( 'p' );
  1357. split( new Position( element, [ 0 ] ) );
  1358. } ).to.throw( CKEditorError, /^writer-split-element-no-parent/ );
  1359. } );
  1360. it( 'should throw if we try to split a document fragment', () => {
  1361. expect( () => {
  1362. const documentFragment = createDocumentFragment();
  1363. split( new Position( documentFragment, [ 0 ] ) );
  1364. } ).to.throw( CKEditorError, /^writer-split-element-no-parent/ );
  1365. } );
  1366. it( 'should split elements to limitElement', () => {
  1367. const div = new Element( 'div', null, p );
  1368. const section = new Element( 'section', null, div );
  1369. root.insertChildren( 0, section );
  1370. split( new Position( p, [ 3 ] ), section );
  1371. expect( root.maxOffset ).to.equal( 1 );
  1372. expect( section.maxOffset ).to.equal( 2 );
  1373. expect( section.getChild( 0 ).name ).to.equal( 'div' );
  1374. expect( section.getChild( 0 ).getChild( 0 ).name ).to.equal( 'p' );
  1375. expect( section.getChild( 0 ).getChild( 0 ).getAttribute( 'key' ) ).to.equal( 'value' );
  1376. expect( count( section.getChild( 0 ).getChild( 0 ).getAttributes() ) ).to.equal( 1 );
  1377. expect( section.getChild( 0 ).getChild( 0 ).getChild( 0 ).data ).to.equal( 'foo' );
  1378. expect( section.getChild( 1 ).name ).to.equal( 'div' );
  1379. expect( section.getChild( 1 ).getChild( 0 ).name ).to.equal( 'p' );
  1380. expect( section.getChild( 1 ).getChild( 0 ).getAttribute( 'key' ) ).to.equal( 'value' );
  1381. expect( count( section.getChild( 1 ).getChild( 0 ).getAttributes() ) ).to.equal( 1 );
  1382. expect( section.getChild( 1 ).getChild( 0 ).getChild( 0 ).data ).to.equal( 'bar' );
  1383. } );
  1384. it( 'should throw when limitElement is not a position ancestor', () => {
  1385. const div = new Element( 'div', null, p );
  1386. const section = new Element( 'section', null, div );
  1387. root.insertChildren( 0, div );
  1388. root.insertChildren( 1, section );
  1389. expect( () => {
  1390. split( new Position( p, [ 3 ] ), section );
  1391. } ).to.throw( CKEditorError, /^writer-split-invalid-limit-element/ );
  1392. } );
  1393. it( 'should throw when trying to use detached writer', () => {
  1394. const writer = new Writer( model, batch );
  1395. expect( () => {
  1396. writer.split( new Position( root, [ 0, 3 ] ) );
  1397. } ).to.throw( CKEditorError, /^writer-incorrect-use/ );
  1398. } );
  1399. } );
  1400. describe( 'wrap()', () => {
  1401. let root, range;
  1402. beforeEach( () => {
  1403. root = doc.createRoot();
  1404. root.insertChildren( 0, new Text( 'foobar' ) );
  1405. range = new Range( new Position( root, [ 2 ] ), new Position( root, [ 4 ] ) );
  1406. } );
  1407. it( 'should wrap flat range with given element', () => {
  1408. const p = new Element( 'p' );
  1409. wrap( range, p );
  1410. expect( root.maxOffset ).to.equal( 5 );
  1411. expect( root.getChild( 0 ).data ).to.equal( 'fo' );
  1412. expect( root.getChild( 1 ) ).to.equal( p );
  1413. expect( p.getChild( 0 ).data ).to.equal( 'ob' );
  1414. expect( root.getChild( 2 ).data ).to.equal( 'ar' );
  1415. } );
  1416. it( 'should wrap flat range with an element of given name', () => {
  1417. wrap( range, 'p' );
  1418. expect( root.maxOffset ).to.equal( 5 );
  1419. expect( root.getChild( 0 ).data ).to.equal( 'fo' );
  1420. expect( root.getChild( 1 ).name ).to.equal( 'p' );
  1421. expect( root.getChild( 1 ).getChild( 0 ).data ).to.equal( 'ob' );
  1422. expect( root.getChild( 2 ).data ).to.equal( 'ar' );
  1423. } );
  1424. it( 'should wrap inside document fragment', () => {
  1425. const docFrag = new DocumentFragment( new Text( 'foo' ) );
  1426. wrap( Range.createIn( docFrag ), 'p' );
  1427. expect( docFrag.maxOffset ).to.equal( 1 );
  1428. expect( docFrag.getChild( 0 ).name ).to.equal( 'p' );
  1429. expect( docFrag.getChild( 0 ).getChild( 0 ).data ).to.equal( 'foo' );
  1430. } );
  1431. it( 'should throw if range to wrap is not flat', () => {
  1432. root.insertChildren( 1, [ new Element( 'p', [], new Text( 'xyz' ) ) ] );
  1433. const notFlatRange = new Range( new Position( root, [ 3 ] ), new Position( root, [ 6, 2 ] ) );
  1434. expect( () => {
  1435. wrap( notFlatRange, 'p' );
  1436. } ).to.throw( CKEditorError, /^writer-wrap-range-not-flat/ );
  1437. } );
  1438. it( 'should throw if element to wrap with has children #1', () => {
  1439. const p = new Element( 'p', [], new Text( 'a' ) );
  1440. expect( () => {
  1441. wrap( range, p );
  1442. } ).to.throw( CKEditorError, /^writer-wrap-element-not-empty/ );
  1443. } );
  1444. it( 'should throw if element to wrap with has children #2', () => {
  1445. const p = new Element( 'p' );
  1446. root.insertChildren( 0, p );
  1447. expect( () => {
  1448. wrap( range, p );
  1449. } ).to.throw( CKEditorError, /^writer-wrap-element-attached/ );
  1450. } );
  1451. it( 'should throw when trying to use detached writer', () => {
  1452. const writer = new Writer( model, batch );
  1453. expect( () => {
  1454. writer.wrap( range, 'p' );
  1455. } ).to.throw( CKEditorError, /^writer-incorrect-use/ );
  1456. } );
  1457. } );
  1458. describe( 'unwrap()', () => {
  1459. let root, p;
  1460. beforeEach( () => {
  1461. root = doc.createRoot();
  1462. p = new Element( 'p', [], new Text( 'xyz' ) );
  1463. root.insertChildren( 0, [ new Text( 'a' ), p, new Text( 'b' ) ] );
  1464. } );
  1465. it( 'should unwrap given element', () => {
  1466. unwrap( p );
  1467. expect( root.maxOffset ).to.equal( 5 );
  1468. expect( root.getChild( 0 ).data ).to.equal( 'axyzb' );
  1469. } );
  1470. it( 'should unwrap inside document fragment', () => {
  1471. const docFrag = new DocumentFragment( new Element( 'p', null, new Text( 'foo' ) ) );
  1472. unwrap( docFrag.getChild( 0 ) );
  1473. expect( docFrag.maxOffset ).to.equal( 3 );
  1474. expect( docFrag.getChild( 0 ).data ).to.equal( 'foo' );
  1475. } );
  1476. it( 'should throw if element to unwrap has no parent', () => {
  1477. const element = new Element( 'p' );
  1478. expect( () => {
  1479. unwrap( element );
  1480. } ).to.throw( CKEditorError, /^writer-unwrap-element-no-parent/ );
  1481. } );
  1482. it( 'should throw when trying to use detached writer', () => {
  1483. const writer = new Writer( model, batch );
  1484. expect( () => {
  1485. writer.unwrap( p );
  1486. } ).to.throw( CKEditorError, /^writer-incorrect-use/ );
  1487. } );
  1488. } );
  1489. describe( 'setMarker()', () => {
  1490. let root, range;
  1491. beforeEach( () => {
  1492. root = doc.createRoot();
  1493. root.appendChildren( new Text( 'foo' ) );
  1494. range = Range.createIn( root );
  1495. } );
  1496. it( 'should add marker to the document marker collection', () => {
  1497. setMarker( 'name', range );
  1498. expect( model.markers.get( 'name' ).getRange().isEqual( range ) ).to.be.true;
  1499. } );
  1500. it( 'should update marker in the document marker collection', () => {
  1501. setMarker( 'name', range );
  1502. const range2 = Range.createFromParentsAndOffsets( root, 0, root, 0 );
  1503. setMarker( 'name', range2 );
  1504. expect( model.markers.get( 'name' ).getRange().isEqual( range2 ) ).to.be.true;
  1505. } );
  1506. it( 'should accept marker instance', () => {
  1507. const marker = model.markers.set( 'name', range );
  1508. const range2 = Range.createFromParentsAndOffsets( root, 0, root, 0 );
  1509. setMarker( marker, range2 );
  1510. const op = batch.deltas[ 0 ].operations[ 0 ];
  1511. expect( model.markers.get( 'name' ).getRange().isEqual( range2 ) ).to.be.true;
  1512. expect( op.oldRange.isEqual( range ) ).to.be.true;
  1513. expect( op.newRange.isEqual( range2 ) ).to.be.true;
  1514. } );
  1515. it( 'should accept empty range parameter if marker instance is passed', () => {
  1516. const marker = model.markers.set( 'name', range );
  1517. const spy = sinon.spy();
  1518. model.on( 'applyOperation', spy );
  1519. setMarker( marker );
  1520. const op = batch.deltas[ 0 ].operations[ 0 ];
  1521. expect( spy.calledOnce ).to.be.true;
  1522. expect( spy.firstCall.args[ 1 ][ 0 ].type ).to.equal( 'marker' );
  1523. expect( op.oldRange ).to.be.null;
  1524. expect( op.newRange.isEqual( range ) ).to.be.true;
  1525. } );
  1526. it( 'should throw if marker with given name does not exist and range is not passed', () => {
  1527. expect( () => {
  1528. setMarker( 'name' );
  1529. } ).to.throw( CKEditorError, /^writer-setMarker-no-range/ );
  1530. } );
  1531. it( 'should throw when trying to use detached writer', () => {
  1532. const marker = model.markers.set( 'name', range );
  1533. const writer = new Writer( model, batch );
  1534. expect( () => {
  1535. writer.setMarker( marker );
  1536. } ).to.throw( CKEditorError, /^writer-incorrect-use/ );
  1537. } );
  1538. } );
  1539. describe( 'removeMarker()', () => {
  1540. let root, range;
  1541. beforeEach( () => {
  1542. root = doc.createRoot();
  1543. root.appendChildren( new Text( 'foo' ) );
  1544. range = Range.createIn( root );
  1545. } );
  1546. it( 'should remove marker from the document marker collection', () => {
  1547. setMarker( 'name', range );
  1548. removeMarker( 'name' );
  1549. expect( model.markers.get( 'name' ) ).to.be.null;
  1550. } );
  1551. it( 'should throw when trying to remove non existing marker', () => {
  1552. expect( () => {
  1553. removeMarker( 'name' );
  1554. } ).to.throw( CKEditorError, /^writer-removeMarker-no-marker/ );
  1555. } );
  1556. it( 'should throw when trying to use detached writer', () => {
  1557. const writer = new Writer( model, batch );
  1558. expect( () => {
  1559. writer.removeMarker( 'name' );
  1560. } ).to.throw( CKEditorError, /^writer-incorrect-use/ );
  1561. } );
  1562. it( 'should accept marker instance', () => {
  1563. setMarker( 'name', range );
  1564. const marker = model.markers.get( 'name' );
  1565. removeMarker( marker );
  1566. expect( model.markers.get( 'name' ) ).to.be.null;
  1567. } );
  1568. } );
  1569. describe( 'setSelection()', () => {
  1570. let root;
  1571. beforeEach( () => {
  1572. model.schema.register( 'p', { inheritAllFrom: '$block' } );
  1573. model.schema.extend( 'p', { allowIn: '$root' } );
  1574. root = doc.createRoot();
  1575. root.appendChildren( [
  1576. new Element( 'p' ),
  1577. new Element( 'p' ),
  1578. new Element( 'p', [], new Text( 'foo' ) )
  1579. ] );
  1580. } );
  1581. it( 'should use DocumentSelection#_setTo method', () => {
  1582. const firstParagraph = root.getNodeByPath( [ 1 ] );
  1583. const setToSpy = sinon.spy( DocumentSelection.prototype, '_setTo' );
  1584. setSelection( firstParagraph );
  1585. expect( setToSpy.calledOnce ).to.be.true;
  1586. setToSpy.restore();
  1587. } );
  1588. it( 'should change document selection ranges', () => {
  1589. const range = new Range( new Position( root, [ 1 ] ), new Position( root, [ 2, 2 ] ) );
  1590. setSelection( range, true );
  1591. expect( model.document.selection._ranges.length ).to.equal( 1 );
  1592. expect( model.document.selection._ranges[ 0 ].start.path ).to.deep.equal( [ 1 ] );
  1593. expect( model.document.selection._ranges[ 0 ].end.path ).to.deep.equal( [ 2, 2 ] );
  1594. expect( model.document.selection.isBackward ).to.be.true;
  1595. } );
  1596. } );
  1597. describe( 'setSelectionFocus()', () => {
  1598. let root;
  1599. beforeEach( () => {
  1600. model.schema.register( 'p', { inheritAllFrom: '$block' } );
  1601. model.schema.extend( 'p', { allowIn: '$root' } );
  1602. root = doc.createRoot();
  1603. root.appendChildren( [
  1604. new Element( 'p' ),
  1605. new Element( 'p' ),
  1606. new Element( 'p', [], new Text( 'foo' ) )
  1607. ] );
  1608. } );
  1609. it( 'should use DocumentSelection#_setFocus method', () => {
  1610. const firstParagraph = root.getNodeByPath( [ 1 ] );
  1611. const setFocusSpy = sinon.spy( DocumentSelection.prototype, '_setFocus' );
  1612. setSelectionFocus( firstParagraph );
  1613. expect( setFocusSpy.calledOnce ).to.be.true;
  1614. setFocusSpy.restore();
  1615. } );
  1616. it( 'should change document selection ranges', () => {
  1617. setSelection( new Position( root, [ 1 ] ) );
  1618. setSelectionFocus( new Position( root, [ 2, 2 ] ) );
  1619. expect( model.document.selection._ranges.length ).to.equal( 1 );
  1620. expect( model.document.selection._ranges[ 0 ].start.path ).to.deep.equal( [ 1 ] );
  1621. expect( model.document.selection._ranges[ 0 ].end.path ).to.deep.equal( [ 2, 2 ] );
  1622. } );
  1623. } );
  1624. describe( 'setSelectionAttribute()', () => {
  1625. const fooStoreAttrKey = DocumentSelection._getStoreAttributeKey( 'foo' );
  1626. let root, rangeInEmptyP, emptyP;
  1627. beforeEach( () => {
  1628. model.schema.register( 'p', { inheritAllFrom: '$block' } );
  1629. model.schema.extend( 'p', { allowIn: '$root' } );
  1630. root = doc.createRoot();
  1631. root.appendChildren( [
  1632. new Element( 'p', [], [] ),
  1633. new Element( 'p' ),
  1634. new Element( 'p', [], new Text( 'foo' ) )
  1635. ] );
  1636. rangeInEmptyP = new Range( new Position( root, [ 0, 0 ] ), new Position( root, [ 0, 0 ] ) );
  1637. emptyP = root.getChild( 0 );
  1638. } );
  1639. it( 'should store attribute if the selection is in empty node', () => {
  1640. setSelection( rangeInEmptyP );
  1641. setSelectionAttribute( 'foo', 'bar' );
  1642. expect( model.document.selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  1643. expect( emptyP.getAttribute( fooStoreAttrKey ) ).to.equal( 'bar' );
  1644. } );
  1645. it( 'should be able to store attributes from the given object', () => {
  1646. setSelection( rangeInEmptyP );
  1647. setSelectionAttribute( { key1: 'foo', key2: 'bar' } );
  1648. expect( model.document.selection.getAttribute( 'key1' ) ).to.equal( 'foo' );
  1649. expect( model.document.selection.getAttribute( 'key2' ) ).to.equal( 'bar' );
  1650. } );
  1651. it( 'should be able to store attributes from the given iterable', () => {
  1652. setSelection( rangeInEmptyP );
  1653. setSelectionAttribute( new Map( [ [ 'key1', 'foo' ], [ 'key2', 'bar' ] ] ) );
  1654. expect( model.document.selection.getAttribute( 'key1' ) ).to.equal( 'foo' );
  1655. expect( model.document.selection.getAttribute( 'key2' ) ).to.equal( 'bar' );
  1656. } );
  1657. } );
  1658. describe( 'removeSelectionAttribute()', () => {
  1659. const fooStoreAttrKey = DocumentSelection._getStoreAttributeKey( 'foo' );
  1660. let root, rangeInEmptyP, emptyP;
  1661. beforeEach( () => {
  1662. model.schema.register( 'p', { inheritAllFrom: '$block' } );
  1663. model.schema.extend( 'p', { allowIn: '$root' } );
  1664. root = doc.createRoot();
  1665. root.appendChildren( [
  1666. new Element( 'p', [], [] ),
  1667. new Element( 'p' ),
  1668. new Element( 'p', [], new Text( 'foo' ) )
  1669. ] );
  1670. rangeInEmptyP = new Range( new Position( root, [ 0, 0 ] ), new Position( root, [ 0, 0 ] ) );
  1671. emptyP = root.getChild( 0 );
  1672. } );
  1673. it( 'should remove stored attribute if the selection is in empty node', () => {
  1674. setSelection( rangeInEmptyP );
  1675. setSelectionAttribute( 'foo', 'bar' );
  1676. removeSelectionAttribute( 'foo' );
  1677. expect( model.document.selection.getAttribute( 'foo' ) ).to.be.undefined;
  1678. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  1679. } );
  1680. it( 'should remove all attributes from the given iterable', () => {
  1681. setSelection( rangeInEmptyP );
  1682. setSelectionAttribute( 'foo', 'bar' );
  1683. setSelectionAttribute( 'foo2', 'bar2' );
  1684. removeSelectionAttribute( [ 'foo', 'foo2' ] );
  1685. expect( model.document.selection.getAttribute( 'foo' ) ).to.be.undefined;
  1686. expect( model.document.selection.getAttribute( 'foo2' ) ).to.be.undefined;
  1687. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  1688. } );
  1689. it( 'should do nothing if attribute does not exist in the selection', () => {
  1690. setSelection( rangeInEmptyP );
  1691. setSelectionAttribute( 'foo', 'bar' );
  1692. setSelectionAttribute( 'foo2', 'bar2' );
  1693. removeSelectionAttribute( [ 'foo', 'baz' ] );
  1694. expect( model.document.selection.getAttribute( 'foo' ) ).to.be.undefined;
  1695. expect( model.document.selection.getAttribute( 'foo2' ) ).to.equal( 'bar2' );
  1696. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  1697. } );
  1698. } );
  1699. function createText( data, attributes ) {
  1700. return model.change( writer => {
  1701. return writer.createText( data, attributes );
  1702. } );
  1703. }
  1704. function createElement( name, attributes ) {
  1705. return model.change( writer => {
  1706. return writer.createElement( name, attributes );
  1707. } );
  1708. }
  1709. function createDocumentFragment() {
  1710. return model.change( writer => {
  1711. return writer.createDocumentFragment();
  1712. } );
  1713. }
  1714. function insert( item, itemOrPosition, offset ) {
  1715. model.enqueueChange( batch, writer => {
  1716. writer.insert( item, itemOrPosition, offset );
  1717. } );
  1718. }
  1719. function insertText( text, attributes, itemOrPosition, offset ) {
  1720. model.enqueueChange( batch, writer => {
  1721. writer.insertText( text, attributes, itemOrPosition, offset );
  1722. } );
  1723. }
  1724. function insertElement( name, attributes, itemOrPosition, offset ) {
  1725. model.enqueueChange( batch, writer => {
  1726. writer.insertElement( name, attributes, itemOrPosition, offset );
  1727. } );
  1728. }
  1729. function append( item, parent ) {
  1730. model.enqueueChange( batch, writer => {
  1731. writer.append( item, parent );
  1732. } );
  1733. }
  1734. function appendText( text, attributes, parent ) {
  1735. model.enqueueChange( batch, writer => {
  1736. writer.appendText( text, attributes, parent );
  1737. } );
  1738. }
  1739. function appendElement( name, attributes, parent ) {
  1740. model.enqueueChange( batch, writer => {
  1741. writer.appendElement( name, attributes, parent );
  1742. } );
  1743. }
  1744. function setAttribute( key, value, itemOrRange ) {
  1745. model.enqueueChange( batch, writer => {
  1746. writer.setAttribute( key, value, itemOrRange );
  1747. } );
  1748. }
  1749. function setAttributes( attributes, itemOrRange ) {
  1750. model.enqueueChange( batch, writer => {
  1751. writer.setAttributes( attributes, itemOrRange );
  1752. } );
  1753. }
  1754. function removeAttribute( key, itemOrRange ) {
  1755. model.enqueueChange( batch, writer => {
  1756. writer.removeAttribute( key, itemOrRange );
  1757. } );
  1758. }
  1759. function clearAttributes( itemOrRange ) {
  1760. model.enqueueChange( batch, writer => {
  1761. writer.clearAttributes( itemOrRange );
  1762. } );
  1763. }
  1764. function move( range, itemOrPosition, offset ) {
  1765. model.enqueueChange( batch, writer => {
  1766. writer.move( range, itemOrPosition, offset );
  1767. } );
  1768. }
  1769. function remove( itemOrRange ) {
  1770. model.enqueueChange( batch, writer => {
  1771. writer.remove( itemOrRange );
  1772. } );
  1773. }
  1774. function merge( position ) {
  1775. model.enqueueChange( batch, writer => {
  1776. writer.merge( position );
  1777. } );
  1778. }
  1779. function rename( element, newName ) {
  1780. model.enqueueChange( batch, writer => {
  1781. writer.rename( element, newName );
  1782. } );
  1783. }
  1784. function split( position, limitElement ) {
  1785. model.enqueueChange( batch, writer => {
  1786. writer.split( position, limitElement );
  1787. } );
  1788. }
  1789. function wrap( range, elementOrString ) {
  1790. model.enqueueChange( batch, writer => {
  1791. writer.wrap( range, elementOrString );
  1792. } );
  1793. }
  1794. function unwrap( element ) {
  1795. model.enqueueChange( batch, writer => {
  1796. writer.unwrap( element );
  1797. } );
  1798. }
  1799. function setMarker( markerOrName, newRange ) {
  1800. model.enqueueChange( batch, writer => {
  1801. writer.setMarker( markerOrName, newRange );
  1802. } );
  1803. }
  1804. function removeMarker( markerOrName ) {
  1805. model.enqueueChange( batch, writer => {
  1806. writer.removeMarker( markerOrName );
  1807. } );
  1808. }
  1809. function setSelection( selectable, backwardSelectionOrOffset ) {
  1810. model.enqueueChange( batch, writer => {
  1811. writer.setSelection( selectable, backwardSelectionOrOffset );
  1812. } );
  1813. }
  1814. function setSelectionFocus( itemOrPosition, offset ) {
  1815. model.enqueueChange( batch, writer => {
  1816. writer.setSelectionFocus( itemOrPosition, offset );
  1817. } );
  1818. }
  1819. function setSelectionAttribute( key, value ) {
  1820. model.enqueueChange( batch, writer => {
  1821. writer.setSelectionAttribute( key, value );
  1822. } );
  1823. }
  1824. function removeSelectionAttribute( key ) {
  1825. model.enqueueChange( batch, writer => {
  1826. writer.removeSelectionAttribute( key );
  1827. } );
  1828. }
  1829. } );