8
0

documentselection.js 41 KB

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