documentselection.js 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import DocumentSelection from '../../src/view/documentselection';
  6. import Selection from '../../src/view/selection';
  7. import Range from '../../src/view/range';
  8. import Document from '../../src/view/document';
  9. import Element from '../../src/view/element';
  10. import Text from '../../src/view/text';
  11. import Position from '../../src/view/position';
  12. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  13. import count from '@ckeditor/ckeditor5-utils/src/count';
  14. import createViewRoot from './_utils/createroot';
  15. import { parse } from '../../src/dev-utils/view';
  16. describe( 'DocumentSelection', () => {
  17. let documentSelection, el, range1, range2, range3;
  18. beforeEach( () => {
  19. const text = new Text( 'xxxxxxxxxxxxxxxxxxxx' );
  20. el = new Element( 'p', null, text );
  21. documentSelection = new DocumentSelection();
  22. range1 = Range.createFromParentsAndOffsets( text, 5, text, 10 );
  23. range2 = Range.createFromParentsAndOffsets( text, 1, text, 2 );
  24. range3 = Range.createFromParentsAndOffsets( text, 12, text, 14 );
  25. } );
  26. describe( 'constructor()', () => {
  27. it( 'should be able to create an empty selection', () => {
  28. const selection = new DocumentSelection();
  29. expect( Array.from( selection.getRanges() ) ).to.deep.equal( [] );
  30. } );
  31. it( 'should be able to create a selection from the given ranges', () => {
  32. const ranges = [ range1, range2, range3 ];
  33. const selection = new DocumentSelection( ranges );
  34. expect( Array.from( selection.getRanges() ) ).to.deep.equal( ranges );
  35. } );
  36. it( 'should be able to create a selection from the given ranges and isLastBackward flag', () => {
  37. const ranges = [ range1, range2, range3 ];
  38. const selection = new DocumentSelection( ranges, { backward: true } );
  39. expect( selection.isBackward ).to.be.true;
  40. } );
  41. it( 'should be able to create a selection from the given range and isLastBackward flag', () => {
  42. const selection = new DocumentSelection( range1, { backward: true } );
  43. expect( Array.from( selection.getRanges() ) ).to.deep.equal( [ range1 ] );
  44. expect( selection.isBackward ).to.be.true;
  45. } );
  46. it( 'should be able to create a selection from the given iterable of ranges and isLastBackward flag', () => {
  47. const ranges = new Set( [ range1, range2, range3 ] );
  48. const selection = new DocumentSelection( ranges, { backward: false } );
  49. expect( Array.from( selection.getRanges() ) ).to.deep.equal( [ range1, range2, range3 ] );
  50. expect( selection.isBackward ).to.be.false;
  51. } );
  52. it( 'should be able to create a collapsed selection at the given position', () => {
  53. const position = range1.start;
  54. const selection = new DocumentSelection( position );
  55. expect( Array.from( selection.getRanges() ).length ).to.equal( 1 );
  56. expect( selection.getFirstRange().start ).to.deep.equal( position );
  57. expect( selection.getFirstRange().end ).to.deep.equal( position );
  58. expect( selection.isBackward ).to.be.false;
  59. } );
  60. it( 'should be able to create a collapsed selection at the given position', () => {
  61. const position = range1.start;
  62. const selection = new DocumentSelection( position );
  63. expect( Array.from( selection.getRanges() ).length ).to.equal( 1 );
  64. expect( selection.getFirstRange().start ).to.deep.equal( position );
  65. expect( selection.getFirstRange().end ).to.deep.equal( position );
  66. expect( selection.isBackward ).to.be.false;
  67. } );
  68. it( 'should be able to create a selection from the other document selection', () => {
  69. const otherSelection = new DocumentSelection( [ range2, range3 ], { backward: true } );
  70. const selection = new DocumentSelection( otherSelection );
  71. expect( Array.from( selection.getRanges() ) ).to.deep.equal( [ range2, range3 ] );
  72. expect( selection.isBackward ).to.be.true;
  73. } );
  74. it( 'should be able to create a selection from the other selection', () => {
  75. const otherSelection = new Selection( [ range2, range3 ], { backward: true } );
  76. const selection = new DocumentSelection( otherSelection );
  77. expect( Array.from( selection.getRanges() ) ).to.deep.equal( [ range2, range3 ] );
  78. expect( selection.isBackward ).to.be.true;
  79. } );
  80. it( 'should be able to create a fake selection from the other fake selection', () => {
  81. const otherSelection = new DocumentSelection( [ range2, range3 ], { fake: true, label: 'foo bar baz' } );
  82. const selection = new DocumentSelection( otherSelection );
  83. expect( selection.isFake ).to.be.true;
  84. expect( selection.fakeSelectionLabel ).to.equal( 'foo bar baz' );
  85. } );
  86. it( 'should throw an error when range is invalid', () => {
  87. expect( () => {
  88. // eslint-disable-next-line no-new
  89. new DocumentSelection( [ { invalid: 'range' } ] );
  90. } ).to.throw( CKEditorError, 'view-selection-invalid-range: Invalid Range.' );
  91. } );
  92. it( 'should throw an error when ranges intersects', () => {
  93. const text = el.getChild( 0 );
  94. const range2 = Range.createFromParentsAndOffsets( text, 7, text, 15 );
  95. expect( () => {
  96. // eslint-disable-next-line no-new
  97. new DocumentSelection( [ range1, range2 ] );
  98. } ).to.throw( CKEditorError, 'view-selection-range-intersects' );
  99. } );
  100. it( 'should throw an error when trying to set to not selectable', () => {
  101. expect( () => {
  102. // eslint-disable-next-line no-new
  103. new DocumentSelection( {} );
  104. } ).to.throw( /view-selection-setTo-not-selectable/ );
  105. } );
  106. } );
  107. describe( 'anchor', () => {
  108. it( 'should return null if no ranges in selection', () => {
  109. expect( documentSelection.anchor ).to.be.null;
  110. } );
  111. it( 'should return start of single range in selection', () => {
  112. documentSelection._setTo( range1 );
  113. const anchor = documentSelection.anchor;
  114. expect( anchor.isEqual( range1.start ) ).to.be.true;
  115. expect( anchor ).to.not.equal( range1.start );
  116. } );
  117. it( 'should return end of single range in selection when added as backward', () => {
  118. documentSelection._setTo( range1, { backward: true } );
  119. const anchor = documentSelection.anchor;
  120. expect( anchor.isEqual( range1.end ) ).to.be.true;
  121. expect( anchor ).to.not.equal( range1.end );
  122. } );
  123. it( 'should get anchor from last inserted range', () => {
  124. documentSelection._setTo( [ range1, range2 ] );
  125. expect( documentSelection.anchor.isEqual( range2.start ) ).to.be.true;
  126. } );
  127. } );
  128. describe( 'focus', () => {
  129. it( 'should return null if no ranges in selection', () => {
  130. expect( documentSelection.focus ).to.be.null;
  131. } );
  132. it( 'should return end of single range in selection', () => {
  133. documentSelection._setTo( range1 );
  134. const focus = documentSelection.focus;
  135. expect( focus.isEqual( range1.end ) ).to.be.true;
  136. } );
  137. it( 'should return start of single range in selection when added as backward', () => {
  138. documentSelection._setTo( range1, { backward: true } );
  139. const focus = documentSelection.focus;
  140. expect( focus.isEqual( range1.start ) ).to.be.true;
  141. expect( focus ).to.not.equal( range1.start );
  142. } );
  143. it( 'should get focus from last inserted range', () => {
  144. documentSelection._setTo( [ range1, range2 ] );
  145. expect( documentSelection.focus.isEqual( range2.end ) ).to.be.true;
  146. } );
  147. } );
  148. describe( '_setFocus()', () => {
  149. it( 'keeps all existing ranges when no modifications needed', () => {
  150. documentSelection._setTo( range1 );
  151. documentSelection._setFocus( documentSelection.focus );
  152. expect( count( documentSelection.getRanges() ) ).to.equal( 1 );
  153. } );
  154. it( 'throws if there are no ranges in selection', () => {
  155. const endPos = Position.createAt( el, 'end' );
  156. expect( () => {
  157. documentSelection._setFocus( endPos );
  158. } ).to.throw( CKEditorError, /view-selection-setFocus-no-ranges/ );
  159. } );
  160. it( 'modifies existing collapsed selection', () => {
  161. const startPos = Position.createAt( el, 1 );
  162. const endPos = Position.createAt( el, 2 );
  163. documentSelection._setTo( startPos );
  164. documentSelection._setFocus( endPos );
  165. expect( documentSelection.anchor.compareWith( startPos ) ).to.equal( 'same' );
  166. expect( documentSelection.focus.compareWith( endPos ) ).to.equal( 'same' );
  167. } );
  168. it( 'makes existing collapsed selection a backward selection', () => {
  169. const startPos = Position.createAt( el, 1 );
  170. const endPos = Position.createAt( el, 0 );
  171. documentSelection._setTo( startPos );
  172. documentSelection._setFocus( endPos );
  173. expect( documentSelection.anchor.compareWith( startPos ) ).to.equal( 'same' );
  174. expect( documentSelection.focus.compareWith( endPos ) ).to.equal( 'same' );
  175. expect( documentSelection.isBackward ).to.be.true;
  176. } );
  177. it( 'modifies existing non-collapsed selection', () => {
  178. const startPos = Position.createAt( el, 1 );
  179. const endPos = Position.createAt( el, 2 );
  180. const newEndPos = Position.createAt( el, 3 );
  181. documentSelection._setTo( new Range( startPos, endPos ) );
  182. documentSelection._setFocus( newEndPos );
  183. expect( documentSelection.anchor.compareWith( startPos ) ).to.equal( 'same' );
  184. expect( documentSelection.focus.compareWith( newEndPos ) ).to.equal( 'same' );
  185. } );
  186. it( 'makes existing non-collapsed selection a backward selection', () => {
  187. const startPos = Position.createAt( el, 1 );
  188. const endPos = Position.createAt( el, 2 );
  189. const newEndPos = Position.createAt( el, 0 );
  190. documentSelection._setTo( new Range( startPos, endPos ) );
  191. documentSelection._setFocus( newEndPos );
  192. expect( documentSelection.anchor.compareWith( startPos ) ).to.equal( 'same' );
  193. expect( documentSelection.focus.compareWith( newEndPos ) ).to.equal( 'same' );
  194. expect( documentSelection.isBackward ).to.be.true;
  195. } );
  196. it( 'makes existing backward selection a forward selection', () => {
  197. const startPos = Position.createAt( el, 1 );
  198. const endPos = Position.createAt( el, 2 );
  199. const newEndPos = Position.createAt( el, 3 );
  200. documentSelection._setTo( new Range( startPos, endPos ), { backward: true } );
  201. documentSelection._setFocus( newEndPos );
  202. expect( documentSelection.anchor.compareWith( endPos ) ).to.equal( 'same' );
  203. expect( documentSelection.focus.compareWith( newEndPos ) ).to.equal( 'same' );
  204. expect( documentSelection.isBackward ).to.be.false;
  205. } );
  206. it( 'modifies existing backward selection', () => {
  207. const startPos = Position.createAt( el, 1 );
  208. const endPos = Position.createAt( el, 2 );
  209. const newEndPos = Position.createAt( el, 0 );
  210. documentSelection._setTo( new Range( startPos, endPos ), { backward: true } );
  211. documentSelection._setFocus( newEndPos );
  212. expect( documentSelection.anchor.compareWith( endPos ) ).to.equal( 'same' );
  213. expect( documentSelection.focus.compareWith( newEndPos ) ).to.equal( 'same' );
  214. expect( documentSelection.isBackward ).to.be.true;
  215. } );
  216. it( 'modifies only the last range', () => {
  217. // Offsets are chosen in this way that the order of adding ranges must count, not their document order.
  218. const startPos1 = Position.createAt( el, 4 );
  219. const endPos1 = Position.createAt( el, 5 );
  220. const startPos2 = Position.createAt( el, 1 );
  221. const endPos2 = Position.createAt( el, 2 );
  222. const newEndPos = Position.createAt( el, 0 );
  223. documentSelection._setTo( [
  224. new Range( startPos1, endPos1 ),
  225. new Range( startPos2, endPos2 )
  226. ] );
  227. documentSelection._setFocus( newEndPos );
  228. const ranges = Array.from( documentSelection.getRanges() );
  229. expect( ranges ).to.have.lengthOf( 2 );
  230. expect( ranges[ 0 ].start.compareWith( startPos1 ) ).to.equal( 'same' );
  231. expect( ranges[ 0 ].end.compareWith( endPos1 ) ).to.equal( 'same' );
  232. expect( documentSelection.anchor.compareWith( startPos2 ) ).to.equal( 'same' );
  233. expect( documentSelection.focus.compareWith( newEndPos ) ).to.equal( 'same' );
  234. expect( documentSelection.isBackward ).to.be.true;
  235. } );
  236. it( 'collapses the selection when extending to the anchor', () => {
  237. const startPos = Position.createAt( el, 1 );
  238. const endPos = Position.createAt( el, 2 );
  239. documentSelection._setTo( new Range( startPos, endPos ) );
  240. documentSelection._setFocus( startPos );
  241. expect( documentSelection.focus.compareWith( startPos ) ).to.equal( 'same' );
  242. expect( documentSelection.isCollapsed ).to.be.true;
  243. } );
  244. it( 'uses Position.createAt', () => {
  245. const startPos = Position.createAt( el, 1 );
  246. const endPos = Position.createAt( el, 2 );
  247. const newEndPos = Position.createAt( el, 4 );
  248. const spy = sinon.stub( Position, 'createAt' ).returns( newEndPos );
  249. documentSelection._setTo( new Range( startPos, endPos ) );
  250. documentSelection._setFocus( el, 'end' );
  251. expect( spy.calledOnce ).to.be.true;
  252. expect( documentSelection.focus.compareWith( newEndPos ) ).to.equal( 'same' );
  253. Position.createAt.restore();
  254. } );
  255. } );
  256. describe( 'isCollapsed', () => {
  257. it( 'should return true when there is single collapsed range', () => {
  258. const range = Range.createFromParentsAndOffsets( el, 5, el, 5 );
  259. documentSelection._setTo( range );
  260. expect( documentSelection.isCollapsed ).to.be.true;
  261. } );
  262. it( 'should return false when there are multiple ranges', () => {
  263. const range1 = Range.createFromParentsAndOffsets( el, 5, el, 5 );
  264. const range2 = Range.createFromParentsAndOffsets( el, 15, el, 15 );
  265. documentSelection._setTo( [ range1, range2 ] );
  266. expect( documentSelection.isCollapsed ).to.be.false;
  267. } );
  268. it( 'should return false when there is not collapsed range', () => {
  269. const range = Range.createFromParentsAndOffsets( el, 15, el, 16 );
  270. documentSelection._setTo( range );
  271. expect( documentSelection.isCollapsed ).to.be.false;
  272. } );
  273. } );
  274. describe( 'rangeCount', () => {
  275. it( 'should return proper range count', () => {
  276. expect( documentSelection.rangeCount ).to.equal( 0 );
  277. documentSelection._setTo( range1 );
  278. expect( documentSelection.rangeCount ).to.equal( 1 );
  279. documentSelection._setTo( [ range1, range2 ] );
  280. expect( documentSelection.rangeCount ).to.equal( 2 );
  281. } );
  282. } );
  283. describe( 'isBackward', () => {
  284. it( 'is defined by the last added range', () => {
  285. const range1 = Range.createFromParentsAndOffsets( el, 5, el, 10 );
  286. const range2 = Range.createFromParentsAndOffsets( el, 15, el, 16 );
  287. documentSelection._setTo( range1, { backward: true } );
  288. expect( documentSelection ).to.have.property( 'isBackward', true );
  289. documentSelection._setTo( [ range1, range2 ] );
  290. expect( documentSelection ).to.have.property( 'isBackward', false );
  291. } );
  292. it( 'is false when last range is collapsed', () => {
  293. const range = Range.createFromParentsAndOffsets( el, 5, el, 5 );
  294. documentSelection._setTo( range, { backward: true } );
  295. expect( documentSelection.isBackward ).to.be.false;
  296. } );
  297. } );
  298. describe( 'getRanges', () => {
  299. it( 'should return iterator with copies of all ranges', () => {
  300. documentSelection._setTo( [ range1, range2 ] );
  301. const iterable = documentSelection.getRanges();
  302. const ranges = Array.from( iterable );
  303. expect( ranges.length ).to.equal( 2 );
  304. expect( ranges[ 0 ].isEqual( range1 ) ).to.be.true;
  305. expect( ranges[ 0 ] ).to.not.equal( range1 );
  306. expect( ranges[ 1 ].isEqual( range2 ) ).to.be.true;
  307. expect( ranges[ 1 ] ).to.not.equal( range2 );
  308. } );
  309. } );
  310. describe( 'getFirstRange', () => {
  311. it( 'should return copy of range with first position', () => {
  312. documentSelection._setTo( [ range1, range2, range3 ] );
  313. const range = documentSelection.getFirstRange();
  314. expect( range.isEqual( range2 ) ).to.be.true;
  315. expect( range ).to.not.equal( range2 );
  316. } );
  317. it( 'should return null if no ranges are present', () => {
  318. expect( documentSelection.getFirstRange() ).to.be.null;
  319. } );
  320. } );
  321. describe( 'getLastRange', () => {
  322. it( 'should return copy of range with last position', () => {
  323. documentSelection._setTo( [ range1, range2, range3 ] );
  324. const range = documentSelection.getLastRange();
  325. expect( range.isEqual( range3 ) ).to.be.true;
  326. expect( range ).to.not.equal( range3 );
  327. } );
  328. it( 'should return null if no ranges are present', () => {
  329. expect( documentSelection.getLastRange() ).to.be.null;
  330. } );
  331. } );
  332. describe( 'getFirstPosition', () => {
  333. it( 'should return copy of first position', () => {
  334. documentSelection._setTo( [ range1, range2, range3 ] );
  335. const position = documentSelection.getFirstPosition();
  336. expect( position.isEqual( range2.start ) ).to.be.true;
  337. expect( position ).to.not.equal( range2.start );
  338. } );
  339. it( 'should return null if no ranges are present', () => {
  340. expect( documentSelection.getFirstPosition() ).to.be.null;
  341. } );
  342. } );
  343. describe( 'getLastPosition', () => {
  344. it( 'should return copy of range with last position', () => {
  345. documentSelection._setTo( [ range1, range2, range3 ] );
  346. const position = documentSelection.getLastPosition();
  347. expect( position.isEqual( range3.end ) ).to.be.true;
  348. expect( position ).to.not.equal( range3.end );
  349. } );
  350. it( 'should return null if no ranges are present', () => {
  351. expect( documentSelection.getLastPosition() ).to.be.null;
  352. } );
  353. } );
  354. describe( 'isEqual', () => {
  355. it( 'should return true if selections equal', () => {
  356. documentSelection._setTo( [ range1, range2 ] );
  357. const otherSelection = new DocumentSelection();
  358. otherSelection._setTo( [ range1, range2 ] );
  359. expect( documentSelection.isEqual( otherSelection ) ).to.be.true;
  360. } );
  361. it( 'should return true if selections equal - DocumentSelection and Selection', () => {
  362. documentSelection._setTo( [ range1, range2 ] );
  363. const otherSelection = new Selection();
  364. otherSelection.setTo( [ range1, range2 ] );
  365. expect( documentSelection.isEqual( otherSelection ) ).to.be.true;
  366. } );
  367. it( 'should return true if backward selections equal', () => {
  368. documentSelection._setTo( range1, { backward: true } );
  369. const otherSelection = new DocumentSelection( [ range1 ], { backward: true } );
  370. expect( documentSelection.isEqual( otherSelection ) ).to.be.true;
  371. } );
  372. it( 'should return true if backward selections equal - DocumentSelection and Selection', () => {
  373. documentSelection._setTo( range1, { backward: true } );
  374. const otherSelection = new Selection( [ range1 ], { backward: true } );
  375. expect( documentSelection.isEqual( otherSelection ) ).to.be.true;
  376. } );
  377. it( 'should return false if ranges count does not equal', () => {
  378. documentSelection._setTo( [ range1, range2 ] );
  379. const otherSelection = new DocumentSelection( [ range1 ] );
  380. expect( documentSelection.isEqual( otherSelection ) ).to.be.false;
  381. } );
  382. it( 'should return false if ranges count does not equal - DocumentSelection and Selection', () => {
  383. documentSelection._setTo( [ range1, range2 ] );
  384. const otherSelection = new Selection( [ range1 ] );
  385. expect( documentSelection.isEqual( otherSelection ) ).to.be.false;
  386. } );
  387. it( 'should return false if ranges (other than the last added one) do not equal', () => {
  388. documentSelection._setTo( [ range1, range3 ] );
  389. const otherSelection = new DocumentSelection( [ range2, range3 ] );
  390. expect( documentSelection.isEqual( otherSelection ) ).to.be.false;
  391. } );
  392. it( 'should return false if ranges (other than the last added one) do not equal - DocumentSelection and Selection', () => {
  393. documentSelection._setTo( [ range1, range3 ] );
  394. const otherSelection = new Selection( [ range2, range3 ] );
  395. expect( documentSelection.isEqual( otherSelection ) ).to.be.false;
  396. } );
  397. it( 'should return false if directions do not equal', () => {
  398. documentSelection._setTo( range1 );
  399. const otherSelection = new DocumentSelection( [ range1 ], { backward: true } );
  400. expect( documentSelection.isEqual( otherSelection ) ).to.be.false;
  401. } );
  402. it( 'should return false if directions do not equal - DocumentSelection and Selection', () => {
  403. documentSelection._setTo( range1 );
  404. const otherSelection = new Selection( [ range1 ], { backward: true } );
  405. expect( documentSelection.isEqual( otherSelection ) ).to.be.false;
  406. } );
  407. it( 'should return false if one selection is fake', () => {
  408. const otherSelection = new DocumentSelection( null, { fake: true } );
  409. expect( documentSelection.isEqual( otherSelection ) ).to.be.false;
  410. } );
  411. it( 'should return false if one selection is fake', () => {
  412. const otherSelection = new DocumentSelection( null, { fake: true } );
  413. expect( documentSelection.isEqual( otherSelection ) ).to.be.false;
  414. } );
  415. it( 'should return true if both selection are fake - DocumentSelection and Selection', () => {
  416. const otherSelection = new Selection( range1, { fake: true } );
  417. documentSelection._setTo( range1, { fake: true } );
  418. expect( documentSelection.isEqual( otherSelection ) ).to.be.true;
  419. } );
  420. it( 'should return false if both selection are fake but have different label', () => {
  421. const otherSelection = new DocumentSelection( [ range1 ], { fake: true, label: 'foo bar baz' } );
  422. documentSelection._setTo( range1, { fake: true, label: 'foo' } );
  423. expect( documentSelection.isEqual( otherSelection ) ).to.be.false;
  424. } );
  425. it( 'should return false if both selection are fake but have different label - DocumentSelection and Selection', () => {
  426. const otherSelection = new Selection( [ range1 ], { fake: true, label: 'foo bar baz' } );
  427. documentSelection._setTo( range1, { fake: true, label: 'foo' } );
  428. expect( documentSelection.isEqual( otherSelection ) ).to.be.false;
  429. } );
  430. it( 'should return true if both selections are empty', () => {
  431. const otherSelection = new DocumentSelection();
  432. expect( documentSelection.isEqual( otherSelection ) ).to.be.true;
  433. } );
  434. it( 'should return true if both selections are empty - DocumentSelection and Selection', () => {
  435. const otherSelection = new Selection();
  436. expect( documentSelection.isEqual( otherSelection ) ).to.be.true;
  437. } );
  438. } );
  439. describe( 'isSimilar', () => {
  440. it( 'should return true if selections equal', () => {
  441. documentSelection._setTo( [ range1, range2 ] );
  442. const otherSelection = new DocumentSelection( [ range1, range2 ] );
  443. expect( documentSelection.isSimilar( otherSelection ) ).to.be.true;
  444. } );
  445. it( 'should return true if selections equal - DocumentSelection and Selection', () => {
  446. documentSelection._setTo( [ range1, range2 ] );
  447. const otherSelection = new Selection( [ range1, range2 ] );
  448. expect( documentSelection.isSimilar( otherSelection ) ).to.be.true;
  449. } );
  450. it( 'should return false if ranges count does not equal', () => {
  451. documentSelection._setTo( [ range1, range2 ] );
  452. const otherSelection = new DocumentSelection( [ range1 ] );
  453. expect( documentSelection.isSimilar( otherSelection ) ).to.be.false;
  454. } );
  455. it( 'should return false if ranges count does not equal - DocumentSelection and Selection', () => {
  456. documentSelection._setTo( [ range1, range2 ] );
  457. const otherSelection = new Selection( [ range1 ] );
  458. expect( documentSelection.isSimilar( otherSelection ) ).to.be.false;
  459. } );
  460. it( 'should return false if trimmed ranges (other than the last added one) are not equal', () => {
  461. documentSelection._setTo( [ range1, range3 ] );
  462. const otherSelection = new DocumentSelection( [ range2, range3 ] );
  463. expect( documentSelection.isSimilar( otherSelection ) ).to.be.false;
  464. } );
  465. it( 'should return false if trimmed ranges (other than the last added one) are not equal - with Selection', () => {
  466. documentSelection._setTo( [ range1, range3 ] );
  467. const otherSelection = new Selection( [ range2, range3 ] );
  468. expect( documentSelection.isSimilar( otherSelection ) ).to.be.false;
  469. } );
  470. it( 'should return false if directions are not equal', () => {
  471. documentSelection._setTo( range1 );
  472. const otherSelection = new DocumentSelection( [ range1 ], { backward: true } );
  473. expect( documentSelection.isSimilar( otherSelection ) ).to.be.false;
  474. } );
  475. it( 'should return false if directions are not equal - DocumentSelection and Selection', () => {
  476. documentSelection._setTo( range1 );
  477. const otherSelection = new Selection( [ range1 ], { backward: true } );
  478. expect( documentSelection.isSimilar( otherSelection ) ).to.be.false;
  479. } );
  480. it( 'should return true if both selections are empty', () => {
  481. const otherSelection = new DocumentSelection();
  482. expect( documentSelection.isSimilar( otherSelection ) ).to.be.true;
  483. } );
  484. it( 'should return true if both selections are empty - DocumentSelection and Selection', () => {
  485. const otherSelection = new Selection();
  486. expect( documentSelection.isSimilar( otherSelection ) ).to.be.true;
  487. } );
  488. it( 'should return true if all ranges trimmed from both selections are equal', () => {
  489. const view = parse(
  490. '<container:p><attribute:span></attribute:span></container:p>' +
  491. '<container:p><attribute:span>xx</attribute:span></container:p>'
  492. );
  493. const p1 = view.getChild( 0 );
  494. const p2 = view.getChild( 1 );
  495. const span1 = p1.getChild( 0 );
  496. const span2 = p2.getChild( 0 );
  497. // <p>[<span>{]</span>}</p><p>[<span>{xx}</span>]</p>
  498. const rangeA1 = Range.createFromParentsAndOffsets( p1, 0, span1, 0 );
  499. const rangeB1 = Range.createFromParentsAndOffsets( span1, 0, p1, 1 );
  500. const rangeA2 = Range.createFromParentsAndOffsets( p2, 0, p2, 1 );
  501. const rangeB2 = Range.createFromParentsAndOffsets( span2, 0, span2, 1 );
  502. documentSelection._setTo( [ rangeA1, rangeA2 ] );
  503. const otherSelection = new DocumentSelection( [ rangeB2, rangeB1 ] );
  504. expect( documentSelection.isSimilar( otherSelection ) ).to.be.true;
  505. expect( otherSelection.isSimilar( documentSelection ) ).to.be.true;
  506. expect( documentSelection.isEqual( otherSelection ) ).to.be.false;
  507. expect( otherSelection.isEqual( documentSelection ) ).to.be.false;
  508. } );
  509. it( 'should return true if all ranges trimmed from both selections are equal - DocumentSelection and Selection', () => {
  510. const view = parse(
  511. '<container:p><attribute:span></attribute:span></container:p>' +
  512. '<container:p><attribute:span>xx</attribute:span></container:p>'
  513. );
  514. const p1 = view.getChild( 0 );
  515. const p2 = view.getChild( 1 );
  516. const span1 = p1.getChild( 0 );
  517. const span2 = p2.getChild( 0 );
  518. // <p>[<span>{]</span>}</p><p>[<span>{xx}</span>]</p>
  519. const rangeA1 = Range.createFromParentsAndOffsets( p1, 0, span1, 0 );
  520. const rangeB1 = Range.createFromParentsAndOffsets( span1, 0, p1, 1 );
  521. const rangeA2 = Range.createFromParentsAndOffsets( p2, 0, p2, 1 );
  522. const rangeB2 = Range.createFromParentsAndOffsets( span2, 0, span2, 1 );
  523. documentSelection._setTo( [ rangeA1, rangeA2 ] );
  524. const otherSelection = new Selection( [ rangeB2, rangeB1 ] );
  525. expect( documentSelection.isSimilar( otherSelection ) ).to.be.true;
  526. expect( otherSelection.isSimilar( documentSelection ) ).to.be.true;
  527. expect( documentSelection.isEqual( otherSelection ) ).to.be.false;
  528. expect( otherSelection.isEqual( documentSelection ) ).to.be.false;
  529. } );
  530. } );
  531. describe( '_setTo()', () => {
  532. describe( 'simple scenarios', () => {
  533. it( 'should set selection ranges from the given selection', () => {
  534. documentSelection._setTo( range1 );
  535. const otherSelection = new DocumentSelection( [ range2, range3 ], { backward: true } );
  536. documentSelection._setTo( otherSelection );
  537. expect( documentSelection.rangeCount ).to.equal( 2 );
  538. expect( documentSelection._ranges[ 0 ].isEqual( range2 ) ).to.be.true;
  539. expect( documentSelection._ranges[ 0 ] ).is.not.equal( range2 );
  540. expect( documentSelection._ranges[ 1 ].isEqual( range3 ) ).to.be.true;
  541. expect( documentSelection._ranges[ 1 ] ).is.not.equal( range3 );
  542. expect( documentSelection.anchor.isEqual( range3.end ) ).to.be.true;
  543. } );
  544. it( 'should set selection on the given Range', () => {
  545. documentSelection._setTo( range1 );
  546. expect( Array.from( documentSelection.getRanges() ) ).to.deep.equal( [ range1 ] );
  547. expect( documentSelection.isBackward ).to.be.false;
  548. } );
  549. it( 'should set selection on the given iterable of Ranges', () => {
  550. documentSelection._setTo( new Set( [ range1, range2 ] ) );
  551. expect( Array.from( documentSelection.getRanges() ) ).to.deep.equal( [ range1, range2 ] );
  552. expect( documentSelection.isBackward ).to.be.false;
  553. } );
  554. it( 'should set collapsed selection on the given Position', () => {
  555. documentSelection._setTo( range1.start );
  556. expect( Array.from( documentSelection.getRanges() ).length ).to.equal( 1 );
  557. expect( Array.from( documentSelection.getRanges() )[ 0 ].start ).to.deep.equal( range1.start );
  558. expect( documentSelection.isBackward ).to.be.false;
  559. expect( documentSelection.isCollapsed ).to.be.true;
  560. } );
  561. it( 'should fire change event', done => {
  562. documentSelection.on( 'change', () => {
  563. expect( documentSelection.rangeCount ).to.equal( 1 );
  564. expect( documentSelection.getFirstRange().isEqual( range1 ) ).to.be.true;
  565. done();
  566. } );
  567. const otherSelection = new DocumentSelection( [ range1 ] );
  568. documentSelection._setTo( otherSelection );
  569. } );
  570. it( 'should set fake state and label', () => {
  571. const label = 'foo bar baz';
  572. const otherSelection = new DocumentSelection( null, { fake: true, label } );
  573. documentSelection._setTo( otherSelection );
  574. expect( documentSelection.isFake ).to.be.true;
  575. expect( documentSelection.fakeSelectionLabel ).to.equal( label );
  576. } );
  577. it( 'should throw an error when trying to set to not selectable', () => {
  578. const otherSelection = new DocumentSelection();
  579. expect( () => {
  580. otherSelection._setTo( {} );
  581. } ).to.throw( /view-selection-setTo-not-selectable/ );
  582. } );
  583. it( 'should throw an error when trying to set to not selectable #2', () => {
  584. const otherSelection = new DocumentSelection();
  585. expect( () => {
  586. otherSelection._setTo();
  587. } ).to.throw( /view-selection-setTo-not-selectable/ );
  588. } );
  589. } );
  590. describe( 'setting collapsed selection', () => {
  591. beforeEach( () => {
  592. documentSelection._setTo( [ range1, range2 ] );
  593. } );
  594. it( 'should collapse selection at position', () => {
  595. const position = new Position( el, 4 );
  596. documentSelection._setTo( position );
  597. const range = documentSelection.getFirstRange();
  598. expect( range.start.parent ).to.equal( el );
  599. expect( range.start.offset ).to.equal( 4 );
  600. expect( range.start.isEqual( range.end ) ).to.be.true;
  601. } );
  602. it( 'should collapse selection at node and offset', () => {
  603. const foo = new Text( 'foo' );
  604. const p = new Element( 'p', null, foo );
  605. documentSelection._setTo( foo, 0 );
  606. let range = documentSelection.getFirstRange();
  607. expect( range.start.parent ).to.equal( foo );
  608. expect( range.start.offset ).to.equal( 0 );
  609. expect( range.start.isEqual( range.end ) ).to.be.true;
  610. documentSelection._setTo( p, 1 );
  611. range = documentSelection.getFirstRange();
  612. expect( range.start.parent ).to.equal( p );
  613. expect( range.start.offset ).to.equal( 1 );
  614. expect( range.start.isEqual( range.end ) ).to.be.true;
  615. } );
  616. it( 'should throw an error when the second parameter is not passed and first is an item', () => {
  617. const foo = new Text( 'foo' );
  618. expect( () => {
  619. documentSelection._setTo( foo );
  620. } ).to.throw( CKEditorError, /view-selection-setTo-required-second-parameter/ );
  621. } );
  622. it( 'should collapse selection at node and flag', () => {
  623. const foo = new Text( 'foo' );
  624. const p = new Element( 'p', null, foo );
  625. documentSelection._setTo( foo, 'end' );
  626. let range = documentSelection.getFirstRange();
  627. expect( range.start.parent ).to.equal( foo );
  628. expect( range.start.offset ).to.equal( 3 );
  629. expect( range.start.isEqual( range.end ) ).to.be.true;
  630. documentSelection._setTo( foo, 'before' );
  631. range = documentSelection.getFirstRange();
  632. expect( range.start.parent ).to.equal( p );
  633. expect( range.start.offset ).to.equal( 0 );
  634. expect( range.start.isEqual( range.end ) ).to.be.true;
  635. documentSelection._setTo( foo, 'after' );
  636. range = documentSelection.getFirstRange();
  637. expect( range.start.parent ).to.equal( p );
  638. expect( range.start.offset ).to.equal( 1 );
  639. expect( range.start.isEqual( range.end ) ).to.be.true;
  640. } );
  641. } );
  642. describe( 'setting collapsed selection at start', () => {
  643. it( 'should collapse to start position and fire change event', done => {
  644. documentSelection._setTo( [ range1, range2, range3 ] );
  645. documentSelection.once( 'change', () => {
  646. expect( documentSelection.rangeCount ).to.equal( 1 );
  647. expect( documentSelection.isCollapsed ).to.be.true;
  648. expect( documentSelection._ranges[ 0 ].start.isEqual( range2.start ) ).to.be.true;
  649. done();
  650. } );
  651. documentSelection._setTo( documentSelection.getFirstPosition() );
  652. } );
  653. } );
  654. describe( 'setting collapsed selection to end', () => {
  655. it( 'should collapse to end position and fire change event', done => {
  656. documentSelection._setTo( [ range1, range2, range3 ] );
  657. documentSelection.once( 'change', () => {
  658. expect( documentSelection.rangeCount ).to.equal( 1 );
  659. expect( documentSelection.isCollapsed ).to.be.true;
  660. expect( documentSelection._ranges[ 0 ].end.isEqual( range3.end ) ).to.be.true;
  661. done();
  662. } );
  663. documentSelection._setTo( documentSelection.getLastPosition() );
  664. } );
  665. } );
  666. describe( 'removing all ranges', () => {
  667. it( 'should remove all ranges and fire change event', done => {
  668. documentSelection._setTo( [ range1, range2 ] );
  669. documentSelection.once( 'change', () => {
  670. expect( documentSelection.rangeCount ).to.equal( 0 );
  671. done();
  672. } );
  673. documentSelection._setTo( null );
  674. } );
  675. } );
  676. describe( 'setting fake selection', () => {
  677. it( 'should allow to set selection to fake', () => {
  678. documentSelection._setTo( range1, { fake: true } );
  679. expect( documentSelection.isFake ).to.be.true;
  680. } );
  681. it( 'should allow to set fake selection label', () => {
  682. const label = 'foo bar baz';
  683. documentSelection._setTo( range1, { fake: true, label } );
  684. expect( documentSelection.fakeSelectionLabel ).to.equal( label );
  685. } );
  686. it( 'should not set label when set to false', () => {
  687. const label = 'foo bar baz';
  688. documentSelection._setTo( range1, { fake: false, label } );
  689. expect( documentSelection.fakeSelectionLabel ).to.equal( '' );
  690. } );
  691. it( 'should reset label when set to false', () => {
  692. const label = 'foo bar baz';
  693. documentSelection._setTo( range1, { fake: true, label } );
  694. documentSelection._setTo( range1 );
  695. expect( documentSelection.fakeSelectionLabel ).to.equal( '' );
  696. } );
  697. it( 'should fire change event', done => {
  698. documentSelection.once( 'change', () => {
  699. expect( documentSelection.isFake ).to.be.true;
  700. expect( documentSelection.fakeSelectionLabel ).to.equal( 'foo bar baz' );
  701. done();
  702. } );
  703. documentSelection._setTo( range1, { fake: true, label: 'foo bar baz' } );
  704. } );
  705. it( 'should be possible to create an empty fake selection', () => {
  706. documentSelection._setTo( null, { fake: true, label: 'foo bar baz' } );
  707. expect( documentSelection.fakeSelectionLabel ).to.equal( 'foo bar baz' );
  708. expect( documentSelection.isFake ).to.be.true;
  709. } );
  710. } );
  711. describe( 'setting selection to itself', () => {
  712. it( 'should correctly set ranges when setting to the same selection', () => {
  713. documentSelection._setTo( [ range1, range2 ] );
  714. documentSelection._setTo( documentSelection );
  715. const ranges = Array.from( documentSelection.getRanges() );
  716. expect( ranges.length ).to.equal( 2 );
  717. expect( ranges[ 0 ].isEqual( range1 ) ).to.be.true;
  718. expect( ranges[ 1 ].isEqual( range2 ) ).to.be.true;
  719. } );
  720. it( 'should correctly set ranges when setting to the same selection\'s ranges', () => {
  721. documentSelection._setTo( [ range1, range2 ] );
  722. documentSelection._setTo( documentSelection.getRanges() );
  723. const ranges = Array.from( documentSelection.getRanges() );
  724. expect( ranges.length ).to.equal( 2 );
  725. expect( ranges[ 0 ].isEqual( range1 ) ).to.be.true;
  726. expect( ranges[ 1 ].isEqual( range2 ) ).to.be.true;
  727. } );
  728. } );
  729. describe( 'throwing errors', () => {
  730. it( 'should throw an error when range is invalid', () => {
  731. expect( () => {
  732. documentSelection._setTo( [ { invalid: 'range' } ] );
  733. } ).to.throw( CKEditorError, 'view-selection-invalid-range: Invalid Range.' );
  734. } );
  735. it( 'should throw when range is intersecting with already added range', () => {
  736. const text = el.getChild( 0 );
  737. const range2 = Range.createFromParentsAndOffsets( text, 7, text, 15 );
  738. expect( () => {
  739. documentSelection._setTo( [ range1, range2 ] );
  740. } ).to.throw( CKEditorError, 'view-selection-range-intersects' );
  741. } );
  742. } );
  743. it( 'should allow setting selection on an item', () => {
  744. const textNode1 = new Text( 'foo' );
  745. const textNode2 = new Text( 'bar' );
  746. const textNode3 = new Text( 'baz' );
  747. const element = new Element( 'p', null, [ textNode1, textNode2, textNode3 ] );
  748. documentSelection._setTo( textNode2, 'on' );
  749. const ranges = Array.from( documentSelection.getRanges() );
  750. expect( ranges.length ).to.equal( 1 );
  751. expect( ranges[ 0 ].start.parent ).to.equal( element );
  752. expect( ranges[ 0 ].start.offset ).to.deep.equal( 1 );
  753. expect( ranges[ 0 ].end.parent ).to.equal( element );
  754. expect( ranges[ 0 ].end.offset ).to.deep.equal( 2 );
  755. } );
  756. it( 'should allow setting selection inside an element', () => {
  757. const element = new Element( 'p', null, [ new Text( 'foo' ), new Text( 'bar' ) ] );
  758. documentSelection._setTo( element, 'in' );
  759. const ranges = Array.from( documentSelection.getRanges() );
  760. expect( ranges.length ).to.equal( 1 );
  761. expect( ranges[ 0 ].start.parent ).to.equal( element );
  762. expect( ranges[ 0 ].start.offset ).to.deep.equal( 0 );
  763. expect( ranges[ 0 ].end.parent ).to.equal( element );
  764. expect( ranges[ 0 ].end.offset ).to.deep.equal( 2 );
  765. } );
  766. it( 'should allow setting backward selection inside an element', () => {
  767. const element = new Element( 'p', null, [ new Text( 'foo' ), new Text( 'bar' ) ] );
  768. documentSelection._setTo( element, 'in', { backward: true } );
  769. const ranges = Array.from( documentSelection.getRanges() );
  770. expect( ranges.length ).to.equal( 1 );
  771. expect( ranges[ 0 ].start.parent ).to.equal( element );
  772. expect( ranges[ 0 ].start.offset ).to.deep.equal( 0 );
  773. expect( ranges[ 0 ].end.parent ).to.equal( element );
  774. expect( ranges[ 0 ].end.offset ).to.deep.equal( 2 );
  775. expect( documentSelection.isBackward ).to.be.true;
  776. } );
  777. } );
  778. describe( 'getEditableElement()', () => {
  779. it( 'should return null if no ranges in selection', () => {
  780. expect( documentSelection.editableElement ).to.be.null;
  781. } );
  782. it( 'should return null if selection is placed in container that is not EditableElement', () => {
  783. documentSelection._setTo( range1 );
  784. expect( documentSelection.editableElement ).to.be.null;
  785. } );
  786. it( 'should return EditableElement when selection is placed inside', () => {
  787. const viewDocument = new Document();
  788. documentSelection._setTo( viewDocument.selection );
  789. const root = createViewRoot( viewDocument, 'div', 'main' );
  790. const element = new Element( 'p' );
  791. root._appendChild( element );
  792. documentSelection._setTo( Range.createFromParentsAndOffsets( element, 0, element, 0 ) );
  793. expect( documentSelection.editableElement ).to.equal( root );
  794. } );
  795. } );
  796. describe( 'isFake', () => {
  797. it( 'should be false for newly created instance', () => {
  798. expect( documentSelection.isFake ).to.be.false;
  799. } );
  800. } );
  801. describe( 'getSelectedElement()', () => {
  802. it( 'should return selected element', () => {
  803. const { selection: documentSelection, view } = parse( 'foo [<b>bar</b>] baz' );
  804. const b = view.getChild( 1 );
  805. expect( documentSelection.getSelectedElement() ).to.equal( b );
  806. } );
  807. it( 'should return null if there is more than one range', () => {
  808. const { selection: documentSelection } = parse( 'foo [<b>bar</b>] [<i>baz</i>]' );
  809. expect( documentSelection.getSelectedElement() ).to.be.null;
  810. } );
  811. it( 'should return null if there is no selection', () => {
  812. expect( documentSelection.getSelectedElement() ).to.be.null;
  813. } );
  814. it( 'should return null if selection is not over single element #1', () => {
  815. const { selection: documentSelection } = parse( 'foo [<b>bar</b> ba}z' );
  816. expect( documentSelection.getSelectedElement() ).to.be.null;
  817. } );
  818. it( 'should return null if selection is not over single element #2', () => {
  819. const { selection: documentSelection } = parse( 'foo <b>{bar}</b> baz' );
  820. expect( documentSelection.getSelectedElement() ).to.be.null;
  821. } );
  822. } );
  823. } );