8
0

documentselection.js 40 KB

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