writer.js 81 KB

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