writer.js 89 KB

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