documentselection.js 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Model from '../../src/model/model';
  6. import Batch from '../../src/model/batch';
  7. import Element from '../../src/model/element';
  8. import Text from '../../src/model/text';
  9. import Range from '../../src/model/range';
  10. import Position from '../../src/model/position';
  11. import LiveRange from '../../src/model/liverange';
  12. import DocumentSelection from '../../src/model/documentselection';
  13. import InsertOperation from '../../src/model/operation/insertoperation';
  14. import MoveOperation from '../../src/model/operation/moveoperation';
  15. import AttributeOperation from '../../src/model/operation/attributeoperation';
  16. import SplitOperation from '../../src/model/operation/splitoperation';
  17. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  18. import Collection from '@ckeditor/ckeditor5-utils/src/collection';
  19. import count from '@ckeditor/ckeditor5-utils/src/count';
  20. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  21. import { setData, getData } from '../../src/dev-utils/model';
  22. describe( 'DocumentSelection', () => {
  23. let model, doc, root, selection, liveRange, range;
  24. testUtils.createSinonSandbox();
  25. const fooStoreAttrKey = DocumentSelection._getStoreAttributeKey( 'foo' );
  26. const abcStoreAttrKey = DocumentSelection._getStoreAttributeKey( 'abc' );
  27. beforeEach( () => {
  28. model = new Model();
  29. doc = model.document;
  30. root = doc.createRoot();
  31. root._appendChild( [
  32. new Element( 'p' ),
  33. new Element( 'p' ),
  34. new Element( 'p', [], new Text( 'foobar' ) ),
  35. new Element( 'p' ),
  36. new Element( 'p' ),
  37. new Element( 'p' ),
  38. new Element( 'p', [], new Text( 'foobar' ) )
  39. ] );
  40. selection = doc.selection;
  41. model.schema.register( 'p', { inheritAllFrom: '$block' } );
  42. model.schema.register( 'widget', {
  43. isObject: true
  44. } );
  45. liveRange = new LiveRange( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) );
  46. range = new Range( new Position( root, [ 2 ] ), new Position( root, [ 2, 2 ] ) );
  47. } );
  48. afterEach( () => {
  49. model.destroy();
  50. liveRange.detach();
  51. } );
  52. describe( 'default range', () => {
  53. it( 'should go to the first editable element', () => {
  54. const ranges = Array.from( selection.getRanges() );
  55. expect( ranges.length ).to.equal( 1 );
  56. expect( selection.anchor.isEqual( new Position( root, [ 0, 0 ] ) ) ).to.be.true;
  57. expect( selection.focus.isEqual( new Position( root, [ 0, 0 ] ) ) ).to.be.true;
  58. expect( selection ).to.have.property( 'isBackward', false );
  59. } );
  60. it( 'should be set to the beginning of the doc if there is no editable element', () => {
  61. model = new Model();
  62. doc = model.document;
  63. root = doc.createRoot();
  64. root._insertChild( 0, new Text( 'foobar' ) );
  65. selection = doc.selection;
  66. const ranges = Array.from( selection.getRanges() );
  67. expect( ranges.length ).to.equal( 1 );
  68. expect( selection.anchor.isEqual( new Position( root, [ 0 ] ) ) ).to.be.true;
  69. expect( selection.focus.isEqual( new Position( root, [ 0 ] ) ) ).to.be.true;
  70. expect( selection ).to.have.property( 'isBackward', false );
  71. expect( count( selection.getAttributes() ) ).to.equal( 0 );
  72. } );
  73. it( 'should skip element when you can not put selection', () => {
  74. model = new Model();
  75. doc = model.document;
  76. root = doc.createRoot();
  77. root._insertChild( 0, [
  78. new Element( 'img' ),
  79. new Element( 'p', [], new Text( 'foobar' ) )
  80. ] );
  81. model.schema.register( 'img' );
  82. model.schema.register( 'p', { inheritAllFrom: '$block' } );
  83. selection = doc.selection;
  84. const ranges = Array.from( selection.getRanges() );
  85. expect( ranges.length ).to.equal( 1 );
  86. expect( selection.anchor.isEqual( new Position( root, [ 1, 0 ] ) ) ).to.be.true;
  87. expect( selection.focus.isEqual( new Position( root, [ 1, 0 ] ) ) ).to.be.true;
  88. expect( selection ).to.have.property( 'isBackward', false );
  89. expect( count( selection.getAttributes() ) ).to.equal( 0 );
  90. } );
  91. } );
  92. describe( 'isCollapsed', () => {
  93. it( 'should be true for the default range (in text position)', () => {
  94. expect( selection.isCollapsed ).to.be.true;
  95. } );
  96. it( 'should be false for the default range (object selection) ', () => {
  97. root._insertChild( 0, new Element( 'widget' ) );
  98. expect( selection.isCollapsed ).to.be.false;
  99. } );
  100. it( 'should back off to the default algorithm if selection has ranges', () => {
  101. selection._setTo( range );
  102. expect( selection.isCollapsed ).to.be.false;
  103. } );
  104. } );
  105. describe( 'anchor', () => {
  106. it( 'should equal the default range\'s start (in text position)', () => {
  107. const expectedPos = new Position( root, [ 0, 0 ] );
  108. expect( selection.anchor.isEqual( expectedPos ) ).to.be.true;
  109. } );
  110. it( 'should equal the default range\'s start (object selection)', () => {
  111. root._insertChild( 0, new Element( 'widget' ) );
  112. const expectedPos = new Position( root, [ 0 ] );
  113. expect( selection.anchor.isEqual( expectedPos ) ).to.be.true;
  114. } );
  115. it( 'should back off to the default algorithm if selection has ranges', () => {
  116. selection._setTo( range );
  117. expect( selection.anchor.isEqual( range.start ) ).to.be.true;
  118. } );
  119. } );
  120. describe( 'focus', () => {
  121. it( 'should equal the default range\'s end (in text position)', () => {
  122. const expectedPos = new Position( root, [ 0, 0 ] );
  123. expect( selection.focus.isEqual( expectedPos ) ).to.be.true;
  124. } );
  125. it( 'should equal the default range\'s end (object selection)', () => {
  126. root._insertChild( 0, new Element( 'widget' ) );
  127. const expectedPos = new Position( root, [ 1 ] );
  128. expect( selection.focus.isEqual( expectedPos ) ).to.be.true;
  129. expect( selection.focus.isEqual( selection.getFirstRange().end ) ).to.be.true;
  130. } );
  131. it( 'should back off to the default algorithm if selection has ranges', () => {
  132. selection._setTo( range );
  133. expect( selection.focus.isEqual( range.end ) ).to.be.true;
  134. } );
  135. } );
  136. describe( 'rangeCount', () => {
  137. it( 'should return proper range count', () => {
  138. expect( selection.rangeCount ).to.equal( 1 );
  139. selection._setTo( new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) ) );
  140. expect( selection.rangeCount ).to.equal( 1 );
  141. selection._setTo( [
  142. new Range( new Position( root, [ 2 ] ), new Position( root, [ 2 ] ) ),
  143. new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) )
  144. ] );
  145. expect( selection.rangeCount ).to.equal( 2 );
  146. } );
  147. } );
  148. describe( 'hasOwnRange', () => {
  149. it( 'should return true if selection has any ranges set', () => {
  150. selection._setTo( new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) ) );
  151. expect( selection.hasOwnRange ).to.be.true;
  152. } );
  153. it( 'should return false if selection has a default range', () => {
  154. expect( selection.hasOwnRange ).to.be.false;
  155. } );
  156. } );
  157. describe( 'markers', () => {
  158. it( 'should implement #markers collection', () => {
  159. expect( selection.markers ).to.instanceof( Collection );
  160. expect( selection.markers ).to.length( 0 );
  161. } );
  162. it( 'should add markers to the collection when selection is inside the marker range', () => {
  163. model.change( writer => {
  164. writer.setSelection( writer.createRange(
  165. writer.createPositionFromPath( root, [ 2, 2 ] ),
  166. writer.createPositionFromPath( root, [ 2, 4 ] )
  167. ) );
  168. writer.addMarker( 'marker-1', {
  169. range: writer.createRange(
  170. writer.createPositionFromPath( root, [ 0, 0 ] ),
  171. writer.createPositionFromPath( root, [ 2, 2 ] )
  172. ),
  173. usingOperation: false
  174. } );
  175. writer.addMarker( 'marker-2', {
  176. range: writer.createRange(
  177. writer.createPositionFromPath( root, [ 2, 2 ] ),
  178. writer.createPositionFromPath( root, [ 2, 4 ] )
  179. ),
  180. usingOperation: false
  181. } );
  182. writer.addMarker( 'marker-3', {
  183. range: writer.createRange(
  184. writer.createPositionFromPath( root, [ 2, 1 ] ),
  185. writer.createPositionFromPath( root, [ 2, 5 ] )
  186. ),
  187. usingOperation: false
  188. } );
  189. writer.addMarker( 'marker-4', {
  190. range: writer.createRange(
  191. writer.createPositionFromPath( root, [ 2, 4 ] ),
  192. writer.createPositionFromPath( root, [ 3, 0 ] )
  193. ),
  194. usingOperation: false
  195. } );
  196. } );
  197. expect( selection.markers.map( marker => marker.name ) ).to.have.members( [ 'marker-2', 'marker-3' ] );
  198. } );
  199. it( 'should update markers after selection change', () => {
  200. model.change( writer => {
  201. writer.setSelection( writer.createRange(
  202. writer.createPositionFromPath( root, [ 2, 1 ] ),
  203. writer.createPositionFromPath( root, [ 2, 2 ] )
  204. ) );
  205. writer.addMarker( 'marker-1', {
  206. range: writer.createRange(
  207. writer.createPositionFromPath( root, [ 2, 0 ] ),
  208. writer.createPositionFromPath( root, [ 2, 6 ] )
  209. ),
  210. usingOperation: false
  211. } );
  212. writer.addMarker( 'marker-2', {
  213. range: writer.createRange(
  214. writer.createPositionFromPath( root, [ 2, 0 ] ),
  215. writer.createPositionFromPath( root, [ 2, 3 ] )
  216. ),
  217. usingOperation: false
  218. } );
  219. writer.addMarker( 'marker-3', {
  220. range: writer.createRange(
  221. writer.createPositionFromPath( root, [ 2, 3 ] ),
  222. writer.createPositionFromPath( root, [ 2, 6 ] )
  223. ),
  224. usingOperation: false
  225. } );
  226. } );
  227. expect( selection.markers.map( marker => marker.name ) ).to.have.members( [ 'marker-1', 'marker-2' ] );
  228. model.change( writer => {
  229. writer.setSelection( writer.createRange(
  230. writer.createPositionFromPath( root, [ 2, 4 ] ),
  231. writer.createPositionFromPath( root, [ 2, 5 ] )
  232. ) );
  233. } );
  234. expect( selection.markers.map( marker => marker.name ) ).to.have.members( [ 'marker-1', 'marker-3' ] );
  235. } );
  236. it( 'should update markers after markers change', () => {
  237. model.change( writer => {
  238. writer.setSelection( writer.createRange(
  239. writer.createPositionFromPath( root, [ 2, 1 ] ),
  240. writer.createPositionFromPath( root, [ 2, 2 ] )
  241. ) );
  242. writer.addMarker( 'marker-1', {
  243. range: writer.createRange(
  244. writer.createPositionFromPath( root, [ 2, 0 ] ),
  245. writer.createPositionFromPath( root, [ 2, 6 ] )
  246. ),
  247. usingOperation: false
  248. } );
  249. writer.addMarker( 'marker-2', {
  250. range: writer.createRange(
  251. writer.createPositionFromPath( root, [ 2, 0 ] ),
  252. writer.createPositionFromPath( root, [ 2, 3 ] )
  253. ),
  254. usingOperation: false
  255. } );
  256. writer.addMarker( 'marker-3', {
  257. range: writer.createRange(
  258. writer.createPositionFromPath( root, [ 2, 3 ] ),
  259. writer.createPositionFromPath( root, [ 2, 6 ] )
  260. ),
  261. usingOperation: false
  262. } );
  263. } );
  264. expect( selection.markers.map( marker => marker.name ), 1 ).to.have.members( [ 'marker-1', 'marker-2' ] );
  265. model.change( writer => {
  266. writer.removeMarker( 'marker-1' );
  267. writer.updateMarker( 'marker-2', {
  268. range: writer.createRange(
  269. writer.createPositionFromPath( root, [ 2, 3 ] ),
  270. writer.createPositionFromPath( root, [ 2, 6 ] )
  271. ),
  272. usingOperation: false
  273. } );
  274. writer.updateMarker( 'marker-3', {
  275. range: writer.createRange(
  276. writer.createPositionFromPath( root, [ 2, 0 ] ),
  277. writer.createPositionFromPath( root, [ 2, 3 ] )
  278. ),
  279. usingOperation: false
  280. } );
  281. } );
  282. expect( selection.markers.map( marker => marker.name ), 2 ).to.have.members( [ 'marker-3' ] );
  283. } );
  284. it( 'should not add marker when collapsed selection is on the marker left bound', () => {
  285. model.change( writer => {
  286. writer.setSelection( writer.createRange(
  287. writer.createPositionFromPath( root, [ 2, 2 ] ),
  288. writer.createPositionFromPath( root, [ 2, 4 ] )
  289. ) );
  290. writer.addMarker( 'marker', {
  291. range: writer.createRange(
  292. writer.createPositionFromPath( root, [ 2, 2 ] )
  293. ),
  294. usingOperation: false
  295. } );
  296. } );
  297. expect( selection.markers ).to.length( 0 );
  298. } );
  299. it( 'should not add marker when collapsed selection is on the marker right bound', () => {
  300. model.change( writer => {
  301. writer.setSelection( writer.createRange(
  302. writer.createPositionFromPath( root, [ 2, 4 ] )
  303. ) );
  304. writer.addMarker( 'marker', {
  305. range: writer.createRange(
  306. writer.createPositionFromPath( root, [ 2, 2 ] ),
  307. writer.createPositionFromPath( root, [ 2, 4 ] )
  308. ),
  309. usingOperation: false
  310. } );
  311. } );
  312. expect( selection.markers ).to.length( 0 );
  313. } );
  314. it( 'should add marker when non-collapsed selection is inside a marker and touches the left bound', () => {
  315. model.change( writer => {
  316. writer.setSelection( writer.createRange(
  317. writer.createPositionFromPath( root, [ 2, 1 ] ),
  318. writer.createPositionFromPath( root, [ 2, 3 ] )
  319. ) );
  320. writer.addMarker( 'marker', {
  321. range: writer.createRange(
  322. writer.createPositionFromPath( root, [ 2, 1 ] ),
  323. writer.createPositionFromPath( root, [ 2, 5 ] )
  324. ),
  325. usingOperation: false
  326. } );
  327. } );
  328. expect( selection.markers.map( marker => marker.name ) ).to.have.members( [ 'marker' ] );
  329. } );
  330. it( 'should add marker when non-collapsed selection is inside a marker and touches the right bound', () => {
  331. model.change( writer => {
  332. writer.setSelection( writer.createRange(
  333. writer.createPositionFromPath( root, [ 2, 2 ] ),
  334. writer.createPositionFromPath( root, [ 2, 5 ] )
  335. ) );
  336. writer.addMarker( 'marker', {
  337. range: writer.createRange(
  338. writer.createPositionFromPath( root, [ 2, 1 ] ),
  339. writer.createPositionFromPath( root, [ 2, 5 ] )
  340. ),
  341. usingOperation: false
  342. } );
  343. } );
  344. expect( selection.markers.map( marker => marker.name ) ).to.have.members( [ 'marker' ] );
  345. } );
  346. it( 'should add marker of selected widget', () => {
  347. root._insertChild( 0, new Element( 'widget' ) );
  348. model.change( writer => {
  349. writer.setSelection( writer.createRange(
  350. writer.createPositionFromPath( root, [ 0 ] ),
  351. writer.createPositionFromPath( root, [ 1 ] )
  352. ) );
  353. writer.addMarker( 'marker', {
  354. range: writer.createRange(
  355. writer.createPositionFromPath( root, [ 0 ] ),
  356. writer.createPositionFromPath( root, [ 1 ] )
  357. ),
  358. usingOperation: false
  359. } );
  360. } );
  361. expect( selection.markers.map( marker => marker.name ) ).to.have.members( [ 'marker' ] );
  362. } );
  363. } );
  364. describe( 'destroy()', () => {
  365. it( 'should unbind all events', () => {
  366. selection._setTo( [ range, liveRange ] );
  367. const ranges = Array.from( selection._selection._ranges );
  368. sinon.spy( ranges[ 0 ], 'detach' );
  369. sinon.spy( ranges[ 1 ], 'detach' );
  370. selection.destroy();
  371. expect( ranges[ 0 ].detach.called ).to.be.true;
  372. expect( ranges[ 1 ].detach.called ).to.be.true;
  373. ranges[ 0 ].detach.restore();
  374. ranges[ 1 ].detach.restore();
  375. } );
  376. } );
  377. describe( 'getFirstRange()', () => {
  378. it( 'should return default range if no ranges were added', () => {
  379. const firstRange = selection.getFirstRange();
  380. expect( firstRange.start.isEqual( new Position( root, [ 0, 0 ] ) ) );
  381. expect( firstRange.end.isEqual( new Position( root, [ 0, 0 ] ) ) );
  382. } );
  383. } );
  384. describe( 'getLastRange()', () => {
  385. it( 'should return default range if no ranges were added', () => {
  386. const lastRange = selection.getLastRange();
  387. expect( lastRange.start.isEqual( new Position( root, [ 0, 0 ] ) ) );
  388. expect( lastRange.end.isEqual( new Position( root, [ 0, 0 ] ) ) );
  389. } );
  390. } );
  391. describe( 'getSelectedElement()', () => {
  392. it( 'should return selected element', () => {
  393. selection._setTo( liveRange );
  394. const p = root.getChild( 0 );
  395. expect( selection.getSelectedElement() ).to.equal( p );
  396. } );
  397. } );
  398. describe( 'is', () => {
  399. it( 'should return true for selection', () => {
  400. expect( selection.is( 'selection' ) ).to.be.true;
  401. } );
  402. it( 'should return true for documentSelection', () => {
  403. expect( selection.is( 'documentSelection' ) ).to.be.true;
  404. } );
  405. it( 'should return false for other values', () => {
  406. expect( selection.is( 'node' ) ).to.be.false;
  407. expect( selection.is( 'text' ) ).to.be.false;
  408. expect( selection.is( 'textProxy' ) ).to.be.false;
  409. expect( selection.is( 'element' ) ).to.be.false;
  410. expect( selection.is( 'rootElement' ) ).to.be.false;
  411. } );
  412. } );
  413. describe( '_setTo() - set collapsed at', () => {
  414. it( 'detaches all existing ranges', () => {
  415. selection._setTo( [ range, liveRange ] );
  416. const spy = testUtils.sinon.spy( LiveRange.prototype, 'detach' );
  417. selection._setTo( root, 0 );
  418. expect( spy.calledTwice ).to.be.true;
  419. } );
  420. } );
  421. describe( '_setFocus()', () => {
  422. it( 'modifies default range', () => {
  423. const startPos = selection.getFirstPosition();
  424. const endPos = Position._createAt( root, 'end' );
  425. selection._setFocus( endPos );
  426. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'same' );
  427. expect( selection.focus.compareWith( endPos ) ).to.equal( 'same' );
  428. } );
  429. it( 'detaches the range it replaces', () => {
  430. const startPos = Position._createAt( root, 1 );
  431. const endPos = Position._createAt( root, 2 );
  432. const newEndPos = Position._createAt( root, 4 );
  433. const spy = testUtils.sinon.spy( LiveRange.prototype, 'detach' );
  434. selection._setTo( new Range( startPos, endPos ) );
  435. selection._setFocus( newEndPos );
  436. expect( spy.calledOnce ).to.be.true;
  437. } );
  438. it( 'refreshes attributes', () => {
  439. const spy = sinon.spy( selection._selection, '_updateAttributes' );
  440. selection._setFocus( Position._createAt( root, 1 ) );
  441. expect( spy.called ).to.be.true;
  442. } );
  443. } );
  444. describe( '_setTo() - remove all ranges', () => {
  445. let spy, ranges;
  446. beforeEach( () => {
  447. selection._setTo( [ liveRange, range ] );
  448. spy = sinon.spy();
  449. selection.on( 'change:range', spy );
  450. ranges = Array.from( selection._selection._ranges );
  451. sinon.spy( ranges[ 0 ], 'detach' );
  452. sinon.spy( ranges[ 1 ], 'detach' );
  453. selection._setTo( null );
  454. } );
  455. afterEach( () => {
  456. ranges[ 0 ].detach.restore();
  457. ranges[ 1 ].detach.restore();
  458. } );
  459. it( 'should remove all stored ranges (and reset to default range)', () => {
  460. expect( Array.from( selection.getRanges() ).length ).to.equal( 1 );
  461. expect( selection.anchor.isEqual( new Position( root, [ 0, 0 ] ) ) ).to.be.true;
  462. expect( selection.focus.isEqual( new Position( root, [ 0, 0 ] ) ) ).to.be.true;
  463. } );
  464. it( 'should detach ranges', () => {
  465. expect( ranges[ 0 ].detach.called ).to.be.true;
  466. expect( ranges[ 1 ].detach.called ).to.be.true;
  467. } );
  468. it( 'should refresh attributes', () => {
  469. const spy = sinon.spy( selection._selection, '_updateAttributes' );
  470. selection._setTo( null );
  471. expect( spy.called ).to.be.true;
  472. } );
  473. } );
  474. describe( '_setTo()', () => {
  475. it( 'should throw an error when range is invalid', () => {
  476. expect( () => {
  477. selection._setTo( [ { invalid: 'range' } ] );
  478. } ).to.throw( CKEditorError, /model-selection-set-ranges-not-range/ );
  479. } );
  480. it( 'should log a warning when trying to set selection to the graveyard', () => {
  481. // eslint-disable-next-line no-undef
  482. const warnStub = sinon.stub( console, 'warn' );
  483. const range = new Range( new Position( model.document.graveyard, [ 0 ] ) );
  484. selection._setTo( range );
  485. expect( warnStub.calledOnce ).to.equal( true );
  486. expect( warnStub.getCall( 0 ).args[ 0 ] ).to.match( /model-selection-range-in-graveyard/ );
  487. expect( selection._ranges ).to.deep.equal( [] );
  488. warnStub.restore();
  489. } );
  490. it( 'should detach removed ranges', () => {
  491. selection._setTo( [ liveRange, range ] );
  492. const oldRanges = Array.from( selection._selection._ranges );
  493. sinon.spy( oldRanges[ 0 ], 'detach' );
  494. sinon.spy( oldRanges[ 1 ], 'detach' );
  495. selection._setTo( [] );
  496. expect( oldRanges[ 0 ].detach.called ).to.be.true;
  497. expect( oldRanges[ 1 ].detach.called ).to.be.true;
  498. } );
  499. it( 'should refresh attributes', () => {
  500. const spy = sinon.spy( selection._selection, '_updateAttributes' );
  501. selection._setTo( [ range ] );
  502. expect( spy.called ).to.be.true;
  503. } );
  504. // See #630.
  505. it( 'should refresh attributes – integration test for #630', () => {
  506. model.schema.extend( '$text', { allowIn: '$root' } );
  507. setData( model, 'f<$text italic="true">[o</$text><$text bold="true">ob]a</$text>r' );
  508. selection._setTo( [ Range._createFromPositionAndShift( selection.getLastRange().end, 0 ) ] );
  509. expect( selection.getAttribute( 'bold' ) ).to.equal( true );
  510. expect( selection.hasAttribute( 'italic' ) ).to.equal( false );
  511. expect( getData( model ) )
  512. .to.equal( 'f<$text italic="true">o</$text><$text bold="true">ob[]a</$text>r' );
  513. } );
  514. } );
  515. describe( '_isStoreAttributeKey', () => {
  516. it( 'should return true if given key is a key of an attribute stored in element by DocumentSelection', () => {
  517. expect( DocumentSelection._isStoreAttributeKey( fooStoreAttrKey ) ).to.be.true;
  518. } );
  519. it( 'should return false if given key is not a key of an attribute stored in element by DocumentSelection', () => {
  520. expect( DocumentSelection._isStoreAttributeKey( 'foo' ) ).to.be.false;
  521. } );
  522. } );
  523. describe( 'attributes', () => {
  524. let fullP, emptyP, rangeInFullP, rangeInEmptyP;
  525. beforeEach( () => {
  526. root._removeChildren( 0, root.childCount );
  527. root._appendChild( [
  528. new Element( 'p', [], new Text( 'foobar' ) ),
  529. new Element( 'p', [], [] )
  530. ] );
  531. fullP = root.getChild( 0 );
  532. emptyP = root.getChild( 1 );
  533. rangeInFullP = new Range( new Position( root, [ 0, 4 ] ), new Position( root, [ 0, 4 ] ) );
  534. rangeInEmptyP = new Range( new Position( root, [ 1, 0 ] ), new Position( root, [ 1, 0 ] ) );
  535. // I've lost 30 mins debugging why my tests fail and that was due to the above code reusing
  536. // a root which wasn't empty (so the ranges didn't actually make much sense).
  537. expect( root.childCount ).to.equal( 2 );
  538. } );
  539. describe( '_setAttribute()', () => {
  540. it( 'should set attribute', () => {
  541. selection._setTo( [ rangeInEmptyP ] );
  542. selection._setAttribute( 'foo', 'bar' );
  543. expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  544. } );
  545. } );
  546. describe( '_removeAttribute()', () => {
  547. it( 'should remove attribute set on the text fragment', () => {
  548. selection._setTo( [ rangeInFullP ] );
  549. selection._setAttribute( 'foo', 'bar' );
  550. selection._removeAttribute( 'foo' );
  551. expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
  552. } );
  553. it( 'should prevent auto update of the attribute even if attribute is not preset yet', () => {
  554. selection._setTo( new Position( root, [ 0, 1 ] ) );
  555. // Remove "foo" attribute that is not present in selection yet.
  556. expect( selection.hasAttribute( 'foo' ) ).to.be.false;
  557. selection._removeAttribute( 'foo' );
  558. // Trigger selecton auto update on document change. It should not get attribute from surrounding text;
  559. model.change( writer => {
  560. writer.setAttribute( 'foo', 'bar', Range._createIn( fullP ) );
  561. } );
  562. expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
  563. } );
  564. } );
  565. describe( '_getStoredAttributes()', () => {
  566. it( 'should return no values if there are no ranges in selection', () => {
  567. const values = Array.from( selection._getStoredAttributes() );
  568. expect( values ).to.deep.equal( [] );
  569. } );
  570. } );
  571. describe( 'are updated on a direct range change', () => {
  572. beforeEach( () => {
  573. root._insertChild( 0, [
  574. new Element( 'p', { p: true } ),
  575. new Text( 'a', { a: true } ),
  576. new Element( 'p', { p: true } ),
  577. new Text( 'b', { b: true } ),
  578. new Text( 'c', { c: true } ),
  579. new Element( 'p', [], [
  580. new Text( 'd', { d: true } )
  581. ] ),
  582. new Element( 'p', { p: true } ),
  583. new Text( 'e', { e: true } )
  584. ] );
  585. } );
  586. it( 'if selection is a range, should find first character in it and copy it\'s attributes', () => {
  587. selection._setTo( [ new Range( new Position( root, [ 2 ] ), new Position( root, [ 5 ] ) ) ] );
  588. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'b', true ] ] );
  589. // Step into elements when looking for first character:
  590. selection._setTo( [ new Range( new Position( root, [ 5 ] ), new Position( root, [ 7 ] ) ) ] );
  591. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'd', true ] ] );
  592. } );
  593. it( 'if selection is collapsed it should seek a character to copy that character\'s attributes', () => {
  594. // Take styles from character before selection.
  595. selection._setTo( [ new Range( new Position( root, [ 2 ] ), new Position( root, [ 2 ] ) ) ] );
  596. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'a', true ] ] );
  597. // If there are none,
  598. // Take styles from character after selection.
  599. selection._setTo( [ new Range( new Position( root, [ 3 ] ), new Position( root, [ 3 ] ) ) ] );
  600. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'b', true ] ] );
  601. // If there are none,
  602. // Look from the selection position to the beginning of node looking for character to take attributes from.
  603. selection._setTo( [ new Range( new Position( root, [ 6 ] ), new Position( root, [ 6 ] ) ) ] );
  604. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'c', true ] ] );
  605. // If there are none,
  606. // Look from the selection position to the end of node looking for character to take attributes from.
  607. selection._setTo( [ new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) ) ] );
  608. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'a', true ] ] );
  609. // If there are no characters to copy attributes from, use stored attributes.
  610. selection._setTo( [ new Range( new Position( root, [ 0, 0 ] ), new Position( root, [ 0, 0 ] ) ) ] );
  611. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [] );
  612. } );
  613. it( 'should overwrite any previously set attributes', () => {
  614. selection._setTo( new Position( root, [ 5, 0 ] ) );
  615. selection._setAttribute( 'x', true );
  616. selection._setAttribute( 'y', true );
  617. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'd', true ], [ 'x', true ], [ 'y', true ] ] );
  618. selection._setTo( new Position( root, [ 1 ] ) );
  619. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'a', true ] ] );
  620. } );
  621. it( 'should fire change:attribute event', () => {
  622. const spy = sinon.spy();
  623. selection.on( 'change:attribute', spy );
  624. selection._setTo( [ new Range( new Position( root, [ 2 ] ), new Position( root, [ 5 ] ) ) ] );
  625. expect( spy.calledOnce ).to.be.true;
  626. } );
  627. it( 'should not fire change:attribute event if attributes did not change', () => {
  628. selection._setTo( new Position( root, [ 5, 0 ] ) );
  629. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'd', true ] ] );
  630. const spy = sinon.spy();
  631. selection.on( 'change:attribute', spy );
  632. selection._setTo( new Position( root, [ 5, 1 ] ) );
  633. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'd', true ] ] );
  634. expect( spy.called ).to.be.false;
  635. } );
  636. } );
  637. // #986
  638. describe( 'are not inherited from the inside of object elements', () => {
  639. beforeEach( () => {
  640. model.schema.register( 'image', {
  641. isObject: true
  642. } );
  643. model.schema.extend( 'image', { allowIn: '$root' } );
  644. model.schema.extend( 'image', { allowIn: '$block' } );
  645. model.schema.register( 'caption' );
  646. model.schema.extend( 'caption', { allowIn: 'image' } );
  647. model.schema.extend( '$text', {
  648. allowIn: [ 'image', 'caption' ],
  649. allowAttributes: 'bold'
  650. } );
  651. } );
  652. it( 'ignores attributes inside an object if selection contains that object', () => {
  653. setData( model, '<p>[<image><$text bold="true">Caption for the image.</$text></image>]</p>' );
  654. expect( selection.hasAttribute( 'bold' ) ).to.equal( false );
  655. } );
  656. it( 'ignores attributes inside an object if selection contains that object (deeper structure)', () => {
  657. setData( model, '<p>[<image><caption><$text bold="true">Caption for the image.</$text></caption></image>]</p>' );
  658. expect( selection.hasAttribute( 'bold' ) ).to.equal( false );
  659. } );
  660. it( 'ignores attributes inside an object if selection contains that object (block level)', () => {
  661. setData( model, '<p>foo</p>[<image><$text bold="true">Caption for the image.</$text></image>]<p>foo</p>' );
  662. expect( selection.hasAttribute( 'bold' ) ).to.equal( false );
  663. } );
  664. it( 'reads attributes from text even if the selection contains an object', () => {
  665. setData( model, '<p>x<$text bold="true">[bar</$text><image></image>foo]</p>' );
  666. expect( selection.getAttribute( 'bold' ) ).to.equal( true );
  667. } );
  668. it( 'reads attributes when the entire selection inside an object', () => {
  669. setData( model, '<p><image><caption><$text bold="true">[bar]</$text></caption></image></p>' );
  670. expect( selection.getAttribute( 'bold' ) ).to.equal( true );
  671. } );
  672. it( 'stops reading attributes if selection starts with an object', () => {
  673. setData( model, '<p>[<image></image><$text bold="true">bar]</$text></p>' );
  674. expect( selection.hasAttribute( 'bold' ) ).to.equal( false );
  675. } );
  676. } );
  677. describe( 'parent element\'s attributes', () => {
  678. it( 'are set using a normal batch', () => {
  679. const batchTypes = [];
  680. model.on( 'applyOperation', ( event, args ) => {
  681. const operation = args[ 0 ];
  682. const batch = operation.batch;
  683. batchTypes.push( batch.type );
  684. } );
  685. selection._setTo( [ rangeInEmptyP ] );
  686. model.change( writer => {
  687. writer.setSelectionAttribute( 'foo', 'bar' );
  688. } );
  689. expect( batchTypes ).to.deep.equal( [ 'default' ] );
  690. expect( emptyP.getAttribute( fooStoreAttrKey ) ).to.equal( 'bar' );
  691. } );
  692. it( 'are removed when any content is inserted (reuses the same batch)', () => {
  693. // Dedupe batches by using a map (multiple change events will be fired).
  694. const batchTypes = new Map();
  695. selection._setTo( rangeInEmptyP );
  696. selection._setAttribute( 'foo', 'bar' );
  697. selection._setAttribute( 'abc', 'bar' );
  698. model.on( 'applyOperation', ( event, args ) => {
  699. const operation = args[ 0 ];
  700. const batch = operation.batch;
  701. batchTypes.set( batch, batch.type );
  702. } );
  703. model.change( writer => {
  704. writer.insertText( 'x', rangeInEmptyP.start );
  705. } );
  706. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  707. expect( emptyP.hasAttribute( abcStoreAttrKey ) ).to.be.false;
  708. expect( Array.from( batchTypes.values() ) ).to.deep.equal( [ 'default' ] );
  709. } );
  710. it( 'are removed when any content is moved into', () => {
  711. selection._setTo( rangeInEmptyP );
  712. selection._setAttribute( 'foo', 'bar' );
  713. model.change( writer => {
  714. writer.move( writer.createRangeOn( fullP.getChild( 0 ) ), rangeInEmptyP.start );
  715. } );
  716. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  717. } );
  718. it( 'are removed when containing element is merged with a non-empty element', () => {
  719. const emptyP2 = new Element( 'p', null, 'x' );
  720. root._appendChild( emptyP2 );
  721. emptyP._setAttribute( fooStoreAttrKey, 'bar' );
  722. emptyP2._setAttribute( fooStoreAttrKey, 'bar' );
  723. model.change( writer => {
  724. // <emptyP>{}<emptyP2>
  725. writer.merge( writer.createPositionAfter( emptyP ) );
  726. } );
  727. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  728. expect( emptyP.parent ).to.equal( root ); // Just to be sure we're checking the right element.
  729. } );
  730. it( 'are removed even when there is no selection in it', () => {
  731. emptyP._setAttribute( fooStoreAttrKey, 'bar' );
  732. selection._setTo( [ rangeInFullP ] );
  733. model.change( writer => {
  734. writer.insertText( 'x', rangeInEmptyP.start );
  735. } );
  736. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  737. } );
  738. it( 'uses model change to clear attributes', () => {
  739. selection._setTo( [ rangeInEmptyP ] );
  740. model.change( writer => {
  741. writer.setSelectionAttribute( 'foo', 'bar' );
  742. writer.insertText( 'x', rangeInEmptyP.start );
  743. // `emptyP` still has the attribute, because attribute clearing is in enqueued block.
  744. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.true;
  745. } );
  746. // When the dust settles, `emptyP` should not have the attribute.
  747. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  748. } );
  749. it( 'are not removed or merged when containing element is merged with another empty element', () => {
  750. const emptyP2 = new Element( 'p', null );
  751. root._appendChild( emptyP2 );
  752. emptyP._setAttribute( fooStoreAttrKey, 'bar' );
  753. emptyP2._setAttribute( abcStoreAttrKey, 'bar' );
  754. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.true;
  755. expect( emptyP.hasAttribute( abcStoreAttrKey ) ).to.be.false;
  756. model.change( writer => {
  757. // <emptyP>{}<emptyP2>
  758. writer.merge( writer.createPositionAfter( emptyP ) );
  759. } );
  760. expect( emptyP.getAttribute( fooStoreAttrKey ) ).to.equal( 'bar' );
  761. expect( emptyP.parent ).to.equal( root ); // Just to be sure we're checking the right element.
  762. } );
  763. // Rename and some other operations don't specify range in doc#change event.
  764. // So let's see if there's no crash or something.
  765. it( 'are not removed on rename', () => {
  766. model.change( writer => {
  767. writer.setSelection( rangeInEmptyP );
  768. writer.setSelectionAttribute( 'foo', 'bar' );
  769. } );
  770. sinon.spy( model, 'enqueueChange' );
  771. model.change( writer => {
  772. writer.rename( emptyP, 'pnew' );
  773. } );
  774. expect( model.enqueueChange.called ).to.be.false;
  775. expect( emptyP.getAttribute( fooStoreAttrKey ) ).to.equal( 'bar' );
  776. } );
  777. } );
  778. } );
  779. describe( '_overrideGravity()', () => {
  780. beforeEach( () => {
  781. model.schema.extend( '$text', {
  782. allowIn: '$root'
  783. } );
  784. } );
  785. it( 'should return the UID', () => {
  786. const overrideUidA = selection._overrideGravity();
  787. const overrideUidB = selection._overrideGravity();
  788. expect( overrideUidA ).to.be.a( 'string' );
  789. expect( overrideUidB ).to.be.a( 'string' );
  790. expect( overrideUidA ).to.not.equal( overrideUidB );
  791. } );
  792. it( 'should mark gravity as overridden', () => {
  793. expect( selection.isGravityOverridden ).to.false;
  794. selection._overrideGravity();
  795. expect( selection.isGravityOverridden ).to.true;
  796. } );
  797. it( 'should not inherit attributes from node before the caret', () => {
  798. setData( model, '<$text bold="true" italic="true">foo[]</$text>' );
  799. expect( Array.from( selection.getAttributeKeys() ) ).to.have.members( [ 'bold', 'italic' ] );
  800. selection._overrideGravity();
  801. expect( Array.from( selection.getAttributeKeys() ) ).to.length( 0 );
  802. } );
  803. it( 'should inherit attributes from node after the caret', () => {
  804. setData( model, '<$text>foo[]</$text><$text bold="true" italic="true">bar</$text>' );
  805. expect( Array.from( selection.getAttributeKeys() ) ).to.length( 0 );
  806. selection._overrideGravity();
  807. expect( Array.from( selection.getAttributeKeys() ) ).to.have.members( [ 'bold', 'italic' ] );
  808. } );
  809. it( 'should not retain attributes that are set explicitly', () => {
  810. setData( model, '<$text italic="true">foo[]</$text>' );
  811. selection._setAttribute( 'bold', true );
  812. expect( Array.from( selection.getAttributeKeys() ) ).to.have.members( [ 'bold', 'italic' ] );
  813. selection._overrideGravity();
  814. expect( Array.from( selection.getAttributeKeys() ) ).to.have.members( [] );
  815. } );
  816. it( 'should retain overridden until selection will not change range by a direct change', () => {
  817. setData( model, '<$text bold="true" italic="true">foo[]</$text><$text italic="true">bar</$text>' );
  818. selection._overrideGravity();
  819. // Changed range but not directly.
  820. model.change( writer => writer.insertText( 'abc', new Position( root, [ 0 ] ) ) );
  821. expect( Array.from( selection.getAttributeKeys() ) ).to.have.members( [ 'italic' ] );
  822. // Changed range directly.
  823. model.change( writer => writer.setSelection( new Position( root, [ 5 ] ) ) );
  824. expect( Array.from( selection.getAttributeKeys() ) ).to.have.members( [ 'bold', 'italic' ] );
  825. } );
  826. } );
  827. describe( '_restoreGravity()', () => {
  828. beforeEach( () => {
  829. model.schema.extend( '$text', {
  830. allowIn: '$root'
  831. } );
  832. } );
  833. it( 'should throw when a wrong, already restored, or no UID provided', () => {
  834. const overrideUid = selection._overrideGravity();
  835. selection._restoreGravity( overrideUid );
  836. // Wrong UID
  837. expect( () => {
  838. selection._restoreGravity( 'foo' );
  839. } ).to.throw( CKEditorError, /^document-selection-gravity-wrong-restore/ );
  840. // Already restored
  841. expect( () => {
  842. selection._restoreGravity( overrideUid );
  843. } ).to.throw( CKEditorError, /^document-selection-gravity-wrong-restore/ );
  844. // No UID
  845. expect( () => {
  846. selection._restoreGravity();
  847. } ).to.throw( CKEditorError, /^document-selection-gravity-wrong-restore/ );
  848. } );
  849. it( 'should revert default gravity when is overridden', () => {
  850. setData( model, '<$text bold="true" italic="true">foo[]</$text>' );
  851. const overrideUid = selection._overrideGravity();
  852. expect( Array.from( selection.getAttributeKeys() ) ).to.length( 0 );
  853. expect( selection.isGravityOverridden ).to.true;
  854. selection._restoreGravity( overrideUid );
  855. expect( Array.from( selection.getAttributeKeys() ) ).to.have.members( [ 'bold', 'italic' ] );
  856. expect( selection.isGravityOverridden ).to.false;
  857. } );
  858. it( 'should be called the same number of times as gravity is overridden to restore it', () => {
  859. setData( model, '<$text bold="true" italic="true">foo[]</$text>' );
  860. const overrideUidA = selection._overrideGravity();
  861. const overrideUidB = selection._overrideGravity();
  862. expect( selection.isGravityOverridden ).to.true;
  863. selection._restoreGravity( overrideUidA );
  864. expect( selection.isGravityOverridden ).to.true;
  865. selection._restoreGravity( overrideUidB );
  866. expect( selection.isGravityOverridden ).to.false;
  867. } );
  868. } );
  869. // DocumentSelection uses LiveRanges so here are only simple test to see if integration is
  870. // working well, without getting into complicated corner cases.
  871. describe( 'after applying an operation should get updated and fire events', () => {
  872. let spyRange;
  873. beforeEach( () => {
  874. root._removeChildren( 0, root.childCount );
  875. root._insertChild( 0, [
  876. new Element( 'p', [], new Text( 'abcdef' ) ),
  877. new Element( 'p', [], new Text( 'foobar' ) ),
  878. new Text( 'xyz' )
  879. ] );
  880. selection._setTo( new Range( new Position( root, [ 0, 2 ] ), new Position( root, [ 1, 4 ] ) ) );
  881. spyRange = sinon.spy();
  882. selection.on( 'change:range', spyRange );
  883. } );
  884. describe( 'InsertOperation', () => {
  885. it( 'before selection', () => {
  886. selection.on( 'change:range', ( evt, data ) => {
  887. expect( data.directChange ).to.be.false;
  888. } );
  889. model.applyOperation(
  890. new InsertOperation(
  891. new Position( root, [ 0, 1 ] ),
  892. 'xyz',
  893. doc.version
  894. )
  895. );
  896. const range = selection.getFirstRange();
  897. expect( range.start.path ).to.deep.equal( [ 0, 5 ] );
  898. expect( range.end.path ).to.deep.equal( [ 1, 4 ] );
  899. expect( spyRange.calledOnce ).to.be.true;
  900. } );
  901. it( 'inside selection', () => {
  902. selection.on( 'change:range', ( evt, data ) => {
  903. expect( data.directChange ).to.be.false;
  904. } );
  905. model.applyOperation(
  906. new InsertOperation(
  907. new Position( root, [ 1, 0 ] ),
  908. 'xyz',
  909. doc.version
  910. )
  911. );
  912. const range = selection.getFirstRange();
  913. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  914. expect( range.end.path ).to.deep.equal( [ 1, 7 ] );
  915. expect( spyRange.calledOnce ).to.be.true;
  916. } );
  917. } );
  918. describe( 'MoveOperation', () => {
  919. it( 'move range from before a selection', () => {
  920. selection.on( 'change:range', ( evt, data ) => {
  921. expect( data.directChange ).to.be.false;
  922. } );
  923. model.applyOperation(
  924. new MoveOperation(
  925. new Position( root, [ 0, 0 ] ),
  926. 2,
  927. new Position( root, [ 2 ] ),
  928. doc.version
  929. )
  930. );
  931. const range = selection.getFirstRange();
  932. expect( range.start.path ).to.deep.equal( [ 0, 0 ] );
  933. expect( range.end.path ).to.deep.equal( [ 1, 4 ] );
  934. expect( spyRange.calledOnce ).to.be.true;
  935. } );
  936. it( 'moved into before a selection', () => {
  937. selection.on( 'change:range', ( evt, data ) => {
  938. expect( data.directChange ).to.be.false;
  939. } );
  940. model.applyOperation(
  941. new MoveOperation(
  942. new Position( root, [ 2 ] ),
  943. 2,
  944. new Position( root, [ 0, 0 ] ),
  945. doc.version
  946. )
  947. );
  948. const range = selection.getFirstRange();
  949. expect( range.start.path ).to.deep.equal( [ 0, 4 ] );
  950. expect( range.end.path ).to.deep.equal( [ 1, 4 ] );
  951. expect( spyRange.calledOnce ).to.be.true;
  952. } );
  953. it( 'move range from inside of selection', () => {
  954. selection.on( 'change:range', ( evt, data ) => {
  955. expect( data.directChange ).to.be.false;
  956. } );
  957. model.applyOperation(
  958. new MoveOperation(
  959. new Position( root, [ 1, 0 ] ),
  960. 2,
  961. new Position( root, [ 2 ] ),
  962. doc.version
  963. )
  964. );
  965. const range = selection.getFirstRange();
  966. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  967. expect( range.end.path ).to.deep.equal( [ 1, 2 ] );
  968. expect( spyRange.calledOnce ).to.be.true;
  969. } );
  970. it( 'moved range intersects with selection', () => {
  971. selection.on( 'change:range', ( evt, data ) => {
  972. expect( data.directChange ).to.be.false;
  973. } );
  974. model.applyOperation(
  975. new MoveOperation(
  976. new Position( root, [ 1, 3 ] ),
  977. 2,
  978. new Position( root, [ 4 ] ),
  979. doc.version
  980. )
  981. );
  982. const range = selection.getFirstRange();
  983. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  984. expect( range.end.path ).to.deep.equal( [ 1, 3 ] );
  985. expect( spyRange.calledOnce ).to.be.true;
  986. } );
  987. it( 'split inside selection (do not break selection)', () => {
  988. selection.on( 'change:range', ( evt, data ) => {
  989. expect( data.directChange ).to.be.false;
  990. } );
  991. const batch = new Batch();
  992. const splitOperation = new SplitOperation( new Position( root, [ 1, 2 ] ), 4, null, 0 );
  993. batch.addOperation( splitOperation );
  994. model.applyOperation( splitOperation );
  995. const range = selection.getFirstRange();
  996. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  997. expect( range.end.path ).to.deep.equal( [ 2, 2 ] );
  998. expect( spyRange.calledOnce ).to.be.true;
  999. } );
  1000. } );
  1001. describe( 'AttributeOperation', () => {
  1002. it( 'changed range includes selection anchor', () => {
  1003. const spyAttribute = sinon.spy();
  1004. selection.on( 'change:attribute', spyAttribute );
  1005. selection.on( 'change:attribute', ( evt, data ) => {
  1006. expect( data.directChange ).to.be.false;
  1007. expect( data.attributeKeys ).to.deep.equal( [ 'foo' ] );
  1008. } );
  1009. model.applyOperation(
  1010. new AttributeOperation(
  1011. new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 5 ] ) ),
  1012. 'foo',
  1013. null,
  1014. 'bar',
  1015. doc.version
  1016. )
  1017. );
  1018. // Attributes are auto updated on document change.
  1019. model.change( () => {} );
  1020. expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  1021. expect( spyAttribute.calledOnce ).to.be.true;
  1022. } );
  1023. it( 'should not overwrite previously set attributes', () => {
  1024. selection._setAttribute( 'foo', 'xyz' );
  1025. const spyAttribute = sinon.spy();
  1026. selection.on( 'change:attribute', spyAttribute );
  1027. model.applyOperation(
  1028. new AttributeOperation(
  1029. new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 5 ] ) ),
  1030. 'foo',
  1031. null,
  1032. 'bar',
  1033. doc.version
  1034. )
  1035. );
  1036. expect( selection.getAttribute( 'foo' ) ).to.equal( 'xyz' );
  1037. expect( spyAttribute.called ).to.be.false;
  1038. } );
  1039. it( 'should not overwrite previously set attributes with same values', () => {
  1040. selection._setAttribute( 'foo', 'xyz' );
  1041. const spyAttribute = sinon.spy();
  1042. selection.on( 'change:attribute', spyAttribute );
  1043. model.applyOperation(
  1044. new AttributeOperation(
  1045. new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 5 ] ) ),
  1046. 'foo',
  1047. null,
  1048. 'xyz',
  1049. doc.version
  1050. )
  1051. );
  1052. expect( selection.getAttribute( 'foo' ) ).to.equal( 'xyz' );
  1053. expect( spyAttribute.called ).to.be.false;
  1054. } );
  1055. it( 'should not overwrite previously removed attributes', () => {
  1056. selection._setAttribute( 'foo', 'xyz' );
  1057. selection._removeAttribute( 'foo' );
  1058. const spyAttribute = sinon.spy();
  1059. selection.on( 'change:attribute', spyAttribute );
  1060. model.applyOperation(
  1061. new AttributeOperation(
  1062. new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 5 ] ) ),
  1063. 'foo',
  1064. null,
  1065. 'bar',
  1066. doc.version
  1067. )
  1068. );
  1069. expect( selection.hasAttribute( 'foo' ) ).to.be.false;
  1070. expect( spyAttribute.called ).to.be.false;
  1071. } );
  1072. } );
  1073. describe( 'MoveOperation to graveyard', () => {
  1074. it( 'fix selection range if it ends up in graveyard #1', () => {
  1075. selection._setTo( new Position( root, [ 1, 3 ] ) );
  1076. model.applyOperation(
  1077. new MoveOperation(
  1078. new Position( root, [ 1, 2 ] ),
  1079. 2,
  1080. new Position( doc.graveyard, [ 0 ] ),
  1081. doc.version
  1082. )
  1083. );
  1084. expect( selection.getFirstPosition().path ).to.deep.equal( [ 1, 2 ] );
  1085. } );
  1086. it( 'fix selection range if it ends up in graveyard #2', () => {
  1087. selection._setTo( [ new Range( new Position( root, [ 1, 2 ] ), new Position( root, [ 1, 4 ] ) ) ] );
  1088. model.applyOperation(
  1089. new MoveOperation(
  1090. new Position( root, [ 1, 2 ] ),
  1091. 2,
  1092. new Position( doc.graveyard, [ 0 ] ),
  1093. doc.version
  1094. )
  1095. );
  1096. expect( selection.getFirstPosition().path ).to.deep.equal( [ 1, 2 ] );
  1097. } );
  1098. it( 'fix selection range if it ends up in graveyard #3', () => {
  1099. selection._setTo( [ new Range( new Position( root, [ 1, 1 ] ), new Position( root, [ 1, 2 ] ) ) ] );
  1100. model.applyOperation(
  1101. new MoveOperation(
  1102. new Position( root, [ 1 ] ),
  1103. 2,
  1104. new Position( doc.graveyard, [ 0 ] ),
  1105. doc.version
  1106. )
  1107. );
  1108. expect( selection.getFirstPosition().path ).to.deep.equal( [ 0, 6 ] );
  1109. } );
  1110. it( 'fix selection range if it ends up in graveyard #4 - whole content removed', () => {
  1111. model.applyOperation(
  1112. new MoveOperation(
  1113. new Position( root, [ 0 ] ),
  1114. 3,
  1115. new Position( doc.graveyard, [ 0 ] ),
  1116. doc.version
  1117. )
  1118. );
  1119. expect( selection.getFirstPosition().path ).to.deep.equal( [ 0 ] );
  1120. model.applyOperation(
  1121. new InsertOperation(
  1122. new Position( root, [ 0 ] ),
  1123. new Element( 'p' ),
  1124. doc.version
  1125. )
  1126. );
  1127. // Now it's clear that it's the default range.
  1128. expect( selection.getFirstPosition().path ).to.deep.equal( [ 0, 0 ] );
  1129. } );
  1130. } );
  1131. it( '`DocumentSelection#change:range` event should be fire once even if selection contains multi-ranges', () => {
  1132. root._removeChildren( 0, root.childCount );
  1133. root._insertChild( 0, [
  1134. new Element( 'p', [], new Text( 'abcdef' ) ),
  1135. new Element( 'p', [], new Text( 'foobar' ) ),
  1136. new Text( 'xyz #2' )
  1137. ] );
  1138. selection._setTo( [
  1139. Range._createIn( root.getNodeByPath( [ 0 ] ) ),
  1140. Range._createIn( root.getNodeByPath( [ 1 ] ) )
  1141. ] );
  1142. spyRange = sinon.spy();
  1143. selection.on( 'change:range', spyRange );
  1144. model.applyOperation(
  1145. new InsertOperation(
  1146. new Position( root, [ 0 ] ),
  1147. 'xyz #1',
  1148. doc.version
  1149. )
  1150. );
  1151. expect( spyRange.calledOnce ).to.be.true;
  1152. } );
  1153. } );
  1154. it( 'should throw if one of ranges starts or ends inside surrogate pair', () => {
  1155. root._removeChildren( 0, root.childCount );
  1156. root._appendChild( '\uD83D\uDCA9' );
  1157. expect( () => {
  1158. doc.selection._setTo( new Range( Position._createAt( root, 0 ), Position._createAt( root, 1 ) ) );
  1159. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  1160. expect( () => {
  1161. doc.selection._setTo( new Range( Position._createAt( root, 1 ), Position._createAt( root, 2 ) ) );
  1162. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  1163. } );
  1164. it( 'should throw if one of ranges starts or ends between base character and combining mark', () => {
  1165. root._removeChildren( 0, root.childCount );
  1166. root._appendChild( 'foo̻̐ͩbar' );
  1167. expect( () => {
  1168. doc.selection._setTo( new Range( Position._createAt( root, 3 ), Position._createAt( root, 9 ) ) );
  1169. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  1170. expect( () => {
  1171. doc.selection._setTo( new Range( Position._createAt( root, 4 ), Position._createAt( root, 9 ) ) );
  1172. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  1173. expect( () => {
  1174. doc.selection._setTo( new Range( Position._createAt( root, 5 ), Position._createAt( root, 9 ) ) );
  1175. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  1176. expect( () => {
  1177. doc.selection._setTo( new Range( Position._createAt( root, 1 ), Position._createAt( root, 3 ) ) );
  1178. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  1179. expect( () => {
  1180. doc.selection._setTo( new Range( Position._createAt( root, 1 ), Position._createAt( root, 4 ) ) );
  1181. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  1182. expect( () => {
  1183. doc.selection._setTo( new Range( Position._createAt( root, 1 ), Position._createAt( root, 5 ) ) );
  1184. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  1185. } );
  1186. } );