documentselection.js 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634
  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. /* globals console */
  6. import Model from '../../src/model/model';
  7. import Batch from '../../src/model/batch';
  8. import Element from '../../src/model/element';
  9. import Text from '../../src/model/text';
  10. import Range from '../../src/model/range';
  11. import Position from '../../src/model/position';
  12. import LiveRange from '../../src/model/liverange';
  13. import DocumentSelection from '../../src/model/documentselection';
  14. import InsertOperation from '../../src/model/operation/insertoperation';
  15. import MoveOperation from '../../src/model/operation/moveoperation';
  16. import AttributeOperation from '../../src/model/operation/attributeoperation';
  17. import SplitOperation from '../../src/model/operation/splitoperation';
  18. import Collection from '@ckeditor/ckeditor5-utils/src/collection';
  19. import count from '@ckeditor/ckeditor5-utils/src/count';
  20. import { setData, getData } from '../../src/dev-utils/model';
  21. import { expectToThrowCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  22. describe( 'DocumentSelection', () => {
  23. let model, doc, root, selection, liveRange, range;
  24. const fooStoreAttrKey = DocumentSelection._getStoreAttributeKey( 'foo' );
  25. const abcStoreAttrKey = DocumentSelection._getStoreAttributeKey( 'abc' );
  26. beforeEach( () => {
  27. model = new Model();
  28. doc = model.document;
  29. root = doc.createRoot();
  30. root._appendChild( [
  31. new Element( 'p' ),
  32. new Element( 'p' ),
  33. new Element( 'p', [], new Text( 'foobar' ) ),
  34. new Element( 'p' ),
  35. new Element( 'p' ),
  36. new Element( 'p' ),
  37. new Element( 'p', [], new Text( 'foobar' ) )
  38. ] );
  39. selection = doc.selection;
  40. model.schema.register( 'p', { inheritAllFrom: '$block' } );
  41. model.schema.register( 'widget', {
  42. isObject: true
  43. } );
  44. liveRange = new LiveRange( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) );
  45. range = new Range( new Position( root, [ 2 ] ), new Position( root, [ 2, 2 ] ) );
  46. } );
  47. afterEach( () => {
  48. sinon.restore();
  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. } );
  374. } );
  375. describe( 'getFirstRange()', () => {
  376. it( 'should return default range if no ranges were added', () => {
  377. const firstRange = selection.getFirstRange();
  378. expect( firstRange.start.isEqual( new Position( root, [ 0, 0 ] ) ) );
  379. expect( firstRange.end.isEqual( new Position( root, [ 0, 0 ] ) ) );
  380. } );
  381. } );
  382. describe( 'getLastRange()', () => {
  383. it( 'should return default range if no ranges were added', () => {
  384. const lastRange = selection.getLastRange();
  385. expect( lastRange.start.isEqual( new Position( root, [ 0, 0 ] ) ) );
  386. expect( lastRange.end.isEqual( new Position( root, [ 0, 0 ] ) ) );
  387. } );
  388. } );
  389. describe( 'getSelectedElement()', () => {
  390. it( 'should return selected element', () => {
  391. selection._setTo( liveRange );
  392. const p = root.getChild( 0 );
  393. expect( selection.getSelectedElement() ).to.equal( p );
  394. } );
  395. } );
  396. describe( 'is()', () => {
  397. it( 'should return true for selection', () => {
  398. expect( selection.is( 'selection' ) ).to.be.true;
  399. expect( selection.is( 'model:selection' ) ).to.be.true;
  400. } );
  401. it( 'should return true for documentSelection', () => {
  402. expect( selection.is( 'documentSelection' ) ).to.be.true;
  403. expect( selection.is( 'model: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( 'model:node' ) ).to.be.false;
  408. expect( selection.is( 'text' ) ).to.be.false;
  409. expect( selection.is( 'textProxy' ) ).to.be.false;
  410. expect( selection.is( 'element' ) ).to.be.false;
  411. expect( selection.is( 'element', 'paragraph' ) ).to.be.false;
  412. expect( selection.is( 'rootElement' ) ).to.be.false;
  413. expect( selection.is( 'view:selection' ) ).to.be.false;
  414. expect( selection.is( 'view:documentSelection' ) ).to.be.false;
  415. } );
  416. } );
  417. describe( '_setTo() - set collapsed at', () => {
  418. it( 'detaches all existing ranges', () => {
  419. selection._setTo( [ range, liveRange ] );
  420. const spy = sinon.spy( LiveRange.prototype, 'detach' );
  421. selection._setTo( root, 0 );
  422. expect( spy.calledTwice ).to.be.true;
  423. } );
  424. } );
  425. describe( '_setFocus()', () => {
  426. it( 'modifies default range', () => {
  427. const startPos = selection.getFirstPosition();
  428. const endPos = Position._createAt( root, 'end' );
  429. selection._setFocus( endPos );
  430. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'same' );
  431. expect( selection.focus.compareWith( endPos ) ).to.equal( 'same' );
  432. } );
  433. it( 'detaches the range it replaces', () => {
  434. const startPos = Position._createAt( root, 1 );
  435. const endPos = Position._createAt( root, 2 );
  436. const newEndPos = Position._createAt( root, 4 );
  437. const spy = sinon.spy( LiveRange.prototype, 'detach' );
  438. selection._setTo( new Range( startPos, endPos ) );
  439. selection._setFocus( newEndPos );
  440. expect( spy.calledOnce ).to.be.true;
  441. } );
  442. it( 'refreshes attributes', () => {
  443. const spy = sinon.spy( selection._selection, '_updateAttributes' );
  444. selection._setFocus( Position._createAt( root, 1 ) );
  445. expect( spy.called ).to.be.true;
  446. } );
  447. } );
  448. describe( '_setTo() - remove all ranges', () => {
  449. let spy, ranges;
  450. beforeEach( () => {
  451. selection._setTo( [ liveRange, range ] );
  452. spy = sinon.spy();
  453. selection.on( 'change:range', spy );
  454. ranges = Array.from( selection._selection._ranges );
  455. sinon.spy( ranges[ 0 ], 'detach' );
  456. sinon.spy( ranges[ 1 ], 'detach' );
  457. selection._setTo( null );
  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. expectToThrowCKEditorError( () => {
  477. selection._setTo( [ { invalid: 'range' } ] );
  478. }, /model-selection-set-ranges-not-range/, model );
  479. } );
  480. it( 'should do nothing when trying to set selection to the graveyard', () => {
  481. // Catches the 'Trying to add a Range that is in the graveyard root. Range rejected.' warning in the CK_DEBUG mode.
  482. sinon.stub( console, 'warn' );
  483. const range = new Range( new Position( model.document.graveyard, [ 0 ] ) );
  484. selection._setTo( range );
  485. expect( selection._ranges ).to.deep.equal( [] );
  486. } );
  487. it( 'should detach removed ranges', () => {
  488. selection._setTo( [ liveRange, range ] );
  489. const oldRanges = Array.from( selection._selection._ranges );
  490. sinon.spy( oldRanges[ 0 ], 'detach' );
  491. sinon.spy( oldRanges[ 1 ], 'detach' );
  492. selection._setTo( [] );
  493. expect( oldRanges[ 0 ].detach.called ).to.be.true;
  494. expect( oldRanges[ 1 ].detach.called ).to.be.true;
  495. } );
  496. it( 'should refresh attributes', () => {
  497. const spy = sinon.spy( selection._selection, '_updateAttributes' );
  498. selection._setTo( [ range ] );
  499. expect( spy.called ).to.be.true;
  500. } );
  501. // See #630.
  502. it( 'should refresh attributes – integration test for #630', () => {
  503. model.schema.extend( '$text', { allowIn: '$root' } );
  504. setData( model, 'f<$text italic="true">[o</$text><$text bold="true">ob]a</$text>r' );
  505. selection._setTo( [ Range._createFromPositionAndShift( selection.getLastRange().end, 0 ) ] );
  506. expect( selection.getAttribute( 'bold' ) ).to.equal( true );
  507. expect( selection.hasAttribute( 'italic' ) ).to.equal( false );
  508. expect( getData( model ) )
  509. .to.equal( 'f<$text italic="true">o</$text><$text bold="true">ob[]a</$text>r' );
  510. } );
  511. } );
  512. describe( '_isStoreAttributeKey', () => {
  513. it( 'should return true if given key is a key of an attribute stored in element by DocumentSelection', () => {
  514. expect( DocumentSelection._isStoreAttributeKey( fooStoreAttrKey ) ).to.be.true;
  515. } );
  516. it( 'should return false if given key is not a key of an attribute stored in element by DocumentSelection', () => {
  517. expect( DocumentSelection._isStoreAttributeKey( 'foo' ) ).to.be.false;
  518. } );
  519. } );
  520. describe( 'attributes', () => {
  521. let fullP, emptyP, rangeInFullP, rangeInEmptyP;
  522. beforeEach( () => {
  523. root._removeChildren( 0, root.childCount );
  524. root._appendChild( [
  525. new Element( 'p', [], new Text( 'foobar' ) ),
  526. new Element( 'p', [], [] )
  527. ] );
  528. fullP = root.getChild( 0 );
  529. emptyP = root.getChild( 1 );
  530. rangeInFullP = new Range( new Position( root, [ 0, 4 ] ), new Position( root, [ 0, 4 ] ) );
  531. rangeInEmptyP = new Range( new Position( root, [ 1, 0 ] ), new Position( root, [ 1, 0 ] ) );
  532. // I've lost 30 mins debugging why my tests fail and that was due to the above code reusing
  533. // a root which wasn't empty (so the ranges didn't actually make much sense).
  534. expect( root.childCount ).to.equal( 2 );
  535. } );
  536. describe( '_setAttribute()', () => {
  537. it( 'should set attribute', () => {
  538. selection._setTo( [ rangeInEmptyP ] );
  539. selection._setAttribute( 'foo', 'bar' );
  540. expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  541. } );
  542. } );
  543. describe( '_removeAttribute()', () => {
  544. it( 'should remove attribute set on the text fragment', () => {
  545. selection._setTo( [ rangeInFullP ] );
  546. selection._setAttribute( 'foo', 'bar' );
  547. selection._removeAttribute( 'foo' );
  548. expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
  549. } );
  550. it( 'should prevent auto update of the attribute even if attribute is not preset yet', () => {
  551. selection._setTo( new Position( root, [ 0, 1 ] ) );
  552. // Remove "foo" attribute that is not present in selection yet.
  553. expect( selection.hasAttribute( 'foo' ) ).to.be.false;
  554. selection._removeAttribute( 'foo' );
  555. // Trigger selecton auto update on document change. It should not get attribute from surrounding text;
  556. model.change( writer => {
  557. writer.setAttribute( 'foo', 'bar', Range._createIn( fullP ) );
  558. } );
  559. expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
  560. } );
  561. } );
  562. describe( '_getStoredAttributes()', () => {
  563. it( 'should return no values if there are no ranges in selection', () => {
  564. const values = Array.from( selection._getStoredAttributes() );
  565. expect( values ).to.deep.equal( [] );
  566. } );
  567. } );
  568. describe( 'are updated on a direct range change', () => {
  569. beforeEach( () => {
  570. root._insertChild( 0, [
  571. new Element( 'p', { p: true } ),
  572. new Text( 'a', { a: true } ),
  573. new Element( 'p', { p: true } ),
  574. new Text( 'b', { b: true } ),
  575. new Text( 'c', { c: true } ),
  576. new Element( 'p', [], [
  577. new Text( 'd', { d: true } )
  578. ] ),
  579. new Element( 'p', { p: true } ),
  580. new Text( 'e', { e: true } )
  581. ] );
  582. } );
  583. it( 'if selection is a range, should find first character in it and copy it\'s attributes', () => {
  584. selection._setTo( [ new Range( new Position( root, [ 2 ] ), new Position( root, [ 5 ] ) ) ] );
  585. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'b', true ] ] );
  586. // Step into elements when looking for first character:
  587. selection._setTo( [ new Range( new Position( root, [ 5 ] ), new Position( root, [ 7 ] ) ) ] );
  588. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'd', true ] ] );
  589. } );
  590. it( 'if selection is collapsed it should seek a character to copy that character\'s attributes', () => {
  591. // Take styles from character before selection.
  592. selection._setTo( [ new Range( new Position( root, [ 2 ] ), new Position( root, [ 2 ] ) ) ] );
  593. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'a', true ] ] );
  594. // If there are none,
  595. // Take styles from character after selection.
  596. selection._setTo( [ new Range( new Position( root, [ 3 ] ), new Position( root, [ 3 ] ) ) ] );
  597. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'b', true ] ] );
  598. // If there are none,
  599. // Look from the selection position to the beginning of node looking for character to take attributes from.
  600. selection._setTo( [ new Range( new Position( root, [ 6 ] ), new Position( root, [ 6 ] ) ) ] );
  601. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'c', true ] ] );
  602. // If there are none,
  603. // Look from the selection position to the end of node looking for character to take attributes from.
  604. selection._setTo( [ new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) ) ] );
  605. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'a', true ] ] );
  606. // If there are no characters to copy attributes from, use stored attributes.
  607. selection._setTo( [ new Range( new Position( root, [ 0, 0 ] ), new Position( root, [ 0, 0 ] ) ) ] );
  608. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [] );
  609. } );
  610. it( 'should overwrite any previously set attributes', () => {
  611. selection._setTo( new Position( root, [ 5, 0 ] ) );
  612. selection._setAttribute( 'x', true );
  613. selection._setAttribute( 'y', true );
  614. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'd', true ], [ 'x', true ], [ 'y', true ] ] );
  615. selection._setTo( new Position( root, [ 1 ] ) );
  616. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'a', true ] ] );
  617. } );
  618. it( 'should fire change:attribute event', () => {
  619. const spy = sinon.spy();
  620. selection.on( 'change:attribute', spy );
  621. selection._setTo( [ new Range( new Position( root, [ 2 ] ), new Position( root, [ 5 ] ) ) ] );
  622. expect( spy.calledOnce ).to.be.true;
  623. } );
  624. it( 'should not fire change:attribute event if attributes did not change', () => {
  625. selection._setTo( new Position( root, [ 5, 0 ] ) );
  626. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'd', true ] ] );
  627. const spy = sinon.spy();
  628. selection.on( 'change:attribute', spy );
  629. selection._setTo( new Position( root, [ 5, 1 ] ) );
  630. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'd', true ] ] );
  631. expect( spy.called ).to.be.false;
  632. } );
  633. } );
  634. // #986
  635. describe( 'are not inherited from the inside of object elements', () => {
  636. beforeEach( () => {
  637. model.schema.register( 'image', {
  638. isObject: true
  639. } );
  640. model.schema.extend( 'image', { allowIn: '$root' } );
  641. model.schema.extend( 'image', { allowIn: '$block' } );
  642. model.schema.register( 'caption' );
  643. model.schema.extend( 'caption', { allowIn: 'image' } );
  644. model.schema.extend( '$text', {
  645. allowIn: [ 'image', 'caption' ],
  646. allowAttributes: 'bold'
  647. } );
  648. } );
  649. it( 'ignores attributes inside an object if selection contains that object', () => {
  650. setData( model, '<p>[<image><$text bold="true">Caption for the image.</$text></image>]</p>' );
  651. expect( selection.hasAttribute( 'bold' ) ).to.equal( false );
  652. } );
  653. it( 'ignores attributes inside an object if selection contains that object (deeper structure)', () => {
  654. setData( model, '<p>[<image><caption><$text bold="true">Caption for the image.</$text></caption></image>]</p>' );
  655. expect( selection.hasAttribute( 'bold' ) ).to.equal( false );
  656. } );
  657. it( 'ignores attributes inside an object if selection contains that object (block level)', () => {
  658. setData( model, '<p>foo</p>[<image><$text bold="true">Caption for the image.</$text></image>]<p>foo</p>' );
  659. expect( selection.hasAttribute( 'bold' ) ).to.equal( false );
  660. } );
  661. it( 'reads attributes from text even if the selection contains an object', () => {
  662. setData( model, '<p>x<$text bold="true">[bar</$text><image></image>foo]</p>' );
  663. expect( selection.getAttribute( 'bold' ) ).to.equal( true );
  664. } );
  665. it( 'reads attributes when the entire selection inside an object', () => {
  666. setData( model, '<p><image><caption><$text bold="true">[bar]</$text></caption></image></p>' );
  667. expect( selection.getAttribute( 'bold' ) ).to.equal( true );
  668. } );
  669. it( 'stops reading attributes if selection starts with an object', () => {
  670. setData( model, '<p>[<image></image><$text bold="true">bar]</$text></p>' );
  671. expect( selection.hasAttribute( 'bold' ) ).to.equal( false );
  672. } );
  673. } );
  674. describe( 'parent element\'s attributes', () => {
  675. it( 'are set using a normal batch', () => {
  676. const batchTypes = [];
  677. model.on( 'applyOperation', ( event, args ) => {
  678. const operation = args[ 0 ];
  679. const batch = operation.batch;
  680. batchTypes.push( batch.type );
  681. } );
  682. selection._setTo( [ rangeInEmptyP ] );
  683. model.change( writer => {
  684. writer.setSelectionAttribute( 'foo', 'bar' );
  685. } );
  686. expect( batchTypes ).to.deep.equal( [ 'default' ] );
  687. expect( emptyP.getAttribute( fooStoreAttrKey ) ).to.equal( 'bar' );
  688. } );
  689. it( 'are removed when any content is inserted (reuses the same batch)', () => {
  690. // Dedupe batches by using a map (multiple change events will be fired).
  691. const batchTypes = new Map();
  692. selection._setTo( rangeInEmptyP );
  693. selection._setAttribute( 'foo', 'bar' );
  694. selection._setAttribute( 'abc', 'bar' );
  695. model.on( 'applyOperation', ( event, args ) => {
  696. const operation = args[ 0 ];
  697. const batch = operation.batch;
  698. batchTypes.set( batch, batch.type );
  699. } );
  700. model.change( writer => {
  701. writer.insertText( 'x', rangeInEmptyP.start );
  702. } );
  703. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  704. expect( emptyP.hasAttribute( abcStoreAttrKey ) ).to.be.false;
  705. expect( Array.from( batchTypes.values() ) ).to.deep.equal( [ 'default' ] );
  706. } );
  707. it( 'are removed when any content is moved into', () => {
  708. selection._setTo( rangeInEmptyP );
  709. selection._setAttribute( 'foo', 'bar' );
  710. model.change( writer => {
  711. writer.move( writer.createRangeOn( fullP.getChild( 0 ) ), rangeInEmptyP.start );
  712. } );
  713. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  714. } );
  715. it( 'are removed when containing element is merged with a non-empty element', () => {
  716. const emptyP2 = new Element( 'p', null, 'x' );
  717. root._appendChild( emptyP2 );
  718. emptyP._setAttribute( fooStoreAttrKey, 'bar' );
  719. emptyP2._setAttribute( fooStoreAttrKey, 'bar' );
  720. model.change( writer => {
  721. // <emptyP>{}<emptyP2>
  722. writer.merge( writer.createPositionAfter( emptyP ) );
  723. } );
  724. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  725. expect( emptyP.parent ).to.equal( root ); // Just to be sure we're checking the right element.
  726. } );
  727. it( 'are removed even when there is no selection in it', () => {
  728. emptyP._setAttribute( fooStoreAttrKey, 'bar' );
  729. selection._setTo( [ rangeInFullP ] );
  730. model.change( writer => {
  731. writer.insertText( 'x', rangeInEmptyP.start );
  732. } );
  733. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  734. } );
  735. it( 'uses model change to clear attributes', () => {
  736. selection._setTo( [ rangeInEmptyP ] );
  737. model.change( writer => {
  738. writer.setSelectionAttribute( 'foo', 'bar' );
  739. writer.insertText( 'x', rangeInEmptyP.start );
  740. // `emptyP` still has the attribute, because attribute clearing is in enqueued block.
  741. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.true;
  742. } );
  743. // When the dust settles, `emptyP` should not have the attribute.
  744. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  745. } );
  746. it( 'are not removed or merged when containing element is merged with another empty element', () => {
  747. const emptyP2 = new Element( 'p', null );
  748. root._appendChild( emptyP2 );
  749. emptyP._setAttribute( fooStoreAttrKey, 'bar' );
  750. emptyP2._setAttribute( abcStoreAttrKey, 'bar' );
  751. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.true;
  752. expect( emptyP.hasAttribute( abcStoreAttrKey ) ).to.be.false;
  753. model.change( writer => {
  754. // <emptyP>{}<emptyP2>
  755. writer.merge( writer.createPositionAfter( emptyP ) );
  756. } );
  757. expect( emptyP.getAttribute( fooStoreAttrKey ) ).to.equal( 'bar' );
  758. expect( emptyP.parent ).to.equal( root ); // Just to be sure we're checking the right element.
  759. } );
  760. // Rename and some other operations don't specify range in doc#change event.
  761. // So let's see if there's no crash or something.
  762. it( 'are not removed on rename', () => {
  763. model.change( writer => {
  764. writer.setSelection( rangeInEmptyP );
  765. writer.setSelectionAttribute( 'foo', 'bar' );
  766. } );
  767. sinon.spy( model, 'enqueueChange' );
  768. model.change( writer => {
  769. writer.rename( emptyP, 'pnew' );
  770. } );
  771. expect( model.enqueueChange.called ).to.be.false;
  772. expect( emptyP.getAttribute( fooStoreAttrKey ) ).to.equal( 'bar' );
  773. } );
  774. } );
  775. } );
  776. describe( '_overrideGravity()', () => {
  777. beforeEach( () => {
  778. model.schema.extend( '$text', {
  779. allowIn: '$root'
  780. } );
  781. } );
  782. it( 'should return the UID', () => {
  783. const overrideUidA = selection._overrideGravity();
  784. const overrideUidB = selection._overrideGravity();
  785. expect( overrideUidA ).to.be.a( 'string' );
  786. expect( overrideUidB ).to.be.a( 'string' );
  787. expect( overrideUidA ).to.not.equal( overrideUidB );
  788. } );
  789. it( 'should mark gravity as overridden', () => {
  790. expect( selection.isGravityOverridden ).to.false;
  791. selection._overrideGravity();
  792. expect( selection.isGravityOverridden ).to.true;
  793. } );
  794. it( 'should not inherit attributes from node before the caret', () => {
  795. setData( model, '<$text bold="true" italic="true">foo[]</$text>' );
  796. expect( Array.from( selection.getAttributeKeys() ) ).to.have.members( [ 'bold', 'italic' ] );
  797. selection._overrideGravity();
  798. expect( Array.from( selection.getAttributeKeys() ) ).to.length( 0 );
  799. } );
  800. it( 'should inherit attributes from node after the caret', () => {
  801. setData( model, '<$text>foo[]</$text><$text bold="true" italic="true">bar</$text>' );
  802. expect( Array.from( selection.getAttributeKeys() ) ).to.length( 0 );
  803. selection._overrideGravity();
  804. expect( Array.from( selection.getAttributeKeys() ) ).to.have.members( [ 'bold', 'italic' ] );
  805. } );
  806. it( 'should not retain attributes that are set explicitly', () => {
  807. setData( model, '<$text italic="true">foo[]</$text>' );
  808. selection._setAttribute( 'bold', true );
  809. expect( Array.from( selection.getAttributeKeys() ) ).to.have.members( [ 'bold', 'italic' ] );
  810. selection._overrideGravity();
  811. expect( Array.from( selection.getAttributeKeys() ) ).to.have.members( [] );
  812. } );
  813. it( 'should retain overridden until selection will not change range by a direct change', () => {
  814. setData( model, '<$text bold="true" italic="true">foo[]</$text><$text italic="true">bar</$text>' );
  815. selection._overrideGravity();
  816. // Changed range but not directly.
  817. model.change( writer => writer.insertText( 'abc', new Position( root, [ 0 ] ) ) );
  818. expect( Array.from( selection.getAttributeKeys() ) ).to.have.members( [ 'italic' ] );
  819. // Changed range directly.
  820. model.change( writer => writer.setSelection( new Position( root, [ 5 ] ) ) );
  821. expect( Array.from( selection.getAttributeKeys() ) ).to.have.members( [ 'bold', 'italic' ] );
  822. } );
  823. } );
  824. describe( '_restoreGravity()', () => {
  825. beforeEach( () => {
  826. model.schema.extend( '$text', {
  827. allowIn: '$root'
  828. } );
  829. } );
  830. it( 'should throw when a wrong, already restored, or no UID provided', () => {
  831. const overrideUid = selection._overrideGravity();
  832. selection._restoreGravity( overrideUid );
  833. // Wrong UID
  834. expectToThrowCKEditorError( () => {
  835. selection._restoreGravity( 'foo' );
  836. }, /^document-selection-gravity-wrong-restore/, model );
  837. // Already restored
  838. expectToThrowCKEditorError( () => {
  839. selection._restoreGravity( overrideUid );
  840. }, /^document-selection-gravity-wrong-restore/, model );
  841. // No UID
  842. expectToThrowCKEditorError( () => {
  843. selection._restoreGravity();
  844. }, /^document-selection-gravity-wrong-restore/, model );
  845. } );
  846. it( 'should revert default gravity when is overridden', () => {
  847. setData( model, '<$text bold="true" italic="true">foo[]</$text>' );
  848. const overrideUid = selection._overrideGravity();
  849. expect( Array.from( selection.getAttributeKeys() ) ).to.length( 0 );
  850. expect( selection.isGravityOverridden ).to.true;
  851. selection._restoreGravity( overrideUid );
  852. expect( Array.from( selection.getAttributeKeys() ) ).to.have.members( [ 'bold', 'italic' ] );
  853. expect( selection.isGravityOverridden ).to.false;
  854. } );
  855. it( 'should be called the same number of times as gravity is overridden to restore it', () => {
  856. setData( model, '<$text bold="true" italic="true">foo[]</$text>' );
  857. const overrideUidA = selection._overrideGravity();
  858. const overrideUidB = selection._overrideGravity();
  859. expect( selection.isGravityOverridden ).to.true;
  860. selection._restoreGravity( overrideUidA );
  861. expect( selection.isGravityOverridden ).to.true;
  862. selection._restoreGravity( overrideUidB );
  863. expect( selection.isGravityOverridden ).to.false;
  864. } );
  865. } );
  866. // https://github.com/ckeditor/ckeditor5-engine/issues/1673
  867. describe( 'refreshing selection data', () => {
  868. it( 'should be up to date before post fixers', () => {
  869. model.schema.extend( '$text', { allowAttributes: 'foo' } );
  870. const p = doc.getRoot().getChild( 0 );
  871. doc.registerPostFixer( () => {
  872. expect( model.document.selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  873. expect( Array.from( model.document.selection.markers, m => m.name ) ).to.deep.equal( [ 'marker' ] );
  874. } );
  875. model.change( writer => {
  876. writer.insertText( 'abcdef', { foo: 'bar' }, p );
  877. writer.addMarker( 'marker', {
  878. range: writer.createRange(
  879. writer.createPositionFromPath( p, [ 1 ] ),
  880. writer.createPositionFromPath( p, [ 5 ] )
  881. ),
  882. usingOperation: false
  883. } );
  884. writer.setSelection( writer.createPositionFromPath( p, [ 3 ] ) );
  885. } );
  886. } );
  887. it( 'should be up to date between post fixers', () => {
  888. model.schema.extend( '$text', { allowAttributes: 'foo' } );
  889. const p = doc.getRoot().getChild( 0 );
  890. doc.registerPostFixer( writer => {
  891. writer.setAttribute( 'foo', 'biz', p.getChild( 0 ) );
  892. writer.removeMarker( 'marker-1' );
  893. writer.addMarker( 'marker-2', {
  894. range: writer.createRange(
  895. writer.createPositionFromPath( p, [ 1 ] ),
  896. writer.createPositionFromPath( p, [ 5 ] )
  897. ),
  898. usingOperation: false
  899. } );
  900. } );
  901. doc.registerPostFixer( () => {
  902. expect( model.document.selection.getAttribute( 'foo' ) ).to.equal( 'biz' );
  903. expect( Array.from( model.document.selection.markers, m => m.name ) ).to.deep.equal( [ 'marker-2' ] );
  904. } );
  905. model.change( writer => {
  906. writer.insertText( 'abcdef', { foo: 'bar' }, p );
  907. writer.addMarker( 'marker-1', {
  908. range: writer.createRange(
  909. writer.createPositionFromPath( p, [ 1 ] ),
  910. writer.createPositionFromPath( p, [ 5 ] )
  911. ),
  912. usingOperation: false
  913. } );
  914. writer.setSelection( writer.createPositionFromPath( p, [ 3 ] ) );
  915. } );
  916. } );
  917. it( 'should be up to date after post fixers (on `change` event)', done => {
  918. model.schema.extend( '$text', { allowAttributes: 'foo' } );
  919. const p = doc.getRoot().getChild( 0 );
  920. doc.on( 'change', () => {
  921. expect( model.document.selection.getAttribute( 'foo' ) ).to.equal( 'biz' );
  922. expect( Array.from( model.document.selection.markers, m => m.name ) ).to.deep.equal( [ 'marker-2' ] );
  923. done();
  924. } );
  925. doc.registerPostFixer( writer => {
  926. writer.setAttribute( 'foo', 'biz', p.getChild( 0 ) );
  927. writer.removeMarker( 'marker-1' );
  928. writer.addMarker( 'marker-2', {
  929. range: writer.createRange(
  930. writer.createPositionFromPath( p, [ 1 ] ),
  931. writer.createPositionFromPath( p, [ 5 ] )
  932. ),
  933. usingOperation: false
  934. } );
  935. } );
  936. model.change( writer => {
  937. writer.insertText( 'abcdef', { foo: 'bar' }, p );
  938. writer.addMarker( 'marker-1', {
  939. range: writer.createRange(
  940. writer.createPositionFromPath( p, [ 1 ] ),
  941. writer.createPositionFromPath( p, [ 5 ] )
  942. ),
  943. usingOperation: false
  944. } );
  945. writer.setSelection( writer.createPositionFromPath( p, [ 3 ] ) );
  946. } );
  947. } );
  948. } );
  949. // DocumentSelection uses LiveRanges so here are only simple test to see if integration is
  950. // working well, without getting into complicated corner cases.
  951. describe( 'after applying an operation should get updated and fire events', () => {
  952. let spyRange;
  953. beforeEach( () => {
  954. root._removeChildren( 0, root.childCount );
  955. root._insertChild( 0, [
  956. new Element( 'p', [], new Text( 'abcdef' ) ),
  957. new Element( 'p', [], new Text( 'foobar' ) ),
  958. new Text( 'xyz' )
  959. ] );
  960. selection._setTo( new Range( new Position( root, [ 0, 2 ] ), new Position( root, [ 1, 4 ] ) ) );
  961. spyRange = sinon.spy();
  962. selection.on( 'change:range', spyRange );
  963. } );
  964. describe( 'InsertOperation', () => {
  965. it( 'before selection', () => {
  966. selection.on( 'change:range', ( evt, data ) => {
  967. expect( data.directChange ).to.be.false;
  968. } );
  969. model.applyOperation(
  970. new InsertOperation(
  971. new Position( root, [ 0, 1 ] ),
  972. 'xyz',
  973. doc.version
  974. )
  975. );
  976. const range = selection.getFirstRange();
  977. expect( range.start.path ).to.deep.equal( [ 0, 5 ] );
  978. expect( range.end.path ).to.deep.equal( [ 1, 4 ] );
  979. expect( spyRange.calledOnce ).to.be.true;
  980. } );
  981. it( 'inside selection', () => {
  982. selection.on( 'change:range', ( evt, data ) => {
  983. expect( data.directChange ).to.be.false;
  984. } );
  985. model.applyOperation(
  986. new InsertOperation(
  987. new Position( root, [ 1, 0 ] ),
  988. 'xyz',
  989. doc.version
  990. )
  991. );
  992. const range = selection.getFirstRange();
  993. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  994. expect( range.end.path ).to.deep.equal( [ 1, 7 ] );
  995. expect( spyRange.calledOnce ).to.be.true;
  996. } );
  997. } );
  998. describe( 'MoveOperation', () => {
  999. it( 'move range from before a selection', () => {
  1000. selection.on( 'change:range', ( evt, data ) => {
  1001. expect( data.directChange ).to.be.false;
  1002. } );
  1003. model.applyOperation(
  1004. new MoveOperation(
  1005. new Position( root, [ 0, 0 ] ),
  1006. 2,
  1007. new Position( root, [ 2 ] ),
  1008. doc.version
  1009. )
  1010. );
  1011. const range = selection.getFirstRange();
  1012. expect( range.start.path ).to.deep.equal( [ 0, 0 ] );
  1013. expect( range.end.path ).to.deep.equal( [ 1, 4 ] );
  1014. expect( spyRange.calledOnce ).to.be.true;
  1015. } );
  1016. it( 'moved into before a selection', () => {
  1017. selection.on( 'change:range', ( evt, data ) => {
  1018. expect( data.directChange ).to.be.false;
  1019. } );
  1020. model.applyOperation(
  1021. new MoveOperation(
  1022. new Position( root, [ 2 ] ),
  1023. 2,
  1024. new Position( root, [ 0, 0 ] ),
  1025. doc.version
  1026. )
  1027. );
  1028. const range = selection.getFirstRange();
  1029. expect( range.start.path ).to.deep.equal( [ 0, 4 ] );
  1030. expect( range.end.path ).to.deep.equal( [ 1, 4 ] );
  1031. expect( spyRange.calledOnce ).to.be.true;
  1032. } );
  1033. it( 'move range from inside of selection', () => {
  1034. selection.on( 'change:range', ( evt, data ) => {
  1035. expect( data.directChange ).to.be.false;
  1036. } );
  1037. model.applyOperation(
  1038. new MoveOperation(
  1039. new Position( root, [ 1, 0 ] ),
  1040. 2,
  1041. new Position( root, [ 2 ] ),
  1042. doc.version
  1043. )
  1044. );
  1045. const range = selection.getFirstRange();
  1046. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  1047. expect( range.end.path ).to.deep.equal( [ 1, 2 ] );
  1048. expect( spyRange.calledOnce ).to.be.true;
  1049. } );
  1050. it( 'moved range intersects with selection', () => {
  1051. selection.on( 'change:range', ( evt, data ) => {
  1052. expect( data.directChange ).to.be.false;
  1053. } );
  1054. model.applyOperation(
  1055. new MoveOperation(
  1056. new Position( root, [ 1, 3 ] ),
  1057. 2,
  1058. new Position( root, [ 4 ] ),
  1059. doc.version
  1060. )
  1061. );
  1062. const range = selection.getFirstRange();
  1063. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  1064. expect( range.end.path ).to.deep.equal( [ 1, 3 ] );
  1065. expect( spyRange.calledOnce ).to.be.true;
  1066. } );
  1067. it( 'split inside selection (do not break selection)', () => {
  1068. selection.on( 'change:range', ( evt, data ) => {
  1069. expect( data.directChange ).to.be.false;
  1070. } );
  1071. const batch = new Batch();
  1072. const splitOperation = new SplitOperation( new Position( root, [ 1, 2 ] ), 4, null, 0 );
  1073. batch.addOperation( splitOperation );
  1074. model.applyOperation( splitOperation );
  1075. const range = selection.getFirstRange();
  1076. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  1077. expect( range.end.path ).to.deep.equal( [ 2, 2 ] );
  1078. expect( spyRange.calledOnce ).to.be.true;
  1079. } );
  1080. } );
  1081. describe( 'AttributeOperation', () => {
  1082. it( 'changed range includes selection anchor', () => {
  1083. const spyAttribute = sinon.spy();
  1084. selection.on( 'change:attribute', spyAttribute );
  1085. selection.on( 'change:attribute', ( evt, data ) => {
  1086. expect( data.directChange ).to.be.false;
  1087. expect( data.attributeKeys ).to.deep.equal( [ 'foo' ] );
  1088. } );
  1089. model.applyOperation(
  1090. new AttributeOperation(
  1091. new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 5 ] ) ),
  1092. 'foo',
  1093. null,
  1094. 'bar',
  1095. doc.version
  1096. )
  1097. );
  1098. // Attributes are auto updated on document change.
  1099. model.change( () => {} );
  1100. expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  1101. expect( spyAttribute.calledOnce ).to.be.true;
  1102. } );
  1103. it( 'should not overwrite previously set attributes', () => {
  1104. selection._setAttribute( 'foo', 'xyz' );
  1105. const spyAttribute = sinon.spy();
  1106. selection.on( 'change:attribute', spyAttribute );
  1107. model.applyOperation(
  1108. new AttributeOperation(
  1109. new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 5 ] ) ),
  1110. 'foo',
  1111. null,
  1112. 'bar',
  1113. doc.version
  1114. )
  1115. );
  1116. expect( selection.getAttribute( 'foo' ) ).to.equal( 'xyz' );
  1117. expect( spyAttribute.called ).to.be.false;
  1118. } );
  1119. it( 'should not overwrite previously set attributes with same values', () => {
  1120. selection._setAttribute( 'foo', 'xyz' );
  1121. const spyAttribute = sinon.spy();
  1122. selection.on( 'change:attribute', spyAttribute );
  1123. model.applyOperation(
  1124. new AttributeOperation(
  1125. new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 5 ] ) ),
  1126. 'foo',
  1127. null,
  1128. 'xyz',
  1129. doc.version
  1130. )
  1131. );
  1132. expect( selection.getAttribute( 'foo' ) ).to.equal( 'xyz' );
  1133. expect( spyAttribute.called ).to.be.false;
  1134. } );
  1135. it( 'should not overwrite previously removed attributes', () => {
  1136. selection._setAttribute( 'foo', 'xyz' );
  1137. selection._removeAttribute( 'foo' );
  1138. const spyAttribute = sinon.spy();
  1139. selection.on( 'change:attribute', spyAttribute );
  1140. model.applyOperation(
  1141. new AttributeOperation(
  1142. new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 5 ] ) ),
  1143. 'foo',
  1144. null,
  1145. 'bar',
  1146. doc.version
  1147. )
  1148. );
  1149. expect( selection.hasAttribute( 'foo' ) ).to.be.false;
  1150. expect( spyAttribute.called ).to.be.false;
  1151. } );
  1152. } );
  1153. describe( 'MoveOperation to graveyard', () => {
  1154. it( 'fix selection range if it ends up in graveyard #1', () => {
  1155. selection._setTo( new Position( root, [ 1, 3 ] ) );
  1156. model.applyOperation(
  1157. new MoveOperation(
  1158. new Position( root, [ 1, 2 ] ),
  1159. 2,
  1160. new Position( doc.graveyard, [ 0 ] ),
  1161. doc.version
  1162. )
  1163. );
  1164. expect( selection.getFirstPosition().path ).to.deep.equal( [ 1, 2 ] );
  1165. } );
  1166. it( 'fix selection range if it ends up in graveyard #2', () => {
  1167. selection._setTo( [ new Range( new Position( root, [ 1, 2 ] ), new Position( root, [ 1, 4 ] ) ) ] );
  1168. model.applyOperation(
  1169. new MoveOperation(
  1170. new Position( root, [ 1, 2 ] ),
  1171. 2,
  1172. new Position( doc.graveyard, [ 0 ] ),
  1173. doc.version
  1174. )
  1175. );
  1176. expect( selection.getFirstPosition().path ).to.deep.equal( [ 1, 2 ] );
  1177. } );
  1178. it( 'fix selection range if it ends up in graveyard #3', () => {
  1179. selection._setTo( [ new Range( new Position( root, [ 1, 1 ] ), new Position( root, [ 1, 2 ] ) ) ] );
  1180. model.applyOperation(
  1181. new MoveOperation(
  1182. new Position( root, [ 1 ] ),
  1183. 2,
  1184. new Position( doc.graveyard, [ 0 ] ),
  1185. doc.version
  1186. )
  1187. );
  1188. expect( selection.getFirstPosition().path ).to.deep.equal( [ 0, 6 ] );
  1189. } );
  1190. it( 'fix selection range if it ends up in graveyard #4 - whole content removed', () => {
  1191. model.applyOperation(
  1192. new MoveOperation(
  1193. new Position( root, [ 0 ] ),
  1194. 3,
  1195. new Position( doc.graveyard, [ 0 ] ),
  1196. doc.version
  1197. )
  1198. );
  1199. expect( selection.getFirstPosition().path ).to.deep.equal( [ 0 ] );
  1200. model.applyOperation(
  1201. new InsertOperation(
  1202. new Position( root, [ 0 ] ),
  1203. new Element( 'p' ),
  1204. doc.version
  1205. )
  1206. );
  1207. // Now it's clear that it's the default range.
  1208. expect( selection.getFirstPosition().path ).to.deep.equal( [ 0, 0 ] );
  1209. } );
  1210. } );
  1211. it( '`DocumentSelection#change:range` event should be fire once even if selection contains multi-ranges', () => {
  1212. root._removeChildren( 0, root.childCount );
  1213. root._insertChild( 0, [
  1214. new Element( 'p', [], new Text( 'abcdef' ) ),
  1215. new Element( 'p', [], new Text( 'foobar' ) ),
  1216. new Text( 'xyz #2' )
  1217. ] );
  1218. selection._setTo( [
  1219. Range._createIn( root.getNodeByPath( [ 0 ] ) ),
  1220. Range._createIn( root.getNodeByPath( [ 1 ] ) )
  1221. ] );
  1222. spyRange = sinon.spy();
  1223. selection.on( 'change:range', spyRange );
  1224. model.applyOperation(
  1225. new InsertOperation(
  1226. new Position( root, [ 0 ] ),
  1227. 'xyz #1',
  1228. doc.version
  1229. )
  1230. );
  1231. expect( spyRange.calledOnce ).to.be.true;
  1232. } );
  1233. } );
  1234. it( 'should throw if one of ranges starts or ends inside surrogate pair', () => {
  1235. root._removeChildren( 0, root.childCount );
  1236. root._appendChild( '\uD83D\uDCA9' );
  1237. expectToThrowCKEditorError( () => {
  1238. doc.selection._setTo( new Range( Position._createAt( root, 0 ), Position._createAt( root, 1 ) ) );
  1239. }, /document-selection-wrong-position/, model );
  1240. expectToThrowCKEditorError( () => {
  1241. doc.selection._setTo( new Range( Position._createAt( root, 1 ), Position._createAt( root, 2 ) ) );
  1242. }, /document-selection-wrong-position/, model );
  1243. } );
  1244. it( 'should throw if one of ranges starts or ends between base character and combining mark', () => {
  1245. root._removeChildren( 0, root.childCount );
  1246. root._appendChild( 'foo̻̐ͩbar' );
  1247. expectToThrowCKEditorError( () => {
  1248. doc.selection._setTo( new Range( Position._createAt( root, 3 ), Position._createAt( root, 9 ) ) );
  1249. }, /document-selection-wrong-position/, model );
  1250. expectToThrowCKEditorError( () => {
  1251. doc.selection._setTo( new Range( Position._createAt( root, 4 ), Position._createAt( root, 9 ) ) );
  1252. }, /document-selection-wrong-position/, model );
  1253. expectToThrowCKEditorError( () => {
  1254. doc.selection._setTo( new Range( Position._createAt( root, 5 ), Position._createAt( root, 9 ) ) );
  1255. }, /document-selection-wrong-position/, model );
  1256. expectToThrowCKEditorError( () => {
  1257. doc.selection._setTo( new Range( Position._createAt( root, 1 ), Position._createAt( root, 3 ) ) );
  1258. }, /document-selection-wrong-position/, model );
  1259. expectToThrowCKEditorError( () => {
  1260. doc.selection._setTo( new Range( Position._createAt( root, 1 ), Position._createAt( root, 4 ) ) );
  1261. }, /document-selection-wrong-position/, model );
  1262. expectToThrowCKEditorError( () => {
  1263. doc.selection._setTo( new Range( Position._createAt( root, 1 ), Position._createAt( root, 5 ) ) );
  1264. }, /document-selection-wrong-position/, model );
  1265. } );
  1266. } );