8
0

writer.js 86 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656
  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( 'setTextData()', () => {
  612. it( 'should update the content for text node', () => {
  613. const textNode = createText( 'foo' );
  614. setTextData( 'bar', textNode );
  615. expect( textNode.data ).to.equal( 'bar' );
  616. } );
  617. } );
  618. describe( 'setAttribute() / removeAttribute()', () => {
  619. let root, spy;
  620. beforeEach( () => {
  621. root = doc.createRoot();
  622. } );
  623. describe( 'change attribute on node', () => {
  624. let node, text;
  625. beforeEach( () => {
  626. node = createElement( 'p', { a: 1 } );
  627. text = createText( 'c', { a: 1 } );
  628. append( node, root );
  629. append( text, root );
  630. spy = sinon.spy( model, 'applyOperation' );
  631. } );
  632. describe( 'setAttribute', () => {
  633. it( 'should create the attribute on element', () => {
  634. setAttribute( 'b', 2, node );
  635. expect( spy.callCount ).to.equal( 1 );
  636. expect( node.getAttribute( 'b' ) ).to.equal( 2 );
  637. } );
  638. it( 'should change the attribute of element', () => {
  639. setAttribute( 'a', 2, node );
  640. expect( spy.callCount ).to.equal( 1 );
  641. expect( node.getAttribute( 'a' ) ).to.equal( 2 );
  642. } );
  643. it( 'should create the attribute on text node', () => {
  644. setAttribute( 'b', 2, text );
  645. expect( spy.callCount ).to.equal( 1 );
  646. expect( root.getChild( 1 ).getAttribute( 'b' ) ).to.equal( 2 );
  647. } );
  648. it( 'should change the attribute of text node', () => {
  649. setAttribute( 'a', 2, text );
  650. expect( spy.callCount ).to.equal( 1 );
  651. expect( root.getChild( 1 ).getAttribute( 'a' ) ).to.equal( 2 );
  652. } );
  653. it( 'should do nothing if the attribute value is the same', () => {
  654. setAttribute( 'a', 1, node );
  655. expect( spy.callCount ).to.equal( 0 );
  656. expect( node.getAttribute( 'a' ) ).to.equal( 1 );
  657. } );
  658. it( 'should throw when trying to use detached writer', () => {
  659. const writer = new Writer( model, batch );
  660. expect( () => {
  661. writer.setAttribute( 'a', 1, node );
  662. } ).to.throw( CKEditorError, /^writer-incorrect-use/ );
  663. } );
  664. } );
  665. describe( 'removeAttribute', () => {
  666. it( 'should remove the attribute from element', () => {
  667. removeAttribute( 'a', node );
  668. expect( spy.callCount ).to.equal( 1 );
  669. expect( node.getAttribute( 'a' ) ).to.be.undefined;
  670. } );
  671. it( 'should remove the attribute from character', () => {
  672. removeAttribute( 'a', text );
  673. expect( spy.callCount ).to.equal( 1 );
  674. expect( root.getChild( 1 ).getAttribute( 'a' ) ).to.be.undefined;
  675. } );
  676. it( 'should do nothing if the attribute is not set', () => {
  677. removeAttribute( 'b', node );
  678. expect( spy.callCount ).to.equal( 0 );
  679. } );
  680. it( 'should throw when trying to use detached writer', () => {
  681. const writer = new Writer( model, batch );
  682. expect( () => {
  683. writer.removeAttribute( 'b', node );
  684. } ).to.throw( CKEditorError, /^writer-incorrect-use/ );
  685. } );
  686. } );
  687. } );
  688. describe( 'change attribute on range', () => {
  689. beforeEach( () => {
  690. const element = createElement( 'e', { a: 2 } );
  691. appendText( 'xxx', { a: 1 }, root );
  692. appendText( 'xxx', root );
  693. appendText( 'xxx', { a: 1 }, root );
  694. appendText( 'xxx', { a: 2 }, root );
  695. appendText( 'xxx', root );
  696. appendText( 'xxx', { a: 1 }, root );
  697. appendText( 'xxx', element );
  698. append( element, root );
  699. appendText( 'xxx', root );
  700. spy = sinon.spy( model, 'applyOperation' );
  701. } );
  702. function getRange( startIndex, endIndex ) {
  703. return new Range(
  704. Position.createFromParentAndOffset( root, startIndex ),
  705. Position.createFromParentAndOffset( root, endIndex )
  706. );
  707. }
  708. function getChangesAttrsCount() {
  709. let totalNumber = 0;
  710. for ( const delta of batch.deltas ) {
  711. for ( const operation of delta.operations ) {
  712. if ( operation.range ) {
  713. totalNumber += count( operation.range.getItems( { singleCharacters: true } ) );
  714. }
  715. }
  716. }
  717. return totalNumber;
  718. }
  719. function getCompressedAttrs() {
  720. // default: 111---111222---1112------
  721. const range = Range.createIn( root );
  722. return Array.from( range.getItems( { singleCharacters: true } ) )
  723. .map( item => item.getAttribute( 'a' ) || '-' )
  724. .join( '' );
  725. }
  726. describe( 'setAttribute', () => {
  727. it( 'should set the attribute on the range', () => {
  728. setAttribute( 'a', 3, getRange( 3, 6 ) );
  729. expect( spy.callCount ).to.equal( 1 );
  730. expect( getChangesAttrsCount() ).to.equal( 3 );
  731. expect( getCompressedAttrs() ).to.equal( '111333111222---1112------' );
  732. } );
  733. it( 'should split the operations if parts of the range have different attributes', () => {
  734. setAttribute( 'a', 3, getRange( 4, 14 ) );
  735. expect( spy.callCount ).to.equal( 4 );
  736. expect( getChangesAttrsCount() ).to.equal( 10 );
  737. expect( getCompressedAttrs() ).to.equal( '111-3333333333-1112------' );
  738. } );
  739. it( 'should split the operations if parts of the part of the range have the attribute', () => {
  740. setAttribute( 'a', 2, getRange( 4, 14 ) );
  741. expect( spy.callCount ).to.equal( 3 );
  742. expect( getChangesAttrsCount() ).to.equal( 7 );
  743. expect( getCompressedAttrs() ).to.equal( '111-2222222222-1112------' );
  744. } );
  745. it( 'should strip the range if the beginning have the attribute', () => {
  746. setAttribute( 'a', 1, getRange( 1, 5 ) );
  747. expect( spy.callCount ).to.equal( 1 );
  748. expect( getChangesAttrsCount() ).to.equal( 2 );
  749. expect( getCompressedAttrs() ).to.equal( '11111-111222---1112------' );
  750. } );
  751. it( 'should strip the range if the ending have the attribute', () => {
  752. setAttribute( 'a', 1, getRange( 13, 17 ) );
  753. expect( spy.callCount ).to.equal( 1 );
  754. expect( getChangesAttrsCount() ).to.equal( 2 );
  755. expect( getCompressedAttrs() ).to.equal( '111---111222-111112------' );
  756. } );
  757. it( 'should do nothing if the range has attribute', () => {
  758. setAttribute( 'a', 1, getRange( 0, 3 ) );
  759. expect( spy.callCount ).to.equal( 0 );
  760. expect( getCompressedAttrs() ).to.equal( '111---111222---1112------' );
  761. } );
  762. it( 'should not check range\'s start position node when creating operations', () => {
  763. const range = new Range(
  764. new Position( root, [ 18, 1 ] ),
  765. new Position( root, [ 19 ] )
  766. );
  767. setAttribute( 'a', 1, range );
  768. expect( spy.callCount ).to.equal( 1 );
  769. expect( getChangesAttrsCount() ).to.equal( 2 );
  770. expect( getCompressedAttrs() ).to.equal( '111---111222---1112-11---' );
  771. } );
  772. it( 'should not change elements attribute if range contains closing tag', () => {
  773. const range = new Range(
  774. new Position( root, [ 18, 1 ] ),
  775. new Position( root, [ 21 ] )
  776. );
  777. setAttribute( 'a', 1, range );
  778. expect( spy.callCount ).to.equal( 1 );
  779. expect( getChangesAttrsCount() ).to.equal( 4 );
  780. expect( getCompressedAttrs() ).to.equal( '111---111222---1112-1111-' );
  781. } );
  782. it( 'should not create an operation if the range contains only closing tag', () => {
  783. const range = new Range(
  784. new Position( root, [ 18, 3 ] ),
  785. new Position( root, [ 19 ] )
  786. );
  787. setAttribute( 'a', 3, range );
  788. expect( spy.callCount ).to.equal( 0 );
  789. expect( getCompressedAttrs() ).to.equal( '111---111222---1112------' );
  790. } );
  791. it( 'should not create an operation if is collapsed', () => {
  792. setAttribute( 'a', 1, getRange( 3, 3 ) );
  793. expect( spy.callCount ).to.equal( 0 );
  794. expect( getCompressedAttrs() ).to.equal( '111---111222---1112------' );
  795. } );
  796. it( 'should create a proper operations for the mixed range', () => {
  797. setAttribute( 'a', 1, getRange( 0, 20 ) );
  798. expect( spy.callCount ).to.equal( 5 );
  799. expect( getChangesAttrsCount() ).to.equal( 14 );
  800. expect( getCompressedAttrs() ).to.equal( '11111111111111111111111--' );
  801. } );
  802. it( 'should throw when trying to use detached writer', () => {
  803. const writer = new Writer( model, batch );
  804. expect( () => {
  805. writer.setAttribute( 'a', 1, getRange( 0, 20 ) );
  806. } ).to.throw( CKEditorError, /^writer-incorrect-use/ );
  807. } );
  808. } );
  809. describe( 'removeAttribute', () => {
  810. it( 'should remove the attribute on the range', () => {
  811. removeAttribute( 'a', getRange( 0, 2 ) );
  812. expect( spy.callCount ).to.equal( 1 );
  813. expect( getChangesAttrsCount() ).to.equal( 2 );
  814. expect( getCompressedAttrs() ).to.equal( '--1---111222---1112------' );
  815. } );
  816. it( 'should split the operations if parts of the range have different attributes', () => {
  817. removeAttribute( 'a', getRange( 7, 11 ) );
  818. expect( spy.callCount ).to.equal( 2 );
  819. expect( getChangesAttrsCount() ).to.equal( 4 );
  820. expect( getCompressedAttrs() ).to.equal( '111---1----2---1112------' );
  821. } );
  822. it( 'should split the operations if parts of the part of the range have no attribute', () => {
  823. removeAttribute( 'a', getRange( 1, 7 ) );
  824. expect( spy.callCount ).to.equal( 2 );
  825. expect( getChangesAttrsCount() ).to.equal( 3 );
  826. expect( getCompressedAttrs() ).to.equal( '1------11222---1112------' );
  827. } );
  828. it( 'should strip the range if the beginning have no attribute', () => {
  829. removeAttribute( 'a', getRange( 4, 12 ) );
  830. expect( spy.callCount ).to.equal( 2 );
  831. expect( getChangesAttrsCount() ).to.equal( 6 );
  832. expect( getCompressedAttrs() ).to.equal( '111------------1112------' );
  833. } );
  834. it( 'should strip the range if the ending have no attribute', () => {
  835. removeAttribute( 'a', getRange( 7, 15 ) );
  836. expect( spy.callCount ).to.equal( 2 );
  837. expect( getChangesAttrsCount() ).to.equal( 5 );
  838. expect( getCompressedAttrs() ).to.equal( '111---1--------1112------' );
  839. } );
  840. it( 'should do nothing if the range has no attribute', () => {
  841. removeAttribute( 'a', getRange( 4, 5 ) );
  842. expect( spy.callCount ).to.equal( 0 );
  843. expect( getCompressedAttrs() ).to.equal( '111---111222---1112------' );
  844. } );
  845. it( 'should not check range\'s start position node when creating operations', () => {
  846. const range = new Range(
  847. new Position( root, [ 18, 3 ] ),
  848. new Position( root, [ 19 ] )
  849. );
  850. removeAttribute( 'a', range );
  851. expect( spy.callCount ).to.equal( 0 );
  852. expect( getChangesAttrsCount() ).to.equal( 0 );
  853. expect( getCompressedAttrs() ).to.equal( '111---111222---1112------' );
  854. } );
  855. it( 'should not apply operation twice in the range contains opening and closing tags', () => {
  856. removeAttribute( 'a', getRange( 18, 22 ) );
  857. expect( spy.callCount ).to.equal( 1 );
  858. expect( getChangesAttrsCount() ).to.equal( 1 );
  859. expect( getCompressedAttrs() ).to.equal( '111---111222---111-------' );
  860. } );
  861. it( 'should not create an operation if range is collapsed', () => {
  862. removeAttribute( 'a', getRange( 3, 3 ) );
  863. expect( spy.callCount ).to.equal( 0 );
  864. expect( getCompressedAttrs() ).to.equal( '111---111222---1112------' );
  865. } );
  866. it( 'should create a proper operations for the mixed range', () => {
  867. removeAttribute( 'a', getRange( 3, 15 ) );
  868. expect( spy.callCount ).to.equal( 2 );
  869. expect( getChangesAttrsCount() ).to.equal( 6 );
  870. expect( getCompressedAttrs() ).to.equal( '111------------1112------' );
  871. } );
  872. it( 'should throw when trying to use detached writer', () => {
  873. const writer = new Writer( model, batch );
  874. expect( () => {
  875. writer.removeAttribute( 'a', getRange( 3, 15 ) );
  876. } ).to.throw( CKEditorError, /^writer-incorrect-use/ );
  877. } );
  878. } );
  879. } );
  880. describe( 'change attribute on root element', () => {
  881. let p;
  882. beforeEach( () => {
  883. p = createElement( 'p', { a: 3 } );
  884. spy = sinon.spy( model, 'applyOperation' );
  885. } );
  886. describe( 'setAttribute', () => {
  887. it( 'should create the attribute on root', () => {
  888. setAttribute( 'b', 2, root );
  889. expect( spy.callCount ).to.equal( 1 );
  890. expect( root.getAttribute( 'b' ) ).to.equal( 2 );
  891. } );
  892. it( 'should create the attribute on detached root', () => {
  893. setAttribute( 'b', 2, p );
  894. expect( spy.callCount ).to.equal( 1 );
  895. expect( p.getAttribute( 'b' ) ).to.equal( 2 );
  896. } );
  897. it( 'should change the attribute of root', () => {
  898. setAttribute( 'a', 2, root );
  899. expect( spy.callCount ).to.equal( 1 );
  900. expect( root.getAttribute( 'a' ) ).to.equal( 2 );
  901. } );
  902. it( 'should change the attribute of detached root', () => {
  903. setAttribute( 'a', 2, p );
  904. expect( spy.callCount ).to.equal( 1 );
  905. expect( p.getAttribute( 'a' ) ).to.equal( 2 );
  906. } );
  907. it( 'should do nothing if the attribute value is the same', () => {
  908. setAttribute( 'a', 1, root );
  909. expect( spy.callCount ).to.equal( 1 );
  910. setAttribute( 'a', 1, root );
  911. expect( spy.callCount ).to.equal( 1 );
  912. expect( root.getAttribute( 'a' ) ).to.equal( 1 );
  913. } );
  914. it( 'should do nothing if the attribute value is the same on detached root', () => {
  915. setAttribute( 'a', 1, p );
  916. expect( spy.callCount ).to.equal( 1 );
  917. setAttribute( 'a', 1, p );
  918. expect( spy.callCount ).to.equal( 1 );
  919. expect( p.getAttribute( 'a' ) ).to.equal( 1 );
  920. } );
  921. it( 'should throw when trying to use detached writer', () => {
  922. const writer = new Writer( model, batch );
  923. expect( () => {
  924. writer.setAttribute( 'a', 1, p );
  925. } ).to.throw( CKEditorError, /^writer-incorrect-use/ );
  926. } );
  927. } );
  928. describe( 'removeAttribute', () => {
  929. it( 'should remove the attribute from root', () => {
  930. setAttribute( 'a', 1, root );
  931. removeAttribute( 'a', root );
  932. expect( spy.callCount ).to.equal( 2 );
  933. expect( root.getAttribute( 'a' ) ).to.be.undefined;
  934. } );
  935. it( 'should do nothing if the attribute is not set', () => {
  936. removeAttribute( 'b', root );
  937. expect( spy.callCount ).to.equal( 0 );
  938. } );
  939. it( 'should throw when trying to use detached writer', () => {
  940. const writer = new Writer( model, batch );
  941. expect( () => {
  942. writer.removeAttribute( 'b', root );
  943. } ).to.throw( CKEditorError, /^writer-incorrect-use/ );
  944. } );
  945. } );
  946. describe( 'clearAttributes', () => {
  947. it( 'should clear attributes from range', () => {
  948. appendText( 'xxx', { a: 1, b: 2, c: 3 }, root );
  949. appendText( 'xxx', root );
  950. appendText( 'xxx', { a: 1 }, root );
  951. appendText( 'xxx', { b: 2 }, root );
  952. appendText( 'xxx', root );
  953. appendElement( 'e', { a: 1 }, root );
  954. appendText( 'xxx', root );
  955. const range = Range.createIn( root );
  956. clearAttributes( range );
  957. let itemsCount = 0;
  958. for ( const item of range.getItems() ) {
  959. itemsCount++;
  960. expect( Array.from( item.getAttributeKeys() ).length ).to.equal( 0 );
  961. }
  962. expect( itemsCount ).to.equal( 3 );
  963. } );
  964. it( 'should clear attributes on element', () => {
  965. const element = createElement( 'x', { a: 1, b: 2, c: 3 }, root );
  966. expect( Array.from( element.getAttributeKeys() ).length ).to.equal( 3 );
  967. clearAttributes( element );
  968. expect( Array.from( element.getAttributeKeys() ).length ).to.equal( 0 );
  969. } );
  970. it( 'should clear attributes on root element', () => {
  971. setAttributes( { a: 1, b: 2, c: 3 }, root );
  972. expect( Array.from( root.getAttributeKeys() ).length ).to.equal( 3 );
  973. clearAttributes( root );
  974. expect( Array.from( root.getAttributeKeys() ).length ).to.equal( 0 );
  975. } );
  976. it( 'should do nothing if there are no attributes', () => {
  977. const element = createElement( 'x' );
  978. expect( Array.from( element.getAttributeKeys() ).length ).to.equal( 0 );
  979. clearAttributes( element );
  980. expect( Array.from( element.getAttributeKeys() ).length ).to.equal( 0 );
  981. } );
  982. it( 'should throw when trying to use detached writer', () => {
  983. const writer = new Writer( model, batch );
  984. const element = createElement( 'x' );
  985. expect( () => {
  986. writer.clearAttributes( element );
  987. } ).to.throw( CKEditorError, /^writer-incorrect-use/ );
  988. } );
  989. } );
  990. } );
  991. it( 'should not add empty delta to the batch', () => {
  992. const nodeA = new Element( 'p', { a: 1 } );
  993. const nodeB = new Element( 'p', { b: 2 } );
  994. root._insertChildren( 0, [ nodeA, nodeB ] );
  995. setAttribute( 'a', 1, nodeA );
  996. expect( batch.deltas.length ).to.equal( 0 );
  997. removeAttribute( 'x', Range.createIn( root ) );
  998. expect( batch.deltas.length ).to.equal( 0 );
  999. } );
  1000. } );
  1001. describe( 'setAttributes()', () => {
  1002. let frag, item;
  1003. beforeEach( () => {
  1004. frag = createDocumentFragment();
  1005. item = createText( 'xxx', { b: 2, c: 3 } );
  1006. appendText( 'xxx', { a: 1 }, frag );
  1007. append( item, frag );
  1008. } );
  1009. it( 'should set attributes one by one on range', () => {
  1010. const range = Range.createIn( frag );
  1011. let spy;
  1012. model.change( writer => {
  1013. // `setAttribute` is a not trivial operation and is deeply tested above, there is no point to duplicate
  1014. // such a big amount of the same tests, so let's use a spy here.
  1015. spy = sinon.spy( writer, 'setAttribute' );
  1016. writer.setAttributes( { a: 3, c: null }, range );
  1017. } );
  1018. // Verify result.
  1019. expect( Array.from( frag.getChild( 0 ).getAttributes() ) ).to.deep.equal( [ [ 'a', 3 ] ] );
  1020. expect( Array.from( frag.getChild( 1 ).getAttributes() ) ).to.deep.equal( [ [ 'b', 2 ], [ 'a', 3 ] ] );
  1021. // Verify operations
  1022. sinon.assert.calledTwice( spy );
  1023. sinon.assert.calledWith( spy.firstCall, 'a', 3, range );
  1024. sinon.assert.calledWith( spy.secondCall, 'c', null, range );
  1025. } );
  1026. it( 'should set attributes one by one on range for map as attributes list', () => {
  1027. const range = Range.createIn( frag );
  1028. let spy;
  1029. model.change( writer => {
  1030. // `setAttribute` is a not trivial operation and is deeply tested above, there is no point to duplicate
  1031. // such a big amount of the same tests, so let's use a spy here.
  1032. spy = sinon.spy( writer, 'setAttribute' );
  1033. writer.setAttributes( new Map( [ [ 'a', 3 ], [ 'c', null ] ] ), range );
  1034. } );
  1035. // Verify result.
  1036. expect( Array.from( frag.getChild( 0 ).getAttributes() ) ).to.deep.equal( [ [ 'a', 3 ] ] );
  1037. expect( Array.from( frag.getChild( 1 ).getAttributes() ) ).to.deep.equal( [ [ 'b', 2 ], [ 'a', 3 ] ] );
  1038. // Verify operations
  1039. sinon.assert.calledTwice( spy );
  1040. sinon.assert.calledWith( spy.firstCall, 'a', 3, range );
  1041. sinon.assert.calledWith( spy.secondCall, 'c', null, range );
  1042. } );
  1043. it( 'should set attributes one by one on item', () => {
  1044. let spy;
  1045. model.change( writer => {
  1046. // `setAttribute` is a not trivial operation and is deeply tested above, there is no point to duplicate
  1047. // such a big amount of the same tests, so let's use a spy here.
  1048. spy = sinon.spy( writer, 'setAttribute' );
  1049. writer.setAttributes( { a: 3, c: null }, item );
  1050. } );
  1051. // Verify result.
  1052. expect( Array.from( item.getAttributes() ) ).to.deep.equal( [ [ 'b', 2 ], [ 'a', 3 ] ] );
  1053. // Verify operations
  1054. sinon.assert.calledTwice( spy );
  1055. sinon.assert.calledWith( spy.firstCall, 'a', 3, item );
  1056. sinon.assert.calledWith( spy.secondCall, 'c', null, item );
  1057. } );
  1058. it( 'should set attributes one by one on item for maps as attributes list', () => {
  1059. let spy;
  1060. model.change( writer => {
  1061. // `setAttribute` is a not trivial operation and is deeply tested above, there is no point to duplicate
  1062. // such a big amount of the same tests, so let's use a spy here.
  1063. spy = sinon.spy( writer, 'setAttribute' );
  1064. writer.setAttributes( new Map( [ [ 'a', 3 ], [ 'c', null ] ] ), item );
  1065. } );
  1066. // Verify result.
  1067. expect( Array.from( item.getAttributes() ) ).to.deep.equal( [ [ 'b', 2 ], [ 'a', 3 ] ] );
  1068. // Verify operations
  1069. sinon.assert.calledTwice( spy );
  1070. sinon.assert.calledWith( spy.firstCall, 'a', 3, item );
  1071. sinon.assert.calledWith( spy.secondCall, 'c', null, item );
  1072. } );
  1073. it( 'should throw when trying to use detached writer', () => {
  1074. const writer = new Writer( model, batch );
  1075. expect( () => {
  1076. writer.setAttributes( new Map( [ [ 'a', 3 ], [ 'c', null ] ] ), item );
  1077. } ).to.throw( CKEditorError, /^writer-incorrect-use/ );
  1078. } );
  1079. } );
  1080. describe( 'merge()', () => {
  1081. let root, p1, p2;
  1082. beforeEach( () => {
  1083. root = doc.createRoot();
  1084. p1 = new Element( 'p', { key1: 'value1' }, new Text( 'foo' ) );
  1085. p2 = new Element( 'p', { key2: 'value2' }, new Text( 'bar' ) );
  1086. root._insertChildren( 0, [ p1, p2 ] );
  1087. } );
  1088. it( 'should merge foo and bar into foobar', () => {
  1089. merge( new Position( root, [ 1 ] ) );
  1090. expect( root.maxOffset ).to.equal( 1 );
  1091. expect( root.getChild( 0 ).name ).to.equal( 'p' );
  1092. expect( root.getChild( 0 ).maxOffset ).to.equal( 6 );
  1093. expect( count( root.getChild( 0 ).getAttributes() ) ).to.equal( 1 );
  1094. expect( root.getChild( 0 ).getAttribute( 'key1' ) ).to.equal( 'value1' );
  1095. expect( root.getChild( 0 ).getChild( 0 ).data ).to.equal( 'foobar' );
  1096. } );
  1097. it( 'should correctly merge in document fragment', () => {
  1098. const docFrag = new DocumentFragment( [
  1099. new Element( 'p', null, 'foo' ),
  1100. new Element( 'p', null, 'bar' )
  1101. ] );
  1102. merge( new Position( docFrag, [ 1 ] ) );
  1103. expect( docFrag.getChild( 0 ).name ).to.equal( 'p' );
  1104. expect( docFrag.getChild( 0 ).getChild( 0 ).data ).to.equal( 'foobar' );
  1105. } );
  1106. it( 'should throw if there is no element after', () => {
  1107. expect( () => {
  1108. merge( new Position( root, [ 2 ] ) );
  1109. } ).to.throw( CKEditorError, /^writer-merge-no-element-after/ );
  1110. } );
  1111. it( 'should throw if there is no element before', () => {
  1112. expect( () => {
  1113. merge( new Position( root, [ 0, 2 ] ) );
  1114. } ).to.throw( CKEditorError, /^writer-merge-no-element-before/ );
  1115. } );
  1116. it( 'should throw when trying to use detached writer', () => {
  1117. const writer = new Writer( model, batch );
  1118. expect( () => {
  1119. writer.merge( new Position( root, [ 1 ] ) );
  1120. } ).to.throw( CKEditorError, /^writer-incorrect-use/ );
  1121. } );
  1122. } );
  1123. describe( 'move()', () => {
  1124. let root, range, div, p;
  1125. beforeEach( () => {
  1126. root = doc.createRoot();
  1127. div = new Element( 'div', [], new Text( 'foobar' ) );
  1128. p = new Element( 'p', [], new Text( 'abcxyz' ) );
  1129. div._insertChildren( 0, [ new Element( 'p', [], new Text( 'gggg' ) ) ] );
  1130. div._insertChildren( 2, [ new Element( 'p', [], new Text( 'hhhh' ) ) ] );
  1131. root._insertChildren( 0, [ div, p ] );
  1132. range = new Range( new Position( root, [ 0, 3 ] ), new Position( root, [ 0, 7 ] ) );
  1133. } );
  1134. it( 'should move flat range of nodes', () => {
  1135. move( range, new Position( root, [ 1, 3 ] ) );
  1136. expect( getNodesAndText( Range.createIn( root.getChild( 0 ) ) ) ).to.equal( 'PggggPfoPhhhhP' );
  1137. expect( getNodesAndText( Range.createIn( root.getChild( 1 ) ) ) ).to.equal( 'abcobarxyz' );
  1138. } );
  1139. it( 'should throw if object to move is not a range', () => {
  1140. expect( () => {
  1141. move( root.getChild( 0 ), new Position( root, [ 1, 3 ] ) );
  1142. } ).to.throw( CKEditorError, /^writer-move-invalid-range/ );
  1143. } );
  1144. it( 'should throw if given range is not flat', () => {
  1145. const notFlatRange = new Range( new Position( root, [ 0, 2, 2 ] ), new Position( root, [ 0, 6 ] ) );
  1146. expect( () => {
  1147. move( notFlatRange, new Position( root, [ 1, 3 ] ) );
  1148. } ).to.throw( CKEditorError, /^writer-move-range-not-flat/ );
  1149. } );
  1150. it( 'should throw if range is going to be moved to the other document', () => {
  1151. const docFrag = createDocumentFragment();
  1152. expect( () => {
  1153. move( range, docFrag );
  1154. } ).to.throw( CKEditorError, /^writer-move-different-document/ );
  1155. } );
  1156. it( 'should throw when trying to use detached writer', () => {
  1157. const writer = new Writer( model, batch );
  1158. expect( () => {
  1159. writer.move( range, new Position( root, [ 1, 3 ] ) );
  1160. } ).to.throw( CKEditorError, /^writer-incorrect-use/ );
  1161. } );
  1162. } );
  1163. describe( 'remove()', () => {
  1164. let div, p, range;
  1165. beforeEach( () => {
  1166. div = createElement( 'div' );
  1167. appendText( 'foobar', div );
  1168. p = createElement( 'p' );
  1169. appendText( 'abcxyz', p );
  1170. insertElement( 'p', div );
  1171. appendElement( 'p', div );
  1172. insertText( 'gggg', new Position( div, [ 0, 0 ] ) );
  1173. insertText( 'hhhh', new Position( div, [ 7, 0 ] ) );
  1174. } );
  1175. describe( 'remove from document', () => {
  1176. let root;
  1177. beforeEach( () => {
  1178. root = doc.createRoot();
  1179. append( div, root );
  1180. append( p, root );
  1181. // Range starts in ROOT > DIV > P > gg|gg.
  1182. // Range ends in ROOT > DIV > ...|ar.
  1183. range = new Range( new Position( root, [ 0, 0, 2 ] ), new Position( root, [ 0, 5 ] ) );
  1184. } );
  1185. it( 'should remove specified node', () => {
  1186. remove( div );
  1187. expect( root.maxOffset ).to.equal( 1 );
  1188. expect( root.childCount ).to.equal( 1 );
  1189. expect( getNodesAndText( Range.createIn( root.getChild( 0 ) ) ) ).to.equal( 'abcxyz' );
  1190. } );
  1191. it( 'should remove specified text node', () => {
  1192. remove( p.getChild( 0 ) );
  1193. expect( getNodesAndText( Range.createOn( p ) ) ).to.equal( 'PP' );
  1194. } );
  1195. it( 'should remove any range of nodes', () => {
  1196. remove( range );
  1197. expect( getNodesAndText( Range.createIn( root.getChild( 0 ) ) ) ).to.equal( 'PggParPhhhhP' );
  1198. expect( getNodesAndText( Range.createIn( root.getChild( 1 ) ) ) ).to.equal( 'abcxyz' );
  1199. } );
  1200. it( 'should create minimal number of remove deltas, each with only one operation', () => {
  1201. batch = new Batch();
  1202. remove( range );
  1203. expect( batch.deltas.length ).to.equal( 2 );
  1204. expect( batch.deltas[ 0 ].operations.length ).to.equal( 1 );
  1205. expect( batch.deltas[ 1 ].operations.length ).to.equal( 1 );
  1206. } );
  1207. it( 'should use RemoveOperation', () => {
  1208. batch = new Batch();
  1209. remove( div );
  1210. expect( batch.deltas[ 0 ].operations[ 0 ].type ).to.equal( 'remove' );
  1211. } );
  1212. it( 'should throw when trying to use detached writer', () => {
  1213. const writer = new Writer( model, batch );
  1214. expect( () => {
  1215. writer.remove( range );
  1216. } ).to.throw( CKEditorError, /^writer-incorrect-use/ );
  1217. } );
  1218. } );
  1219. describe( 'remove from document fragment', () => {
  1220. let frag;
  1221. beforeEach( () => {
  1222. frag = createDocumentFragment();
  1223. append( div, frag );
  1224. append( p, frag );
  1225. // Range starts in FRAG > DIV > P > gg|gg.
  1226. // Range ends in FRAG > DIV > ...|ar.
  1227. range = new Range( new Position( frag, [ 0, 0, 2 ] ), new Position( frag, [ 0, 5 ] ) );
  1228. } );
  1229. it( 'should remove specified node', () => {
  1230. remove( div );
  1231. expect( frag.maxOffset ).to.equal( 1 );
  1232. expect( frag.childCount ).to.equal( 1 );
  1233. expect( getNodesAndText( Range.createIn( frag.getChild( 0 ) ) ) ).to.equal( 'abcxyz' );
  1234. } );
  1235. it( 'should remove specified text node', () => {
  1236. remove( p.getChild( 0 ) );
  1237. expect( getNodesAndText( Range.createOn( p ) ) ).to.equal( 'PP' );
  1238. } );
  1239. it( 'should remove any range of nodes', () => {
  1240. remove( range );
  1241. expect( getNodesAndText( Range.createIn( frag.getChild( 0 ) ) ) ).to.equal( 'PggParPhhhhP' );
  1242. expect( getNodesAndText( Range.createIn( frag.getChild( 1 ) ) ) ).to.equal( 'abcxyz' );
  1243. } );
  1244. it( 'should create minimal number of remove deltas, each with only one operation', () => {
  1245. batch = new Batch();
  1246. remove( range );
  1247. expect( batch.deltas.length ).to.equal( 2 );
  1248. expect( batch.deltas[ 0 ].operations.length ).to.equal( 1 );
  1249. expect( batch.deltas[ 1 ].operations.length ).to.equal( 1 );
  1250. } );
  1251. it( 'should use DetachOperation', () => {
  1252. batch = new Batch();
  1253. remove( div );
  1254. expect( batch.deltas[ 0 ].operations[ 0 ].type ).to.equal( 'detach' );
  1255. } );
  1256. it( 'should throw when trying to use detached writer', () => {
  1257. const writer = new Writer( model, batch );
  1258. expect( () => {
  1259. writer.remove( range );
  1260. } ).to.throw( CKEditorError, /^writer-incorrect-use/ );
  1261. } );
  1262. } );
  1263. } );
  1264. describe( 'rename()', () => {
  1265. it( 'should rename given element', () => {
  1266. const root = doc.createRoot();
  1267. const p = new Element( 'p', null, new Text( 'abc' ) );
  1268. root._appendChildren( p );
  1269. rename( p, 'h' );
  1270. expect( root.maxOffset ).to.equal( 1 );
  1271. expect( root.getChild( 0 ) ).to.have.property( 'name', 'h' );
  1272. } );
  1273. it( 'should rename in document fragment', () => {
  1274. const docFrag = new DocumentFragment();
  1275. const p = new Element( 'p' );
  1276. docFrag._appendChildren( p );
  1277. rename( p, 'h' );
  1278. expect( docFrag.maxOffset ).to.equal( 1 );
  1279. expect( docFrag.getChild( 0 ) ).to.have.property( 'name', 'h' );
  1280. } );
  1281. it( 'should throw if not an Element instance is passed', () => {
  1282. expect( () => {
  1283. rename( new Text( 'abc' ), 'h' );
  1284. } ).to.throw( CKEditorError, /^writer-rename-not-element-instance/ );
  1285. } );
  1286. it( 'should throw when trying to use detached writer', () => {
  1287. const writer = new Writer( model, batch );
  1288. const p = new Element( 'p', null, new Text( 'abc' ) );
  1289. expect( () => {
  1290. writer.rename( p, 'h' );
  1291. } ).to.throw( CKEditorError, /^writer-incorrect-use/ );
  1292. } );
  1293. } );
  1294. describe( 'split()', () => {
  1295. let root, p;
  1296. beforeEach( () => {
  1297. root = doc.createRoot();
  1298. p = new Element( 'p', { key: 'value' }, new Text( 'foobar' ) );
  1299. root._insertChildren( 0, p );
  1300. } );
  1301. it( 'should split foobar to foo and bar', () => {
  1302. split( new Position( root, [ 0, 3 ] ) );
  1303. expect( root.maxOffset ).to.equal( 2 );
  1304. expect( root.getChild( 0 ).name ).to.equal( 'p' );
  1305. expect( root.getChild( 0 ).maxOffset ).to.equal( 3 );
  1306. expect( count( root.getChild( 0 ).getAttributes() ) ).to.equal( 1 );
  1307. expect( root.getChild( 0 ).getAttribute( 'key' ) ).to.equal( 'value' );
  1308. expect( root.getChild( 0 ).getChild( 0 ).data ).to.equal( 'foo' );
  1309. expect( root.getChild( 1 ).name ).to.equal( 'p' );
  1310. expect( root.getChild( 1 ).maxOffset ).to.equal( 3 );
  1311. expect( count( root.getChild( 1 ).getAttributes() ) ).to.equal( 1 );
  1312. expect( root.getChild( 1 ).getAttribute( 'key' ) ).to.equal( 'value' );
  1313. expect( root.getChild( 1 ).getChild( 0 ).data ).to.equal( 'bar' );
  1314. } );
  1315. it( 'should split inside document fragment', () => {
  1316. const docFrag = new DocumentFragment();
  1317. docFrag._appendChildren( new Element( 'p', null, new Text( 'foobar' ) ) );
  1318. split( new Position( docFrag, [ 0, 3 ] ) );
  1319. expect( docFrag.maxOffset ).to.equal( 2 );
  1320. expect( docFrag.getChild( 0 ).name ).to.equal( 'p' );
  1321. expect( docFrag.getChild( 0 ).maxOffset ).to.equal( 3 );
  1322. expect( docFrag.getChild( 0 ).getChild( 0 ).data ).to.equal( 'foo' );
  1323. expect( docFrag.getChild( 1 ).name ).to.equal( 'p' );
  1324. expect( docFrag.getChild( 1 ).maxOffset ).to.equal( 3 );
  1325. expect( docFrag.getChild( 1 ).getChild( 0 ).data ).to.equal( 'bar' );
  1326. } );
  1327. it( 'should create an empty paragraph if we split at the end', () => {
  1328. split( new Position( root, [ 0, 6 ] ) );
  1329. expect( root.maxOffset ).to.equal( 2 );
  1330. expect( root.getChild( 0 ).name ).to.equal( 'p' );
  1331. expect( root.getChild( 0 ).maxOffset ).to.equal( 6 );
  1332. expect( count( root.getChild( 0 ).getAttributes() ) ).to.equal( 1 );
  1333. expect( root.getChild( 0 ).getAttribute( 'key' ) ).to.equal( 'value' );
  1334. expect( root.getChild( 0 ).getChild( 0 ).data ).to.equal( 'foobar' );
  1335. expect( root.getChild( 1 ).name ).to.equal( 'p' );
  1336. expect( root.getChild( 1 ).maxOffset ).to.equal( 0 );
  1337. expect( count( root.getChild( 1 ).getAttributes() ) ).to.equal( 1 );
  1338. expect( root.getChild( 1 ).getAttribute( 'key' ) ).to.equal( 'value' );
  1339. } );
  1340. it( 'should throw if we try to split a root', () => {
  1341. expect( () => {
  1342. split( new Position( root, [ 0 ] ) );
  1343. } ).to.throw( CKEditorError, /^writer-split-element-no-parent/ );
  1344. } );
  1345. it( 'should throw if we try to split an element with no parent', () => {
  1346. expect( () => {
  1347. const element = createElement( 'p' );
  1348. split( new Position( element, [ 0 ] ) );
  1349. } ).to.throw( CKEditorError, /^writer-split-element-no-parent/ );
  1350. } );
  1351. it( 'should throw if we try to split a document fragment', () => {
  1352. expect( () => {
  1353. const documentFragment = createDocumentFragment();
  1354. split( new Position( documentFragment, [ 0 ] ) );
  1355. } ).to.throw( CKEditorError, /^writer-split-element-no-parent/ );
  1356. } );
  1357. it( 'should split elements to limitElement', () => {
  1358. const div = new Element( 'div', null, p );
  1359. const section = new Element( 'section', null, div );
  1360. root._insertChildren( 0, section );
  1361. split( new Position( p, [ 3 ] ), section );
  1362. expect( root.maxOffset ).to.equal( 1 );
  1363. expect( section.maxOffset ).to.equal( 2 );
  1364. expect( section.getChild( 0 ).name ).to.equal( 'div' );
  1365. expect( section.getChild( 0 ).getChild( 0 ).name ).to.equal( 'p' );
  1366. expect( section.getChild( 0 ).getChild( 0 ).getAttribute( 'key' ) ).to.equal( 'value' );
  1367. expect( count( section.getChild( 0 ).getChild( 0 ).getAttributes() ) ).to.equal( 1 );
  1368. expect( section.getChild( 0 ).getChild( 0 ).getChild( 0 ).data ).to.equal( 'foo' );
  1369. expect( section.getChild( 1 ).name ).to.equal( 'div' );
  1370. expect( section.getChild( 1 ).getChild( 0 ).name ).to.equal( 'p' );
  1371. expect( section.getChild( 1 ).getChild( 0 ).getAttribute( 'key' ) ).to.equal( 'value' );
  1372. expect( count( section.getChild( 1 ).getChild( 0 ).getAttributes() ) ).to.equal( 1 );
  1373. expect( section.getChild( 1 ).getChild( 0 ).getChild( 0 ).data ).to.equal( 'bar' );
  1374. } );
  1375. it( 'should throw when limitElement is not a position ancestor', () => {
  1376. const div = new Element( 'div', null, p );
  1377. const section = new Element( 'section', null, div );
  1378. root._insertChildren( 0, div );
  1379. root._insertChildren( 1, section );
  1380. expect( () => {
  1381. split( new Position( p, [ 3 ] ), section );
  1382. } ).to.throw( CKEditorError, /^writer-split-invalid-limit-element/ );
  1383. } );
  1384. it( 'should throw when trying to use detached writer', () => {
  1385. const writer = new Writer( model, batch );
  1386. expect( () => {
  1387. writer.split( new Position( root, [ 0, 3 ] ) );
  1388. } ).to.throw( CKEditorError, /^writer-incorrect-use/ );
  1389. } );
  1390. } );
  1391. describe( 'wrap()', () => {
  1392. let root, range;
  1393. beforeEach( () => {
  1394. root = doc.createRoot();
  1395. root._insertChildren( 0, new Text( 'foobar' ) );
  1396. range = new Range( new Position( root, [ 2 ] ), new Position( root, [ 4 ] ) );
  1397. } );
  1398. it( 'should wrap flat range with given element', () => {
  1399. const p = new Element( 'p' );
  1400. wrap( range, p );
  1401. expect( root.maxOffset ).to.equal( 5 );
  1402. expect( root.getChild( 0 ).data ).to.equal( 'fo' );
  1403. expect( root.getChild( 1 ) ).to.equal( p );
  1404. expect( p.getChild( 0 ).data ).to.equal( 'ob' );
  1405. expect( root.getChild( 2 ).data ).to.equal( 'ar' );
  1406. } );
  1407. it( 'should wrap flat range with an element of given name', () => {
  1408. wrap( range, 'p' );
  1409. expect( root.maxOffset ).to.equal( 5 );
  1410. expect( root.getChild( 0 ).data ).to.equal( 'fo' );
  1411. expect( root.getChild( 1 ).name ).to.equal( 'p' );
  1412. expect( root.getChild( 1 ).getChild( 0 ).data ).to.equal( 'ob' );
  1413. expect( root.getChild( 2 ).data ).to.equal( 'ar' );
  1414. } );
  1415. it( 'should wrap inside document fragment', () => {
  1416. const docFrag = new DocumentFragment( new Text( 'foo' ) );
  1417. wrap( Range.createIn( docFrag ), 'p' );
  1418. expect( docFrag.maxOffset ).to.equal( 1 );
  1419. expect( docFrag.getChild( 0 ).name ).to.equal( 'p' );
  1420. expect( docFrag.getChild( 0 ).getChild( 0 ).data ).to.equal( 'foo' );
  1421. } );
  1422. it( 'should throw if range to wrap is not flat', () => {
  1423. root._insertChildren( 1, [ new Element( 'p', [], new Text( 'xyz' ) ) ] );
  1424. const notFlatRange = new Range( new Position( root, [ 3 ] ), new Position( root, [ 6, 2 ] ) );
  1425. expect( () => {
  1426. wrap( notFlatRange, 'p' );
  1427. } ).to.throw( CKEditorError, /^writer-wrap-range-not-flat/ );
  1428. } );
  1429. it( 'should throw if element to wrap with has children #1', () => {
  1430. const p = new Element( 'p', [], new Text( 'a' ) );
  1431. expect( () => {
  1432. wrap( range, p );
  1433. } ).to.throw( CKEditorError, /^writer-wrap-element-not-empty/ );
  1434. } );
  1435. it( 'should throw if element to wrap with has children #2', () => {
  1436. const p = new Element( 'p' );
  1437. root._insertChildren( 0, p );
  1438. expect( () => {
  1439. wrap( range, p );
  1440. } ).to.throw( CKEditorError, /^writer-wrap-element-attached/ );
  1441. } );
  1442. it( 'should throw when trying to use detached writer', () => {
  1443. const writer = new Writer( model, batch );
  1444. expect( () => {
  1445. writer.wrap( range, 'p' );
  1446. } ).to.throw( CKEditorError, /^writer-incorrect-use/ );
  1447. } );
  1448. } );
  1449. describe( 'unwrap()', () => {
  1450. let root, p;
  1451. beforeEach( () => {
  1452. root = doc.createRoot();
  1453. p = new Element( 'p', [], new Text( 'xyz' ) );
  1454. root._insertChildren( 0, [ new Text( 'a' ), p, new Text( 'b' ) ] );
  1455. } );
  1456. it( 'should unwrap given element', () => {
  1457. unwrap( p );
  1458. expect( root.maxOffset ).to.equal( 5 );
  1459. expect( root.getChild( 0 ).data ).to.equal( 'axyzb' );
  1460. } );
  1461. it( 'should unwrap inside document fragment', () => {
  1462. const docFrag = new DocumentFragment( new Element( 'p', null, new Text( 'foo' ) ) );
  1463. unwrap( docFrag.getChild( 0 ) );
  1464. expect( docFrag.maxOffset ).to.equal( 3 );
  1465. expect( docFrag.getChild( 0 ).data ).to.equal( 'foo' );
  1466. } );
  1467. it( 'should throw if element to unwrap has no parent', () => {
  1468. const element = new Element( 'p' );
  1469. expect( () => {
  1470. unwrap( element );
  1471. } ).to.throw( CKEditorError, /^writer-unwrap-element-no-parent/ );
  1472. } );
  1473. it( 'should throw when trying to use detached writer', () => {
  1474. const writer = new Writer( model, batch );
  1475. expect( () => {
  1476. writer.unwrap( p );
  1477. } ).to.throw( CKEditorError, /^writer-incorrect-use/ );
  1478. } );
  1479. } );
  1480. describe( 'setMarker()', () => {
  1481. let root, range;
  1482. beforeEach( () => {
  1483. root = doc.createRoot();
  1484. root._appendChildren( new Text( 'foo' ) );
  1485. range = Range.createIn( root );
  1486. } );
  1487. it( 'should throw if options.usingOperations is not defined', () => {
  1488. expect( () => {
  1489. setMarker( 'name' );
  1490. } ).to.throw( CKEditorError, /^writer-setMarker-no-usingOperations/ );
  1491. } );
  1492. it( 'should throw if name and range is defined but options.usingOperations is not defined', () => {
  1493. expect( () => {
  1494. setMarker( 'name', { range } );
  1495. } ).to.throw( CKEditorError, /^writer-setMarker-no-usingOperations/ );
  1496. } );
  1497. it( 'should add marker to the document marker collection', () => {
  1498. setMarker( 'name', { range, usingOperation: false } );
  1499. expect( model.markers.get( 'name' ).getRange().isEqual( range ) ).to.be.true;
  1500. } );
  1501. it( 'should return marker if that was set directly', () => {
  1502. const marker = setMarker( 'name', { range, usingOperation: false } );
  1503. expect( model.markers.get( 'name' ) ).to.equal( marker );
  1504. } );
  1505. it( 'should return marker if that was set using operation API', () => {
  1506. const marker = setMarker( 'name', { range, usingOperation: true } );
  1507. expect( model.markers.get( 'name' ) ).to.equal( marker );
  1508. } );
  1509. it( 'should return marker with properly set managedUsingOperations (to true)', () => {
  1510. const marker = setMarker( 'name', { range, usingOperation: true } );
  1511. expect( marker.managedUsingOperations ).to.equal( true );
  1512. } );
  1513. it( 'should return marker with properly set managedUsingOperations (to false)', () => {
  1514. const marker = setMarker( 'name', { range, usingOperation: false } );
  1515. expect( marker.managedUsingOperations ).to.equal( false );
  1516. } );
  1517. it( 'should throw when trying to update existing marker in the document marker collection', () => {
  1518. setMarker( 'name', { range, usingOperation: false } );
  1519. const range2 = Range.createFromParentsAndOffsets( root, 0, root, 0 );
  1520. expect( () => {
  1521. setMarker( 'name', { range: range2, usingOperation: false } );
  1522. } ).to.throw( CKEditorError, /^writer-setMarker-marker-exists/ );
  1523. } );
  1524. it( 'should use operations when having set usingOperation to true', () => {
  1525. const spy = sinon.spy();
  1526. model.on( 'applyOperation', spy );
  1527. setMarker( 'name', { range, usingOperation: true } );
  1528. const op = batch.deltas[ 0 ].operations[ 0 ];
  1529. expect( spy.calledOnce ).to.be.true;
  1530. expect( spy.firstCall.args[ 1 ][ 0 ].type ).to.equal( 'marker' );
  1531. expect( op.oldRange ).to.be.null;
  1532. expect( op.newRange.isEqual( range ) ).to.be.true;
  1533. } );
  1534. it( 'should throw if marker with given name does not exist and range is not passed', () => {
  1535. expect( () => {
  1536. setMarker( 'name', { usingOperation: true } );
  1537. } ).to.throw( CKEditorError, /^writer-setMarker-no-range/ );
  1538. } );
  1539. it( 'should throw if marker is set directly and range is not passed', () => {
  1540. expect( () => {
  1541. setMarker( 'name', { usingOperation: false } );
  1542. } ).to.throw( CKEditorError, /^writer-setMarker-no-range/ );
  1543. } );
  1544. it( 'should throw when trying to use detached writer', () => {
  1545. const marker = setMarker( 'name', { range, usingOperation: false } );
  1546. const writer = new Writer( model, batch );
  1547. expect( () => {
  1548. writer.setMarker( marker, null, { usingOperation: true } );
  1549. } ).to.throw( CKEditorError, /^writer-incorrect-use/ );
  1550. } );
  1551. } );
  1552. describe( 'updateMarker()', () => {
  1553. let root, range;
  1554. beforeEach( () => {
  1555. root = doc.createRoot();
  1556. root._appendChildren( new Text( 'foo' ) );
  1557. range = Range.createIn( root );
  1558. } );
  1559. it( 'should accept marker instance', () => {
  1560. const marker = setMarker( 'name', { range, usingOperation: true } );
  1561. const range2 = Range.createFromParentsAndOffsets( root, 0, root, 0 );
  1562. updateMarker( marker, { range: range2, usingOperation: true } );
  1563. expect( batch.deltas.length ).to.equal( 2 );
  1564. const op = batch.deltas[ 1 ].operations[ 0 ];
  1565. expect( marker.getRange().isEqual( range2 ) ).to.be.true;
  1566. expect( op.oldRange.isEqual( range ) ).to.be.true;
  1567. expect( op.newRange.isEqual( range2 ) ).to.be.true;
  1568. } );
  1569. it( 'should not use operations when updating marker which does not use operations', () => {
  1570. const spy = sinon.spy();
  1571. model.on( 'applyOperation', spy );
  1572. const marker = setMarker( 'name', { range, usingOperation: false } );
  1573. const range2 = Range.createFromParentsAndOffsets( root, 0, root, 0 );
  1574. updateMarker( marker, { range: range2, usingOperation: false } );
  1575. sinon.assert.notCalled( spy );
  1576. } );
  1577. it( 'should accept empty range parameter if marker instance is passed and usingOperation is set to true', () => {
  1578. const marker = setMarker( 'name', { range, usingOperation: true } );
  1579. const spy = sinon.spy();
  1580. model.on( 'applyOperation', spy );
  1581. updateMarker( marker, { usingOperation: true } );
  1582. const op = batch.deltas[ 0 ].operations[ 0 ];
  1583. expect( spy.calledOnce ).to.be.true;
  1584. expect( spy.firstCall.args[ 1 ][ 0 ].type ).to.equal( 'marker' );
  1585. expect( op.oldRange ).to.be.null;
  1586. expect( op.newRange.isEqual( range ) ).to.be.true;
  1587. } );
  1588. it( 'should create additional operation when marker type changes to not managed using operation', () => {
  1589. const spy = sinon.spy();
  1590. model.on( 'applyOperation', spy );
  1591. setMarker( 'name', { range, usingOperation: true } );
  1592. updateMarker( 'name', { range } );
  1593. const marker = model.markers.get( 'name' );
  1594. const op1 = batch.deltas[ 0 ].operations[ 0 ];
  1595. const op2 = batch.deltas[ 1 ].operations[ 0 ];
  1596. expect( spy.calledTwice ).to.be.true;
  1597. expect( spy.firstCall.args[ 1 ][ 0 ].type ).to.equal( 'marker' );
  1598. expect( spy.secondCall.args[ 1 ][ 0 ].type ).to.equal( 'marker' );
  1599. expect( op1.oldRange ).to.be.null;
  1600. expect( op1.newRange.isEqual( range ) ).to.be.true;
  1601. expect( op2.oldRange.isEqual( range ) ).to.be.true;
  1602. expect( op2.newRange ).to.be.null;
  1603. expect( marker.managedUsingOperations ).to.be.false;
  1604. } );
  1605. it( 'should enable changing marker to be managed using operation', () => {
  1606. const spy = sinon.spy();
  1607. model.on( 'applyOperation', spy );
  1608. setMarker( 'name', { range, usingOperation: false } );
  1609. updateMarker( 'name', { range, usingOperation: true } );
  1610. const marker = model.markers.get( 'name' );
  1611. expect( spy.calledOnce ).to.be.true;
  1612. expect( spy.firstCall.args[ 1 ][ 0 ].type ).to.equal( 'marker' );
  1613. expect( marker.managedUsingOperations ).to.be.true;
  1614. } );
  1615. it( 'should enable changing marker to be not managed using operation', () => {
  1616. const spy = sinon.spy();
  1617. model.on( 'applyOperation', spy );
  1618. setMarker( 'name', { range, usingOperation: true } );
  1619. updateMarker( 'name', { range, usingOperation: false } );
  1620. const marker = model.markers.get( 'name' );
  1621. sinon.assert.calledTwice( spy );
  1622. expect( marker.managedUsingOperations ).to.be.false;
  1623. } );
  1624. it( 'should throw when marker provided by name does not exists', () => {
  1625. expect( () => {
  1626. updateMarker( 'name', { usingOperation: false } );
  1627. } ).to.throw( CKEditorError, /^writer-updateMarker-marker-not-exists/ );
  1628. } );
  1629. it( 'should throw when trying to use detached writer', () => {
  1630. const marker = setMarker( 'name', { range, usingOperation: false } );
  1631. const writer = new Writer( model, batch );
  1632. expect( () => {
  1633. writer.updateMarker( marker, { usingOperation: true } );
  1634. } ).to.throw( CKEditorError, /^writer-incorrect-use/ );
  1635. } );
  1636. } );
  1637. describe( 'removeMarker()', () => {
  1638. let root, range;
  1639. beforeEach( () => {
  1640. root = doc.createRoot();
  1641. root._appendChildren( new Text( 'foo' ) );
  1642. range = Range.createIn( root );
  1643. } );
  1644. it( 'should remove marker from the document marker collection', () => {
  1645. setMarker( 'name', { range, usingOperation: false } );
  1646. removeMarker( 'name' );
  1647. expect( model.markers.get( 'name' ) ).to.be.null;
  1648. } );
  1649. it( 'should throw when trying to remove non existing marker', () => {
  1650. expect( () => {
  1651. removeMarker( 'name' );
  1652. } ).to.throw( CKEditorError, /^writer-removeMarker-no-marker/ );
  1653. } );
  1654. it( 'should throw when trying to use detached writer', () => {
  1655. const writer = new Writer( model, batch );
  1656. expect( () => {
  1657. writer.removeMarker( 'name' );
  1658. } ).to.throw( CKEditorError, /^writer-incorrect-use/ );
  1659. } );
  1660. it( 'should accept marker instance', () => {
  1661. setMarker( 'name', { range, usingOperation: false } );
  1662. const marker = model.markers.get( 'name' );
  1663. removeMarker( marker );
  1664. expect( model.markers.get( 'name' ) ).to.be.null;
  1665. } );
  1666. it( 'should use MarkerOperation when marker was created using operation', () => {
  1667. setMarker( 'name', { range, usingOperation: true } );
  1668. const marker = model.markers.get( 'name' );
  1669. const spy = sinon.spy();
  1670. model.on( 'applyOperation', spy );
  1671. removeMarker( marker );
  1672. expect( spy.calledOnce ).to.be.true;
  1673. expect( spy.firstCall.args[ 1 ][ 0 ].type ).to.equal( 'marker' );
  1674. expect( model.markers.get( 'name' ) ).to.be.null;
  1675. } );
  1676. } );
  1677. describe( 'setSelection()', () => {
  1678. let root;
  1679. beforeEach( () => {
  1680. model.schema.register( 'p', { inheritAllFrom: '$block' } );
  1681. model.schema.extend( 'p', { allowIn: '$root' } );
  1682. root = doc.createRoot();
  1683. root._appendChildren( [
  1684. new Element( 'p' ),
  1685. new Element( 'p' ),
  1686. new Element( 'p', [], new Text( 'foo' ) )
  1687. ] );
  1688. } );
  1689. it( 'should use DocumentSelection#_setTo method', () => {
  1690. const firstParagraph = root.getNodeByPath( [ 1 ] );
  1691. const setToSpy = sinon.spy( DocumentSelection.prototype, '_setTo' );
  1692. setSelection( firstParagraph, 0 );
  1693. expect( setToSpy.calledOnce ).to.be.true;
  1694. setToSpy.restore();
  1695. } );
  1696. it( 'should change document selection ranges', () => {
  1697. const range = new Range( new Position( root, [ 1 ] ), new Position( root, [ 2, 2 ] ) );
  1698. setSelection( range, { backward: true } );
  1699. expect( model.document.selection._ranges.length ).to.equal( 1 );
  1700. expect( model.document.selection._ranges[ 0 ].start.path ).to.deep.equal( [ 1 ] );
  1701. expect( model.document.selection._ranges[ 0 ].end.path ).to.deep.equal( [ 2, 2 ] );
  1702. expect( model.document.selection.isBackward ).to.be.true;
  1703. } );
  1704. } );
  1705. describe( 'setSelectionFocus()', () => {
  1706. let root;
  1707. beforeEach( () => {
  1708. model.schema.register( 'p', { inheritAllFrom: '$block' } );
  1709. model.schema.extend( 'p', { allowIn: '$root' } );
  1710. root = doc.createRoot();
  1711. root._appendChildren( [
  1712. new Element( 'p' ),
  1713. new Element( 'p' ),
  1714. new Element( 'p', [], new Text( 'foo' ) )
  1715. ] );
  1716. } );
  1717. it( 'should use DocumentSelection#_setFocus method', () => {
  1718. const firstParagraph = root.getNodeByPath( [ 1 ] );
  1719. const setFocusSpy = sinon.spy( DocumentSelection.prototype, '_setFocus' );
  1720. setSelectionFocus( firstParagraph );
  1721. expect( setFocusSpy.calledOnce ).to.be.true;
  1722. setFocusSpy.restore();
  1723. } );
  1724. it( 'should change document selection ranges', () => {
  1725. setSelection( new Position( root, [ 1 ] ) );
  1726. setSelectionFocus( new Position( root, [ 2, 2 ] ) );
  1727. expect( model.document.selection._ranges.length ).to.equal( 1 );
  1728. expect( model.document.selection._ranges[ 0 ].start.path ).to.deep.equal( [ 1 ] );
  1729. expect( model.document.selection._ranges[ 0 ].end.path ).to.deep.equal( [ 2, 2 ] );
  1730. } );
  1731. } );
  1732. describe( 'setSelectionAttribute()', () => {
  1733. const fooStoreAttrKey = DocumentSelection._getStoreAttributeKey( 'foo' );
  1734. let root, rangeInEmptyP, emptyP;
  1735. beforeEach( () => {
  1736. model.schema.register( 'p', { inheritAllFrom: '$block' } );
  1737. model.schema.extend( 'p', { allowIn: '$root' } );
  1738. root = doc.createRoot();
  1739. root._appendChildren( [
  1740. new Element( 'p', [], [] ),
  1741. new Element( 'p' ),
  1742. new Element( 'p', [], new Text( 'foo' ) )
  1743. ] );
  1744. rangeInEmptyP = new Range( new Position( root, [ 0, 0 ] ), new Position( root, [ 0, 0 ] ) );
  1745. emptyP = root.getChild( 0 );
  1746. } );
  1747. it( 'should store attribute if the selection is in empty node', () => {
  1748. setSelection( rangeInEmptyP );
  1749. setSelectionAttribute( 'foo', 'bar' );
  1750. expect( model.document.selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  1751. expect( emptyP.getAttribute( fooStoreAttrKey ) ).to.equal( 'bar' );
  1752. } );
  1753. it( 'should be able to store attributes from the given object', () => {
  1754. setSelection( rangeInEmptyP );
  1755. setSelectionAttribute( { key1: 'foo', key2: 'bar' } );
  1756. expect( model.document.selection.getAttribute( 'key1' ) ).to.equal( 'foo' );
  1757. expect( model.document.selection.getAttribute( 'key2' ) ).to.equal( 'bar' );
  1758. } );
  1759. it( 'should be able to store attributes from the given iterable', () => {
  1760. setSelection( rangeInEmptyP );
  1761. setSelectionAttribute( new Map( [ [ 'key1', 'foo' ], [ 'key2', 'bar' ] ] ) );
  1762. expect( model.document.selection.getAttribute( 'key1' ) ).to.equal( 'foo' );
  1763. expect( model.document.selection.getAttribute( 'key2' ) ).to.equal( 'bar' );
  1764. } );
  1765. } );
  1766. describe( 'removeSelectionAttribute()', () => {
  1767. const fooStoreAttrKey = DocumentSelection._getStoreAttributeKey( 'foo' );
  1768. let root, rangeInEmptyP, emptyP;
  1769. beforeEach( () => {
  1770. model.schema.register( 'p', { inheritAllFrom: '$block' } );
  1771. model.schema.extend( 'p', { allowIn: '$root' } );
  1772. root = doc.createRoot();
  1773. root._appendChildren( [
  1774. new Element( 'p', [], [] ),
  1775. new Element( 'p' ),
  1776. new Element( 'p', [], new Text( 'foo' ) )
  1777. ] );
  1778. rangeInEmptyP = new Range( new Position( root, [ 0, 0 ] ), new Position( root, [ 0, 0 ] ) );
  1779. emptyP = root.getChild( 0 );
  1780. } );
  1781. it( 'should remove stored attribute if the selection is in empty node', () => {
  1782. setSelection( rangeInEmptyP );
  1783. setSelectionAttribute( 'foo', 'bar' );
  1784. removeSelectionAttribute( 'foo' );
  1785. expect( model.document.selection.getAttribute( 'foo' ) ).to.be.undefined;
  1786. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  1787. } );
  1788. it( 'should remove all attributes from the given iterable', () => {
  1789. setSelection( rangeInEmptyP );
  1790. setSelectionAttribute( 'foo', 'bar' );
  1791. setSelectionAttribute( 'foo2', 'bar2' );
  1792. removeSelectionAttribute( [ 'foo', 'foo2' ] );
  1793. expect( model.document.selection.getAttribute( 'foo' ) ).to.be.undefined;
  1794. expect( model.document.selection.getAttribute( 'foo2' ) ).to.be.undefined;
  1795. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  1796. } );
  1797. it( 'should do nothing if attribute does not exist in the selection', () => {
  1798. setSelection( rangeInEmptyP );
  1799. setSelectionAttribute( 'foo', 'bar' );
  1800. setSelectionAttribute( 'foo2', 'bar2' );
  1801. removeSelectionAttribute( [ 'foo', 'baz' ] );
  1802. expect( model.document.selection.getAttribute( 'foo' ) ).to.be.undefined;
  1803. expect( model.document.selection.getAttribute( 'foo2' ) ).to.equal( 'bar2' );
  1804. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  1805. } );
  1806. } );
  1807. describe( 'overrideSelectionGravity()', () => {
  1808. it( 'should use DocumentSelection#_overrideGravity', () => {
  1809. const overrideGravitySpy = sinon.spy( DocumentSelection.prototype, '_overrideGravity' );
  1810. overrideSelectionGravity();
  1811. sinon.assert.calledOnce( overrideGravitySpy );
  1812. overrideGravitySpy.restore();
  1813. } );
  1814. it( 'should not get attributes from the node before the caret when gravity is overridden', () => {
  1815. const root = doc.createRoot();
  1816. root._appendChildren( [
  1817. new Text( 'foo', { foo: true } ),
  1818. new Text( 'bar', { foo: true, bar: true } ),
  1819. new Text( 'biz', { foo: true } )
  1820. ] );
  1821. setSelection( new Position( root, [ 6 ] ) );
  1822. expect( Array.from( model.document.selection.getAttributeKeys() ) ).to.deep.equal( [ 'foo', 'bar' ] );
  1823. overrideSelectionGravity();
  1824. expect( Array.from( model.document.selection.getAttributeKeys() ) ).to.deep.equal( [ 'foo' ] );
  1825. expect( model.document.selection.isGravityOverridden ).to.true;
  1826. // Disable override by moving selection.
  1827. setSelection( new Position( root, [ 5 ] ) );
  1828. expect( Array.from( model.document.selection.getAttributeKeys() ) ).to.deep.equal( [ 'foo', 'bar' ] );
  1829. expect( model.document.selection.isGravityOverridden ).to.false;
  1830. } );
  1831. it( 'should allow to restorer gravity in a custom way', () => {
  1832. const root = doc.createRoot();
  1833. root._appendChildren( [ new Text( 'foobar', { foo: true } ) ] );
  1834. setSelection( new Position( root, [ 1 ] ) );
  1835. overrideSelectionGravity( true );
  1836. // Moving selection does not restore gravity.
  1837. setSelection( new Position( root, [ 2 ] ) );
  1838. expect( model.document.selection.isGravityOverridden ).to.true;
  1839. // We need to do it manually.
  1840. restoreSelectionGravity();
  1841. expect( model.document.selection.isGravityOverridden ).to.false;
  1842. } );
  1843. } );
  1844. describe( 'restoreSelectionGravity()', () => {
  1845. it( 'should use DocumentSelection#_restoreGravity', () => {
  1846. const restoreGravitySpy = sinon.spy( DocumentSelection.prototype, '_restoreGravity' );
  1847. restoreSelectionGravity();
  1848. sinon.assert.calledOnce( restoreGravitySpy );
  1849. restoreGravitySpy.restore();
  1850. } );
  1851. it( 'should restore overridden gravity to default', () => {
  1852. const root = doc.createRoot();
  1853. root._appendChildren( [
  1854. new Text( 'foo', { foo: true } ),
  1855. new Text( 'bar', { foo: true, bar: true } ),
  1856. new Text( 'biz', { foo: true } )
  1857. ] );
  1858. setSelection( new Position( root, [ 6 ] ) );
  1859. overrideSelectionGravity();
  1860. expect( Array.from( model.document.selection.getAttributeKeys() ) ).to.deep.equal( [ 'foo' ] );
  1861. restoreSelectionGravity();
  1862. expect( Array.from( model.document.selection.getAttributeKeys() ) ).to.deep.equal( [ 'foo', 'bar' ] );
  1863. } );
  1864. } );
  1865. function createText( data, attributes ) {
  1866. return model.change( writer => {
  1867. return writer.createText( data, attributes );
  1868. } );
  1869. }
  1870. function createElement( name, attributes ) {
  1871. return model.change( writer => {
  1872. return writer.createElement( name, attributes );
  1873. } );
  1874. }
  1875. function createDocumentFragment() {
  1876. return model.change( writer => {
  1877. return writer.createDocumentFragment();
  1878. } );
  1879. }
  1880. function insert( item, itemOrPosition, offset ) {
  1881. model.enqueueChange( batch, writer => {
  1882. writer.insert( item, itemOrPosition, offset );
  1883. } );
  1884. }
  1885. function insertText( text, attributes, itemOrPosition, offset ) {
  1886. model.enqueueChange( batch, writer => {
  1887. writer.insertText( text, attributes, itemOrPosition, offset );
  1888. } );
  1889. }
  1890. function insertElement( name, attributes, itemOrPosition, offset ) {
  1891. model.enqueueChange( batch, writer => {
  1892. writer.insertElement( name, attributes, itemOrPosition, offset );
  1893. } );
  1894. }
  1895. function append( item, parent ) {
  1896. model.enqueueChange( batch, writer => {
  1897. writer.append( item, parent );
  1898. } );
  1899. }
  1900. function appendText( text, attributes, parent ) {
  1901. model.enqueueChange( batch, writer => {
  1902. writer.appendText( text, attributes, parent );
  1903. } );
  1904. }
  1905. function appendElement( name, attributes, parent ) {
  1906. model.enqueueChange( batch, writer => {
  1907. writer.appendElement( name, attributes, parent );
  1908. } );
  1909. }
  1910. function setTextData( value, textNode ) {
  1911. model.enqueueChange( batch, writer => {
  1912. writer.setTextData( value, textNode );
  1913. } );
  1914. }
  1915. function setAttribute( key, value, itemOrRange ) {
  1916. model.enqueueChange( batch, writer => {
  1917. writer.setAttribute( key, value, itemOrRange );
  1918. } );
  1919. }
  1920. function setAttributes( attributes, itemOrRange ) {
  1921. model.enqueueChange( batch, writer => {
  1922. writer.setAttributes( attributes, itemOrRange );
  1923. } );
  1924. }
  1925. function removeAttribute( key, itemOrRange ) {
  1926. model.enqueueChange( batch, writer => {
  1927. writer.removeAttribute( key, itemOrRange );
  1928. } );
  1929. }
  1930. function clearAttributes( itemOrRange ) {
  1931. model.enqueueChange( batch, writer => {
  1932. writer.clearAttributes( itemOrRange );
  1933. } );
  1934. }
  1935. function move( range, itemOrPosition, offset ) {
  1936. model.enqueueChange( batch, writer => {
  1937. writer.move( range, itemOrPosition, offset );
  1938. } );
  1939. }
  1940. function remove( itemOrRange ) {
  1941. model.enqueueChange( batch, writer => {
  1942. writer.remove( itemOrRange );
  1943. } );
  1944. }
  1945. function merge( position ) {
  1946. model.enqueueChange( batch, writer => {
  1947. writer.merge( position );
  1948. } );
  1949. }
  1950. function rename( element, newName ) {
  1951. model.enqueueChange( batch, writer => {
  1952. writer.rename( element, newName );
  1953. } );
  1954. }
  1955. function split( position, limitElement ) {
  1956. model.enqueueChange( batch, writer => {
  1957. writer.split( position, limitElement );
  1958. } );
  1959. }
  1960. function wrap( range, elementOrString ) {
  1961. model.enqueueChange( batch, writer => {
  1962. writer.wrap( range, elementOrString );
  1963. } );
  1964. }
  1965. function unwrap( element ) {
  1966. model.enqueueChange( batch, writer => {
  1967. writer.unwrap( element );
  1968. } );
  1969. }
  1970. function setMarker( name, options ) {
  1971. let marker;
  1972. model.enqueueChange( batch, writer => {
  1973. marker = writer.setMarker( name, options );
  1974. } );
  1975. return marker;
  1976. }
  1977. function updateMarker( markerOrNameOrRange, rangeOrManagedUsingOperations, options ) {
  1978. model.enqueueChange( batch, writer => {
  1979. writer.updateMarker( markerOrNameOrRange, rangeOrManagedUsingOperations, options );
  1980. } );
  1981. }
  1982. function removeMarker( markerOrName, options ) {
  1983. model.enqueueChange( batch, writer => {
  1984. writer.removeMarker( markerOrName, options );
  1985. } );
  1986. }
  1987. function setSelection( selectable, optionsOrPlaceOrOffset, options ) {
  1988. model.enqueueChange( batch, writer => {
  1989. writer.setSelection( selectable, optionsOrPlaceOrOffset, options );
  1990. } );
  1991. }
  1992. function setSelectionFocus( itemOrPosition, offset ) {
  1993. model.enqueueChange( batch, writer => {
  1994. writer.setSelectionFocus( itemOrPosition, offset );
  1995. } );
  1996. }
  1997. function setSelectionAttribute( key, value ) {
  1998. model.enqueueChange( batch, writer => {
  1999. writer.setSelectionAttribute( key, value );
  2000. } );
  2001. }
  2002. function removeSelectionAttribute( key ) {
  2003. model.enqueueChange( batch, writer => {
  2004. writer.removeSelectionAttribute( key );
  2005. } );
  2006. }
  2007. function overrideSelectionGravity( customRestore ) {
  2008. model.change( writer => {
  2009. writer.overrideSelectionGravity( customRestore );
  2010. } );
  2011. }
  2012. function restoreSelectionGravity() {
  2013. model.change( writer => {
  2014. writer.restoreSelectionGravity();
  2015. } );
  2016. }
  2017. } );