writer.js 74 KB

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