writer.js 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130
  1. /**
  2. * @license Copyright (c) 2003-2017, 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. describe( 'Writer', () => {
  19. let model, doc, batch;
  20. beforeEach( () => {
  21. model = new Model();
  22. batch = new Batch();
  23. doc = model.document;
  24. } );
  25. describe( 'constructor()', () => {
  26. let writer;
  27. beforeEach( () => {
  28. writer = new Writer( model, batch );
  29. } );
  30. it( 'should have model instance', () => {
  31. expect( writer.model ).to.instanceof( Model );
  32. } );
  33. it( 'should have batch instance', () => {
  34. expect( writer.batch ).to.instanceof( Batch );
  35. } );
  36. } );
  37. describe( 'createText()', () => {
  38. it( 'should create text node', () => {
  39. const text = createText( 'foo' );
  40. expect( text ).to.instanceof( Text );
  41. expect( text.data ).to.equal( 'foo' );
  42. expect( Array.from( text.getAttributes() ) ).to.length( 0 );
  43. } );
  44. it( 'should create text with attributes', () => {
  45. const text = createText( 'foo', { foo: 'bar', biz: 'baz' } );
  46. expect( Array.from( text.getAttributes() ) ).to.deep.equal( [ [ 'foo', 'bar' ], [ 'biz', 'baz' ] ] );
  47. } );
  48. } );
  49. describe( 'createElement()', () => {
  50. it( 'should create element', () => {
  51. const element = createElement( 'foo' );
  52. expect( element ).to.instanceof( Element );
  53. expect( element.name ).to.equal( 'foo' );
  54. expect( Array.from( element.getAttributes() ) ).to.length( 0 );
  55. } );
  56. it( 'should create element with attributes', () => {
  57. const element = createText( 'foo', { foo: 'bar', biz: 'baz' } );
  58. expect( Array.from( element.getAttributes() ) ).to.deep.equal( [ [ 'foo', 'bar' ], [ 'biz', 'baz' ] ] );
  59. } );
  60. } );
  61. describe( 'createDocumentFragment()', () => {
  62. it( 'should create element', () => {
  63. const element = createDocumentFragment();
  64. expect( element ).to.instanceof( DocumentFragment );
  65. } );
  66. } );
  67. describe( 'insert()', () => {
  68. it( 'should insert node at given position', () => {
  69. const parent = createDocumentFragment();
  70. const child = createElement( 'child' );
  71. const textChild = createText( 'textChild' );
  72. insert( child, new Position( parent, [ 0 ] ) );
  73. insert( textChild, new Position( parent, [ 1 ] ) );
  74. expect( Array.from( parent ) ).to.deep.equal( [ child, textChild ] );
  75. } );
  76. it( 'should insert node at the beginning of given element', () => {
  77. const parent = createDocumentFragment();
  78. const child1 = createElement( 'child' );
  79. const child2 = createElement( 'child' );
  80. insert( child1, parent );
  81. insert( child2, parent );
  82. expect( Array.from( parent.getChildren() ) ).to.deep.equal( [ child2, child1 ] );
  83. } );
  84. it( 'should insert node at the end of given element', () => {
  85. const parent = createDocumentFragment();
  86. const child1 = createElement( 'child' );
  87. const child2 = createElement( 'child' );
  88. insert( child1, parent );
  89. insert( child2, parent, 'end' );
  90. expect( Array.from( parent.getChildren() ) ).to.deep.equal( [ child1, child2 ] );
  91. } );
  92. it( 'should insert node at the given offset of given element', () => {
  93. const parent = createDocumentFragment();
  94. const child1 = createElement( 'child' );
  95. const child2 = createElement( 'child' );
  96. const child3 = createElement( 'child' );
  97. insert( child3, parent );
  98. insert( child1, parent );
  99. insert( child2, parent, 1 );
  100. expect( Array.from( parent.getChildren() ) ).to.deep.equal( [ child1, child2, child3 ] );
  101. } );
  102. it( 'should insert node before the given node', () => {
  103. const parent = createDocumentFragment();
  104. const child1 = createElement( 'child' );
  105. const child2 = createElement( 'child' );
  106. const child3 = createElement( 'child' );
  107. insert( child3, parent );
  108. insert( child1, parent );
  109. insert( child2, child3, 'before' );
  110. expect( Array.from( parent.getChildren() ) ).to.deep.equal( [ child1, child2, child3 ] );
  111. } );
  112. it( 'should insert node after the given node', () => {
  113. const parent = createDocumentFragment();
  114. const child1 = createElement( 'child' );
  115. const child2 = createElement( 'child' );
  116. const child3 = createElement( 'child' );
  117. insert( child3, parent );
  118. insert( child1, parent );
  119. insert( child2, child1, 'after' );
  120. expect( Array.from( parent.getChildren() ) ).to.deep.equal( [ child1, child2, child3 ] );
  121. } );
  122. it( 'should create proper delta for inserting element', () => {
  123. const parent = createDocumentFragment();
  124. const element = createElement( 'child' );
  125. const spy = sinon.spy( model, 'applyOperation' );
  126. insert( element, parent );
  127. sinon.assert.calledOnce( spy );
  128. expect( spy.lastCall.args[ 0 ].type ).to.equal( 'insert' );
  129. expect( spy.lastCall.args[ 0 ].delta ).to.instanceof( InsertDelta );
  130. expect( spy.lastCall.args[ 0 ].delta ).to.not.instanceof( WeakInsertDelta );
  131. expect( spy.lastCall.args[ 0 ].delta.batch ).to.equal( batch );
  132. } );
  133. it( 'should create proper delta for inserting text', () => {
  134. const parent = createDocumentFragment();
  135. const text = createText( 'child' );
  136. const spy = sinon.spy( model, 'applyOperation' );
  137. insert( text, parent );
  138. sinon.assert.calledOnce( spy );
  139. expect( spy.lastCall.args[ 0 ].type ).to.equal( 'insert' );
  140. expect( spy.lastCall.args[ 0 ].delta ).to.instanceof( WeakInsertDelta );
  141. expect( spy.lastCall.args[ 0 ].delta.batch ).to.equal( batch );
  142. } );
  143. it( 'should move element from one parent to the other within the same document (documentA -> documentA)', () => {
  144. const root = doc.createRoot();
  145. const parent1 = createElement( 'parent' );
  146. const parent2 = createElement( 'parent' );
  147. const node = createText( 'foo' );
  148. insert( node, parent1 );
  149. insert( parent1, root );
  150. insert( parent2, root );
  151. const spy = sinon.spy( model, 'applyOperation' );
  152. insert( node, parent2 );
  153. // Verify result.
  154. expect( Array.from( parent1.getChildren() ) ).to.deep.equal( [] );
  155. expect( Array.from( parent2.getChildren() ) ).to.deep.equal( [ node ] );
  156. // Verify deltas and operations.
  157. sinon.assert.calledOnce( spy );
  158. expect( spy.firstCall.args[ 0 ].type ).to.equal( 'move' );
  159. expect( spy.firstCall.args[ 0 ].delta.batch ).to.equal( batch );
  160. } );
  161. it( 'should move element from one parent to the other within the same document (documentA -> documentB)', () => {
  162. const rootA = doc.createRoot( '$root', 'A' );
  163. const rootB = doc.createRoot( '$root', 'B' );
  164. const node = createText( 'foo' );
  165. insert( node, rootA );
  166. const spy = sinon.spy( model, 'applyOperation' );
  167. insert( node, rootB );
  168. // Verify result.
  169. expect( Array.from( rootA.getChildren() ) ).to.deep.equal( [] );
  170. expect( Array.from( rootB.getChildren() ) ).to.deep.equal( [ node ] );
  171. // Verify deltas and operations.
  172. sinon.assert.calledOnce( spy );
  173. expect( spy.firstCall.args[ 0 ].type ).to.equal( 'move' );
  174. expect( spy.firstCall.args[ 0 ].delta.batch ).to.equal( batch );
  175. } );
  176. it( 'should move element from one parent to the other within the same document (docFragA -> docFragA)', () => {
  177. const docFragA = createDocumentFragment();
  178. const parent1 = createElement( 'parent' );
  179. const parent2 = createElement( 'parent' );
  180. const node = createText( 'foo' );
  181. insert( node, parent1 );
  182. insert( parent1, docFragA );
  183. insert( parent2, docFragA );
  184. const spy = sinon.spy( model, 'applyOperation' );
  185. insert( node, parent2 );
  186. // Verify result.
  187. expect( Array.from( parent1.getChildren() ) ).to.deep.equal( [] );
  188. expect( Array.from( parent2.getChildren() ) ).to.deep.equal( [ node ] );
  189. // Verify deltas and operations.
  190. sinon.assert.calledOnce( spy );
  191. expect( spy.firstCall.args[ 0 ].type ).to.equal( 'move' );
  192. expect( spy.firstCall.args[ 0 ].delta.batch ).to.equal( batch );
  193. } );
  194. it( 'should move element from one parent to the other within different document (document -> docFrag)', () => {
  195. const root = doc.createRoot();
  196. const docFrag = createDocumentFragment();
  197. const node = createText( 'foo' );
  198. insert( node, root );
  199. const spy = sinon.spy( model, 'applyOperation' );
  200. insert( node, docFrag );
  201. // Verify result.
  202. expect( Array.from( root.getChildren() ) ).to.deep.equal( [] );
  203. expect( Array.from( docFrag.getChildren() ) ).to.deep.equal( [ node ] );
  204. // Verify deltas and operations.
  205. sinon.assert.calledTwice( spy );
  206. expect( spy.firstCall.args[ 0 ].type ).to.equal( 'remove' );
  207. expect( spy.firstCall.args[ 0 ].delta.batch ).to.equal( batch );
  208. expect( spy.secondCall.args[ 0 ].type ).to.equal( 'insert' );
  209. expect( spy.secondCall.args[ 0 ].delta.batch ).to.equal( batch );
  210. } );
  211. it( 'should move element from one parent to the other within different document (docFragA -> docFragB)', () => {
  212. const docFragA = createDocumentFragment();
  213. const docFragB = createDocumentFragment();
  214. const node = createText( 'foo' );
  215. insert( node, docFragA );
  216. const spy = sinon.spy( model, 'applyOperation' );
  217. insert( node, docFragB );
  218. // Verify result.
  219. expect( Array.from( docFragA ) ).to.deep.equal( [] );
  220. expect( Array.from( docFragB ) ).to.deep.equal( [ node ] );
  221. // Verify deltas and operations.
  222. sinon.assert.calledTwice( spy );
  223. expect( spy.firstCall.args[ 0 ].type ).to.equal( 'detach' );
  224. expect( spy.firstCall.args[ 0 ].delta.batch ).to.equal( batch );
  225. expect( spy.secondCall.args[ 0 ].type ).to.equal( 'insert' );
  226. expect( spy.secondCall.args[ 0 ].delta.batch ).to.equal( batch );
  227. } );
  228. it( 'should transfer markers from given DocumentFragment', () => {
  229. const root = doc.createRoot();
  230. const docFrag = createDocumentFragment();
  231. appendText( 'abcd', root );
  232. appendElement( 'p', docFrag );
  233. insertText( 'foo bar', new Position( docFrag, [ 0, 0 ] ) );
  234. const marker = new Range( new Position( docFrag, [ 0, 1 ] ), new Position( docFrag, [ 0, 5 ] ) );
  235. docFrag.markers.set( 'marker', marker );
  236. insert( docFrag, new Position( root, [ 2 ] ) );
  237. expect( Array.from( model.markers ).length ).to.equal( 1 );
  238. const range = model.markers.get( 'marker' ).getRange();
  239. expect( range.root ).to.equal( root );
  240. expect( range.start.path ).to.deep.equal( [ 2, 1 ] );
  241. expect( range.end.path ).to.deep.equal( [ 2, 5 ] );
  242. } );
  243. it( 'should set each marker as a separate operation', () => {
  244. const root = doc.createRoot();
  245. const spy = sinon.spy();
  246. const docFrag = createDocumentFragment();
  247. appendText( 'abcd', root );
  248. appendElement( 'p', docFrag );
  249. insertText( 'foo bar', new Position( docFrag, [ 0, 0 ] ) );
  250. const marker1 = new Range( new Position( docFrag, [ 0, 1 ] ), new Position( docFrag, [ 0, 2 ] ) );
  251. const marker2 = new Range( new Position( docFrag, [ 0, 5 ] ), new Position( docFrag, [ 0, 6 ] ) );
  252. docFrag.markers.set( 'marker1', marker1 );
  253. docFrag.markers.set( 'marker2', marker2 );
  254. doc.on( 'change', spy );
  255. insert( docFrag, new Position( root, [ 2 ] ) );
  256. sinon.assert.calledThrice( spy );
  257. expect( spy.firstCall.args[ 1 ] ).to.equal( 'insert' );
  258. expect( spy.secondCall.args[ 1 ] ).to.equal( 'marker' );
  259. expect( spy.thirdCall.args[ 1 ] ).to.equal( 'marker' );
  260. } );
  261. it( 'should throw when trying to use detached writer', () => {
  262. const writer = new Writer( model, batch );
  263. const root = doc.createRoot();
  264. const node = createText( 'foo' );
  265. expect( () => {
  266. writer.insert( node, root );
  267. } ).to.throw( CKEditorError, /^writer-detached-writer-tries-to-modify-model/ );
  268. } );
  269. } );
  270. describe( 'insertText()', () => {
  271. it( 'should create and insert text node with attributes at given position', () => {
  272. const parent = createDocumentFragment();
  273. insertText( 'foo', { bar: 'biz' }, new Position( parent, [ 0 ] ) );
  274. expect( parent.childCount ).to.equal( 1 );
  275. expect( parent.getChild( 0 ) ).to.instanceof( Text );
  276. expect( parent.getChild( 0 ).data ).to.equal( 'foo' );
  277. expect( Array.from( parent.getChild( 0 ).getAttributes() ) ).to.deep.equal( [ [ 'bar', 'biz' ] ] );
  278. } );
  279. it( 'should create and insert text node with no attributes at given position', () => {
  280. const parent = createDocumentFragment();
  281. insertText( 'foo', null, new Position( parent, [ 0 ] ) );
  282. expect( parent.childCount ).to.equal( 1 );
  283. expect( parent.getChild( 0 ) ).to.instanceof( Text );
  284. expect( parent.getChild( 0 ).data ).to.equal( 'foo' );
  285. expect( Array.from( parent.getChild( 0 ).getAttributes() ) ).to.deep.equal( [] );
  286. } );
  287. it( 'should create and insert text node omitting attributes param', () => {
  288. const parent = createDocumentFragment();
  289. insertText( 'foo', new Position( parent, [ 0 ] ) );
  290. expect( parent.childCount ).to.equal( 1 );
  291. expect( parent.getChild( 0 ) ).to.instanceof( Text );
  292. expect( parent.getChild( 0 ).data ).to.equal( 'foo' );
  293. expect( Array.from( parent.getChild( 0 ).getAttributes() ) ).to.deep.equal( [] );
  294. } );
  295. it( 'should create and insert text node at the beginning of given element', () => {
  296. const parent = createDocumentFragment();
  297. insert( createElement( 'child' ), parent );
  298. insertText( 'foo', parent );
  299. expect( parent.childCount ).to.equal( 2 );
  300. expect( parent.getChild( 0 ) ).to.instanceof( Text );
  301. expect( parent.getChild( 1 ) ).to.instanceof( Element );
  302. } );
  303. it( 'should create and insert text node at the end of given element', () => {
  304. const parent = createDocumentFragment();
  305. insert( createElement( 'child' ), parent );
  306. insertText( 'foo', parent, 'end' );
  307. expect( parent.childCount ).to.equal( 2 );
  308. expect( parent.getChild( 0 ) ).to.instanceof( Element );
  309. expect( parent.getChild( 1 ) ).to.instanceof( Text );
  310. } );
  311. it( 'should create and insert text node at the given offset of given element', () => {
  312. const parent = createDocumentFragment();
  313. insert( createElement( 'child' ), parent );
  314. insert( createElement( 'child' ), parent );
  315. insertText( 'foo', parent, 1 );
  316. expect( parent.childCount ).to.equal( 3 );
  317. expect( parent.getChild( 0 ) ).to.instanceof( Element );
  318. expect( parent.getChild( 1 ) ).to.instanceof( Text );
  319. expect( parent.getChild( 2 ) ).to.instanceof( Element );
  320. } );
  321. it( 'should create and insert text node before the given node', () => {
  322. const parent = createDocumentFragment();
  323. const child1 = createElement( 'child' );
  324. const child2 = createElement( 'child' );
  325. insert( child1, parent );
  326. insert( child2, parent, 'end' );
  327. insertText( 'foo', child2, 'before' );
  328. expect( parent.childCount ).to.equal( 3 );
  329. expect( parent.getChild( 0 ) ).to.instanceof( Element );
  330. expect( parent.getChild( 1 ) ).to.instanceof( Text );
  331. expect( parent.getChild( 2 ) ).to.instanceof( Element );
  332. } );
  333. it( 'should create and insert text node after the given node', () => {
  334. const parent = createDocumentFragment();
  335. const child1 = createElement( 'child' );
  336. const child2 = createElement( 'child' );
  337. insert( child1, parent );
  338. insert( child2, parent, 'end' );
  339. insertText( 'foo', child1, 'after' );
  340. expect( parent.childCount ).to.equal( 3 );
  341. expect( parent.getChild( 0 ) ).to.instanceof( Element );
  342. expect( parent.getChild( 1 ) ).to.instanceof( Text );
  343. expect( parent.getChild( 2 ) ).to.instanceof( Element );
  344. } );
  345. it( 'should create proper delta', () => {
  346. const parent = createDocumentFragment();
  347. const spy = sinon.spy( model, 'applyOperation' );
  348. insertText( 'foo', parent );
  349. sinon.assert.calledOnce( spy );
  350. expect( spy.lastCall.args[ 0 ].type ).to.equal( 'insert' );
  351. expect( spy.lastCall.args[ 0 ].delta ).to.instanceof( WeakInsertDelta );
  352. expect( spy.lastCall.args[ 0 ].delta.batch ).to.equal( batch );
  353. } );
  354. it( 'should throw when trying to use detached writer', () => {
  355. const writer = new Writer( model, batch );
  356. const parent = createDocumentFragment();
  357. expect( () => {
  358. writer.insertText( 'foo', parent );
  359. } ).to.throw( CKEditorError, /^writer-detached-writer-tries-to-modify-model/ );
  360. } );
  361. } );
  362. describe( 'insertElement()', () => {
  363. it( 'should create and insert element with attributes at given position', () => {
  364. const parent = createDocumentFragment();
  365. insertElement( 'foo', { bar: 'biz' }, new Position( parent, [ 0 ] ) );
  366. expect( parent.childCount ).to.equal( 1 );
  367. expect( parent.getChild( 0 ) ).to.instanceof( Element );
  368. expect( parent.getChild( 0 ).name ).to.equal( 'foo' );
  369. expect( Array.from( parent.getChild( 0 ).getAttributes() ) ).to.deep.equal( [ [ 'bar', 'biz' ] ] );
  370. } );
  371. it( 'should create and insert element with no attributes at given position', () => {
  372. const parent = createDocumentFragment();
  373. insertElement( 'foo', null, new Position( parent, [ 0 ] ) );
  374. expect( parent.childCount ).to.equal( 1 );
  375. expect( parent.getChild( 0 ) ).to.instanceof( Element );
  376. expect( parent.getChild( 0 ).name ).to.equal( 'foo' );
  377. expect( Array.from( parent.getChild( 0 ).getAttributes() ) ).to.deep.equal( [] );
  378. } );
  379. it( 'should create and insert element with no attributes omitting attributes param', () => {
  380. const parent = createDocumentFragment();
  381. insertElement( 'foo', new Position( parent, [ 0 ] ) );
  382. expect( parent.childCount ).to.equal( 1 );
  383. expect( parent.getChild( 0 ) ).to.instanceof( Element );
  384. expect( parent.getChild( 0 ).name ).to.equal( 'foo' );
  385. expect( Array.from( parent.getChild( 0 ).getAttributes() ) ).to.deep.equal( [] );
  386. } );
  387. it( 'should create and insert element at the beginning of given element', () => {
  388. const parent = createDocumentFragment();
  389. insert( createElement( 'child' ), parent );
  390. insertElement( 'foo', parent );
  391. expect( parent.childCount ).to.equal( 2 );
  392. expect( parent.getChild( 0 ).name ).to.equal( 'foo' );
  393. expect( parent.getChild( 1 ).name ).to.equal( 'child' );
  394. } );
  395. it( 'should create and insert element at the end of given element', () => {
  396. const parent = createDocumentFragment();
  397. insert( createElement( 'child' ), parent );
  398. insertElement( 'foo', parent, 'end' );
  399. expect( parent.childCount ).to.equal( 2 );
  400. expect( parent.getChild( 0 ).name ).to.equal( 'child' );
  401. expect( parent.getChild( 1 ).name ).to.equal( 'foo' );
  402. } );
  403. it( 'should create and insert element at the given offset of given element', () => {
  404. const parent = createDocumentFragment();
  405. insert( createElement( 'child1' ), parent );
  406. insert( createElement( 'child2' ), parent, 'end' );
  407. insertElement( 'foo', parent, 1 );
  408. expect( parent.childCount ).to.equal( 3 );
  409. expect( parent.getChild( 0 ).name ).to.equal( 'child1' );
  410. expect( parent.getChild( 1 ).name ).to.equal( 'foo' );
  411. expect( parent.getChild( 2 ).name ).to.equal( 'child2' );
  412. } );
  413. it( 'should create and insert element before the given node', () => {
  414. const parent = createDocumentFragment();
  415. const child1 = createElement( 'child1' );
  416. const child2 = createElement( 'child2' );
  417. insert( child1, parent );
  418. insert( child2, parent, 'end' );
  419. insertElement( 'foo', child2, 'before' );
  420. expect( parent.childCount ).to.equal( 3 );
  421. expect( parent.getChild( 0 ).name ).to.equal( 'child1' );
  422. expect( parent.getChild( 1 ).name ).to.equal( 'foo' );
  423. expect( parent.getChild( 2 ).name ).to.equal( 'child2' );
  424. } );
  425. it( 'should create and insert element after the given node', () => {
  426. const parent = createDocumentFragment();
  427. const child1 = createElement( 'child1' );
  428. const child2 = createElement( 'child2' );
  429. insert( child1, parent );
  430. insert( child2, parent, 'end' );
  431. insertElement( 'foo', child1, 'after' );
  432. expect( parent.childCount ).to.equal( 3 );
  433. expect( parent.getChild( 0 ).name ).to.equal( 'child1' );
  434. expect( parent.getChild( 1 ).name ).to.equal( 'foo' );
  435. expect( parent.getChild( 2 ).name ).to.equal( 'child2' );
  436. } );
  437. it( 'should create proper delta', () => {
  438. const parent = createDocumentFragment();
  439. const spy = sinon.spy( model, 'applyOperation' );
  440. insertText( 'foo', parent );
  441. sinon.assert.calledOnce( spy );
  442. expect( spy.lastCall.args[ 0 ].type ).to.equal( 'insert' );
  443. expect( spy.lastCall.args[ 0 ].delta ).to.instanceof( InsertDelta );
  444. expect( spy.lastCall.args[ 0 ].delta.batch ).to.equal( batch );
  445. } );
  446. it( 'should throw when trying to use detached writer', () => {
  447. const writer = new Writer( model, batch );
  448. const child = createElement( 'child' );
  449. expect( () => {
  450. writer.insertElement( 'foo', child, 'after' );
  451. } ).to.throw( CKEditorError, /^writer-detached-writer-tries-to-modify-model/ );
  452. } );
  453. } );
  454. describe( 'append()', () => {
  455. it( 'should insert element at the end of the parent', () => {
  456. const parent = createDocumentFragment();
  457. const childText = createText( 'foo' );
  458. const childElement = createElement( 'foo' );
  459. append( childText, parent );
  460. append( childElement, parent );
  461. expect( Array.from( parent ) ).to.deep.equal( [ childText, childElement ] );
  462. } );
  463. it( 'should create proper delta', () => {
  464. const parent = createDocumentFragment();
  465. const text = createText( 'foo' );
  466. const spy = sinon.spy( model, 'applyOperation' );
  467. append( text, parent );
  468. sinon.assert.calledOnce( spy );
  469. expect( spy.lastCall.args[ 0 ].type ).to.equal( 'insert' );
  470. expect( spy.lastCall.args[ 0 ].delta.batch ).to.equal( batch );
  471. } );
  472. it( 'should move element from one parent to the other within the same document (documentA -> documentA)', () => {
  473. const rootA = doc.createRoot();
  474. const parent1 = createElement( 'parent' );
  475. const parent2 = createElement( 'parent' );
  476. const node = createText( 'foo' );
  477. insert( node, parent1 );
  478. insert( parent1, rootA );
  479. insert( parent2, rootA );
  480. const spy = sinon.spy( model, 'applyOperation' );
  481. append( node, parent2 );
  482. // Verify result.
  483. expect( Array.from( parent1.getChildren() ) ).to.deep.equal( [] );
  484. expect( Array.from( parent2.getChildren() ) ).to.deep.equal( [ node ] );
  485. // Verify deltas and operations.
  486. sinon.assert.calledOnce( spy );
  487. expect( spy.firstCall.args[ 0 ].type ).to.equal( 'move' );
  488. expect( spy.firstCall.args[ 0 ].delta.batch ).to.equal( batch );
  489. } );
  490. it( 'should move element from one parent to the other within the same document (documentA -> documentB)', () => {
  491. const rootA = doc.createRoot( '$root', 'A' );
  492. const rootB = doc.createRoot( '$root', 'B' );
  493. const node = createText( 'foo' );
  494. insert( node, rootA );
  495. const spy = sinon.spy( model, 'applyOperation' );
  496. append( node, rootB );
  497. // Verify result.
  498. expect( Array.from( rootA.getChildren() ) ).to.deep.equal( [] );
  499. expect( Array.from( rootB.getChildren() ) ).to.deep.equal( [ node ] );
  500. // Verify deltas and operations.
  501. sinon.assert.calledOnce( spy );
  502. expect( spy.firstCall.args[ 0 ].type ).to.equal( 'move' );
  503. expect( spy.firstCall.args[ 0 ].delta.batch ).to.equal( batch );
  504. } );
  505. it( 'should move element from one parent to the other within the same document (docFragA -> docFragA)', () => {
  506. const docFragA = createDocumentFragment();
  507. const parent1 = createElement( 'parent' );
  508. const parent2 = createElement( 'parent' );
  509. const node = createText( 'foo' );
  510. insert( node, parent1 );
  511. insert( parent1, docFragA );
  512. insert( parent2, docFragA );
  513. const spy = sinon.spy( model, 'applyOperation' );
  514. append( node, parent2 );
  515. // Verify result.
  516. expect( Array.from( parent1.getChildren() ) ).to.deep.equal( [] );
  517. expect( Array.from( parent2.getChildren() ) ).to.deep.equal( [ node ] );
  518. // Verify deltas and operations.
  519. sinon.assert.calledOnce( spy );
  520. expect( spy.firstCall.args[ 0 ].type ).to.equal( 'move' );
  521. expect( spy.firstCall.args[ 0 ].delta.batch ).to.equal( batch );
  522. } );
  523. it( 'should move element from one parent to the other within different document (document -> docFrag)', () => {
  524. const root = doc.createRoot();
  525. const docFrag = createDocumentFragment();
  526. const node = createText( 'foo' );
  527. insert( node, root );
  528. const spy = sinon.spy( model, 'applyOperation' );
  529. append( node, docFrag );
  530. // Verify result.
  531. expect( Array.from( root.getChildren() ) ).to.deep.equal( [] );
  532. expect( Array.from( docFrag.getChildren() ) ).to.deep.equal( [ node ] );
  533. // Verify deltas and operations.
  534. sinon.assert.calledTwice( spy );
  535. expect( spy.firstCall.args[ 0 ].type ).to.equal( 'remove' );
  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. it( 'should move element from one parent to the other within different document (docFragA -> docFragB)', () => {
  541. const docFragA = createDocumentFragment();
  542. const docFragB = createDocumentFragment();
  543. const node = createText( 'foo' );
  544. insert( node, docFragA );
  545. const spy = sinon.spy( model, 'applyOperation' );
  546. append( node, docFragB );
  547. // Verify result.
  548. expect( Array.from( docFragA ) ).to.deep.equal( [] );
  549. expect( Array.from( docFragB ) ).to.deep.equal( [ node ] );
  550. // Verify deltas and operations.
  551. sinon.assert.calledTwice( spy );
  552. expect( spy.firstCall.args[ 0 ].type ).to.equal( 'detach' );
  553. expect( spy.firstCall.args[ 0 ].delta.batch ).to.equal( batch );
  554. expect( spy.secondCall.args[ 0 ].type ).to.equal( 'insert' );
  555. expect( spy.secondCall.args[ 0 ].delta.batch ).to.equal( batch );
  556. } );
  557. } );
  558. describe( 'appendText()', () => {
  559. it( 'should create and insert text node with attributes at the end of the parent', () => {
  560. const parent = createDocumentFragment();
  561. appendText( 'foo', { bar: 'biz' }, parent );
  562. appendText( 'bar', { biz: 'bar' }, parent );
  563. expect( parent.childCount ).to.equal( 2 );
  564. expect( parent.getChild( 0 ).data ).to.equal( 'foo' );
  565. expect( Array.from( parent.getChild( 0 ).getAttributes() ) ).to.deep.equal( [ [ 'bar', 'biz' ] ] );
  566. expect( parent.getChild( 1 ).data ).to.equal( 'bar' );
  567. expect( Array.from( parent.getChild( 1 ).getAttributes() ) ).to.deep.equal( [ [ 'biz', 'bar' ] ] );
  568. } );
  569. it( 'should create and insert text node with no attributes at the end of the parent', () => {
  570. const parent = createDocumentFragment();
  571. appendText( 'foo', null, parent );
  572. expect( parent.childCount ).to.equal( 1 );
  573. expect( parent.getChild( 0 ) ).to.instanceof( Text );
  574. expect( parent.getChild( 0 ).data ).to.equal( 'foo' );
  575. expect( Array.from( parent.getChild( 0 ).getAttributes() ) ).to.deep.equal( [] );
  576. } );
  577. it( 'should create and insert text node with no attributes omitting attributes param', () => {
  578. const parent = createDocumentFragment();
  579. appendText( 'foo', parent );
  580. expect( parent.childCount ).to.equal( 1 );
  581. expect( parent.getChild( 0 ) ).to.instanceof( Text );
  582. expect( parent.getChild( 0 ).data ).to.equal( 'foo' );
  583. expect( Array.from( parent.getChild( 0 ).getAttributes() ) ).to.deep.equal( [] );
  584. } );
  585. it( 'should create proper delta and operations', () => {
  586. const parent = createDocumentFragment();
  587. const spy = sinon.spy( model, 'applyOperation' );
  588. appendText( 'foo', parent );
  589. sinon.assert.calledOnce( spy );
  590. expect( spy.firstCall.args[ 0 ].type ).to.equal( 'insert' );
  591. expect( spy.firstCall.args[ 0 ].delta ).to.instanceof( WeakInsertDelta );
  592. expect( spy.firstCall.args[ 0 ].delta.batch ).to.equal( batch );
  593. } );
  594. it( 'should throw when trying to use detached writer', () => {
  595. const writer = new Writer( model, batch );
  596. const parent = createDocumentFragment();
  597. expect( () => {
  598. writer.appendText( 'foo', parent );
  599. } ).to.throw( CKEditorError, /^writer-detached-writer-tries-to-modify-model/ );
  600. } );
  601. } );
  602. describe( 'appendElement()', () => {
  603. it( 'should create and insert element with attributes at the end of the parent', () => {
  604. const parent = createDocumentFragment();
  605. appendElement( 'foo', { bar: 'biz' }, parent );
  606. appendElement( 'bar', { biz: 'bar' }, parent );
  607. expect( parent.childCount ).to.equal( 2 );
  608. expect( parent.getChild( 0 ).name ).to.equal( 'foo' );
  609. expect( Array.from( parent.getChild( 0 ).getAttributes() ) ).to.deep.equal( [ [ 'bar', 'biz' ] ] );
  610. expect( parent.getChild( 1 ).name ).to.equal( 'bar' );
  611. expect( Array.from( parent.getChild( 1 ).getAttributes() ) ).to.deep.equal( [ [ 'biz', 'bar' ] ] );
  612. } );
  613. it( 'should create and insert element with no attributes at the end of the parent', () => {
  614. const parent = createDocumentFragment();
  615. appendElement( 'foo', null, parent );
  616. expect( parent.childCount ).to.equal( 1 );
  617. expect( parent.getChild( 0 ).name ).to.equal( 'foo' );
  618. expect( Array.from( parent.getChild( 0 ).getAttributes() ) ).to.deep.equal( [] );
  619. } );
  620. it( 'should create and insert element with no attributes omitting attributes param', () => {
  621. const parent = createDocumentFragment();
  622. appendElement( 'foo', parent );
  623. expect( parent.childCount ).to.equal( 1 );
  624. expect( parent.getChild( 0 ).name ).to.equal( 'foo' );
  625. expect( Array.from( parent.getChild( 0 ).getAttributes() ) ).to.deep.equal( [] );
  626. } );
  627. it( 'should create proper delta and operation', () => {
  628. const parent = createDocumentFragment();
  629. const spy = sinon.spy( model, 'applyOperation' );
  630. appendElement( 'foo', parent );
  631. sinon.assert.calledOnce( spy );
  632. expect( spy.firstCall.args[ 0 ].type ).to.equal( 'insert' );
  633. expect( spy.firstCall.args[ 0 ].delta ).to.instanceof( InsertDelta ).to.not.instanceof( WeakInsertDelta );
  634. expect( spy.firstCall.args[ 0 ].delta.batch ).to.equal( batch );
  635. } );
  636. it( 'should throw when trying to use detached writer', () => {
  637. const writer = new Writer( model, batch );
  638. const parent = createDocumentFragment();
  639. expect( () => {
  640. writer.appendElement( 'foo', parent );
  641. } ).to.throw( CKEditorError, /^writer-detached-writer-tries-to-modify-model/ );
  642. } );
  643. } );
  644. describe( 'setAttribute() / removeAttribute()', () => {
  645. let root, spy;
  646. beforeEach( () => {
  647. root = doc.createRoot();
  648. } );
  649. describe( 'change attribute on node', () => {
  650. let node, text;
  651. beforeEach( () => {
  652. node = createElement( 'p', { a: 1 } );
  653. text = createText( 'c', { a: 1 } );
  654. append( node, root );
  655. append( text, root );
  656. spy = sinon.spy( model, 'applyOperation' );
  657. } );
  658. describe( 'setAttribute', () => {
  659. it( 'should create the attribute on element', () => {
  660. setAttribute( 'b', 2, node );
  661. expect( spy.callCount ).to.equal( 1 );
  662. expect( node.getAttribute( 'b' ) ).to.equal( 2 );
  663. } );
  664. it( 'should change the attribute of element', () => {
  665. setAttribute( 'a', 2, node );
  666. expect( spy.callCount ).to.equal( 1 );
  667. expect( node.getAttribute( 'a' ) ).to.equal( 2 );
  668. } );
  669. it( 'should create the attribute on text node', () => {
  670. setAttribute( 'b', 2, text );
  671. expect( spy.callCount ).to.equal( 1 );
  672. expect( root.getChild( 1 ).getAttribute( 'b' ) ).to.equal( 2 );
  673. } );
  674. it( 'should change the attribute of text node', () => {
  675. setAttribute( 'a', 2, text );
  676. expect( spy.callCount ).to.equal( 1 );
  677. expect( root.getChild( 1 ).getAttribute( 'a' ) ).to.equal( 2 );
  678. } );
  679. it( 'should do nothing if the attribute value is the same', () => {
  680. setAttribute( 'a', 1, node );
  681. expect( spy.callCount ).to.equal( 0 );
  682. expect( node.getAttribute( 'a' ) ).to.equal( 1 );
  683. } );
  684. it( 'should throw when trying to use detached writer', () => {
  685. const writer = new Writer( model, batch );
  686. expect( () => {
  687. writer.setAttribute( 'a', 1, node );
  688. } ).to.throw( CKEditorError, /^writer-detached-writer-tries-to-modify-model/ );
  689. } );
  690. } );
  691. describe( 'removeAttribute', () => {
  692. it( 'should remove the attribute from element', () => {
  693. removeAttribute( 'a', node );
  694. expect( spy.callCount ).to.equal( 1 );
  695. expect( node.getAttribute( 'a' ) ).to.be.undefined;
  696. } );
  697. it( 'should remove the attribute from character', () => {
  698. removeAttribute( 'a', text );
  699. expect( spy.callCount ).to.equal( 1 );
  700. expect( root.getChild( 1 ).getAttribute( 'a' ) ).to.be.undefined;
  701. } );
  702. it( 'should do nothing if the attribute is not set', () => {
  703. removeAttribute( 'b', node );
  704. expect( spy.callCount ).to.equal( 0 );
  705. } );
  706. it( 'should throw when trying to use detached writer', () => {
  707. const writer = new Writer( model, batch );
  708. expect( () => {
  709. writer.removeAttribute( 'b', node );
  710. } ).to.throw( CKEditorError, /^writer-detached-writer-tries-to-modify-model/ );
  711. } );
  712. } );
  713. } );
  714. describe( 'change attribute on range', () => {
  715. beforeEach( () => {
  716. const element = createElement( 'e', { a: 2 } );
  717. appendText( 'xxx', { a: 1 }, root );
  718. appendText( 'xxx', root );
  719. appendText( 'xxx', { a: 1 }, root );
  720. appendText( 'xxx', { a: 2 }, root );
  721. appendText( 'xxx', root );
  722. appendText( 'xxx', { a: 1 }, root );
  723. appendText( 'xxx', element );
  724. append( element, root );
  725. appendText( 'xxx', root );
  726. spy = sinon.spy( model, 'applyOperation' );
  727. } );
  728. function getRange( startIndex, endIndex ) {
  729. return new Range(
  730. Position.createFromParentAndOffset( root, startIndex ),
  731. Position.createFromParentAndOffset( root, endIndex )
  732. );
  733. }
  734. function getChangesAttrsCount() {
  735. let totalNumber = 0;
  736. for ( const delta of batch.deltas ) {
  737. for ( const operation of delta.operations ) {
  738. if ( operation.range ) {
  739. totalNumber += count( operation.range.getItems( { singleCharacters: true } ) );
  740. }
  741. }
  742. }
  743. return totalNumber;
  744. }
  745. function getCompressedAttrs() {
  746. // default: 111---111222---1112------
  747. const range = Range.createIn( root );
  748. return Array.from( range.getItems( { singleCharacters: true } ) )
  749. .map( item => item.getAttribute( 'a' ) || '-' )
  750. .join( '' );
  751. }
  752. describe( 'setAttribute', () => {
  753. it( 'should set the attribute on the range', () => {
  754. setAttribute( 'a', 3, getRange( 3, 6 ) );
  755. expect( spy.callCount ).to.equal( 1 );
  756. expect( getChangesAttrsCount() ).to.equal( 3 );
  757. expect( getCompressedAttrs() ).to.equal( '111333111222---1112------' );
  758. } );
  759. it( 'should split the operations if parts of the range have different attributes', () => {
  760. setAttribute( 'a', 3, getRange( 4, 14 ) );
  761. expect( spy.callCount ).to.equal( 4 );
  762. expect( getChangesAttrsCount() ).to.equal( 10 );
  763. expect( getCompressedAttrs() ).to.equal( '111-3333333333-1112------' );
  764. } );
  765. it( 'should split the operations if parts of the part of the range have the attribute', () => {
  766. setAttribute( 'a', 2, getRange( 4, 14 ) );
  767. expect( spy.callCount ).to.equal( 3 );
  768. expect( getChangesAttrsCount() ).to.equal( 7 );
  769. expect( getCompressedAttrs() ).to.equal( '111-2222222222-1112------' );
  770. } );
  771. it( 'should strip the range if the beginning have the attribute', () => {
  772. setAttribute( 'a', 1, getRange( 1, 5 ) );
  773. expect( spy.callCount ).to.equal( 1 );
  774. expect( getChangesAttrsCount() ).to.equal( 2 );
  775. expect( getCompressedAttrs() ).to.equal( '11111-111222---1112------' );
  776. } );
  777. it( 'should strip the range if the ending have the attribute', () => {
  778. setAttribute( 'a', 1, getRange( 13, 17 ) );
  779. expect( spy.callCount ).to.equal( 1 );
  780. expect( getChangesAttrsCount() ).to.equal( 2 );
  781. expect( getCompressedAttrs() ).to.equal( '111---111222-111112------' );
  782. } );
  783. it( 'should do nothing if the range has attribute', () => {
  784. setAttribute( 'a', 1, getRange( 0, 3 ) );
  785. expect( spy.callCount ).to.equal( 0 );
  786. expect( getCompressedAttrs() ).to.equal( '111---111222---1112------' );
  787. } );
  788. it( 'should not check range\'s start position node when creating operations', () => {
  789. const range = new Range(
  790. new Position( root, [ 18, 1 ] ),
  791. new Position( root, [ 19 ] )
  792. );
  793. setAttribute( 'a', 1, range );
  794. expect( spy.callCount ).to.equal( 1 );
  795. expect( getChangesAttrsCount() ).to.equal( 2 );
  796. expect( getCompressedAttrs() ).to.equal( '111---111222---1112-11---' );
  797. } );
  798. it( 'should not change elements attribute if range contains closing tag', () => {
  799. const range = new Range(
  800. new Position( root, [ 18, 1 ] ),
  801. new Position( root, [ 21 ] )
  802. );
  803. setAttribute( 'a', 1, range );
  804. expect( spy.callCount ).to.equal( 1 );
  805. expect( getChangesAttrsCount() ).to.equal( 4 );
  806. expect( getCompressedAttrs() ).to.equal( '111---111222---1112-1111-' );
  807. } );
  808. it( 'should not create an operation if the range contains only closing tag', () => {
  809. const range = new Range(
  810. new Position( root, [ 18, 3 ] ),
  811. new Position( root, [ 19 ] )
  812. );
  813. setAttribute( 'a', 3, range );
  814. expect( spy.callCount ).to.equal( 0 );
  815. expect( getCompressedAttrs() ).to.equal( '111---111222---1112------' );
  816. } );
  817. it( 'should not create an operation if is collapsed', () => {
  818. setAttribute( 'a', 1, getRange( 3, 3 ) );
  819. expect( spy.callCount ).to.equal( 0 );
  820. expect( getCompressedAttrs() ).to.equal( '111---111222---1112------' );
  821. } );
  822. it( 'should create a proper operations for the mixed range', () => {
  823. setAttribute( 'a', 1, getRange( 0, 20 ) );
  824. expect( spy.callCount ).to.equal( 5 );
  825. expect( getChangesAttrsCount() ).to.equal( 14 );
  826. expect( getCompressedAttrs() ).to.equal( '11111111111111111111111--' );
  827. } );
  828. it( 'should throw when trying to use detached writer', () => {
  829. const writer = new Writer( model, batch );
  830. expect( () => {
  831. writer.setAttribute( 'a', 1, getRange( 0, 20 ) );
  832. } ).to.throw( CKEditorError, /^writer-detached-writer-tries-to-modify-model/ );
  833. } );
  834. } );
  835. describe( 'removeAttribute', () => {
  836. it( 'should remove the attribute on the range', () => {
  837. removeAttribute( 'a', getRange( 0, 2 ) );
  838. expect( spy.callCount ).to.equal( 1 );
  839. expect( getChangesAttrsCount() ).to.equal( 2 );
  840. expect( getCompressedAttrs() ).to.equal( '--1---111222---1112------' );
  841. } );
  842. it( 'should split the operations if parts of the range have different attributes', () => {
  843. removeAttribute( 'a', getRange( 7, 11 ) );
  844. expect( spy.callCount ).to.equal( 2 );
  845. expect( getChangesAttrsCount() ).to.equal( 4 );
  846. expect( getCompressedAttrs() ).to.equal( '111---1----2---1112------' );
  847. } );
  848. it( 'should split the operations if parts of the part of the range have no attribute', () => {
  849. removeAttribute( 'a', getRange( 1, 7 ) );
  850. expect( spy.callCount ).to.equal( 2 );
  851. expect( getChangesAttrsCount() ).to.equal( 3 );
  852. expect( getCompressedAttrs() ).to.equal( '1------11222---1112------' );
  853. } );
  854. it( 'should strip the range if the beginning have no attribute', () => {
  855. removeAttribute( 'a', getRange( 4, 12 ) );
  856. expect( spy.callCount ).to.equal( 2 );
  857. expect( getChangesAttrsCount() ).to.equal( 6 );
  858. expect( getCompressedAttrs() ).to.equal( '111------------1112------' );
  859. } );
  860. it( 'should strip the range if the ending have no attribute', () => {
  861. removeAttribute( 'a', getRange( 7, 15 ) );
  862. expect( spy.callCount ).to.equal( 2 );
  863. expect( getChangesAttrsCount() ).to.equal( 5 );
  864. expect( getCompressedAttrs() ).to.equal( '111---1--------1112------' );
  865. } );
  866. it( 'should do nothing if the range has no attribute', () => {
  867. removeAttribute( 'a', getRange( 4, 5 ) );
  868. expect( spy.callCount ).to.equal( 0 );
  869. expect( getCompressedAttrs() ).to.equal( '111---111222---1112------' );
  870. } );
  871. it( 'should not check range\'s start position node when creating operations', () => {
  872. const range = new Range(
  873. new Position( root, [ 18, 3 ] ),
  874. new Position( root, [ 19 ] )
  875. );
  876. removeAttribute( 'a', range );
  877. expect( spy.callCount ).to.equal( 0 );
  878. expect( getChangesAttrsCount() ).to.equal( 0 );
  879. expect( getCompressedAttrs() ).to.equal( '111---111222---1112------' );
  880. } );
  881. it( 'should not apply operation twice in the range contains opening and closing tags', () => {
  882. removeAttribute( 'a', getRange( 18, 22 ) );
  883. expect( spy.callCount ).to.equal( 1 );
  884. expect( getChangesAttrsCount() ).to.equal( 1 );
  885. expect( getCompressedAttrs() ).to.equal( '111---111222---111-------' );
  886. } );
  887. it( 'should not create an operation if range is collapsed', () => {
  888. removeAttribute( 'a', getRange( 3, 3 ) );
  889. expect( spy.callCount ).to.equal( 0 );
  890. expect( getCompressedAttrs() ).to.equal( '111---111222---1112------' );
  891. } );
  892. it( 'should create a proper operations for the mixed range', () => {
  893. removeAttribute( 'a', getRange( 3, 15 ) );
  894. expect( spy.callCount ).to.equal( 2 );
  895. expect( getChangesAttrsCount() ).to.equal( 6 );
  896. expect( getCompressedAttrs() ).to.equal( '111------------1112------' );
  897. } );
  898. it( 'should throw when trying to use detached writer', () => {
  899. const writer = new Writer( model, batch );
  900. expect( () => {
  901. writer.removeAttribute( 'a', getRange( 3, 15 ) );
  902. } ).to.throw( CKEditorError, /^writer-detached-writer-tries-to-modify-model/ );
  903. } );
  904. } );
  905. } );
  906. describe( 'change attribute on root element', () => {
  907. let p;
  908. beforeEach( () => {
  909. p = createElement( 'p', { a: 3 } );
  910. spy = sinon.spy( model, 'applyOperation' );
  911. } );
  912. describe( 'setAttribute', () => {
  913. it( 'should create the attribute on root', () => {
  914. setAttribute( 'b', 2, root );
  915. expect( spy.callCount ).to.equal( 1 );
  916. expect( root.getAttribute( 'b' ) ).to.equal( 2 );
  917. } );
  918. it( 'should create the attribute on detached root', () => {
  919. setAttribute( 'b', 2, p );
  920. expect( spy.callCount ).to.equal( 1 );
  921. expect( p.getAttribute( 'b' ) ).to.equal( 2 );
  922. } );
  923. it( 'should change the attribute of root', () => {
  924. setAttribute( 'a', 2, root );
  925. expect( spy.callCount ).to.equal( 1 );
  926. expect( root.getAttribute( 'a' ) ).to.equal( 2 );
  927. } );
  928. it( 'should change the attribute of detached root', () => {
  929. setAttribute( 'a', 2, p );
  930. expect( spy.callCount ).to.equal( 1 );
  931. expect( p.getAttribute( 'a' ) ).to.equal( 2 );
  932. } );
  933. it( 'should do nothing if the attribute value is the same', () => {
  934. setAttribute( 'a', 1, root );
  935. expect( spy.callCount ).to.equal( 1 );
  936. setAttribute( 'a', 1, root );
  937. expect( spy.callCount ).to.equal( 1 );
  938. expect( root.getAttribute( 'a' ) ).to.equal( 1 );
  939. } );
  940. it( 'should do nothing if the attribute value is the same on detached root', () => {
  941. setAttribute( 'a', 1, p );
  942. expect( spy.callCount ).to.equal( 1 );
  943. setAttribute( 'a', 1, p );
  944. expect( spy.callCount ).to.equal( 1 );
  945. expect( p.getAttribute( 'a' ) ).to.equal( 1 );
  946. } );
  947. it( 'should throw when trying to use detached writer', () => {
  948. const writer = new Writer( model, batch );
  949. expect( () => {
  950. writer.setAttribute( 'a', 1, p );
  951. } ).to.throw( CKEditorError, /^writer-detached-writer-tries-to-modify-model/ );
  952. } );
  953. } );
  954. describe( 'removeAttribute', () => {
  955. it( 'should remove the attribute from root', () => {
  956. setAttribute( 'a', 1, root );
  957. removeAttribute( 'a', root );
  958. expect( spy.callCount ).to.equal( 2 );
  959. expect( root.getAttribute( 'a' ) ).to.be.undefined;
  960. } );
  961. it( 'should do nothing if the attribute is not set', () => {
  962. removeAttribute( 'b', root );
  963. expect( spy.callCount ).to.equal( 0 );
  964. } );
  965. it( 'should throw when trying to use detached writer', () => {
  966. const writer = new Writer( model, batch );
  967. expect( () => {
  968. writer.removeAttribute( 'b', root );
  969. } ).to.throw( CKEditorError, /^writer-detached-writer-tries-to-modify-model/ );
  970. } );
  971. } );
  972. describe( 'clearAttributes', () => {
  973. it( 'should clear attributes from range', () => {
  974. appendText( 'xxx', { a: 1, b: 2, c: 3 }, root );
  975. appendText( 'xxx', root );
  976. appendText( 'xxx', { a: 1 }, root );
  977. appendText( 'xxx', { b: 2 }, root );
  978. appendText( 'xxx', root );
  979. appendElement( 'e', { a: 1 }, root );
  980. appendText( 'xxx', root );
  981. const range = Range.createIn( root );
  982. clearAttributes( range );
  983. let itemsCount = 0;
  984. for ( const item of range.getItems() ) {
  985. itemsCount++;
  986. expect( Array.from( item.getAttributeKeys() ).length ).to.equal( 0 );
  987. }
  988. expect( itemsCount ).to.equal( 3 );
  989. } );
  990. it( 'should clear attributes on element', () => {
  991. const element = createElement( 'x', { a: 1, b: 2, c: 3 }, root );
  992. expect( Array.from( element.getAttributeKeys() ).length ).to.equal( 3 );
  993. clearAttributes( element );
  994. expect( Array.from( element.getAttributeKeys() ).length ).to.equal( 0 );
  995. } );
  996. it( 'should clear attributes on root element', () => {
  997. setAttributes( { a: 1, b: 2, c: 3 }, root );
  998. expect( Array.from( root.getAttributeKeys() ).length ).to.equal( 3 );
  999. clearAttributes( root );
  1000. expect( Array.from( root.getAttributeKeys() ).length ).to.equal( 0 );
  1001. } );
  1002. it( 'should do nothing if there are no attributes', () => {
  1003. const element = createElement( 'x' );
  1004. expect( Array.from( element.getAttributeKeys() ).length ).to.equal( 0 );
  1005. clearAttributes( element );
  1006. expect( Array.from( element.getAttributeKeys() ).length ).to.equal( 0 );
  1007. } );
  1008. it( 'should throw when trying to use detached writer', () => {
  1009. const writer = new Writer( model, batch );
  1010. const element = createElement( 'x' );
  1011. expect( () => {
  1012. writer.clearAttributes( element );
  1013. } ).to.throw( CKEditorError, /^writer-detached-writer-tries-to-modify-model/ );
  1014. } );
  1015. } );
  1016. } );
  1017. it( 'should not add empty delta to the batch', () => {
  1018. const nodeA = new Element( 'p', { a: 1 } );
  1019. const nodeB = new Element( 'p', { b: 2 } );
  1020. root.insertChildren( 0, [ nodeA, nodeB ] );
  1021. setAttribute( 'a', 1, nodeA );
  1022. expect( batch.deltas.length ).to.equal( 0 );
  1023. removeAttribute( 'x', Range.createIn( root ) );
  1024. expect( batch.deltas.length ).to.equal( 0 );
  1025. } );
  1026. } );
  1027. describe( 'setAttributes()', () => {
  1028. let frag, item;
  1029. beforeEach( () => {
  1030. frag = createDocumentFragment();
  1031. item = createText( 'xxx', { b: 2, c: 3 } );
  1032. appendText( 'xxx', { a: 1 }, frag );
  1033. append( item, frag );
  1034. } );
  1035. it( 'should set attributes one by one on range', () => {
  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( { 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 range for map as attributes list', () => {
  1053. const range = Range.createIn( frag );
  1054. let spy;
  1055. model.change( writer => {
  1056. // `setAttribute` is a not trivial operation and is deeply tested above, there is no point to duplicate
  1057. // such a big amount of the same tests, so let's use a spy here.
  1058. spy = sinon.spy( writer, 'setAttribute' );
  1059. writer.setAttributes( new Map( [ [ 'a', 3 ], [ 'c', null ] ] ), range );
  1060. } );
  1061. // Verify result.
  1062. expect( Array.from( frag.getChild( 0 ).getAttributes() ) ).to.deep.equal( [ [ 'a', 3 ] ] );
  1063. expect( Array.from( frag.getChild( 1 ).getAttributes() ) ).to.deep.equal( [ [ 'b', 2 ], [ 'a', 3 ] ] );
  1064. // Verify operations
  1065. sinon.assert.calledTwice( spy );
  1066. sinon.assert.calledWith( spy.firstCall, 'a', 3, range );
  1067. sinon.assert.calledWith( spy.secondCall, 'c', null, range );
  1068. } );
  1069. it( 'should set attributes one by one on item', () => {
  1070. let spy;
  1071. model.change( writer => {
  1072. // `setAttribute` is a not trivial operation and is deeply tested above, there is no point to duplicate
  1073. // such a big amount of the same tests, so let's use a spy here.
  1074. spy = sinon.spy( writer, 'setAttribute' );
  1075. writer.setAttributes( { a: 3, c: null }, item );
  1076. } );
  1077. // Verify result.
  1078. expect( Array.from( item.getAttributes() ) ).to.deep.equal( [ [ 'b', 2 ], [ 'a', 3 ] ] );
  1079. // Verify operations
  1080. sinon.assert.calledTwice( spy );
  1081. sinon.assert.calledWith( spy.firstCall, 'a', 3, item );
  1082. sinon.assert.calledWith( spy.secondCall, 'c', null, item );
  1083. } );
  1084. it( 'should set attributes one by one on item for maps as attributes list', () => {
  1085. let spy;
  1086. model.change( writer => {
  1087. // `setAttribute` is a not trivial operation and is deeply tested above, there is no point to duplicate
  1088. // such a big amount of the same tests, so let's use a spy here.
  1089. spy = sinon.spy( writer, 'setAttribute' );
  1090. writer.setAttributes( new Map( [ [ 'a', 3 ], [ 'c', null ] ] ), item );
  1091. } );
  1092. // Verify result.
  1093. expect( Array.from( item.getAttributes() ) ).to.deep.equal( [ [ 'b', 2 ], [ 'a', 3 ] ] );
  1094. // Verify operations
  1095. sinon.assert.calledTwice( spy );
  1096. sinon.assert.calledWith( spy.firstCall, 'a', 3, item );
  1097. sinon.assert.calledWith( spy.secondCall, 'c', null, item );
  1098. } );
  1099. it( 'should throw when trying to use detached writer', () => {
  1100. const writer = new Writer( model, batch );
  1101. expect( () => {
  1102. writer.setAttributes( new Map( [ [ 'a', 3 ], [ 'c', null ] ] ), item );
  1103. } ).to.throw( CKEditorError, /^writer-detached-writer-tries-to-modify-model/ );
  1104. } );
  1105. } );
  1106. describe( 'merge()', () => {
  1107. let root, p1, p2;
  1108. beforeEach( () => {
  1109. root = doc.createRoot();
  1110. p1 = new Element( 'p', { key1: 'value1' }, new Text( 'foo' ) );
  1111. p2 = new Element( 'p', { key2: 'value2' }, new Text( 'bar' ) );
  1112. root.insertChildren( 0, [ p1, p2 ] );
  1113. } );
  1114. it( 'should merge foo and bar into foobar', () => {
  1115. merge( new Position( root, [ 1 ] ) );
  1116. expect( root.maxOffset ).to.equal( 1 );
  1117. expect( root.getChild( 0 ).name ).to.equal( 'p' );
  1118. expect( root.getChild( 0 ).maxOffset ).to.equal( 6 );
  1119. expect( count( root.getChild( 0 ).getAttributes() ) ).to.equal( 1 );
  1120. expect( root.getChild( 0 ).getAttribute( 'key1' ) ).to.equal( 'value1' );
  1121. expect( root.getChild( 0 ).getChild( 0 ).data ).to.equal( 'foobar' );
  1122. } );
  1123. it( 'should throw if there is no element after', () => {
  1124. expect( () => {
  1125. merge( new Position( root, [ 2 ] ) );
  1126. } ).to.throw( CKEditorError, /^writer-merge-no-element-after/ );
  1127. } );
  1128. it( 'should throw if there is no element before', () => {
  1129. expect( () => {
  1130. merge( new Position( root, [ 0, 2 ] ) );
  1131. } ).to.throw( CKEditorError, /^writer-merge-no-element-before/ );
  1132. } );
  1133. it( 'should throw when trying to use detached writer', () => {
  1134. const writer = new Writer( model, batch );
  1135. expect( () => {
  1136. writer.merge( new Position( root, [ 1 ] ) );
  1137. } ).to.throw( CKEditorError, /^writer-detached-writer-tries-to-modify-model/ );
  1138. } );
  1139. } );
  1140. describe( 'move()', () => {
  1141. let root, range, div, p;
  1142. beforeEach( () => {
  1143. root = doc.createRoot();
  1144. div = new Element( 'div', [], new Text( 'foobar' ) );
  1145. p = new Element( 'p', [], new Text( 'abcxyz' ) );
  1146. div.insertChildren( 0, [ new Element( 'p', [], new Text( 'gggg' ) ) ] );
  1147. div.insertChildren( 2, [ new Element( 'p', [], new Text( 'hhhh' ) ) ] );
  1148. root.insertChildren( 0, [ div, p ] );
  1149. range = new Range( new Position( root, [ 0, 3 ] ), new Position( root, [ 0, 7 ] ) );
  1150. } );
  1151. it( 'should move flat range of nodes', () => {
  1152. move( range, new Position( root, [ 1, 3 ] ) );
  1153. expect( getNodesAndText( Range.createIn( root.getChild( 0 ) ) ) ).to.equal( 'PggggPfoPhhhhP' );
  1154. expect( getNodesAndText( Range.createIn( root.getChild( 1 ) ) ) ).to.equal( 'abcobarxyz' );
  1155. } );
  1156. it( 'should throw if object to move is not a range', () => {
  1157. expect( () => {
  1158. move( root.getChild( 0 ), new Position( root, [ 1, 3 ] ) );
  1159. } ).to.throw( CKEditorError, /^writer-move-invalid-range/ );
  1160. } );
  1161. it( 'should throw if given range is not flat', () => {
  1162. const notFlatRange = new Range( new Position( root, [ 0, 2, 2 ] ), new Position( root, [ 0, 6 ] ) );
  1163. expect( () => {
  1164. move( notFlatRange, new Position( root, [ 1, 3 ] ) );
  1165. } ).to.throw( CKEditorError, /^writer-move-range-not-flat/ );
  1166. } );
  1167. it( 'should throw if range is going to be moved to the other document', () => {
  1168. const docFrag = createDocumentFragment();
  1169. expect( () => {
  1170. move( range, docFrag );
  1171. } ).to.throw( CKEditorError, /^writer-move-different-document/ );
  1172. } );
  1173. it( 'should throw when trying to use detached writer', () => {
  1174. const writer = new Writer( model, batch );
  1175. expect( () => {
  1176. writer.move( range, new Position( root, [ 1, 3 ] ) );
  1177. } ).to.throw( CKEditorError, /^writer-detached-writer-tries-to-modify-model/ );
  1178. } );
  1179. } );
  1180. describe( 'remove()', () => {
  1181. let div, p, range;
  1182. beforeEach( () => {
  1183. div = createElement( 'div' );
  1184. appendText( 'foobar', div );
  1185. p = createElement( 'p' );
  1186. appendText( 'abcxyz', p );
  1187. insertElement( 'p', div );
  1188. appendElement( 'p', div );
  1189. insertText( 'gggg', new Position( div, [ 0, 0 ] ) );
  1190. insertText( 'hhhh', new Position( div, [ 7, 0 ] ) );
  1191. } );
  1192. describe( 'remove from document', () => {
  1193. let root;
  1194. beforeEach( () => {
  1195. root = doc.createRoot();
  1196. append( div, root );
  1197. append( p, root );
  1198. // Range starts in ROOT > DIV > P > gg|gg.
  1199. // Range ends in ROOT > DIV > ...|ar.
  1200. range = new Range( new Position( root, [ 0, 0, 2 ] ), new Position( root, [ 0, 5 ] ) );
  1201. } );
  1202. it( 'should remove specified node', () => {
  1203. remove( div );
  1204. expect( root.maxOffset ).to.equal( 1 );
  1205. expect( root.childCount ).to.equal( 1 );
  1206. expect( getNodesAndText( Range.createIn( root.getChild( 0 ) ) ) ).to.equal( 'abcxyz' );
  1207. } );
  1208. it( 'should remove specified text node', () => {
  1209. remove( p.getChild( 0 ) );
  1210. expect( getNodesAndText( Range.createOn( p ) ) ).to.equal( 'PP' );
  1211. } );
  1212. it( 'should remove any range of nodes', () => {
  1213. remove( range );
  1214. expect( getNodesAndText( Range.createIn( root.getChild( 0 ) ) ) ).to.equal( 'PggParPhhhhP' );
  1215. expect( getNodesAndText( Range.createIn( root.getChild( 1 ) ) ) ).to.equal( 'abcxyz' );
  1216. } );
  1217. it( 'should create minimal number of remove deltas, each with only one operation', () => {
  1218. batch = new Batch();
  1219. remove( range );
  1220. expect( batch.deltas.length ).to.equal( 2 );
  1221. expect( batch.deltas[ 0 ].operations.length ).to.equal( 1 );
  1222. expect( batch.deltas[ 1 ].operations.length ).to.equal( 1 );
  1223. } );
  1224. it( 'should use RemoveOperation', () => {
  1225. batch = new Batch();
  1226. remove( div );
  1227. expect( batch.deltas[ 0 ].operations[ 0 ].type ).to.equal( 'remove' );
  1228. } );
  1229. it( 'should throw when trying to use detached writer', () => {
  1230. const writer = new Writer( model, batch );
  1231. expect( () => {
  1232. writer.remove( range );
  1233. } ).to.throw( CKEditorError, /^writer-detached-writer-tries-to-modify-model/ );
  1234. } );
  1235. } );
  1236. describe( 'remove from document fragment', () => {
  1237. let frag;
  1238. beforeEach( () => {
  1239. frag = createDocumentFragment();
  1240. append( div, frag );
  1241. append( p, frag );
  1242. // Range starts in FRAG > DIV > P > gg|gg.
  1243. // Range ends in FRAG > DIV > ...|ar.
  1244. range = new Range( new Position( frag, [ 0, 0, 2 ] ), new Position( frag, [ 0, 5 ] ) );
  1245. } );
  1246. it( 'should remove specified node', () => {
  1247. remove( div );
  1248. expect( frag.maxOffset ).to.equal( 1 );
  1249. expect( frag.childCount ).to.equal( 1 );
  1250. expect( getNodesAndText( Range.createIn( frag.getChild( 0 ) ) ) ).to.equal( 'abcxyz' );
  1251. } );
  1252. it( 'should remove specified text node', () => {
  1253. remove( p.getChild( 0 ) );
  1254. expect( getNodesAndText( Range.createOn( p ) ) ).to.equal( 'PP' );
  1255. } );
  1256. it( 'should remove any range of nodes', () => {
  1257. remove( range );
  1258. expect( getNodesAndText( Range.createIn( frag.getChild( 0 ) ) ) ).to.equal( 'PggParPhhhhP' );
  1259. expect( getNodesAndText( Range.createIn( frag.getChild( 1 ) ) ) ).to.equal( 'abcxyz' );
  1260. } );
  1261. it( 'should create minimal number of remove deltas, each with only one operation', () => {
  1262. batch = new Batch();
  1263. remove( range );
  1264. expect( batch.deltas.length ).to.equal( 2 );
  1265. expect( batch.deltas[ 0 ].operations.length ).to.equal( 1 );
  1266. expect( batch.deltas[ 1 ].operations.length ).to.equal( 1 );
  1267. } );
  1268. it( 'should use DetachOperation', () => {
  1269. batch = new Batch();
  1270. remove( div );
  1271. expect( batch.deltas[ 0 ].operations[ 0 ].type ).to.equal( 'detach' );
  1272. } );
  1273. it( 'should throw when trying to use detached writer', () => {
  1274. const writer = new Writer( model, batch );
  1275. expect( () => {
  1276. writer.remove( range );
  1277. } ).to.throw( CKEditorError, /^writer-detached-writer-tries-to-modify-model/ );
  1278. } );
  1279. } );
  1280. } );
  1281. describe( 'rename()', () => {
  1282. let root;
  1283. beforeEach( () => {
  1284. root = doc.createRoot();
  1285. const p = new Element( 'p', null, new Text( 'abc' ) );
  1286. root.appendChildren( p );
  1287. rename( p, 'h' );
  1288. } );
  1289. it( 'should rename given element', () => {
  1290. expect( root.maxOffset ).to.equal( 1 );
  1291. expect( root.getChild( 0 ) ).to.have.property( 'name', 'h' );
  1292. } );
  1293. it( 'should throw if not an Element instance is passed', () => {
  1294. expect( () => {
  1295. rename( new Text( 'abc' ), 'h' );
  1296. } ).to.throw( CKEditorError, /^writer-rename-not-element-instance/ );
  1297. } );
  1298. it( 'should throw when trying to use detached writer', () => {
  1299. const writer = new Writer( model, batch );
  1300. const p = new Element( 'p', null, new Text( 'abc' ) );
  1301. expect( () => {
  1302. writer.rename( p, 'h' );
  1303. } ).to.throw( CKEditorError, /^writer-detached-writer-tries-to-modify-model/ );
  1304. } );
  1305. } );
  1306. describe( 'split()', () => {
  1307. let root, p;
  1308. beforeEach( () => {
  1309. root = doc.createRoot();
  1310. p = new Element( 'p', { key: 'value' }, new Text( 'foobar' ) );
  1311. root.insertChildren( 0, p );
  1312. } );
  1313. it( 'should split foobar to foo and bar', () => {
  1314. split( new Position( root, [ 0, 3 ] ) );
  1315. expect( root.maxOffset ).to.equal( 2 );
  1316. expect( root.getChild( 0 ).name ).to.equal( 'p' );
  1317. expect( root.getChild( 0 ).maxOffset ).to.equal( 3 );
  1318. expect( count( root.getChild( 0 ).getAttributes() ) ).to.equal( 1 );
  1319. expect( root.getChild( 0 ).getAttribute( 'key' ) ).to.equal( 'value' );
  1320. expect( root.getChild( 0 ).getChild( 0 ).data ).to.equal( 'foo' );
  1321. expect( root.getChild( 1 ).name ).to.equal( 'p' );
  1322. expect( root.getChild( 1 ).maxOffset ).to.equal( 3 );
  1323. expect( count( root.getChild( 1 ).getAttributes() ) ).to.equal( 1 );
  1324. expect( root.getChild( 1 ).getAttribute( 'key' ) ).to.equal( 'value' );
  1325. expect( root.getChild( 1 ).getChild( 0 ).data ).to.equal( 'bar' );
  1326. } );
  1327. it( 'should create an empty paragraph if we split at the end', () => {
  1328. split( new Position( root, [ 0, 6 ] ) );
  1329. expect( root.maxOffset ).to.equal( 2 );
  1330. expect( root.getChild( 0 ).name ).to.equal( 'p' );
  1331. expect( root.getChild( 0 ).maxOffset ).to.equal( 6 );
  1332. expect( count( root.getChild( 0 ).getAttributes() ) ).to.equal( 1 );
  1333. expect( root.getChild( 0 ).getAttribute( 'key' ) ).to.equal( 'value' );
  1334. expect( root.getChild( 0 ).getChild( 0 ).data ).to.equal( 'foobar' );
  1335. expect( root.getChild( 1 ).name ).to.equal( 'p' );
  1336. expect( root.getChild( 1 ).maxOffset ).to.equal( 0 );
  1337. expect( count( root.getChild( 1 ).getAttributes() ) ).to.equal( 1 );
  1338. expect( root.getChild( 1 ).getAttribute( 'key' ) ).to.equal( 'value' );
  1339. } );
  1340. it( 'should throw if we try to split a root', () => {
  1341. expect( () => {
  1342. split( new Position( root, [ 0 ] ) );
  1343. } ).to.throw( CKEditorError, /^writer-split-element-no-parent/ );
  1344. } );
  1345. it( 'should throw if we try to split an element with no parent', () => {
  1346. expect( () => {
  1347. const element = createElement( 'p' );
  1348. split( new Position( element, [ 0 ] ) );
  1349. } ).to.throw( CKEditorError, /^writer-split-element-no-parent/ );
  1350. } );
  1351. it( 'should throw if we try to split a document fragment', () => {
  1352. expect( () => {
  1353. const documentFragment = createDocumentFragment();
  1354. split( new Position( documentFragment, [ 0 ] ) );
  1355. } ).to.throw( CKEditorError, /^writer-split-element-no-parent/ );
  1356. } );
  1357. it( 'should throw when trying to use detached writer', () => {
  1358. const writer = new Writer( model, batch );
  1359. expect( () => {
  1360. writer.split( new Position( root, [ 0, 3 ] ) );
  1361. } ).to.throw( CKEditorError, /^writer-detached-writer-tries-to-modify-model/ );
  1362. } );
  1363. } );
  1364. describe( 'wrap()', () => {
  1365. let root, range;
  1366. beforeEach( () => {
  1367. root = doc.createRoot();
  1368. root.insertChildren( 0, new Text( 'foobar' ) );
  1369. range = new Range( new Position( root, [ 2 ] ), new Position( root, [ 4 ] ) );
  1370. } );
  1371. it( 'should wrap flat range with given element', () => {
  1372. const p = new Element( 'p' );
  1373. wrap( range, p );
  1374. expect( root.maxOffset ).to.equal( 5 );
  1375. expect( root.getChild( 0 ).data ).to.equal( 'fo' );
  1376. expect( root.getChild( 1 ) ).to.equal( p );
  1377. expect( p.getChild( 0 ).data ).to.equal( 'ob' );
  1378. expect( root.getChild( 2 ).data ).to.equal( 'ar' );
  1379. } );
  1380. it( 'should wrap flat range with an element of given name', () => {
  1381. wrap( range, 'p' );
  1382. expect( root.maxOffset ).to.equal( 5 );
  1383. expect( root.getChild( 0 ).data ).to.equal( 'fo' );
  1384. expect( root.getChild( 1 ).name ).to.equal( 'p' );
  1385. expect( root.getChild( 1 ).getChild( 0 ).data ).to.equal( 'ob' );
  1386. expect( root.getChild( 2 ).data ).to.equal( 'ar' );
  1387. } );
  1388. it( 'should throw if range to wrap is not flat', () => {
  1389. root.insertChildren( 1, [ new Element( 'p', [], new Text( 'xyz' ) ) ] );
  1390. const notFlatRange = new Range( new Position( root, [ 3 ] ), new Position( root, [ 6, 2 ] ) );
  1391. expect( () => {
  1392. wrap( notFlatRange, 'p' );
  1393. } ).to.throw( CKEditorError, /^writer-wrap-range-not-flat/ );
  1394. } );
  1395. it( 'should throw if element to wrap with has children #1', () => {
  1396. const p = new Element( 'p', [], new Text( 'a' ) );
  1397. expect( () => {
  1398. wrap( range, p );
  1399. } ).to.throw( CKEditorError, /^writer-wrap-element-not-empty/ );
  1400. } );
  1401. it( 'should throw if element to wrap with has children #2', () => {
  1402. const p = new Element( 'p' );
  1403. root.insertChildren( 0, p );
  1404. expect( () => {
  1405. wrap( range, p );
  1406. } ).to.throw( CKEditorError, /^writer-wrap-element-attached/ );
  1407. } );
  1408. it( 'should throw when trying to use detached writer', () => {
  1409. const writer = new Writer( model, batch );
  1410. expect( () => {
  1411. writer.wrap( range, 'p' );
  1412. } ).to.throw( CKEditorError, /^writer-detached-writer-tries-to-modify-model/ );
  1413. } );
  1414. } );
  1415. describe( 'unwrap()', () => {
  1416. let root, p;
  1417. beforeEach( () => {
  1418. root = doc.createRoot();
  1419. p = new Element( 'p', [], new Text( 'xyz' ) );
  1420. root.insertChildren( 0, [ new Text( 'a' ), p, new Text( 'b' ) ] );
  1421. } );
  1422. it( 'should unwrap given element', () => {
  1423. unwrap( p );
  1424. expect( root.maxOffset ).to.equal( 5 );
  1425. expect( root.getChild( 0 ).data ).to.equal( 'axyzb' );
  1426. } );
  1427. it( 'should throw if element to unwrap has no parent', () => {
  1428. const element = new Element( 'p' );
  1429. expect( () => {
  1430. unwrap( element );
  1431. } ).to.throw( CKEditorError, /^writer-unwrap-element-no-parent/ );
  1432. } );
  1433. it( 'should throw when trying to use detached writer', () => {
  1434. const writer = new Writer( model, batch );
  1435. expect( () => {
  1436. writer.unwrap( p );
  1437. } ).to.throw( CKEditorError, /^writer-detached-writer-tries-to-modify-model/ );
  1438. } );
  1439. } );
  1440. describe( 'setMarker()', () => {
  1441. let root, range;
  1442. beforeEach( () => {
  1443. root = doc.createRoot();
  1444. root.appendChildren( new Text( 'foo' ) );
  1445. range = Range.createIn( root );
  1446. } );
  1447. it( 'should add marker to the document marker collection', () => {
  1448. setMarker( 'name', range );
  1449. expect( model.markers.get( 'name' ).getRange().isEqual( range ) ).to.be.true;
  1450. } );
  1451. it( 'should update marker in the document marker collection', () => {
  1452. setMarker( 'name', range );
  1453. const range2 = Range.createFromParentsAndOffsets( root, 0, root, 0 );
  1454. setMarker( 'name', range2 );
  1455. expect( model.markers.get( 'name' ).getRange().isEqual( range2 ) ).to.be.true;
  1456. } );
  1457. it( 'should accept marker instance', () => {
  1458. const marker = model.markers.set( 'name', range );
  1459. const range2 = Range.createFromParentsAndOffsets( root, 0, root, 0 );
  1460. setMarker( marker, range2 );
  1461. const op = batch.deltas[ 0 ].operations[ 0 ];
  1462. expect( model.markers.get( 'name' ).getRange().isEqual( range2 ) ).to.be.true;
  1463. expect( op.oldRange.isEqual( range ) ).to.be.true;
  1464. expect( op.newRange.isEqual( range2 ) ).to.be.true;
  1465. } );
  1466. it( 'should accept empty range parameter if marker instance is passed', () => {
  1467. const marker = model.markers.set( 'name', range );
  1468. const spy = sinon.spy();
  1469. doc.on( 'change', spy );
  1470. setMarker( marker );
  1471. const op = batch.deltas[ 0 ].operations[ 0 ];
  1472. sinon.assert.calledOnce( spy );
  1473. sinon.assert.calledWith( spy, sinon.match.any, 'marker' );
  1474. expect( op.oldRange ).to.be.null;
  1475. expect( op.newRange.isEqual( range ) ).to.be.true;
  1476. } );
  1477. it( 'should throw if marker with given name does not exist and range is not passed', () => {
  1478. expect( () => {
  1479. setMarker( 'name' );
  1480. } ).to.throw( CKEditorError, /^writer-setMarker-no-range/ );
  1481. } );
  1482. it( 'should throw when trying to use detached writer', () => {
  1483. const marker = model.markers.set( 'name', range );
  1484. const writer = new Writer( model, batch );
  1485. expect( () => {
  1486. writer.setMarker( marker );
  1487. } ).to.throw( CKEditorError, /^writer-detached-writer-tries-to-modify-model/ );
  1488. } );
  1489. } );
  1490. describe( 'removeMarker()', () => {
  1491. let root, range;
  1492. beforeEach( () => {
  1493. root = doc.createRoot();
  1494. root.appendChildren( new Text( 'foo' ) );
  1495. range = Range.createIn( root );
  1496. } );
  1497. it( 'should remove marker from the document marker collection', () => {
  1498. setMarker( 'name', range );
  1499. removeMarker( 'name' );
  1500. expect( model.markers.get( 'name' ) ).to.be.null;
  1501. } );
  1502. it( 'should throw when trying to remove non existing marker', () => {
  1503. expect( () => {
  1504. removeMarker( 'name' );
  1505. } ).to.throw( CKEditorError, /^writer-removeMarker-no-marker/ );
  1506. } );
  1507. it( 'should throw when trying to use detached writer', () => {
  1508. const writer = new Writer( model, batch );
  1509. expect( () => {
  1510. writer.removeMarker( 'name' );
  1511. } ).to.throw( CKEditorError, /^writer-detached-writer-tries-to-modify-model/ );
  1512. } );
  1513. it( 'should accept marker instance', () => {
  1514. setMarker( 'name', range );
  1515. const marker = model.markers.get( 'name' );
  1516. removeMarker( marker );
  1517. expect( model.markers.get( 'name' ) ).to.be.null;
  1518. } );
  1519. } );
  1520. function createText( data, attributes ) {
  1521. return model.change( writer => {
  1522. return writer.createText( data, attributes );
  1523. } );
  1524. }
  1525. function createElement( name, attributes ) {
  1526. return model.change( writer => {
  1527. return writer.createElement( name, attributes );
  1528. } );
  1529. }
  1530. function createDocumentFragment() {
  1531. return model.change( writer => {
  1532. return writer.createDocumentFragment();
  1533. } );
  1534. }
  1535. function insert( item, itemOrPosition, offset ) {
  1536. model.enqueueChange( batch, writer => {
  1537. writer.insert( item, itemOrPosition, offset );
  1538. } );
  1539. }
  1540. function insertText( text, attributes, itemOrPosition, offset ) {
  1541. model.enqueueChange( batch, writer => {
  1542. writer.insertText( text, attributes, itemOrPosition, offset );
  1543. } );
  1544. }
  1545. function insertElement( name, attributes, itemOrPosition, offset ) {
  1546. model.enqueueChange( batch, writer => {
  1547. writer.insertElement( name, attributes, itemOrPosition, offset );
  1548. } );
  1549. }
  1550. function append( item, parent ) {
  1551. model.enqueueChange( batch, writer => {
  1552. writer.append( item, parent );
  1553. } );
  1554. }
  1555. function appendText( text, attributes, parent ) {
  1556. model.enqueueChange( batch, writer => {
  1557. writer.appendText( text, attributes, parent );
  1558. } );
  1559. }
  1560. function appendElement( name, attributes, parent ) {
  1561. model.enqueueChange( batch, writer => {
  1562. writer.appendElement( name, attributes, parent );
  1563. } );
  1564. }
  1565. function setAttribute( key, value, itemOrRange ) {
  1566. model.enqueueChange( batch, writer => {
  1567. writer.setAttribute( key, value, itemOrRange );
  1568. } );
  1569. }
  1570. function setAttributes( attributes, itemOrRange ) {
  1571. model.enqueueChange( batch, writer => {
  1572. writer.setAttributes( attributes, itemOrRange );
  1573. } );
  1574. }
  1575. function removeAttribute( key, itemOrRange ) {
  1576. model.enqueueChange( batch, writer => {
  1577. writer.removeAttribute( key, itemOrRange );
  1578. } );
  1579. }
  1580. function clearAttributes( itemOrRange ) {
  1581. model.enqueueChange( batch, writer => {
  1582. writer.clearAttributes( itemOrRange );
  1583. } );
  1584. }
  1585. function move( range, itemOrPosition, offset ) {
  1586. model.enqueueChange( batch, writer => {
  1587. writer.move( range, itemOrPosition, offset );
  1588. } );
  1589. }
  1590. function remove( itemOrRange ) {
  1591. model.enqueueChange( batch, writer => {
  1592. writer.remove( itemOrRange );
  1593. } );
  1594. }
  1595. function merge( position ) {
  1596. model.enqueueChange( batch, writer => {
  1597. writer.merge( position );
  1598. } );
  1599. }
  1600. function rename( element, newName ) {
  1601. model.enqueueChange( batch, writer => {
  1602. writer.rename( element, newName );
  1603. } );
  1604. }
  1605. function split( position ) {
  1606. model.enqueueChange( batch, writer => {
  1607. writer.split( position );
  1608. } );
  1609. }
  1610. function wrap( range, elementOrString ) {
  1611. model.enqueueChange( batch, writer => {
  1612. writer.wrap( range, elementOrString );
  1613. } );
  1614. }
  1615. function unwrap( element ) {
  1616. model.enqueueChange( batch, writer => {
  1617. writer.unwrap( element );
  1618. } );
  1619. }
  1620. function setMarker( markerOrName, newRange ) {
  1621. model.enqueueChange( batch, writer => {
  1622. writer.setMarker( markerOrName, newRange );
  1623. } );
  1624. }
  1625. function removeMarker( markerOrName ) {
  1626. model.enqueueChange( batch, writer => {
  1627. writer.removeMarker( markerOrName );
  1628. } );
  1629. }
  1630. } );