selection.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: model */
  6. 'use strict';
  7. import Document from '/ckeditor5/engine/model/document.js';
  8. import Element from '/ckeditor5/engine/model/element.js';
  9. import Text from '/ckeditor5/engine/model/text.js';
  10. import Range from '/ckeditor5/engine/model/range.js';
  11. import Position from '/ckeditor5/engine/model/position.js';
  12. import LiveRange from '/ckeditor5/engine/model/liverange.js';
  13. import Selection from '/ckeditor5/engine/model/selection.js';
  14. import InsertOperation from '/ckeditor5/engine/model/operation/insertoperation.js';
  15. import MoveOperation from '/ckeditor5/engine/model/operation/moveoperation.js';
  16. import CKEditorError from '/ckeditor5/utils/ckeditorerror.js';
  17. import testUtils from '/tests/ckeditor5/_utils/utils.js';
  18. import count from '/ckeditor5/utils/count.js';
  19. testUtils.createSinonSandbox();
  20. describe( 'Selection', () => {
  21. let attrFooBar;
  22. before( () => {
  23. attrFooBar = { foo: 'bar' };
  24. } );
  25. let doc, root, selection, liveRange, range;
  26. beforeEach( () => {
  27. doc = new Document();
  28. root = doc.createRoot( 'root' );
  29. root.insertChildren( 0, [
  30. new Element( 'p' ),
  31. new Element( 'p' ),
  32. new Element( 'p', [], 'foobar' ),
  33. new Element( 'p' ),
  34. new Element( 'p' ),
  35. new Element( 'p' ),
  36. new Element( 'p', [], 'foobar' )
  37. ] );
  38. selection = doc.selection;
  39. liveRange = new LiveRange( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) );
  40. range = new Range( new Position( root, [ 2 ] ), new Position( root, [ 2, 2 ] ) );
  41. } );
  42. afterEach( () => {
  43. doc.destroy();
  44. liveRange.detach();
  45. } );
  46. it( 'should be set to default range when just created', () => {
  47. const ranges = Array.from( selection.getRanges() );
  48. expect( ranges.length ).to.equal( 1 );
  49. expect( selection.anchor.isEqual( new Position( root, [ 0 ] ) ) ).to.be.true;
  50. expect( selection.focus.isEqual( new Position( root, [ 0 ] ) ) ).to.be.true;
  51. expect( selection ).to.have.property( 'isBackward', false );
  52. expect( selection._attrs ).to.be.instanceof( Map );
  53. expect( selection._attrs.size ).to.equal( 0 );
  54. } );
  55. describe( 'isCollapsed', () => {
  56. it( 'should return true for default range', () => {
  57. expect( selection.isCollapsed ).to.be.true;
  58. } );
  59. it( 'should return true when there is single collapsed ranges', () => {
  60. selection.addRange( new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) ) );
  61. expect( selection.isCollapsed ).to.be.true;
  62. } );
  63. it( 'should return false when there are multiple ranges', () => {
  64. selection.addRange( new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) ) );
  65. selection.addRange( new Range( new Position( root, [ 2 ] ), new Position( root, [ 2 ] ) ) );
  66. expect( selection.isCollapsed ).to.be.false;
  67. } );
  68. it( 'should return false when there is not collapsed range', () => {
  69. selection.addRange( range );
  70. expect( selection.isCollapsed ).to.be.false;
  71. } );
  72. } );
  73. describe( 'rangeCount', () => {
  74. it( 'should return proper range count', () => {
  75. expect( selection.rangeCount ).to.equal( 1 );
  76. selection.addRange( new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) ) );
  77. expect( selection.rangeCount ).to.equal( 1 );
  78. selection.addRange( new Range( new Position( root, [ 2 ] ), new Position( root, [ 2 ] ) ) );
  79. expect( selection.rangeCount ).to.equal( 2 );
  80. } );
  81. } );
  82. describe( 'isBackward', () => {
  83. it( 'is defined by the last added range', () => {
  84. selection.addRange( range, true );
  85. expect( selection ).to.have.property( 'isBackward', true );
  86. selection.addRange( liveRange );
  87. expect( selection ).to.have.property( 'isBackward', false );
  88. } );
  89. it( 'is false when last range is collapsed', () => {
  90. const pos = Position.createAt( root, 0 );
  91. selection.addRange( new Range( pos, pos ), true );
  92. expect( selection.isBackward ).to.be.false;
  93. } );
  94. } );
  95. describe( 'addRange', () => {
  96. it( 'should copy added ranges and store multiple ranges', () => {
  97. selection.addRange( liveRange );
  98. selection.addRange( range );
  99. const ranges = selection._ranges;
  100. expect( ranges.length ).to.equal( 2 );
  101. expect( ranges[ 0 ].isEqual( liveRange ) ).to.be.true;
  102. expect( ranges[ 1 ].isEqual( range ) ).to.be.true;
  103. expect( ranges[ 0 ] ).not.to.be.equal( liveRange );
  104. expect( ranges[ 1 ] ).not.to.be.equal( range );
  105. } );
  106. it( 'should set anchor and focus to the start and end of the most recently added range', () => {
  107. selection.addRange( liveRange );
  108. expect( selection.anchor.path ).to.deep.equal( [ 0 ] );
  109. expect( selection.focus.path ).to.deep.equal( [ 1 ] );
  110. selection.addRange( range );
  111. expect( selection.anchor.path ).to.deep.equal( [ 2 ] );
  112. expect( selection.focus.path ).to.deep.equal( [ 2, 2 ] );
  113. } );
  114. it( 'should set anchor and focus to the end and start of the most recently added range if backward flag was used', () => {
  115. selection.addRange( liveRange, true );
  116. expect( selection.anchor.path ).to.deep.equal( [ 1 ] );
  117. expect( selection.focus.path ).to.deep.equal( [ 0 ] );
  118. selection.addRange( range, true );
  119. expect( selection.anchor.path ).to.deep.equal( [ 2, 2 ] );
  120. expect( selection.focus.path ).to.deep.equal( [ 2 ] );
  121. } );
  122. it( 'should return a copy of (not a reference to) array of stored ranges', () => {
  123. selection.addRange( liveRange );
  124. const ranges = Array.from( selection.getRanges() );
  125. selection.addRange( range );
  126. expect( ranges.length ).to.equal( 1 );
  127. expect( ranges[ 0 ].isEqual( liveRange ) ).to.be.true;
  128. } );
  129. it( 'should convert added Range to LiveRange', () => {
  130. selection.addRange( range );
  131. const ranges = selection._ranges;
  132. expect( ranges[ 0 ] ).to.be.instanceof( LiveRange );
  133. } );
  134. it( 'should fire change:range event when adding a range', () => {
  135. let spy = sinon.spy();
  136. selection.on( 'change:range', spy );
  137. selection.addRange( range );
  138. expect( spy.called ).to.be.true;
  139. } );
  140. it( 'should unbind all events when destroyed', () => {
  141. selection.addRange( liveRange );
  142. selection.addRange( range );
  143. const ranges = selection._ranges;
  144. sinon.spy( ranges[ 0 ], 'detach' );
  145. sinon.spy( ranges[ 1 ], 'detach' );
  146. selection.destroy();
  147. expect( ranges[ 0 ].detach.called ).to.be.true;
  148. expect( ranges[ 1 ].detach.called ).to.be.true;
  149. ranges[ 0 ].detach.restore();
  150. ranges[ 1 ].detach.restore();
  151. } );
  152. it( 'should throw an error if added range intersects with already stored range', () => {
  153. selection.addRange( liveRange );
  154. expect( () => {
  155. selection.addRange(
  156. new Range(
  157. new Position( root, [ 0, 4 ] ),
  158. new Position( root, [ 1, 2 ] )
  159. )
  160. );
  161. } ).to.throw( CKEditorError, /selection-range-intersects/ );
  162. } );
  163. } );
  164. describe( 'collapse', () => {
  165. it( 'detaches all existing ranges', () => {
  166. selection.addRange( range );
  167. selection.addRange( liveRange );
  168. const spy = testUtils.sinon.spy( LiveRange.prototype, 'detach' );
  169. selection.collapse( root );
  170. expect( spy.calledTwice ).to.be.true;
  171. } );
  172. it( 'fires change:range', () => {
  173. const spy = sinon.spy();
  174. selection.on( 'change:range', spy );
  175. selection.collapse( root );
  176. expect( spy.calledOnce ).to.be.true;
  177. } );
  178. it( 'sets selection at the 0 offset if second parameter not passed', () => {
  179. selection.collapse( root );
  180. expect( selection ).to.have.property( 'isCollapsed', true );
  181. const focus = selection.focus;
  182. expect( focus ).to.have.property( 'parent', root );
  183. expect( focus ).to.have.property( 'offset', 0 );
  184. } );
  185. it( 'sets selection at given offset in given parent', () => {
  186. selection.collapse( root, 3 );
  187. expect( selection ).to.have.property( 'isCollapsed', true );
  188. const focus = selection.focus;
  189. expect( focus ).to.have.property( 'parent', root );
  190. expect( focus ).to.have.property( 'offset', 3 );
  191. } );
  192. it( 'sets selection at the end of the given parent', () => {
  193. selection.collapse( root, 'END' );
  194. expect( selection ).to.have.property( 'isCollapsed', true );
  195. const focus = selection.focus;
  196. expect( focus ).to.have.property( 'parent', root );
  197. expect( focus ).to.have.property( 'offset', root.getChildCount() );
  198. } );
  199. it( 'sets selection before the specified element', () => {
  200. selection.collapse( root.getChild( 1 ), 'BEFORE' );
  201. expect( selection ).to.have.property( 'isCollapsed', true );
  202. const focus = selection.focus;
  203. expect( focus ).to.have.property( 'parent', root );
  204. expect( focus ).to.have.property( 'offset', 1 );
  205. } );
  206. it( 'sets selection after the specified element', () => {
  207. selection.collapse( root.getChild( 1 ), 'AFTER' );
  208. expect( selection ).to.have.property( 'isCollapsed', true );
  209. const focus = selection.focus;
  210. expect( focus ).to.have.property( 'parent', root );
  211. expect( focus ).to.have.property( 'offset', 2 );
  212. } );
  213. it( 'sets selection at the specified position', () => {
  214. const pos = Position.createFromParentAndOffset( root, 3 );
  215. selection.collapse( pos );
  216. expect( selection ).to.have.property( 'isCollapsed', true );
  217. const focus = selection.focus;
  218. expect( focus ).to.have.property( 'parent', root );
  219. expect( focus ).to.have.property( 'offset', 3 );
  220. } );
  221. } );
  222. describe( 'setFocus', () => {
  223. it( 'keeps all existing ranges and fires no change:range when no modifications needed', () => {
  224. selection.addRange( range );
  225. selection.addRange( liveRange );
  226. const spy = sinon.spy();
  227. selection.on( 'change:range', spy );
  228. selection.setFocus( selection.focus );
  229. expect( count( selection.getRanges() ) ).to.equal( 2 );
  230. expect( spy.callCount ).to.equal( 0 );
  231. } );
  232. it( 'fires change:range', () => {
  233. selection.addRange( range );
  234. const spy = sinon.spy();
  235. selection.on( 'change:range', spy );
  236. selection.setFocus( Position.createAt( root, 'END' ) );
  237. expect( spy.calledOnce ).to.be.true;
  238. } );
  239. it( 'modifies default range', () => {
  240. const startPos = selection.getFirstPosition();
  241. const endPos = Position.createAt( root, 'END' );
  242. selection.setFocus( endPos );
  243. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'SAME' );
  244. expect( selection.focus.compareWith( endPos ) ).to.equal( 'SAME' );
  245. } );
  246. it( 'modifies existing collapsed selection', () => {
  247. const startPos = Position.createAt( root, 1 );
  248. const endPos = Position.createAt( root, 2 );
  249. selection.collapse( startPos );
  250. selection.setFocus( endPos );
  251. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'SAME' );
  252. expect( selection.focus.compareWith( endPos ) ).to.equal( 'SAME' );
  253. } );
  254. it( 'makes existing collapsed selection a backward selection', () => {
  255. const startPos = Position.createAt( root, 1 );
  256. const endPos = Position.createAt( root, 0 );
  257. selection.collapse( startPos );
  258. selection.setFocus( endPos );
  259. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'SAME' );
  260. expect( selection.focus.compareWith( endPos ) ).to.equal( 'SAME' );
  261. expect( selection.isBackward ).to.be.true;
  262. } );
  263. it( 'modifies existing non-collapsed selection', () => {
  264. const startPos = Position.createAt( root, 1 );
  265. const endPos = Position.createAt( root, 2 );
  266. const newEndPos = Position.createAt( root, 3 );
  267. selection.addRange( new Range( startPos, endPos ) );
  268. selection.setFocus( newEndPos );
  269. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'SAME' );
  270. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'SAME' );
  271. } );
  272. it( 'makes existing non-collapsed selection a backward selection', () => {
  273. const startPos = Position.createAt( root, 1 );
  274. const endPos = Position.createAt( root, 2 );
  275. const newEndPos = Position.createAt( root, 0 );
  276. selection.addRange( new Range( startPos, endPos ) );
  277. selection.setFocus( newEndPos );
  278. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'SAME' );
  279. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'SAME' );
  280. expect( selection.isBackward ).to.be.true;
  281. } );
  282. it( 'makes existing backward selection a forward selection', () => {
  283. const startPos = Position.createAt( root, 1 );
  284. const endPos = Position.createAt( root, 2 );
  285. const newEndPos = Position.createAt( root, 3 );
  286. selection.addRange( new Range( startPos, endPos ), true );
  287. selection.setFocus( newEndPos );
  288. expect( selection.anchor.compareWith( endPos ) ).to.equal( 'SAME' );
  289. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'SAME' );
  290. expect( selection.isBackward ).to.be.false;
  291. } );
  292. it( 'modifies existing backward selection', () => {
  293. const startPos = Position.createAt( root, 1 );
  294. const endPos = Position.createAt( root, 2 );
  295. const newEndPos = Position.createAt( root, 0 );
  296. selection.addRange( new Range( startPos, endPos ), true );
  297. selection.setFocus( newEndPos );
  298. expect( selection.anchor.compareWith( endPos ) ).to.equal( 'SAME' );
  299. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'SAME' );
  300. expect( selection.isBackward ).to.be.true;
  301. } );
  302. it( 'modifies only the last range', () => {
  303. // Offsets are chosen in this way that the order of adding ranges must count, not their document order.
  304. const startPos1 = Position.createAt( root, 4 );
  305. const endPos1 = Position.createAt( root, 5 );
  306. const startPos2 = Position.createAt( root, 1 );
  307. const endPos2 = Position.createAt( root, 2 );
  308. const newEndPos = Position.createAt( root, 0 );
  309. selection.addRange( new Range( startPos1, endPos1 ) );
  310. selection.addRange( new Range( startPos2, endPos2 ) );
  311. const spy = sinon.spy();
  312. selection.on( 'change:range', spy );
  313. selection.setFocus( newEndPos );
  314. const ranges = Array.from( selection.getRanges() );
  315. expect( ranges ).to.have.lengthOf( 2 );
  316. expect( ranges[ 0 ].start.compareWith( startPos1 ) ).to.equal( 'SAME' );
  317. expect( ranges[ 0 ].end.compareWith( endPos1 ) ).to.equal( 'SAME' );
  318. expect( selection.anchor.compareWith( startPos2 ) ).to.equal( 'SAME' );
  319. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'SAME' );
  320. expect( selection.isBackward ).to.be.true;
  321. expect( spy.calledOnce ).to.be.true;
  322. } );
  323. it( 'collapses the selection when extending to the anchor', () => {
  324. const startPos = Position.createAt( root, 1 );
  325. const endPos = Position.createAt( root, 2 );
  326. selection.addRange( new Range( startPos, endPos ) );
  327. selection.setFocus( startPos );
  328. expect( selection.focus.compareWith( startPos ) ).to.equal( 'SAME' );
  329. expect( selection.isCollapsed ).to.be.true;
  330. } );
  331. it( 'uses Position.createAt', () => {
  332. const startPos = Position.createAt( root, 1 );
  333. const endPos = Position.createAt( root, 2 );
  334. const newEndPos = Position.createAt( root, 4 );
  335. const spy = testUtils.sinon.stub( Position, 'createAt', () => newEndPos );
  336. selection.addRange( new Range( startPos, endPos ) );
  337. selection.setFocus( root, 'END' );
  338. expect( spy.calledOnce ).to.be.true;
  339. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'SAME' );
  340. } );
  341. it( 'detaches the range it replaces', () => {
  342. const startPos = Position.createAt( root, 1 );
  343. const endPos = Position.createAt( root, 2 );
  344. const newEndPos = Position.createAt( root, 4 );
  345. const spy = testUtils.sinon.spy( LiveRange.prototype, 'detach' );
  346. selection.addRange( new Range( startPos, endPos ) );
  347. selection.setFocus( newEndPos );
  348. expect( spy.calledOnce ).to.be.true;
  349. } );
  350. } );
  351. describe( 'removeAllRanges', () => {
  352. let spy, ranges;
  353. beforeEach( () => {
  354. selection.addRange( liveRange );
  355. selection.addRange( range );
  356. spy = sinon.spy();
  357. selection.on( 'change:range', spy );
  358. ranges = selection._ranges;
  359. sinon.spy( ranges[ 0 ], 'detach' );
  360. sinon.spy( ranges[ 1 ], 'detach' );
  361. selection.removeAllRanges();
  362. } );
  363. afterEach( () => {
  364. ranges[ 0 ].detach.restore();
  365. ranges[ 1 ].detach.restore();
  366. } );
  367. it( 'should remove all stored ranges (and reset to default range)', () => {
  368. expect( Array.from( selection.getRanges() ).length ).to.equal( 1 );
  369. expect( selection.anchor.isEqual( new Position( root, [ 0 ] ) ) ).to.be.true;
  370. expect( selection.focus.isEqual( new Position( root, [ 0 ] ) ) ).to.be.true;
  371. } );
  372. it( 'should fire exactly one update event', () => {
  373. expect( spy.calledOnce ).to.be.true;
  374. } );
  375. it( 'should detach ranges', () => {
  376. expect( ranges[ 0 ].detach.called ).to.be.true;
  377. expect( ranges[ 1 ].detach.called ).to.be.true;
  378. } );
  379. } );
  380. describe( 'setRanges', () => {
  381. let newRanges, spy, oldRanges;
  382. before( () => {
  383. newRanges = [
  384. new Range( new Position( root, [ 4 ] ), new Position( root, [ 5 ] ) ),
  385. new Range( new Position( root, [ 5, 0 ] ), new Position( root, [ 6, 0 ] ) )
  386. ];
  387. } );
  388. beforeEach( () => {
  389. selection.addRange( liveRange );
  390. selection.addRange( range );
  391. spy = sinon.spy();
  392. selection.on( 'change:range', spy );
  393. oldRanges = selection._ranges;
  394. sinon.spy( oldRanges[ 0 ], 'detach' );
  395. sinon.spy( oldRanges[ 1 ], 'detach' );
  396. } );
  397. afterEach( () => {
  398. oldRanges[ 0 ].detach.restore();
  399. oldRanges[ 1 ].detach.restore();
  400. } );
  401. it( 'should remove all ranges and add given ranges', () => {
  402. selection.setRanges( newRanges );
  403. let ranges = selection._ranges;
  404. expect( ranges.length ).to.equal( 2 );
  405. expect( ranges[ 0 ].isEqual( newRanges[ 0 ] ) ).to.be.true;
  406. expect( ranges[ 1 ].isEqual( newRanges[ 1 ] ) ).to.be.true;
  407. } );
  408. it( 'should use last range from given array to get anchor and focus position', () => {
  409. selection.setRanges( newRanges );
  410. expect( selection.anchor.path ).to.deep.equal( [ 5, 0 ] );
  411. expect( selection.focus.path ).to.deep.equal( [ 6, 0 ] );
  412. } );
  413. it( 'should acknowledge backward flag when setting anchor and focus', () => {
  414. selection.setRanges( newRanges, true );
  415. expect( selection.anchor.path ).to.deep.equal( [ 6, 0 ] );
  416. expect( selection.focus.path ).to.deep.equal( [ 5, 0 ] );
  417. } );
  418. it( 'should fire exactly one update event', () => {
  419. selection.setRanges( newRanges );
  420. expect( spy.calledOnce ).to.be.true;
  421. } );
  422. it( 'should detach removed LiveRanges', () => {
  423. selection.setRanges( newRanges );
  424. expect( oldRanges[ 0 ].detach.called ).to.be.true;
  425. expect( oldRanges[ 1 ].detach.called ).to.be.true;
  426. } );
  427. } );
  428. describe( 'getFirstRange', () => {
  429. it( 'should return a range which start position is before all other ranges\' start positions', () => {
  430. // This will not be the first range despite being added as first
  431. selection.addRange( new Range( new Position( root, [ 4 ] ), new Position( root, [ 5 ] ) ) );
  432. // This should be the first range.
  433. selection.addRange( new Range( new Position( root, [ 1 ] ), new Position( root, [ 4 ] ) ) );
  434. // A random range that is not first.
  435. selection.addRange( new Range( new Position( root, [ 6 ] ), new Position( root, [ 7 ] ) ) );
  436. let range = selection.getFirstRange();
  437. expect( range.start.path ).to.deep.equal( [ 1 ] );
  438. expect( range.end.path ).to.deep.equal( [ 4 ] );
  439. } );
  440. } );
  441. describe( 'getFirstPosition', () => {
  442. it( 'should return a position that is in selection and is before any other position from the selection', () => {
  443. // This will not be a range containing the first position despite being added as first
  444. selection.addRange( new Range( new Position( root, [ 4 ] ), new Position( root, [ 5 ] ) ) );
  445. // This should be the first range.
  446. selection.addRange( new Range( new Position( root, [ 1 ] ), new Position( root, [ 4 ] ) ) );
  447. // A random range that is not first.
  448. selection.addRange( new Range( new Position( root, [ 6 ] ), new Position( root, [ 7 ] ) ) );
  449. let position = selection.getFirstPosition();
  450. expect( position.path ).to.deep.equal( [ 1 ] );
  451. } );
  452. } );
  453. // Selection uses LiveRanges so here are only simple test to see if integration is
  454. // working well, without getting into complicated corner cases.
  455. describe( 'after applying an operation should get updated and not fire update event', () => {
  456. let spy;
  457. beforeEach( () => {
  458. root.insertChildren( 0, [ new Element( 'ul', [], 'abcdef' ), new Element( 'p', [], 'foobar' ), 'xyz' ] );
  459. selection.addRange( new Range( new Position( root, [ 0, 2 ] ), new Position( root, [ 1, 4 ] ) ) );
  460. spy = sinon.spy();
  461. selection.on( 'change:range', spy );
  462. } );
  463. describe( 'InsertOperation', () => {
  464. it( 'before selection', () => {
  465. doc.applyOperation(
  466. new InsertOperation(
  467. new Position( root, [ 0, 1 ] ),
  468. 'xyz',
  469. doc.version
  470. )
  471. );
  472. let range = selection._ranges[ 0 ];
  473. expect( range.start.path ).to.deep.equal( [ 0, 5 ] );
  474. expect( range.end.path ).to.deep.equal( [ 1, 4 ] );
  475. expect( spy.called ).to.be.false;
  476. } );
  477. it( 'inside selection', () => {
  478. doc.applyOperation(
  479. new InsertOperation(
  480. new Position( root, [ 1, 0 ] ),
  481. 'xyz',
  482. doc.version
  483. )
  484. );
  485. let range = selection._ranges[ 0 ];
  486. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  487. expect( range.end.path ).to.deep.equal( [ 1, 7 ] );
  488. expect( spy.called ).to.be.false;
  489. } );
  490. } );
  491. describe( 'MoveOperation', () => {
  492. it( 'move range from before a selection', () => {
  493. doc.applyOperation(
  494. new MoveOperation(
  495. new Position( root, [ 0, 0 ] ),
  496. 2,
  497. new Position( root, [ 2 ] ),
  498. doc.version
  499. )
  500. );
  501. let range = selection._ranges[ 0 ];
  502. expect( range.start.path ).to.deep.equal( [ 0, 0 ] );
  503. expect( range.end.path ).to.deep.equal( [ 1, 4 ] );
  504. expect( spy.called ).to.be.false;
  505. } );
  506. it( 'moved into before a selection', () => {
  507. doc.applyOperation(
  508. new MoveOperation(
  509. new Position( root, [ 2 ] ),
  510. 2,
  511. new Position( root, [ 0, 0 ] ),
  512. doc.version
  513. )
  514. );
  515. let range = selection._ranges[ 0 ];
  516. expect( range.start.path ).to.deep.equal( [ 0, 4 ] );
  517. expect( range.end.path ).to.deep.equal( [ 1, 4 ] );
  518. expect( spy.called ).to.be.false;
  519. } );
  520. it( 'move range from inside of selection', () => {
  521. doc.applyOperation(
  522. new MoveOperation(
  523. new Position( root, [ 1, 0 ] ),
  524. 2,
  525. new Position( root, [ 2 ] ),
  526. doc.version
  527. )
  528. );
  529. let range = selection._ranges[ 0 ];
  530. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  531. expect( range.end.path ).to.deep.equal( [ 1, 2 ] );
  532. expect( spy.called ).to.be.false;
  533. } );
  534. it( 'moved range intersects with selection', () => {
  535. doc.applyOperation(
  536. new MoveOperation(
  537. new Position( root, [ 1, 3 ] ),
  538. 2,
  539. new Position( root, [ 4 ] ),
  540. doc.version
  541. )
  542. );
  543. let range = selection._ranges[ 0 ];
  544. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  545. expect( range.end.path ).to.deep.equal( [ 1, 3 ] );
  546. expect( spy.called ).to.be.false;
  547. } );
  548. it( 'split inside selection (do not break selection)', () => {
  549. doc.applyOperation(
  550. new InsertOperation(
  551. new Position( root, [ 2 ] ),
  552. new Element( 'p' ),
  553. doc.version
  554. )
  555. );
  556. doc.applyOperation(
  557. new MoveOperation(
  558. new Position( root, [ 1, 2 ] ),
  559. 4,
  560. new Position( root, [ 2, 0 ] ),
  561. doc.version
  562. )
  563. );
  564. let range = selection._ranges[ 0 ];
  565. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  566. expect( range.end.path ).to.deep.equal( [ 2, 2 ] );
  567. expect( spy.called ).to.be.false;
  568. } );
  569. } );
  570. } );
  571. describe( 'attributes interface', () => {
  572. let fullP, emptyP, rangeInFullP, rangeInEmptyP;
  573. beforeEach( () => {
  574. root.insertChildren( 0, [
  575. new Element( 'p', [], 'foobar' ),
  576. new Element( 'p', [], [] )
  577. ] );
  578. fullP = root.getChild( 0 );
  579. emptyP = root.getChild( 1 );
  580. rangeInFullP = new Range( new Position( root, [ 0, 4 ] ), new Position( root, [ 0, 4 ] ) );
  581. rangeInEmptyP = new Range( new Position( root, [ 1, 0 ] ), new Position( root, [ 1, 0 ] ) );
  582. } );
  583. describe( 'setAttribute', () => {
  584. it( 'should set given attribute on the selection', () => {
  585. selection.setRanges( [ rangeInFullP ] );
  586. selection.setAttribute( 'foo', 'bar' );
  587. expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  588. expect( fullP.hasAttribute( Selection._getStoreAttributeKey( 'foo' ) ) ).to.be.false;
  589. } );
  590. it( 'should store attribute if the selection is in empty node', () => {
  591. selection.setRanges( [ rangeInEmptyP ] );
  592. selection.setAttribute( 'foo', 'bar' );
  593. expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  594. expect( emptyP.getAttribute( Selection._getStoreAttributeKey( 'foo' ) ) ).to.equal( 'bar' );
  595. } );
  596. it( 'should fire change:attribute event', () => {
  597. let spy = sinon.spy();
  598. selection.on( 'change:attribute', spy );
  599. selection.setAttribute( 'foo', 'bar' );
  600. expect( spy.called ).to.be.true;
  601. } );
  602. } );
  603. describe( 'hasAttribute', () => {
  604. it( 'should return true if element contains attribute with given key', () => {
  605. selection.setRanges( [ rangeInFullP ] );
  606. selection.setAttribute( 'foo', 'bar' );
  607. expect( selection.hasAttribute( 'foo' ) ).to.be.true;
  608. } );
  609. it( 'should return false if element does not contain attribute with given key', () => {
  610. expect( selection.hasAttribute( 'abc' ) ).to.be.false;
  611. } );
  612. } );
  613. describe( 'getAttribute', () => {
  614. it( 'should return undefined if element does not contain given attribute', () => {
  615. expect( selection.getAttribute( 'abc' ) ).to.be.undefined;
  616. } );
  617. } );
  618. describe( 'getAttributes', () => {
  619. it( 'should return an iterator that iterates over all attributes set on the text fragment', () => {
  620. selection.setRanges( [ rangeInFullP ] );
  621. selection.setAttribute( 'foo', 'bar' );
  622. selection.setAttribute( 'abc', 'xyz' );
  623. let attrs = Array.from( selection.getAttributes() );
  624. expect( attrs ).to.deep.equal( [ [ 'foo', 'bar' ], [ 'abc', 'xyz' ] ] );
  625. } );
  626. } );
  627. describe( 'setAttributesTo', () => {
  628. it( 'should remove all attributes set on element and set the given ones', () => {
  629. selection.setRanges( [ rangeInFullP ] );
  630. selection.setAttribute( 'abc', 'xyz' );
  631. selection.setAttributesTo( { foo: 'bar' } );
  632. expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  633. expect( selection.getAttribute( 'abc' ) ).to.be.undefined;
  634. expect( fullP.hasAttribute( Selection._getStoreAttributeKey( 'foo' ) ) ).to.be.false;
  635. expect( fullP.hasAttribute( Selection._getStoreAttributeKey( 'abc' ) ) ).to.be.false;
  636. } );
  637. it( 'should remove all stored attributes and store the given ones if the selection is in empty node', () => {
  638. selection.setRanges( [ rangeInEmptyP ] );
  639. selection.setAttribute( 'abc', 'xyz' );
  640. selection.setAttributesTo( { foo: 'bar' } );
  641. expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  642. expect( selection.getAttribute( 'abc' ) ).to.be.undefined;
  643. expect( emptyP.getAttribute( Selection._getStoreAttributeKey( 'foo' ) ) ).to.equal( 'bar' );
  644. expect( emptyP.hasAttribute( Selection._getStoreAttributeKey( 'abc' ) ) ).to.be.false;
  645. } );
  646. it( 'should fire change:attribute event', () => {
  647. let spy = sinon.spy();
  648. selection.on( 'change:attribute', spy );
  649. selection.setAttributesTo( { foo: 'bar' } );
  650. expect( spy.called ).to.be.true;
  651. } );
  652. } );
  653. describe( 'removeAttribute', () => {
  654. it( 'should remove attribute set on the text fragment', () => {
  655. selection.setRanges( [ rangeInFullP ] );
  656. selection.setAttribute( 'foo', 'bar' );
  657. selection.removeAttribute( 'foo' );
  658. expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
  659. expect( fullP.hasAttribute( Selection._getStoreAttributeKey( 'foo' ) ) ).to.be.false;
  660. } );
  661. it( 'should remove stored attribute if the selection is in empty node', () => {
  662. selection.setRanges( [ rangeInEmptyP ] );
  663. selection.setAttribute( 'foo', 'bar' );
  664. selection.removeAttribute( 'foo' );
  665. expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
  666. expect( emptyP.hasAttribute( Selection._getStoreAttributeKey( 'foo' ) ) ).to.be.false;
  667. } );
  668. it( 'should fire change:attribute event', () => {
  669. let spy = sinon.spy();
  670. selection.on( 'change:attribute', spy );
  671. selection.removeAttribute( 'foo' );
  672. expect( spy.called ).to.be.true;
  673. } );
  674. } );
  675. describe( 'clearAttributes', () => {
  676. it( 'should remove all attributes from the element', () => {
  677. selection.setRanges( [ rangeInFullP ] );
  678. selection.setAttribute( 'foo', 'bar' );
  679. selection.setAttribute( 'abc', 'xyz' );
  680. selection.clearAttributes();
  681. expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
  682. expect( selection.getAttribute( 'abc' ) ).to.be.undefined;
  683. expect( fullP.hasAttribute( Selection._getStoreAttributeKey( 'foo' ) ) ).to.be.false;
  684. expect( fullP.hasAttribute( Selection._getStoreAttributeKey( 'abc' ) ) ).to.be.false;
  685. } );
  686. it( 'should remove all stored attributes if the selection is in empty node', () => {
  687. selection.setRanges( [ rangeInEmptyP ] );
  688. selection.setAttribute( 'foo', 'bar' );
  689. selection.setAttribute( 'abc', 'xyz' );
  690. selection.clearAttributes();
  691. expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
  692. expect( selection.getAttribute( 'abc' ) ).to.be.undefined;
  693. expect( emptyP.hasAttribute( Selection._getStoreAttributeKey( 'foo' ) ) ).to.be.false;
  694. expect( emptyP.hasAttribute( Selection._getStoreAttributeKey( 'abc' ) ) ).to.be.false;
  695. } );
  696. it( 'should fire change:attribute event', () => {
  697. let spy = sinon.spy();
  698. selection.on( 'change:attribute', spy );
  699. selection.clearAttributes();
  700. expect( spy.called ).to.be.true;
  701. } );
  702. } );
  703. } );
  704. describe( '_updateAttributes', () => {
  705. beforeEach( () => {
  706. root.insertChildren( 0, [
  707. new Element( 'p', { p: true } ),
  708. new Text( 'a', { a: true } ),
  709. new Element( 'p', { p: true } ),
  710. new Text( 'b', { b: true } ),
  711. new Text( 'c', { c: true } ),
  712. new Element( 'p', [], [
  713. new Text( 'd', { d: true } )
  714. ] ),
  715. new Element( 'p', { p: true } ),
  716. new Text( 'e', { e: true } )
  717. ] );
  718. } );
  719. it( 'if selection is a range, should find first character in it and copy it\'s attributes', () => {
  720. selection.setRanges( [ new Range( new Position( root, [ 2 ] ), new Position( root, [ 5 ] ) ) ] );
  721. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'b', true ] ] );
  722. // Step into elements when looking for first character:
  723. selection.setRanges( [ new Range( new Position( root, [ 5 ] ), new Position( root, [ 7 ] ) ) ] );
  724. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'd', true ] ] );
  725. } );
  726. it( 'if selection is collapsed it should seek a character to copy that character\'s attributes', () => {
  727. // Take styles from character before selection.
  728. selection.setRanges( [ new Range( new Position( root, [ 2 ] ), new Position( root, [ 2 ] ) ) ] );
  729. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'a', true ] ] );
  730. // If there are none,
  731. // Take styles from character after selection.
  732. selection.setRanges( [ new Range( new Position( root, [ 3 ] ), new Position( root, [ 3 ] ) ) ] );
  733. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'b', true ] ] );
  734. // If there are none,
  735. // Look from the selection position to the beginning of node looking for character to take attributes from.
  736. selection.setRanges( [ new Range( new Position( root, [ 6 ] ), new Position( root, [ 6 ] ) ) ] );
  737. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'c', true ] ] );
  738. // If there are none,
  739. // Look from the selection position to the end of node looking for character to take attributes from.
  740. selection.setRanges( [ new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) ) ] );
  741. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'a', true ] ] );
  742. // If there are no characters to copy attributes from, use stored attributes.
  743. selection.setRanges( [ new Range( new Position( root, [ 0, 0 ] ), new Position( root, [ 0, 0 ] ) ) ] );
  744. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [] );
  745. } );
  746. it( 'should fire change:attribute event', () => {
  747. let spy = sinon.spy();
  748. selection.on( 'change:attribute', spy );
  749. selection.setRanges( [ new Range( new Position( root, [ 2 ] ), new Position( root, [ 5 ] ) ) ] );
  750. expect( spy.called ).to.be.true;
  751. } );
  752. } );
  753. describe( '_getStoredAttributes', () => {
  754. it( 'should return no values if there are no ranges in selection', () => {
  755. let values = Array.from( selection._getStoredAttributes() );
  756. expect( values ).to.deep.equal( [] );
  757. } );
  758. } );
  759. } );