writer.js 88 KB

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