8
0

selection.js 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Document from '../../src/model/document';
  6. import Element from '../../src/model/element';
  7. import Text from '../../src/model/text';
  8. import Range from '../../src/model/range';
  9. import Position from '../../src/model/position';
  10. import LiveRange from '../../src/model/liverange';
  11. import Selection from '../../src/model/selection';
  12. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  13. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  14. import count from '@ckeditor/ckeditor5-utils/src/count';
  15. import { parse, setData } from '../../src/dev-utils/model';
  16. import Schema from '../../src/model/schema';
  17. testUtils.createSinonSandbox();
  18. describe( 'Selection', () => {
  19. let doc, root, selection, liveRange, range, range1, range2, range3;
  20. beforeEach( () => {
  21. doc = new Document();
  22. root = doc.createRoot();
  23. root.appendChildren( [
  24. new Element( 'p' ),
  25. new Element( 'p' ),
  26. new Element( 'p', [], new Text( 'foobar' ) ),
  27. new Element( 'p' ),
  28. new Element( 'p' ),
  29. new Element( 'p' ),
  30. new Element( 'p', [], new Text( 'foobar' ) )
  31. ] );
  32. selection = new Selection();
  33. liveRange = new LiveRange( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) );
  34. range = new Range( new Position( root, [ 2 ] ), new Position( root, [ 2, 2 ] ) );
  35. range1 = new Range( new Position( root, [ 1 ] ), new Position( root, [ 4 ] ) );
  36. range2 = new Range( new Position( root, [ 4 ] ), new Position( root, [ 5 ] ) );
  37. range3 = new Range( new Position( root, [ 6 ] ), new Position( root, [ 7 ] ) );
  38. } );
  39. afterEach( () => {
  40. doc.destroy();
  41. liveRange.detach();
  42. } );
  43. describe( 'constructor()', () => {
  44. it( 'should be able to create an empty selection', () => {
  45. const selection = new Selection();
  46. expect( Array.from( selection.getRanges() ) ).to.deep.equal( [] );
  47. } );
  48. it( 'should be able to create a selection from the given ranges', () => {
  49. const ranges = [ range1, range2, range3 ];
  50. const selection = new Selection( ranges );
  51. expect( Array.from( selection.getRanges() ) ).to.deep.equal( ranges );
  52. } );
  53. it( 'should be able to create a selection from the given ranges and isLastBackward flag', () => {
  54. const ranges = [ range1, range2, range3 ];
  55. const selection = new Selection( ranges, true );
  56. expect( selection.isBackward ).to.be.true;
  57. } );
  58. } );
  59. describe( 'isCollapsed', () => {
  60. it( 'should return false for empty selection', () => {
  61. expect( selection.isCollapsed ).to.be.false;
  62. } );
  63. it( 'should return true when there is single collapsed ranges', () => {
  64. selection.addRange( new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) ) );
  65. expect( selection.isCollapsed ).to.be.true;
  66. } );
  67. it( 'should return false when there are multiple ranges', () => {
  68. selection.addRange( new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) ) );
  69. selection.addRange( new Range( new Position( root, [ 2 ] ), new Position( root, [ 2 ] ) ) );
  70. expect( selection.isCollapsed ).to.be.false;
  71. } );
  72. it( 'should return false when there is not collapsed range', () => {
  73. selection.addRange( range );
  74. expect( selection.isCollapsed ).to.be.false;
  75. } );
  76. } );
  77. describe( 'rangeCount', () => {
  78. it( 'should return proper range count', () => {
  79. expect( selection.rangeCount ).to.equal( 0 );
  80. selection.addRange( new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) ) );
  81. expect( selection.rangeCount ).to.equal( 1 );
  82. selection.addRange( new Range( new Position( root, [ 2 ] ), new Position( root, [ 2 ] ) ) );
  83. expect( selection.rangeCount ).to.equal( 2 );
  84. } );
  85. } );
  86. describe( 'isBackward', () => {
  87. it( 'is defined by the last added range', () => {
  88. selection.addRange( range, true );
  89. expect( selection ).to.have.property( 'isBackward', true );
  90. selection.addRange( liveRange );
  91. expect( selection ).to.have.property( 'isBackward', false );
  92. } );
  93. it( 'is false when last range is collapsed', () => {
  94. const pos = Position.createAt( root, 0 );
  95. selection.addRange( new Range( pos, pos ), true );
  96. expect( selection.isBackward ).to.be.false;
  97. } );
  98. } );
  99. describe( 'focus', () => {
  100. let r3;
  101. beforeEach( () => {
  102. const r1 = Range.createFromParentsAndOffsets( root, 2, root, 4 );
  103. const r2 = Range.createFromParentsAndOffsets( root, 4, root, 6 );
  104. r3 = Range.createFromParentsAndOffsets( root, 1, root, 2 );
  105. selection.addRange( r1 );
  106. selection.addRange( r2 );
  107. } );
  108. it( 'should return correct focus when last added range is not backward one', () => {
  109. selection.addRange( r3 );
  110. expect( selection.focus.isEqual( r3.end ) ).to.be.true;
  111. } );
  112. it( 'should return correct focus when last added range is backward one', () => {
  113. selection.addRange( r3, true );
  114. expect( selection.focus.isEqual( r3.start ) ).to.be.true;
  115. } );
  116. it( 'should return null if no ranges in selection', () => {
  117. selection.removeAllRanges();
  118. expect( selection.focus ).to.be.null;
  119. } );
  120. } );
  121. describe( 'addRange()', () => {
  122. it( 'should copy added ranges and store multiple ranges', () => {
  123. selection.addRange( liveRange );
  124. selection.addRange( range );
  125. const ranges = selection._ranges;
  126. expect( ranges.length ).to.equal( 2 );
  127. expect( ranges[ 0 ].isEqual( liveRange ) ).to.be.true;
  128. expect( ranges[ 1 ].isEqual( range ) ).to.be.true;
  129. expect( ranges[ 0 ] ).not.to.be.equal( liveRange );
  130. expect( ranges[ 1 ] ).not.to.be.equal( range );
  131. } );
  132. it( 'should set anchor and focus to the start and end of the most recently added range', () => {
  133. selection.addRange( liveRange );
  134. expect( selection.anchor.path ).to.deep.equal( [ 0 ] );
  135. expect( selection.focus.path ).to.deep.equal( [ 1 ] );
  136. selection.addRange( range );
  137. expect( selection.anchor.path ).to.deep.equal( [ 2 ] );
  138. expect( selection.focus.path ).to.deep.equal( [ 2, 2 ] );
  139. } );
  140. it( 'should set anchor and focus to the end and start of the most recently added range if backward flag was used', () => {
  141. selection.addRange( liveRange, true );
  142. expect( selection.anchor.path ).to.deep.equal( [ 1 ] );
  143. expect( selection.focus.path ).to.deep.equal( [ 0 ] );
  144. selection.addRange( range, true );
  145. expect( selection.anchor.path ).to.deep.equal( [ 2, 2 ] );
  146. expect( selection.focus.path ).to.deep.equal( [ 2 ] );
  147. } );
  148. it( 'should return a copy of (not a reference to) array of stored ranges', () => {
  149. selection.addRange( liveRange );
  150. const ranges = Array.from( selection.getRanges() );
  151. selection.addRange( range );
  152. expect( ranges.length ).to.equal( 1 );
  153. expect( ranges[ 0 ].isEqual( liveRange ) ).to.be.true;
  154. } );
  155. it( 'should fire change:range event when adding a range', () => {
  156. let spy = sinon.spy();
  157. selection.on( 'change:range', spy );
  158. selection.addRange( range );
  159. expect( spy.called ).to.be.true;
  160. } );
  161. it( 'should throw an error when range is invalid', () => {
  162. expect( () => {
  163. selection.addRange( { invalid: 'range' } );
  164. } ).to.throw( CKEditorError, /model-selection-added-not-range/ );
  165. } );
  166. it( 'should throw an error if added range intersects with already stored range', () => {
  167. selection.addRange( liveRange );
  168. expect( () => {
  169. selection.addRange(
  170. new Range(
  171. new Position( root, [ 0, 4 ] ),
  172. new Position( root, [ 1, 2 ] )
  173. )
  174. );
  175. } ).to.throw( CKEditorError, /model-selection-range-intersects/ );
  176. } );
  177. } );
  178. describe( 'collapse()', () => {
  179. it( 'fires change:range', () => {
  180. const spy = sinon.spy();
  181. selection.on( 'change:range', spy );
  182. selection.collapse( root );
  183. expect( spy.calledOnce ).to.be.true;
  184. } );
  185. it( 'sets selection at the 0 offset if second parameter not passed', () => {
  186. selection.collapse( root );
  187. expect( selection ).to.have.property( 'isCollapsed', true );
  188. const focus = selection.focus;
  189. expect( focus ).to.have.property( 'parent', root );
  190. expect( focus ).to.have.property( 'offset', 0 );
  191. } );
  192. it( 'sets selection at given offset in given parent', () => {
  193. selection.collapse( root, 3 );
  194. expect( selection ).to.have.property( 'isCollapsed', true );
  195. const focus = selection.focus;
  196. expect( focus ).to.have.property( 'parent', root );
  197. expect( focus ).to.have.property( 'offset', 3 );
  198. } );
  199. it( 'sets selection at the end of the given parent', () => {
  200. selection.collapse( root, 'end' );
  201. expect( selection ).to.have.property( 'isCollapsed', true );
  202. const focus = selection.focus;
  203. expect( focus ).to.have.property( 'parent', root );
  204. expect( focus ).to.have.property( 'offset', root.maxOffset );
  205. } );
  206. it( 'sets selection before the specified element', () => {
  207. selection.collapse( root.getChild( 1 ), 'before' );
  208. expect( selection ).to.have.property( 'isCollapsed', true );
  209. const focus = selection.focus;
  210. expect( focus ).to.have.property( 'parent', root );
  211. expect( focus ).to.have.property( 'offset', 1 );
  212. } );
  213. it( 'sets selection after the specified element', () => {
  214. selection.collapse( root.getChild( 1 ), 'after' );
  215. expect( selection ).to.have.property( 'isCollapsed', true );
  216. const focus = selection.focus;
  217. expect( focus ).to.have.property( 'parent', root );
  218. expect( focus ).to.have.property( 'offset', 2 );
  219. } );
  220. it( 'sets selection at the specified position', () => {
  221. const pos = Position.createFromParentAndOffset( root, 3 );
  222. selection.collapse( pos );
  223. expect( selection ).to.have.property( 'isCollapsed', true );
  224. const focus = selection.focus;
  225. expect( focus ).to.have.property( 'parent', root );
  226. expect( focus ).to.have.property( 'offset', 3 );
  227. } );
  228. } );
  229. describe( 'setFocus()', () => {
  230. it( 'keeps all existing ranges and fires no change:range when no modifications needed', () => {
  231. selection.addRange( range );
  232. selection.addRange( liveRange );
  233. const spy = sinon.spy();
  234. selection.on( 'change:range', spy );
  235. selection.setFocus( selection.focus );
  236. expect( count( selection.getRanges() ) ).to.equal( 2 );
  237. expect( spy.callCount ).to.equal( 0 );
  238. } );
  239. it( 'fires change:range', () => {
  240. selection.addRange( range );
  241. const spy = sinon.spy();
  242. selection.on( 'change:range', spy );
  243. selection.setFocus( Position.createAt( root, 'end' ) );
  244. expect( spy.calledOnce ).to.be.true;
  245. } );
  246. it( 'throws if there are no ranges in selection', () => {
  247. const endPos = Position.createAt( root, 'end' );
  248. expect( () => {
  249. selection.setFocus( endPos );
  250. } ).to.throw( CKEditorError, /model-selection-setFocus-no-ranges/ );
  251. } );
  252. it( 'modifies existing collapsed selection', () => {
  253. const startPos = Position.createAt( root, 1 );
  254. const endPos = Position.createAt( root, 2 );
  255. selection.collapse( startPos );
  256. selection.setFocus( endPos );
  257. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'same' );
  258. expect( selection.focus.compareWith( endPos ) ).to.equal( 'same' );
  259. } );
  260. it( 'makes existing collapsed selection a backward selection', () => {
  261. const startPos = Position.createAt( root, 1 );
  262. const endPos = Position.createAt( root, 0 );
  263. selection.collapse( startPos );
  264. selection.setFocus( endPos );
  265. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'same' );
  266. expect( selection.focus.compareWith( endPos ) ).to.equal( 'same' );
  267. expect( selection.isBackward ).to.be.true;
  268. } );
  269. it( 'modifies existing non-collapsed selection', () => {
  270. const startPos = Position.createAt( root, 1 );
  271. const endPos = Position.createAt( root, 2 );
  272. const newEndPos = Position.createAt( root, 3 );
  273. selection.addRange( new Range( startPos, endPos ) );
  274. selection.setFocus( newEndPos );
  275. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'same' );
  276. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'same' );
  277. } );
  278. it( 'makes existing non-collapsed selection a backward selection', () => {
  279. const startPos = Position.createAt( root, 1 );
  280. const endPos = Position.createAt( root, 2 );
  281. const newEndPos = Position.createAt( root, 0 );
  282. selection.addRange( new Range( startPos, endPos ) );
  283. selection.setFocus( newEndPos );
  284. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'same' );
  285. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'same' );
  286. expect( selection.isBackward ).to.be.true;
  287. } );
  288. it( 'makes existing backward selection a forward selection', () => {
  289. const startPos = Position.createAt( root, 1 );
  290. const endPos = Position.createAt( root, 2 );
  291. const newEndPos = Position.createAt( root, 3 );
  292. selection.addRange( new Range( startPos, endPos ), true );
  293. selection.setFocus( newEndPos );
  294. expect( selection.anchor.compareWith( endPos ) ).to.equal( 'same' );
  295. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'same' );
  296. expect( selection.isBackward ).to.be.false;
  297. } );
  298. it( 'modifies existing backward selection', () => {
  299. const startPos = Position.createAt( root, 1 );
  300. const endPos = Position.createAt( root, 2 );
  301. const newEndPos = Position.createAt( root, 0 );
  302. selection.addRange( new Range( startPos, endPos ), true );
  303. selection.setFocus( newEndPos );
  304. expect( selection.anchor.compareWith( endPos ) ).to.equal( 'same' );
  305. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'same' );
  306. expect( selection.isBackward ).to.be.true;
  307. } );
  308. it( 'modifies only the last range', () => {
  309. // Offsets are chosen in this way that the order of adding ranges must count, not their document order.
  310. const startPos1 = Position.createAt( root, 4 );
  311. const endPos1 = Position.createAt( root, 5 );
  312. const startPos2 = Position.createAt( root, 1 );
  313. const endPos2 = Position.createAt( root, 2 );
  314. const newEndPos = Position.createAt( root, 0 );
  315. selection.addRange( new Range( startPos1, endPos1 ) );
  316. selection.addRange( new Range( startPos2, endPos2 ) );
  317. const spy = sinon.spy();
  318. selection.on( 'change:range', spy );
  319. selection.setFocus( newEndPos );
  320. const ranges = Array.from( selection.getRanges() );
  321. expect( ranges ).to.have.lengthOf( 2 );
  322. expect( ranges[ 0 ].start.compareWith( startPos1 ) ).to.equal( 'same' );
  323. expect( ranges[ 0 ].end.compareWith( endPos1 ) ).to.equal( 'same' );
  324. expect( selection.anchor.compareWith( startPos2 ) ).to.equal( 'same' );
  325. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'same' );
  326. expect( selection.isBackward ).to.be.true;
  327. expect( spy.calledOnce ).to.be.true;
  328. } );
  329. it( 'collapses the selection when extending to the anchor', () => {
  330. const startPos = Position.createAt( root, 1 );
  331. const endPos = Position.createAt( root, 2 );
  332. selection.addRange( new Range( startPos, endPos ) );
  333. selection.setFocus( startPos );
  334. expect( selection.focus.compareWith( startPos ) ).to.equal( 'same' );
  335. expect( selection.isCollapsed ).to.be.true;
  336. } );
  337. it( 'uses Position.createAt', () => {
  338. const startPos = Position.createAt( root, 1 );
  339. const endPos = Position.createAt( root, 2 );
  340. const newEndPos = Position.createAt( root, 4 );
  341. const spy = testUtils.sinon.stub( Position, 'createAt', () => newEndPos );
  342. selection.addRange( new Range( startPos, endPos ) );
  343. selection.setFocus( root, 'end' );
  344. expect( spy.calledOnce ).to.be.true;
  345. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'same' );
  346. } );
  347. } );
  348. describe( 'removeAllRanges()', () => {
  349. let spy;
  350. it( 'should remove all stored ranges', () => {
  351. selection.addRange( liveRange );
  352. selection.addRange( range );
  353. selection.removeAllRanges();
  354. expect( Array.from( selection.getRanges() ).length ).to.equal( 0 );
  355. } );
  356. it( 'should fire exactly one change:range event', () => {
  357. selection.addRange( liveRange );
  358. selection.addRange( range );
  359. spy = sinon.spy();
  360. selection.on( 'change:range', spy );
  361. selection.removeAllRanges();
  362. expect( spy.calledOnce ).to.be.true;
  363. } );
  364. it( 'should not fire change:range event if there were no ranges', () => {
  365. spy = sinon.spy();
  366. selection.on( 'change:range', spy );
  367. selection.removeAllRanges();
  368. expect( spy.called ).to.be.false;
  369. } );
  370. } );
  371. describe( 'setRanges()', () => {
  372. let newRanges, spy, oldRanges;
  373. beforeEach( () => {
  374. newRanges = [
  375. new Range( new Position( root, [ 4 ] ), new Position( root, [ 5 ] ) ),
  376. new Range( new Position( root, [ 5, 0 ] ), new Position( root, [ 6, 0 ] ) )
  377. ];
  378. selection.addRange( liveRange );
  379. selection.addRange( range );
  380. spy = sinon.spy();
  381. selection.on( 'change:range', spy );
  382. oldRanges = selection._ranges;
  383. } );
  384. it( 'should throw an error when range is invalid', () => {
  385. expect( () => {
  386. selection.setRanges( [ { invalid: 'range' } ] );
  387. } ).to.throw( CKEditorError, /model-selection-added-not-range/ );
  388. } );
  389. it( 'should remove all ranges and add given ranges', () => {
  390. selection.setRanges( newRanges );
  391. let ranges = Array.from( selection.getRanges() );
  392. expect( ranges ).to.deep.equal( newRanges );
  393. } );
  394. it( 'should use last range from given array to get anchor and focus position', () => {
  395. selection.setRanges( newRanges );
  396. expect( selection.anchor.path ).to.deep.equal( [ 5, 0 ] );
  397. expect( selection.focus.path ).to.deep.equal( [ 6, 0 ] );
  398. } );
  399. it( 'should acknowledge backward flag when setting anchor and focus', () => {
  400. selection.setRanges( newRanges, true );
  401. expect( selection.anchor.path ).to.deep.equal( [ 6, 0 ] );
  402. expect( selection.focus.path ).to.deep.equal( [ 5, 0 ] );
  403. } );
  404. it( 'should fire exactly one change:range event', () => {
  405. selection.setRanges( newRanges );
  406. expect( spy.calledOnce ).to.be.true;
  407. } );
  408. it( 'should fire change:range event with correct parameters', () => {
  409. selection.on( 'change:range', ( evt, data ) => {
  410. expect( data.directChange ).to.be.true;
  411. } );
  412. selection.setRanges( newRanges );
  413. } );
  414. it( 'should not fire change:range event if given ranges are the same', () => {
  415. selection.setRanges( [ liveRange, range ] );
  416. expect( spy.calledOnce ).to.be.false;
  417. } );
  418. } );
  419. describe( 'setTo()', () => {
  420. it( 'should set selection to be same as given selection, using setRanges method', () => {
  421. const spy = sinon.spy( selection, 'setRanges' );
  422. const otherSelection = new Selection();
  423. otherSelection.addRange( range1 );
  424. otherSelection.addRange( range2, true );
  425. selection.setTo( otherSelection );
  426. expect( Array.from( selection.getRanges() ) ).to.deep.equal( [ range1, range2 ] );
  427. expect( selection.isBackward ).to.be.true;
  428. expect( selection.setRanges.calledOnce ).to.be.true;
  429. spy.restore();
  430. } );
  431. } );
  432. describe( 'getFirstRange()', () => {
  433. it( 'should return null if no ranges were added', () => {
  434. expect( selection.getFirstRange() ).to.be.null;
  435. } );
  436. it( 'should return a range which start position is before all other ranges\' start positions', () => {
  437. // This will not be the first range despite being added as first
  438. selection.addRange( range2 );
  439. // This should be the first range.
  440. selection.addRange( range1 );
  441. // A random range that is not first.
  442. selection.addRange( range3 );
  443. let range = selection.getFirstRange();
  444. expect( range.start.path ).to.deep.equal( [ 1 ] );
  445. expect( range.end.path ).to.deep.equal( [ 4 ] );
  446. } );
  447. } );
  448. describe( 'getFirstPosition()', () => {
  449. it( 'should return null if no ranges were added', () => {
  450. expect( selection.getFirstPosition() ).to.be.null;
  451. } );
  452. it( 'should return a position that is in selection and is before any other position from the selection', () => {
  453. // This will not be the first range despite being added as first
  454. selection.addRange( range2 );
  455. // This should be the first range.
  456. selection.addRange( range1 );
  457. // A random range that is not first.
  458. selection.addRange( range3 );
  459. let position = selection.getFirstPosition();
  460. expect( position.path ).to.deep.equal( [ 1 ] );
  461. } );
  462. } );
  463. describe( 'getLastRange()', () => {
  464. it( 'should return null if no ranges were added', () => {
  465. expect( selection.getLastRange() ).to.be.null;
  466. } );
  467. it( 'should return a range which start position is before all other ranges\' start positions', () => {
  468. selection.addRange( range3 );
  469. selection.addRange( range1 );
  470. selection.addRange( range2 );
  471. let range = selection.getLastRange();
  472. expect( range.start.path ).to.deep.equal( [ 6 ] );
  473. expect( range.end.path ).to.deep.equal( [ 7 ] );
  474. } );
  475. } );
  476. describe( 'getLastPosition()', () => {
  477. it( 'should return null if no ranges were added', () => {
  478. expect( selection.getLastPosition() ).to.be.null;
  479. } );
  480. it( 'should return a position that is in selection and is before any other position from the selection', () => {
  481. selection.addRange( range3 );
  482. selection.addRange( range1 );
  483. selection.addRange( range2 );
  484. let position = selection.getLastPosition();
  485. expect( position.path ).to.deep.equal( [ 7 ] );
  486. } );
  487. } );
  488. describe( 'isEqual()', () => {
  489. it( 'should return true if selections equal', () => {
  490. selection.addRange( range1 );
  491. selection.addRange( range2 );
  492. const otherSelection = new Selection();
  493. otherSelection.addRange( range1 );
  494. otherSelection.addRange( range2 );
  495. expect( selection.isEqual( otherSelection ) ).to.be.true;
  496. } );
  497. it( 'should return true if backward selections equal', () => {
  498. selection.addRange( range1, true );
  499. const otherSelection = new Selection();
  500. otherSelection.addRange( range1, true );
  501. expect( selection.isEqual( otherSelection ) ).to.be.true;
  502. } );
  503. it( 'should return true if both selections have no ranges', () => {
  504. const otherSelection = new Selection();
  505. expect( selection.isEqual( otherSelection ) ).to.be.true;
  506. } );
  507. it( 'should return false if ranges count does not equal', () => {
  508. selection.addRange( range1 );
  509. selection.addRange( range2 );
  510. const otherSelection = new Selection();
  511. otherSelection.addRange( range2 );
  512. expect( selection.isEqual( otherSelection ) ).to.be.false;
  513. } );
  514. it( 'should return false if ranges (other than the last added range) do not equal', () => {
  515. selection.addRange( range1 );
  516. selection.addRange( range3 );
  517. const otherSelection = new Selection();
  518. otherSelection.addRange( range2 );
  519. otherSelection.addRange( range3 );
  520. expect( selection.isEqual( otherSelection ) ).to.be.false;
  521. } );
  522. it( 'should return false if directions do not equal', () => {
  523. selection.addRange( range1 );
  524. const otherSelection = new Selection();
  525. otherSelection.addRange( range1, true );
  526. expect( selection.isEqual( otherSelection ) ).to.be.false;
  527. } );
  528. } );
  529. describe( 'collapseToStart()', () => {
  530. it( 'should collapse to start position and fire change event', () => {
  531. selection.setRanges( [ range2, range1, range3 ] );
  532. const spy = sinon.spy();
  533. selection.on( 'change:range', spy );
  534. selection.collapseToStart();
  535. expect( selection.rangeCount ).to.equal( 1 );
  536. expect( selection.isCollapsed ).to.be.true;
  537. expect( selection.getFirstPosition().isEqual( range1.start ) ).to.be.true;
  538. expect( spy.calledOnce ).to.be.true;
  539. } );
  540. it( 'should do nothing if selection was already collapsed', () => {
  541. selection.collapse( range1.start );
  542. const spy = sinon.spy( selection, 'fire' );
  543. selection.collapseToStart();
  544. expect( spy.notCalled ).to.be.true;
  545. spy.restore();
  546. } );
  547. it( 'should do nothing if no ranges present', () => {
  548. const spy = sinon.spy( selection, 'fire' );
  549. selection.collapseToStart();
  550. spy.restore();
  551. expect( spy.notCalled ).to.be.true;
  552. } );
  553. } );
  554. describe( 'collapseToEnd()', () => {
  555. it( 'should collapse to start position and fire change:range event', () => {
  556. selection.setRanges( [ range2, range3, range1 ] );
  557. const spy = sinon.spy();
  558. selection.on( 'change:range', spy );
  559. selection.collapseToEnd();
  560. expect( selection.rangeCount ).to.equal( 1 );
  561. expect( selection.isCollapsed ).to.be.true;
  562. expect( selection.getLastPosition().isEqual( range3.end ) ).to.be.true;
  563. expect( spy.calledOnce ).to.be.true;
  564. } );
  565. it( 'should do nothing if selection was already collapsed', () => {
  566. selection.collapse( range1.start );
  567. const spy = sinon.spy( selection, 'fire' );
  568. selection.collapseToEnd();
  569. expect( spy.notCalled ).to.be.true;
  570. spy.restore();
  571. } );
  572. it( 'should do nothing if selection has no ranges', () => {
  573. const spy = sinon.spy( selection, 'fire' );
  574. selection.collapseToEnd();
  575. expect( spy.notCalled ).to.be.true;
  576. spy.restore();
  577. } );
  578. } );
  579. describe( 'createFromSelection()', () => {
  580. it( 'should return a Selection instance with same ranges and direction as given selection', () => {
  581. selection.addRange( liveRange );
  582. selection.addRange( range, true );
  583. const snapshot = Selection.createFromSelection( selection );
  584. expect( selection.isBackward ).to.equal( snapshot.isBackward );
  585. const selectionRanges = Array.from( selection.getRanges() );
  586. const snapshotRanges = Array.from( snapshot.getRanges() );
  587. expect( selectionRanges.length ).to.equal( snapshotRanges.length );
  588. for ( let i = 0; i < selectionRanges.length; i++ ) {
  589. expect( selectionRanges[ i ].isEqual( snapshotRanges[ i ] ) ).to.be.true;
  590. }
  591. } );
  592. } );
  593. describe( 'getSelectedElement()', () => {
  594. let schema;
  595. beforeEach( () => {
  596. schema = new Schema();
  597. schema.registerItem( 'p', '$block' );
  598. } );
  599. it( 'should return selected element', () => {
  600. const { selection, model } = parse( '<p>foo</p>[<p>bar</p>]<p>baz</p>', schema );
  601. const p = model.getChild( 1 );
  602. expect( selection.getSelectedElement() ).to.equal( p );
  603. } );
  604. it( 'should return null if there is more than one range', () => {
  605. const { selection } = parse( '[<p>foo</p>][<p>bar</p>]<p>baz</p>', schema );
  606. expect( selection.getSelectedElement() ).to.be.null;
  607. } );
  608. it( 'should return null if there is no selection', () => {
  609. expect( selection.getSelectedElement() ).to.be.null;
  610. } );
  611. it( 'should return null if selection is not over single element #1', () => {
  612. const { selection } = parse( '<p>foo</p>[<p>bar</p><p>baz}</p>', schema );
  613. expect( selection.getSelectedElement() ).to.be.null;
  614. } );
  615. it( 'should return null if selection is not over single element #2', () => {
  616. const { selection } = parse( '<p>{bar}</p>', schema );
  617. expect( selection.getSelectedElement() ).to.be.null;
  618. } );
  619. } );
  620. describe( 'getSelectedBlocks()', () => {
  621. beforeEach( () => {
  622. doc.schema.registerItem( 'p', '$block' );
  623. doc.schema.registerItem( 'h', '$block' );
  624. doc.schema.registerItem( 'blockquote' );
  625. doc.schema.allow( { name: 'blockquote', inside: '$root' } );
  626. doc.schema.allow( { name: '$block', inside: 'blockquote' } );
  627. doc.schema.registerItem( 'image' );
  628. doc.schema.allow( { name: 'image', inside: '$root' } );
  629. doc.schema.allow( { name: '$text', inside: 'image' } );
  630. // Special block which can contain another blocks.
  631. doc.schema.registerItem( 'nestedBlock', '$block' );
  632. doc.schema.allow( { name: 'nestedBlock', inside: '$block' } );
  633. } );
  634. it( 'returns an iterator', () => {
  635. setData( doc, '<p>a</p><p>[]b</p><p>c</p>' );
  636. expect( doc.selection.getSelectedBlocks().next ).to.be.a( 'function' );
  637. } );
  638. it( 'returns block for a collapsed selection', () => {
  639. setData( doc, '<p>a</p><p>[]b</p><p>c</p>' );
  640. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'b' ] );
  641. } );
  642. it( 'returns block for a non collapsed selection', () => {
  643. setData( doc, '<p>a</p><p>[b]</p><p>c</p>' );
  644. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'b' ] );
  645. } );
  646. it( 'returns two blocks for a non collapsed selection', () => {
  647. setData( doc, '<p>a</p><h>[b</h><p>c]</p><p>d</p>' );
  648. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'b', 'c' ] );
  649. } );
  650. // This isn't a behaviour that we like (https://github.com/ckeditor/ckeditor5-heading/issues/9) but I don't
  651. // want to work on this issue now, when porting this method from the list/heading features.
  652. // It has to be covered separately.
  653. it( 'returns two blocks for a non collapsed selection if only end of selection is touching a block', () => {
  654. setData( doc, '<p>a</p><h>b[</h><p>]c</p><p>d</p>' );
  655. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'b', 'c' ] );
  656. } );
  657. it( 'returns proper block for a multi-range selection', () => {
  658. setData( doc, '<p>a</p><h>[b</h><p>c]</p><p>d</p><p>[e]</p>' );
  659. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'b', 'c', 'e' ] );
  660. } );
  661. it( 'does not return a block twice if two ranges are anchored in it', () => {
  662. setData( doc, '<p>[a]b[c]</p>' );
  663. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'abc' ] );
  664. } );
  665. it( 'returns only blocks', () => {
  666. setData( doc, '<p>[a</p><image>b</image><p>c]</p>' );
  667. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'a', 'c' ] );
  668. } );
  669. it( 'gets deeper into the tree', () => {
  670. setData( doc, '<p>[a</p><blockquote><p>b</p><p>c</p></blockquote><p>d]</p>' );
  671. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'a', 'b', 'c', 'd' ] );
  672. } );
  673. it( 'gets deeper into the tree (end deeper)', () => {
  674. setData( doc, '<p>[a</p><blockquote><p>b]</p><p>c</p></blockquote><p>d</p>' );
  675. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'a', 'b' ] );
  676. } );
  677. it( 'gets deeper into the tree (start deeper)', () => {
  678. setData( doc, '<p>a</p><blockquote><p>b</p><p>[c</p></blockquote><p>d]</p>' );
  679. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'c', 'd' ] );
  680. } );
  681. it( 'returns an empty array if none of the selected elements is a block', () => {
  682. setData( doc, '<p>a</p><image>[a</image><image>b]</image><p>b</p>' );
  683. expect( toText( doc.selection.getSelectedBlocks() ) ).to.be.empty;
  684. } );
  685. it( 'returns an empty array if the selected element is not a block', () => {
  686. setData( doc, '<p>a</p><image>[]a</image><p>b</p>' );
  687. expect( toText( doc.selection.getSelectedBlocks() ) ).to.be.empty;
  688. } );
  689. // Super edge case – should not happen (blocks should never be nested),
  690. // but since the code handles it already it's worth testing.
  691. it( 'returns only the lowest block if blocks are nested', () => {
  692. setData( doc, '<nestedBlock>a<nestedBlock>[]b</nestedBlock></nestedBlock>' );
  693. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'b' ] );
  694. } );
  695. // Like above but trickier.
  696. it( 'returns only the lowest block if blocks are nested', () => {
  697. setData(
  698. doc,
  699. '<nestedBlock>a<nestedBlock>[b</nestedBlock></nestedBlock>' +
  700. '<nestedBlock>c<nestedBlock>d]</nestedBlock></nestedBlock>'
  701. );
  702. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'b', 'd' ] );
  703. } );
  704. it( 'returns nothing if directly in a root', () => {
  705. doc.createRoot( 'p', 'inlineOnlyRoot' );
  706. setData( doc, 'a[b]c', { rootName: 'inlineOnlyRoot' } );
  707. expect( toText( doc.selection.getSelectedBlocks() ) ).to.be.empty;
  708. } );
  709. function toText( elements ) {
  710. return Array.from( elements ).map( el => el.getChild( 0 ).data );
  711. }
  712. } );
  713. describe( 'attributes interface', () => {
  714. let rangeInFullP;
  715. beforeEach( () => {
  716. root.insertChildren( 0, [
  717. new Element( 'p', [], new Text( 'foobar' ) ),
  718. new Element( 'p', [], [] )
  719. ] );
  720. rangeInFullP = new Range( new Position( root, [ 0, 4 ] ), new Position( root, [ 0, 4 ] ) );
  721. } );
  722. describe( 'setAttribute()', () => {
  723. it( 'should set given attribute on the selection', () => {
  724. selection.setRanges( [ rangeInFullP ] );
  725. selection.setAttribute( 'foo', 'bar' );
  726. expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  727. } );
  728. it( 'should fire change:attribute event with correct parameters', () => {
  729. selection.on( 'change:attribute', ( evt, data ) => {
  730. expect( data.directChange ).to.be.true;
  731. expect( data.attributeKeys ).to.deep.equal( [ 'foo' ] );
  732. } );
  733. selection.setAttribute( 'foo', 'bar' );
  734. } );
  735. it( 'should not fire change:attribute event if attribute with same key and value was already set', () => {
  736. selection.setAttribute( 'foo', 'bar' );
  737. let spy = sinon.spy();
  738. selection.on( 'change:attribute', spy );
  739. selection.setAttribute( 'foo', 'bar' );
  740. expect( spy.called ).to.be.false;
  741. } );
  742. } );
  743. describe( 'getAttribute()', () => {
  744. it( 'should return undefined if element does not contain given attribute', () => {
  745. expect( selection.getAttribute( 'abc' ) ).to.be.undefined;
  746. } );
  747. } );
  748. describe( 'getAttributes()', () => {
  749. it( 'should return an iterator that iterates over all attributes set on selection', () => {
  750. selection.setRanges( [ rangeInFullP ] );
  751. selection.setAttribute( 'foo', 'bar' );
  752. selection.setAttribute( 'abc', 'xyz' );
  753. let attrs = Array.from( selection.getAttributes() );
  754. expect( attrs ).to.deep.equal( [ [ 'foo', 'bar' ], [ 'abc', 'xyz' ] ] );
  755. } );
  756. } );
  757. describe( 'getAttributeKeys()', () => {
  758. it( 'should return iterator that iterates over all attribute keys set on selection', () => {
  759. selection.setRanges( [ rangeInFullP ] );
  760. selection.setAttribute( 'foo', 'bar' );
  761. selection.setAttribute( 'abc', 'xyz' );
  762. let attrs = Array.from( selection.getAttributeKeys() );
  763. expect( attrs ).to.deep.equal( [ 'foo', 'abc' ] );
  764. } );
  765. } );
  766. describe( 'hasAttribute()', () => {
  767. it( 'should return true if element contains attribute with given key', () => {
  768. selection.setRanges( [ rangeInFullP ] );
  769. selection.setAttribute( 'foo', 'bar' );
  770. expect( selection.hasAttribute( 'foo' ) ).to.be.true;
  771. } );
  772. it( 'should return false if element does not contain attribute with given key', () => {
  773. expect( selection.hasAttribute( 'abc' ) ).to.be.false;
  774. } );
  775. } );
  776. describe( 'clearAttributes()', () => {
  777. it( 'should remove all attributes from the element', () => {
  778. selection.setRanges( [ rangeInFullP ] );
  779. selection.setAttribute( 'foo', 'bar' );
  780. selection.setAttribute( 'abc', 'xyz' );
  781. selection.clearAttributes();
  782. expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
  783. expect( selection.getAttribute( 'abc' ) ).to.be.undefined;
  784. } );
  785. it( 'should fire change:attribute event with correct parameters', () => {
  786. selection.setAttribute( 'foo', 'bar' );
  787. selection.on( 'change:attribute', ( evt, data ) => {
  788. expect( data.directChange ).to.be.true;
  789. expect( data.attributeKeys ).to.deep.equal( [ 'foo' ] );
  790. } );
  791. selection.clearAttributes();
  792. } );
  793. it( 'should not fire change:attribute event if there were no attributes', () => {
  794. let spy = sinon.spy();
  795. selection.on( 'change:attribute', spy );
  796. selection.clearAttributes();
  797. expect( spy.called ).to.be.false;
  798. } );
  799. } );
  800. describe( 'removeAttribute()', () => {
  801. it( 'should remove attribute', () => {
  802. selection.setRanges( [ rangeInFullP ] );
  803. selection.setAttribute( 'foo', 'bar' );
  804. selection.removeAttribute( 'foo' );
  805. expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
  806. } );
  807. it( 'should fire change:attribute event with correct parameters', () => {
  808. selection.setAttribute( 'foo', 'bar' );
  809. selection.on( 'change:attribute', ( evt, data ) => {
  810. expect( data.directChange ).to.be.true;
  811. expect( data.attributeKeys ).to.deep.equal( [ 'foo' ] );
  812. } );
  813. selection.removeAttribute( 'foo' );
  814. } );
  815. it( 'should not fire change:attribute event if such attribute did not exist', () => {
  816. let spy = sinon.spy();
  817. selection.on( 'change:attribute', spy );
  818. selection.removeAttribute( 'foo' );
  819. expect( spy.called ).to.be.false;
  820. } );
  821. } );
  822. describe( 'setAttributesTo()', () => {
  823. it( 'should remove all attributes set on element and set the given ones', () => {
  824. selection.setAttribute( 'abc', 'xyz' );
  825. selection.setAttributesTo( { foo: 'bar' } );
  826. expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  827. expect( selection.getAttribute( 'abc' ) ).to.be.undefined;
  828. } );
  829. it( 'should fire only one change:attribute event', () => {
  830. selection.setAttributesTo( { foo: 'bar', xxx: 'yyy' } );
  831. let spy = sinon.spy();
  832. selection.on( 'change:attribute', spy );
  833. selection.setAttributesTo( { foo: 'bar', abc: 'def' } );
  834. expect( spy.calledOnce ).to.be.true;
  835. } );
  836. it( 'should fire change:attribute event with correct parameters', () => {
  837. selection.setAttributesTo( { foo: 'bar', xxx: 'yyy' } );
  838. selection.on( 'change:attribute', ( evt, data ) => {
  839. expect( data.directChange ).to.be.true;
  840. expect( data.attributeKeys ).to.deep.equal( [ 'abc', 'xxx' ] );
  841. } );
  842. selection.setAttributesTo( { foo: 'bar', abc: 'def' } );
  843. } );
  844. it( 'should not fire change:attribute event if attributes had not changed', () => {
  845. selection.setRanges( [ rangeInFullP ] );
  846. selection.setAttributesTo( { foo: 'bar', xxx: 'yyy' } );
  847. let spy = sinon.spy();
  848. selection.on( 'change:attribute', spy );
  849. selection.setAttributesTo( { xxx: 'yyy', foo: 'bar' } );
  850. expect( spy.called ).to.be.false;
  851. } );
  852. } );
  853. } );
  854. } );