documentselection.js 41 KB

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