writer.js 70 KB

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