writer.js 62 KB

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