documentselection.js 51 KB

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