selection.js 34 KB

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