documentselection.js 41 KB

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