writer.js 63 KB

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