8
0

documentselection.js 51 KB

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