writer.js 91 KB

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