selection.js 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Document from '../../src/model/document';
  6. import Element from '../../src/model/element';
  7. import Text from '../../src/model/text';
  8. import Range from '../../src/model/range';
  9. import Position from '../../src/model/position';
  10. import LiveRange from '../../src/model/liverange';
  11. import Selection from '../../src/model/selection';
  12. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  13. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  14. import count from '@ckeditor/ckeditor5-utils/src/count';
  15. import { parse, setData } from '../../src/dev-utils/model';
  16. import Schema from '../../src/model/schema';
  17. testUtils.createSinonSandbox();
  18. describe( 'Selection', () => {
  19. let doc, root, selection, liveRange, range, range1, range2, range3;
  20. beforeEach( () => {
  21. doc = new Document();
  22. root = doc.createRoot();
  23. root.appendChildren( [
  24. new Element( 'p' ),
  25. new Element( 'p' ),
  26. new Element( 'p', [], new Text( 'foobar' ) ),
  27. new Element( 'p' ),
  28. new Element( 'p' ),
  29. new Element( 'p' ),
  30. new Element( 'p', [], new Text( 'foobar' ) )
  31. ] );
  32. selection = new Selection();
  33. liveRange = new LiveRange( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) );
  34. range = new Range( new Position( root, [ 2 ] ), new Position( root, [ 2, 2 ] ) );
  35. range1 = new Range( new Position( root, [ 1 ] ), new Position( root, [ 4 ] ) );
  36. range2 = new Range( new Position( root, [ 4 ] ), new Position( root, [ 5 ] ) );
  37. range3 = new Range( new Position( root, [ 6 ] ), new Position( root, [ 7 ] ) );
  38. } );
  39. afterEach( () => {
  40. doc.destroy();
  41. liveRange.detach();
  42. } );
  43. describe( 'constructor()', () => {
  44. it( 'should be able to create an empty selection', () => {
  45. const selection = new Selection();
  46. expect( Array.from( selection.getRanges() ) ).to.deep.equal( [] );
  47. } );
  48. it( 'should be able to create a selection from the given ranges', () => {
  49. const ranges = [ range1, range2, range3 ];
  50. const selection = new Selection( ranges );
  51. expect( Array.from( selection.getRanges() ) ).to.deep.equal( ranges );
  52. } );
  53. it( 'should be able to create a selection from the given ranges and isLastBackward flag', () => {
  54. const ranges = [ range1, range2, range3 ];
  55. const selection = new Selection( ranges, true );
  56. expect( selection.isBackward ).to.be.true;
  57. } );
  58. } );
  59. describe( 'isCollapsed', () => {
  60. it( 'should return false for empty selection', () => {
  61. expect( selection.isCollapsed ).to.be.false;
  62. } );
  63. it( 'should return true when there is single collapsed ranges', () => {
  64. selection.addRange( new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) ) );
  65. expect( selection.isCollapsed ).to.be.true;
  66. } );
  67. it( 'should return false when there are multiple ranges', () => {
  68. selection.addRange( new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) ) );
  69. selection.addRange( new Range( new Position( root, [ 2 ] ), new Position( root, [ 2 ] ) ) );
  70. expect( selection.isCollapsed ).to.be.false;
  71. } );
  72. it( 'should return false when there is not collapsed range', () => {
  73. selection.addRange( range );
  74. expect( selection.isCollapsed ).to.be.false;
  75. } );
  76. } );
  77. describe( 'rangeCount', () => {
  78. it( 'should return proper range count', () => {
  79. expect( selection.rangeCount ).to.equal( 0 );
  80. selection.addRange( new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) ) );
  81. expect( selection.rangeCount ).to.equal( 1 );
  82. selection.addRange( new Range( new Position( root, [ 2 ] ), new Position( root, [ 2 ] ) ) );
  83. expect( selection.rangeCount ).to.equal( 2 );
  84. } );
  85. } );
  86. describe( 'isBackward', () => {
  87. it( 'is defined by the last added range', () => {
  88. selection.addRange( range, true );
  89. expect( selection ).to.have.property( 'isBackward', true );
  90. selection.addRange( liveRange );
  91. expect( selection ).to.have.property( 'isBackward', false );
  92. } );
  93. it( 'is false when last range is collapsed', () => {
  94. const pos = Position.createAt( root, 0 );
  95. selection.addRange( new Range( pos, pos ), true );
  96. expect( selection.isBackward ).to.be.false;
  97. } );
  98. } );
  99. describe( 'focus', () => {
  100. let r3;
  101. beforeEach( () => {
  102. const r1 = Range.createFromParentsAndOffsets( root, 2, root, 4 );
  103. const r2 = Range.createFromParentsAndOffsets( root, 4, root, 6 );
  104. r3 = Range.createFromParentsAndOffsets( root, 1, root, 2 );
  105. selection.addRange( r1 );
  106. selection.addRange( r2 );
  107. } );
  108. it( 'should return correct focus when last added range is not backward one', () => {
  109. selection.addRange( r3 );
  110. expect( selection.focus.isEqual( r3.end ) ).to.be.true;
  111. } );
  112. it( 'should return correct focus when last added range is backward one', () => {
  113. selection.addRange( r3, true );
  114. expect( selection.focus.isEqual( r3.start ) ).to.be.true;
  115. } );
  116. it( 'should return null if no ranges in selection', () => {
  117. selection.removeAllRanges();
  118. expect( selection.focus ).to.be.null;
  119. } );
  120. } );
  121. describe( 'addRange()', () => {
  122. it( 'should copy added ranges and store multiple ranges', () => {
  123. selection.addRange( liveRange );
  124. selection.addRange( range );
  125. const ranges = selection._ranges;
  126. expect( ranges.length ).to.equal( 2 );
  127. expect( ranges[ 0 ].isEqual( liveRange ) ).to.be.true;
  128. expect( ranges[ 1 ].isEqual( range ) ).to.be.true;
  129. expect( ranges[ 0 ] ).not.to.be.equal( liveRange );
  130. expect( ranges[ 1 ] ).not.to.be.equal( range );
  131. } );
  132. it( 'should set anchor and focus to the start and end of the most recently added range', () => {
  133. selection.addRange( liveRange );
  134. expect( selection.anchor.path ).to.deep.equal( [ 0 ] );
  135. expect( selection.focus.path ).to.deep.equal( [ 1 ] );
  136. selection.addRange( range );
  137. expect( selection.anchor.path ).to.deep.equal( [ 2 ] );
  138. expect( selection.focus.path ).to.deep.equal( [ 2, 2 ] );
  139. } );
  140. it( 'should set anchor and focus to the end and start of the most recently added range if backward flag was used', () => {
  141. selection.addRange( liveRange, true );
  142. expect( selection.anchor.path ).to.deep.equal( [ 1 ] );
  143. expect( selection.focus.path ).to.deep.equal( [ 0 ] );
  144. selection.addRange( range, true );
  145. expect( selection.anchor.path ).to.deep.equal( [ 2, 2 ] );
  146. expect( selection.focus.path ).to.deep.equal( [ 2 ] );
  147. } );
  148. it( 'should return a copy of (not a reference to) array of stored ranges', () => {
  149. selection.addRange( liveRange );
  150. const ranges = Array.from( selection.getRanges() );
  151. selection.addRange( range );
  152. expect( ranges.length ).to.equal( 1 );
  153. expect( ranges[ 0 ].isEqual( liveRange ) ).to.be.true;
  154. } );
  155. it( 'should fire change:range event when adding a range', () => {
  156. const spy = sinon.spy();
  157. selection.on( 'change:range', spy );
  158. selection.addRange( range );
  159. expect( spy.called ).to.be.true;
  160. } );
  161. it( 'should throw an error when range is invalid', () => {
  162. expect( () => {
  163. selection.addRange( { invalid: 'range' } );
  164. } ).to.throw( CKEditorError, /model-selection-added-not-range/ );
  165. } );
  166. it( 'should throw an error if added range intersects with already stored range', () => {
  167. selection.addRange( liveRange );
  168. expect( () => {
  169. selection.addRange(
  170. new Range(
  171. new Position( root, [ 0, 4 ] ),
  172. new Position( root, [ 1, 2 ] )
  173. )
  174. );
  175. } ).to.throw( CKEditorError, /model-selection-range-intersects/ );
  176. } );
  177. } );
  178. describe( 'setCollapsedAt()', () => {
  179. it( 'fires change:range', () => {
  180. const spy = sinon.spy();
  181. selection.on( 'change:range', spy );
  182. selection.setCollapsedAt( root );
  183. expect( spy.calledOnce ).to.be.true;
  184. } );
  185. it( 'sets selection at the 0 offset if second parameter not passed', () => {
  186. selection.setCollapsedAt( root );
  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', 0 );
  191. } );
  192. it( 'sets selection at given offset in given parent', () => {
  193. selection.setCollapsedAt( root, 3 );
  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', 3 );
  198. } );
  199. it( 'sets selection at the end of the given parent', () => {
  200. selection.setCollapsedAt( root, 'end' );
  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', root.maxOffset );
  205. } );
  206. it( 'sets selection before the specified element', () => {
  207. selection.setCollapsedAt( root.getChild( 1 ), 'before' );
  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', 1 );
  212. } );
  213. it( 'sets selection after the specified element', () => {
  214. selection.setCollapsedAt( root.getChild( 1 ), 'after' );
  215. expect( selection ).to.have.property( 'isCollapsed', true );
  216. const focus = selection.focus;
  217. expect( focus ).to.have.property( 'parent', root );
  218. expect( focus ).to.have.property( 'offset', 2 );
  219. } );
  220. it( 'sets selection at the specified position', () => {
  221. const pos = Position.createFromParentAndOffset( root, 3 );
  222. selection.setCollapsedAt( pos );
  223. expect( selection ).to.have.property( 'isCollapsed', true );
  224. const focus = selection.focus;
  225. expect( focus ).to.have.property( 'parent', root );
  226. expect( focus ).to.have.property( 'offset', 3 );
  227. } );
  228. } );
  229. describe( 'setFocus()', () => {
  230. it( 'keeps all existing ranges and fires no change:range when no modifications needed', () => {
  231. selection.addRange( range );
  232. selection.addRange( liveRange );
  233. const spy = sinon.spy();
  234. selection.on( 'change:range', spy );
  235. selection.setFocus( selection.focus );
  236. expect( count( selection.getRanges() ) ).to.equal( 2 );
  237. expect( spy.callCount ).to.equal( 0 );
  238. } );
  239. it( 'fires change:range', () => {
  240. selection.addRange( range );
  241. const spy = sinon.spy();
  242. selection.on( 'change:range', spy );
  243. selection.setFocus( Position.createAt( root, 'end' ) );
  244. expect( spy.calledOnce ).to.be.true;
  245. } );
  246. it( 'throws if there are no ranges in selection', () => {
  247. const endPos = Position.createAt( root, 'end' );
  248. expect( () => {
  249. selection.setFocus( endPos );
  250. } ).to.throw( CKEditorError, /model-selection-setFocus-no-ranges/ );
  251. } );
  252. it( 'modifies existing collapsed selection', () => {
  253. const startPos = Position.createAt( root, 1 );
  254. const endPos = Position.createAt( root, 2 );
  255. selection.setCollapsedAt( startPos );
  256. selection.setFocus( endPos );
  257. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'same' );
  258. expect( selection.focus.compareWith( endPos ) ).to.equal( 'same' );
  259. } );
  260. it( 'makes existing collapsed selection a backward selection', () => {
  261. const startPos = Position.createAt( root, 1 );
  262. const endPos = Position.createAt( root, 0 );
  263. selection.setCollapsedAt( startPos );
  264. selection.setFocus( endPos );
  265. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'same' );
  266. expect( selection.focus.compareWith( endPos ) ).to.equal( 'same' );
  267. expect( selection.isBackward ).to.be.true;
  268. } );
  269. it( 'modifies existing non-collapsed selection', () => {
  270. const startPos = Position.createAt( root, 1 );
  271. const endPos = Position.createAt( root, 2 );
  272. const newEndPos = Position.createAt( root, 3 );
  273. selection.addRange( new Range( startPos, endPos ) );
  274. selection.setFocus( newEndPos );
  275. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'same' );
  276. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'same' );
  277. } );
  278. it( 'makes existing non-collapsed selection a backward selection', () => {
  279. const startPos = Position.createAt( root, 1 );
  280. const endPos = Position.createAt( root, 2 );
  281. const newEndPos = Position.createAt( root, 0 );
  282. selection.addRange( new Range( startPos, endPos ) );
  283. selection.setFocus( newEndPos );
  284. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'same' );
  285. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'same' );
  286. expect( selection.isBackward ).to.be.true;
  287. } );
  288. it( 'makes existing backward selection a forward selection', () => {
  289. const startPos = Position.createAt( root, 1 );
  290. const endPos = Position.createAt( root, 2 );
  291. const newEndPos = Position.createAt( root, 3 );
  292. selection.addRange( new Range( startPos, endPos ), true );
  293. selection.setFocus( newEndPos );
  294. expect( selection.anchor.compareWith( endPos ) ).to.equal( 'same' );
  295. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'same' );
  296. expect( selection.isBackward ).to.be.false;
  297. } );
  298. it( 'modifies existing backward selection', () => {
  299. const startPos = Position.createAt( root, 1 );
  300. const endPos = Position.createAt( root, 2 );
  301. const newEndPos = Position.createAt( root, 0 );
  302. selection.addRange( new Range( startPos, endPos ), true );
  303. selection.setFocus( newEndPos );
  304. expect( selection.anchor.compareWith( endPos ) ).to.equal( 'same' );
  305. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'same' );
  306. expect( selection.isBackward ).to.be.true;
  307. } );
  308. it( 'modifies only the last range', () => {
  309. // Offsets are chosen in this way that the order of adding ranges must count, not their document order.
  310. const startPos1 = Position.createAt( root, 4 );
  311. const endPos1 = Position.createAt( root, 5 );
  312. const startPos2 = Position.createAt( root, 1 );
  313. const endPos2 = Position.createAt( root, 2 );
  314. const newEndPos = Position.createAt( root, 0 );
  315. selection.addRange( new Range( startPos1, endPos1 ) );
  316. selection.addRange( new Range( startPos2, endPos2 ) );
  317. const spy = sinon.spy();
  318. selection.on( 'change:range', spy );
  319. selection.setFocus( newEndPos );
  320. const ranges = Array.from( selection.getRanges() );
  321. expect( ranges ).to.have.lengthOf( 2 );
  322. expect( ranges[ 0 ].start.compareWith( startPos1 ) ).to.equal( 'same' );
  323. expect( ranges[ 0 ].end.compareWith( endPos1 ) ).to.equal( 'same' );
  324. expect( selection.anchor.compareWith( startPos2 ) ).to.equal( 'same' );
  325. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'same' );
  326. expect( selection.isBackward ).to.be.true;
  327. expect( spy.calledOnce ).to.be.true;
  328. } );
  329. it( 'collapses the selection when extending to the anchor', () => {
  330. const startPos = Position.createAt( root, 1 );
  331. const endPos = Position.createAt( root, 2 );
  332. selection.addRange( new Range( startPos, endPos ) );
  333. selection.setFocus( startPos );
  334. expect( selection.focus.compareWith( startPos ) ).to.equal( 'same' );
  335. expect( selection.isCollapsed ).to.be.true;
  336. } );
  337. it( 'uses Position.createAt', () => {
  338. const startPos = Position.createAt( root, 1 );
  339. const endPos = Position.createAt( root, 2 );
  340. const newEndPos = Position.createAt( root, 4 );
  341. const spy = testUtils.sinon.stub( Position, 'createAt' ).returns( newEndPos );
  342. selection.addRange( new Range( startPos, endPos ) );
  343. selection.setFocus( root, 'end' );
  344. expect( spy.calledOnce ).to.be.true;
  345. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'same' );
  346. } );
  347. } );
  348. describe( 'removeAllRanges()', () => {
  349. let spy;
  350. it( 'should remove all stored ranges', () => {
  351. selection.addRange( liveRange );
  352. selection.addRange( range );
  353. selection.removeAllRanges();
  354. expect( Array.from( selection.getRanges() ).length ).to.equal( 0 );
  355. } );
  356. it( 'should fire exactly one change:range event', () => {
  357. selection.addRange( liveRange );
  358. selection.addRange( range );
  359. spy = sinon.spy();
  360. selection.on( 'change:range', spy );
  361. selection.removeAllRanges();
  362. expect( spy.calledOnce ).to.be.true;
  363. } );
  364. it( 'should not fire change:range event if there were no ranges', () => {
  365. spy = sinon.spy();
  366. selection.on( 'change:range', spy );
  367. selection.removeAllRanges();
  368. expect( spy.called ).to.be.false;
  369. } );
  370. } );
  371. describe( 'setRanges()', () => {
  372. let newRanges, spy;
  373. beforeEach( () => {
  374. newRanges = [
  375. new Range( new Position( root, [ 4 ] ), new Position( root, [ 5 ] ) ),
  376. new Range( new Position( root, [ 5, 0 ] ), new Position( root, [ 6, 0 ] ) )
  377. ];
  378. selection.addRange( liveRange );
  379. selection.addRange( range );
  380. spy = sinon.spy();
  381. selection.on( 'change:range', spy );
  382. } );
  383. it( 'should throw an error when range is invalid', () => {
  384. expect( () => {
  385. selection.setRanges( [ { invalid: 'range' } ] );
  386. } ).to.throw( CKEditorError, /model-selection-added-not-range/ );
  387. } );
  388. it( 'should remove all ranges and add given ranges', () => {
  389. selection.setRanges( newRanges );
  390. const ranges = Array.from( selection.getRanges() );
  391. expect( ranges ).to.deep.equal( newRanges );
  392. } );
  393. it( 'should use last range from given array to get anchor and focus position', () => {
  394. selection.setRanges( newRanges );
  395. expect( selection.anchor.path ).to.deep.equal( [ 5, 0 ] );
  396. expect( selection.focus.path ).to.deep.equal( [ 6, 0 ] );
  397. } );
  398. it( 'should acknowledge backward flag when setting anchor and focus', () => {
  399. selection.setRanges( newRanges, true );
  400. expect( selection.anchor.path ).to.deep.equal( [ 6, 0 ] );
  401. expect( selection.focus.path ).to.deep.equal( [ 5, 0 ] );
  402. } );
  403. it( 'should fire exactly one change:range event', () => {
  404. selection.setRanges( newRanges );
  405. expect( spy.calledOnce ).to.be.true;
  406. } );
  407. it( 'should fire change:range event with correct parameters', () => {
  408. selection.on( 'change:range', ( evt, data ) => {
  409. expect( data.directChange ).to.be.true;
  410. } );
  411. selection.setRanges( newRanges );
  412. } );
  413. it( 'should not fire change:range event if given ranges are the same', () => {
  414. selection.setRanges( [ liveRange, range ] );
  415. expect( spy.calledOnce ).to.be.false;
  416. } );
  417. } );
  418. describe( 'setTo()', () => {
  419. it( 'should set selection to be same as given selection, using setRanges method', () => {
  420. const spy = sinon.spy( selection, 'setRanges' );
  421. const otherSelection = new Selection();
  422. otherSelection.addRange( range1 );
  423. otherSelection.addRange( range2, true );
  424. selection.setTo( otherSelection );
  425. expect( Array.from( selection.getRanges() ) ).to.deep.equal( [ range1, range2 ] );
  426. expect( selection.isBackward ).to.be.true;
  427. expect( selection.setRanges.calledOnce ).to.be.true;
  428. spy.restore();
  429. } );
  430. it( 'should set selection on the given Range using setRanges method', () => {
  431. const spy = sinon.spy( selection, 'setRanges' );
  432. selection.setTo( range1 );
  433. expect( Array.from( selection.getRanges() ) ).to.deep.equal( [ range1 ] );
  434. expect( selection.isBackward ).to.be.false;
  435. expect( selection.setRanges.calledOnce ).to.be.true;
  436. spy.restore();
  437. } );
  438. it( 'should set selection on the given iterable of Ranges using setRanges method', () => {
  439. const spy = sinon.spy( selection, 'setRanges' );
  440. selection.setTo( new Set( [ range1, range2 ] ) );
  441. expect( Array.from( selection.getRanges() ) ).to.deep.equal( [ range1, range2 ] );
  442. expect( selection.isBackward ).to.be.false;
  443. expect( selection.setRanges.calledOnce ).to.be.true;
  444. spy.restore();
  445. } );
  446. it( 'should set collapsed selection on the given Position using setRanges method', () => {
  447. const spy = sinon.spy( selection, 'setRanges' );
  448. const position = new Position( root, [ 4 ] );
  449. selection.setTo( position );
  450. expect( Array.from( selection.getRanges() ).length ).to.equal( 1 );
  451. expect( Array.from( selection.getRanges() )[ 0 ].start ).to.deep.equal( position );
  452. expect( selection.isBackward ).to.be.false;
  453. expect( selection.isCollapsed ).to.be.true;
  454. expect( selection.setRanges.calledOnce ).to.be.true;
  455. spy.restore();
  456. } );
  457. } );
  458. describe( 'getFirstRange()', () => {
  459. it( 'should return null if no ranges were added', () => {
  460. expect( selection.getFirstRange() ).to.be.null;
  461. } );
  462. it( 'should return a range which start position is before all other ranges\' start positions', () => {
  463. // This will not be the first range despite being added as first
  464. selection.addRange( range2 );
  465. // This should be the first range.
  466. selection.addRange( range1 );
  467. // A random range that is not first.
  468. selection.addRange( range3 );
  469. const range = selection.getFirstRange();
  470. expect( range.start.path ).to.deep.equal( [ 1 ] );
  471. expect( range.end.path ).to.deep.equal( [ 4 ] );
  472. } );
  473. } );
  474. describe( 'getFirstPosition()', () => {
  475. it( 'should return null if no ranges were added', () => {
  476. expect( selection.getFirstPosition() ).to.be.null;
  477. } );
  478. it( 'should return a position that is in selection and is before any other position from the selection', () => {
  479. // This will not be the first range despite being added as first
  480. selection.addRange( range2 );
  481. // This should be the first range.
  482. selection.addRange( range1 );
  483. // A random range that is not first.
  484. selection.addRange( range3 );
  485. const position = selection.getFirstPosition();
  486. expect( position.path ).to.deep.equal( [ 1 ] );
  487. } );
  488. } );
  489. describe( 'getLastRange()', () => {
  490. it( 'should return null if no ranges were added', () => {
  491. expect( selection.getLastRange() ).to.be.null;
  492. } );
  493. it( 'should return a range which start position is before all other ranges\' start positions', () => {
  494. selection.addRange( range3 );
  495. selection.addRange( range1 );
  496. selection.addRange( range2 );
  497. const range = selection.getLastRange();
  498. expect( range.start.path ).to.deep.equal( [ 6 ] );
  499. expect( range.end.path ).to.deep.equal( [ 7 ] );
  500. } );
  501. } );
  502. describe( 'getLastPosition()', () => {
  503. it( 'should return null if no ranges were added', () => {
  504. expect( selection.getLastPosition() ).to.be.null;
  505. } );
  506. it( 'should return a position that is in selection and is before any other position from the selection', () => {
  507. selection.addRange( range3 );
  508. selection.addRange( range1 );
  509. selection.addRange( range2 );
  510. const position = selection.getLastPosition();
  511. expect( position.path ).to.deep.equal( [ 7 ] );
  512. } );
  513. } );
  514. describe( 'isEqual()', () => {
  515. it( 'should return true if selections equal', () => {
  516. selection.addRange( range1 );
  517. selection.addRange( range2 );
  518. const otherSelection = new Selection();
  519. otherSelection.addRange( range1 );
  520. otherSelection.addRange( range2 );
  521. expect( selection.isEqual( otherSelection ) ).to.be.true;
  522. } );
  523. it( 'should return true if backward selections equal', () => {
  524. selection.addRange( range1, true );
  525. const otherSelection = new Selection();
  526. otherSelection.addRange( range1, true );
  527. expect( selection.isEqual( otherSelection ) ).to.be.true;
  528. } );
  529. it( 'should return true if both selections have no ranges', () => {
  530. const otherSelection = new Selection();
  531. expect( selection.isEqual( otherSelection ) ).to.be.true;
  532. } );
  533. it( 'should return false if ranges count does not equal', () => {
  534. selection.addRange( range1 );
  535. selection.addRange( range2 );
  536. const otherSelection = new Selection();
  537. otherSelection.addRange( range2 );
  538. expect( selection.isEqual( otherSelection ) ).to.be.false;
  539. } );
  540. it( 'should return false if ranges (other than the last added range) do not equal', () => {
  541. selection.addRange( range1 );
  542. selection.addRange( range3 );
  543. const otherSelection = new Selection();
  544. otherSelection.addRange( range2 );
  545. otherSelection.addRange( range3 );
  546. expect( selection.isEqual( otherSelection ) ).to.be.false;
  547. } );
  548. it( 'should return false if directions do not equal', () => {
  549. selection.addRange( range1 );
  550. const otherSelection = new Selection();
  551. otherSelection.addRange( range1, true );
  552. expect( selection.isEqual( otherSelection ) ).to.be.false;
  553. } );
  554. } );
  555. describe( 'collapseToStart()', () => {
  556. it( 'should collapse to start position and fire change event', () => {
  557. selection.setRanges( [ range2, range1, range3 ] );
  558. const spy = sinon.spy();
  559. selection.on( 'change:range', spy );
  560. selection.collapseToStart();
  561. expect( selection.rangeCount ).to.equal( 1 );
  562. expect( selection.isCollapsed ).to.be.true;
  563. expect( selection.getFirstPosition().isEqual( range1.start ) ).to.be.true;
  564. expect( spy.calledOnce ).to.be.true;
  565. } );
  566. it( 'should do nothing if selection was already collapsed', () => {
  567. selection.setCollapsedAt( range1.start );
  568. const spy = sinon.spy( selection, 'fire' );
  569. selection.collapseToStart();
  570. expect( spy.notCalled ).to.be.true;
  571. spy.restore();
  572. } );
  573. it( 'should do nothing if no ranges present', () => {
  574. const spy = sinon.spy( selection, 'fire' );
  575. selection.collapseToStart();
  576. spy.restore();
  577. expect( spy.notCalled ).to.be.true;
  578. } );
  579. } );
  580. describe( 'collapseToEnd()', () => {
  581. it( 'should collapse to start position and fire change:range event', () => {
  582. selection.setRanges( [ range2, range3, range1 ] );
  583. const spy = sinon.spy();
  584. selection.on( 'change:range', spy );
  585. selection.collapseToEnd();
  586. expect( selection.rangeCount ).to.equal( 1 );
  587. expect( selection.isCollapsed ).to.be.true;
  588. expect( selection.getLastPosition().isEqual( range3.end ) ).to.be.true;
  589. expect( spy.calledOnce ).to.be.true;
  590. } );
  591. it( 'should do nothing if selection was already collapsed', () => {
  592. selection.setCollapsedAt( range1.start );
  593. const spy = sinon.spy( selection, 'fire' );
  594. selection.collapseToEnd();
  595. expect( spy.notCalled ).to.be.true;
  596. spy.restore();
  597. } );
  598. it( 'should do nothing if selection has no ranges', () => {
  599. const spy = sinon.spy( selection, 'fire' );
  600. selection.collapseToEnd();
  601. expect( spy.notCalled ).to.be.true;
  602. spy.restore();
  603. } );
  604. } );
  605. describe( 'createFromSelection()', () => {
  606. it( 'should return a Selection instance with same ranges and direction as given selection', () => {
  607. selection.addRange( liveRange );
  608. selection.addRange( range, true );
  609. const snapshot = Selection.createFromSelection( selection );
  610. expect( selection.isBackward ).to.equal( snapshot.isBackward );
  611. const selectionRanges = Array.from( selection.getRanges() );
  612. const snapshotRanges = Array.from( snapshot.getRanges() );
  613. expect( selectionRanges.length ).to.equal( snapshotRanges.length );
  614. for ( let i = 0; i < selectionRanges.length; i++ ) {
  615. expect( selectionRanges[ i ].isEqual( snapshotRanges[ i ] ) ).to.be.true;
  616. }
  617. } );
  618. } );
  619. describe( 'getSelectedElement()', () => {
  620. let schema;
  621. beforeEach( () => {
  622. schema = new Schema();
  623. schema.registerItem( 'p', '$block' );
  624. } );
  625. it( 'should return selected element', () => {
  626. const { selection, model } = parse( '<p>foo</p>[<p>bar</p>]<p>baz</p>', schema );
  627. const p = model.getChild( 1 );
  628. expect( selection.getSelectedElement() ).to.equal( p );
  629. } );
  630. it( 'should return null if there is more than one range', () => {
  631. const { selection } = parse( '[<p>foo</p>][<p>bar</p>]<p>baz</p>', schema );
  632. expect( selection.getSelectedElement() ).to.be.null;
  633. } );
  634. it( 'should return null if there is no selection', () => {
  635. expect( selection.getSelectedElement() ).to.be.null;
  636. } );
  637. it( 'should return null if selection is not over single element #1', () => {
  638. const { selection } = parse( '<p>foo</p>[<p>bar</p><p>baz}</p>', schema );
  639. expect( selection.getSelectedElement() ).to.be.null;
  640. } );
  641. it( 'should return null if selection is not over single element #2', () => {
  642. const { selection } = parse( '<p>{bar}</p>', schema );
  643. expect( selection.getSelectedElement() ).to.be.null;
  644. } );
  645. } );
  646. describe( 'getSelectedBlocks()', () => {
  647. beforeEach( () => {
  648. doc.schema.registerItem( 'p', '$block' );
  649. doc.schema.registerItem( 'h', '$block' );
  650. doc.schema.registerItem( 'blockquote' );
  651. doc.schema.allow( { name: 'blockquote', inside: '$root' } );
  652. doc.schema.allow( { name: '$block', inside: 'blockquote' } );
  653. doc.schema.registerItem( 'image' );
  654. doc.schema.allow( { name: 'image', inside: '$root' } );
  655. doc.schema.allow( { name: 'image', inside: '$block' } );
  656. doc.schema.allow( { name: '$text', inside: 'image' } );
  657. // Special block which can contain another blocks.
  658. doc.schema.registerItem( 'nestedBlock', '$block' );
  659. doc.schema.allow( { name: 'nestedBlock', inside: '$block' } );
  660. } );
  661. it( 'returns an iterator', () => {
  662. setData( doc, '<p>a</p><p>[]b</p><p>c</p>' );
  663. expect( doc.selection.getSelectedBlocks().next ).to.be.a( 'function' );
  664. } );
  665. it( 'returns block for a collapsed selection', () => {
  666. setData( doc, '<p>a</p><p>[]b</p><p>c</p>' );
  667. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'b' ] );
  668. } );
  669. it( 'returns block for a collapsed selection (empty block)', () => {
  670. setData( doc, '<p>a</p><p>[]</p><p>c</p>' );
  671. const blocks = Array.from( doc.selection.getSelectedBlocks() );
  672. expect( blocks ).to.have.length( 1 );
  673. expect( blocks[ 0 ].childCount ).to.equal( 0 );
  674. } );
  675. it( 'returns block for a non collapsed selection', () => {
  676. setData( doc, '<p>a</p><p>[b]</p><p>c</p>' );
  677. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'b' ] );
  678. } );
  679. it( 'returns two blocks for a non collapsed selection', () => {
  680. setData( doc, '<p>a</p><h>[b</h><p>c]</p><p>d</p>' );
  681. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'b', 'c' ] );
  682. } );
  683. it( 'returns two blocks for a non collapsed selection (starts at block end)', () => {
  684. setData( doc, '<p>a</p><h>b[</h><p>c]</p><p>d</p>' );
  685. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'b', 'c' ] );
  686. } );
  687. it( 'returns proper block for a multi-range selection', () => {
  688. setData( doc, '<p>a</p><h>[b</h><p>c]</p><p>d</p><p>[e]</p>' );
  689. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'b', 'c', 'e' ] );
  690. } );
  691. it( 'does not return a block twice if two ranges are anchored in it', () => {
  692. setData( doc, '<p>[a]b[c]</p>' );
  693. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'abc' ] );
  694. } );
  695. it( 'returns only blocks', () => {
  696. setData( doc, '<p>[a</p><image>b</image><p>c]</p>' );
  697. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'a', 'c' ] );
  698. } );
  699. it( 'gets deeper into the tree', () => {
  700. setData( doc, '<p>[a</p><blockquote><p>b</p><p>c</p></blockquote><p>d]</p>' );
  701. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'a', 'b', 'c', 'd' ] );
  702. } );
  703. it( 'gets deeper into the tree (end deeper)', () => {
  704. setData( doc, '<p>[a</p><blockquote><p>b]</p><p>c</p></blockquote><p>d</p>' );
  705. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'a', 'b' ] );
  706. } );
  707. it( 'gets deeper into the tree (start deeper)', () => {
  708. setData( doc, '<p>a</p><blockquote><p>b</p><p>[c</p></blockquote><p>d]</p>' );
  709. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'c', 'd' ] );
  710. } );
  711. it( 'returns an empty array if none of the selected elements is a block', () => {
  712. setData( doc, '<p>a</p><image>[a</image><image>b]</image><p>b</p>' );
  713. expect( toText( doc.selection.getSelectedBlocks() ) ).to.be.empty;
  714. } );
  715. it( 'returns an empty array if the selected element is not a block', () => {
  716. setData( doc, '<p>a</p><image>[]a</image><p>b</p>' );
  717. expect( toText( doc.selection.getSelectedBlocks() ) ).to.be.empty;
  718. } );
  719. // Super edge case – should not happen (blocks should never be nested),
  720. // but since the code handles it already it's worth testing.
  721. it( 'returns only the lowest block if blocks are nested', () => {
  722. setData( doc, '<nestedBlock>a<nestedBlock>[]b</nestedBlock></nestedBlock>' );
  723. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'b' ] );
  724. } );
  725. // Like above but trickier.
  726. it( 'returns only the lowest block if blocks are nested', () => {
  727. setData(
  728. doc,
  729. '<nestedBlock>a<nestedBlock>[b</nestedBlock></nestedBlock>' +
  730. '<nestedBlock>c<nestedBlock>d]</nestedBlock></nestedBlock>'
  731. );
  732. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'b', 'd' ] );
  733. } );
  734. it( 'returns nothing if directly in a root', () => {
  735. doc.createRoot( 'p', 'inlineOnlyRoot' );
  736. setData( doc, 'a[b]c', { rootName: 'inlineOnlyRoot' } );
  737. expect( toText( doc.selection.getSelectedBlocks() ) ).to.be.empty;
  738. } );
  739. describe( '#984', () => {
  740. it( 'does not return the last block if none of its content is selected', () => {
  741. setData( doc, '<p>[a</p><p>b</p><p>]c</p>' );
  742. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'a', 'b' ] );
  743. } );
  744. it( 'returns only the first block for a non collapsed selection if only end of selection is touching a block', () => {
  745. setData( doc, '<p>a</p><h>b[</h><p>]c</p><p>d</p>' );
  746. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'b' ] );
  747. } );
  748. it( 'does not return the last block if none of its content is selected (nested case)', () => {
  749. setData( doc, '<p>[a</p><nestedBlock><nestedBlock>]b</nestedBlock></nestedBlock>' );
  750. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'a' ] );
  751. } );
  752. // Like a super edge case, we can live with this behavior as I don't even know what we could expect here
  753. // since only the innermost block is considerd a block to return (so the <nB>b...</nB> needs to be ignored).
  754. it( 'does not return the last block if none of its content is selected (nested case, wrapper with a content)', () => {
  755. setData( doc, '<p>[a</p><nestedBlock>b<nestedBlock>]c</nestedBlock></nestedBlock>' );
  756. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'a' ] );
  757. } );
  758. it( 'returns the last block if at least one of its child nodes is selected', () => {
  759. setData( doc, '<p>[a</p><p>b</p><p><image></image>]c</p>' );
  760. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'a', 'b', 'c' ] );
  761. } );
  762. // I needed these last 2 cases to justify the use of isTouching() instead of simple `offset == 0` check.
  763. it( 'returns the last block if at least one of its child nodes is selected (end in an inline element)', () => {
  764. setData( doc, '<p>[a</p><p>b</p><p><image>x]</image>c</p>' );
  765. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'a', 'b', 'c' ] );
  766. } );
  767. it(
  768. 'does not return the last block if at least one of its child nodes is selected ' +
  769. '(end in an inline element, no content selected)',
  770. () => {
  771. setData( doc, '<p>[a</p><p>b</p><p><image>]x</image>c</p>' );
  772. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'a', 'b' ] );
  773. }
  774. );
  775. } );
  776. // Map all elements to data of its first child text node.
  777. function toText( elements ) {
  778. return Array.from( elements ).map( el => {
  779. return Array.from( el.getChildren() ).find( child => child.data ).data;
  780. } );
  781. }
  782. } );
  783. describe( 'attributes interface', () => {
  784. let rangeInFullP;
  785. beforeEach( () => {
  786. root.insertChildren( 0, [
  787. new Element( 'p', [], new Text( 'foobar' ) ),
  788. new Element( 'p', [], [] )
  789. ] );
  790. rangeInFullP = new Range( new Position( root, [ 0, 4 ] ), new Position( root, [ 0, 4 ] ) );
  791. } );
  792. describe( 'setAttribute()', () => {
  793. it( 'should set given attribute on the selection', () => {
  794. selection.setRanges( [ rangeInFullP ] );
  795. selection.setAttribute( 'foo', 'bar' );
  796. expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  797. } );
  798. it( 'should fire change:attribute event with correct parameters', () => {
  799. selection.on( 'change:attribute', ( evt, data ) => {
  800. expect( data.directChange ).to.be.true;
  801. expect( data.attributeKeys ).to.deep.equal( [ 'foo' ] );
  802. } );
  803. selection.setAttribute( 'foo', 'bar' );
  804. } );
  805. it( 'should not fire change:attribute event if attribute with same key and value was already set', () => {
  806. selection.setAttribute( 'foo', 'bar' );
  807. const spy = sinon.spy();
  808. selection.on( 'change:attribute', spy );
  809. selection.setAttribute( 'foo', 'bar' );
  810. expect( spy.called ).to.be.false;
  811. } );
  812. } );
  813. describe( 'getAttribute()', () => {
  814. it( 'should return undefined if element does not contain given attribute', () => {
  815. expect( selection.getAttribute( 'abc' ) ).to.be.undefined;
  816. } );
  817. } );
  818. describe( 'getAttributes()', () => {
  819. it( 'should return an iterator that iterates over all attributes set on selection', () => {
  820. selection.setRanges( [ rangeInFullP ] );
  821. selection.setAttribute( 'foo', 'bar' );
  822. selection.setAttribute( 'abc', 'xyz' );
  823. const attrs = Array.from( selection.getAttributes() );
  824. expect( attrs ).to.deep.equal( [ [ 'foo', 'bar' ], [ 'abc', 'xyz' ] ] );
  825. } );
  826. } );
  827. describe( 'getAttributeKeys()', () => {
  828. it( 'should return iterator that iterates over all attribute keys set on selection', () => {
  829. selection.setRanges( [ rangeInFullP ] );
  830. selection.setAttribute( 'foo', 'bar' );
  831. selection.setAttribute( 'abc', 'xyz' );
  832. const attrs = Array.from( selection.getAttributeKeys() );
  833. expect( attrs ).to.deep.equal( [ 'foo', 'abc' ] );
  834. } );
  835. } );
  836. describe( 'hasAttribute()', () => {
  837. it( 'should return true if element contains attribute with given key', () => {
  838. selection.setRanges( [ rangeInFullP ] );
  839. selection.setAttribute( 'foo', 'bar' );
  840. expect( selection.hasAttribute( 'foo' ) ).to.be.true;
  841. } );
  842. it( 'should return false if element does not contain attribute with given key', () => {
  843. expect( selection.hasAttribute( 'abc' ) ).to.be.false;
  844. } );
  845. } );
  846. describe( 'clearAttributes()', () => {
  847. it( 'should remove all attributes from the element', () => {
  848. selection.setRanges( [ rangeInFullP ] );
  849. selection.setAttribute( 'foo', 'bar' );
  850. selection.setAttribute( 'abc', 'xyz' );
  851. selection.clearAttributes();
  852. expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
  853. expect( selection.getAttribute( 'abc' ) ).to.be.undefined;
  854. } );
  855. it( 'should fire change:attribute event with correct parameters', () => {
  856. selection.setAttribute( 'foo', 'bar' );
  857. selection.on( 'change:attribute', ( evt, data ) => {
  858. expect( data.directChange ).to.be.true;
  859. expect( data.attributeKeys ).to.deep.equal( [ 'foo' ] );
  860. } );
  861. selection.clearAttributes();
  862. } );
  863. it( 'should not fire change:attribute event if there were no attributes', () => {
  864. const spy = sinon.spy();
  865. selection.on( 'change:attribute', spy );
  866. selection.clearAttributes();
  867. expect( spy.called ).to.be.false;
  868. } );
  869. } );
  870. describe( 'removeAttribute()', () => {
  871. it( 'should remove attribute', () => {
  872. selection.setRanges( [ rangeInFullP ] );
  873. selection.setAttribute( 'foo', 'bar' );
  874. selection.removeAttribute( 'foo' );
  875. expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
  876. } );
  877. it( 'should fire change:attribute event with correct parameters', () => {
  878. selection.setAttribute( 'foo', 'bar' );
  879. selection.on( 'change:attribute', ( evt, data ) => {
  880. expect( data.directChange ).to.be.true;
  881. expect( data.attributeKeys ).to.deep.equal( [ 'foo' ] );
  882. } );
  883. selection.removeAttribute( 'foo' );
  884. } );
  885. it( 'should not fire change:attribute event if such attribute did not exist', () => {
  886. const spy = sinon.spy();
  887. selection.on( 'change:attribute', spy );
  888. selection.removeAttribute( 'foo' );
  889. expect( spy.called ).to.be.false;
  890. } );
  891. } );
  892. describe( 'setAttributesTo()', () => {
  893. it( 'should remove all attributes set on element and set the given ones', () => {
  894. selection.setAttribute( 'abc', 'xyz' );
  895. selection.setAttributesTo( { foo: 'bar' } );
  896. expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  897. expect( selection.getAttribute( 'abc' ) ).to.be.undefined;
  898. } );
  899. it( 'should fire only one change:attribute event', () => {
  900. selection.setAttributesTo( { foo: 'bar', xxx: 'yyy' } );
  901. const spy = sinon.spy();
  902. selection.on( 'change:attribute', spy );
  903. selection.setAttributesTo( { foo: 'bar', abc: 'def' } );
  904. expect( spy.calledOnce ).to.be.true;
  905. } );
  906. it( 'should fire change:attribute event with correct parameters', () => {
  907. selection.setAttributesTo( { foo: 'bar', xxx: 'yyy' } );
  908. selection.on( 'change:attribute', ( evt, data ) => {
  909. expect( data.directChange ).to.be.true;
  910. expect( data.attributeKeys ).to.deep.equal( [ 'abc', 'xxx' ] );
  911. } );
  912. selection.setAttributesTo( { foo: 'bar', abc: 'def' } );
  913. } );
  914. it( 'should not fire change:attribute event if attributes had not changed', () => {
  915. selection.setRanges( [ rangeInFullP ] );
  916. selection.setAttributesTo( { foo: 'bar', xxx: 'yyy' } );
  917. const spy = sinon.spy();
  918. selection.on( 'change:attribute', spy );
  919. selection.setAttributesTo( { xxx: 'yyy', foo: 'bar' } );
  920. expect( spy.called ).to.be.false;
  921. } );
  922. } );
  923. } );
  924. } );