8
0

documentselection.js 41 KB

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