writer.js 90 KB

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