selection.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  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 Range from '/ckeditor5/engine/model/range.js';
  10. import Position from '/ckeditor5/engine/model/position.js';
  11. import LiveRange from '/ckeditor5/engine/model/liverange.js';
  12. import Selection from '/ckeditor5/engine/model/selection.js';
  13. import CKEditorError from '/ckeditor5/utils/ckeditorerror.js';
  14. import testUtils from '/tests/ckeditor5/_utils/utils.js';
  15. import count from '/ckeditor5/utils/count.js';
  16. testUtils.createSinonSandbox();
  17. describe( 'Selection', () => {
  18. let doc, root, selection, liveRange, range;
  19. beforeEach( () => {
  20. doc = new Document();
  21. root = doc.createRoot();
  22. root.appendChildren( [
  23. new Element( 'p' ),
  24. new Element( 'p' ),
  25. new Element( 'p', [], 'foobar' ),
  26. new Element( 'p' ),
  27. new Element( 'p' ),
  28. new Element( 'p' ),
  29. new Element( 'p', [], 'foobar' )
  30. ] );
  31. selection = new Selection();
  32. liveRange = new LiveRange( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) );
  33. range = new Range( new Position( root, [ 2 ] ), new Position( root, [ 2, 2 ] ) );
  34. } );
  35. afterEach( () => {
  36. doc.destroy();
  37. liveRange.detach();
  38. } );
  39. describe( 'isCollapsed', () => {
  40. it( 'should return false for empty selection', () => {
  41. expect( selection.isCollapsed ).to.be.false;
  42. } );
  43. it( 'should return true when there is single collapsed ranges', () => {
  44. selection.addRange( new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) ) );
  45. expect( selection.isCollapsed ).to.be.true;
  46. } );
  47. it( 'should return false when there are multiple ranges', () => {
  48. selection.addRange( new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) ) );
  49. selection.addRange( new Range( new Position( root, [ 2 ] ), new Position( root, [ 2 ] ) ) );
  50. expect( selection.isCollapsed ).to.be.false;
  51. } );
  52. it( 'should return false when there is not collapsed range', () => {
  53. selection.addRange( range );
  54. expect( selection.isCollapsed ).to.be.false;
  55. } );
  56. } );
  57. describe( 'rangeCount', () => {
  58. it( 'should return proper range count', () => {
  59. expect( selection.rangeCount ).to.equal( 0 );
  60. selection.addRange( new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) ) );
  61. expect( selection.rangeCount ).to.equal( 1 );
  62. selection.addRange( new Range( new Position( root, [ 2 ] ), new Position( root, [ 2 ] ) ) );
  63. expect( selection.rangeCount ).to.equal( 2 );
  64. } );
  65. } );
  66. describe( 'isBackward', () => {
  67. it( 'is defined by the last added range', () => {
  68. selection.addRange( range, true );
  69. expect( selection ).to.have.property( 'isBackward', true );
  70. selection.addRange( liveRange );
  71. expect( selection ).to.have.property( 'isBackward', false );
  72. } );
  73. it( 'is false when last range is collapsed', () => {
  74. const pos = Position.createAt( root, 0 );
  75. selection.addRange( new Range( pos, pos ), true );
  76. expect( selection.isBackward ).to.be.false;
  77. } );
  78. } );
  79. describe( 'addRange', () => {
  80. it( 'should copy added ranges and store multiple ranges', () => {
  81. selection.addRange( liveRange );
  82. selection.addRange( range );
  83. const ranges = selection._ranges;
  84. expect( ranges.length ).to.equal( 2 );
  85. expect( ranges[ 0 ].isEqual( liveRange ) ).to.be.true;
  86. expect( ranges[ 1 ].isEqual( range ) ).to.be.true;
  87. expect( ranges[ 0 ] ).not.to.be.equal( liveRange );
  88. expect( ranges[ 1 ] ).not.to.be.equal( range );
  89. } );
  90. it( 'should set anchor and focus to the start and end of the most recently added range', () => {
  91. selection.addRange( liveRange );
  92. expect( selection.anchor.path ).to.deep.equal( [ 0 ] );
  93. expect( selection.focus.path ).to.deep.equal( [ 1 ] );
  94. selection.addRange( range );
  95. expect( selection.anchor.path ).to.deep.equal( [ 2 ] );
  96. expect( selection.focus.path ).to.deep.equal( [ 2, 2 ] );
  97. } );
  98. it( 'should set anchor and focus to the end and start of the most recently added range if backward flag was used', () => {
  99. selection.addRange( liveRange, true );
  100. expect( selection.anchor.path ).to.deep.equal( [ 1 ] );
  101. expect( selection.focus.path ).to.deep.equal( [ 0 ] );
  102. selection.addRange( range, true );
  103. expect( selection.anchor.path ).to.deep.equal( [ 2, 2 ] );
  104. expect( selection.focus.path ).to.deep.equal( [ 2 ] );
  105. } );
  106. it( 'should return a copy of (not a reference to) array of stored ranges', () => {
  107. selection.addRange( liveRange );
  108. const ranges = Array.from( selection.getRanges() );
  109. selection.addRange( range );
  110. expect( ranges.length ).to.equal( 1 );
  111. expect( ranges[ 0 ].isEqual( liveRange ) ).to.be.true;
  112. } );
  113. it( 'should fire change:range event when adding a range', () => {
  114. let spy = sinon.spy();
  115. selection.on( 'change:range', spy );
  116. selection.addRange( range );
  117. expect( spy.called ).to.be.true;
  118. } );
  119. it( 'should throw an error if added range intersects with already stored range', () => {
  120. selection.addRange( liveRange );
  121. expect( () => {
  122. selection.addRange(
  123. new Range(
  124. new Position( root, [ 0, 4 ] ),
  125. new Position( root, [ 1, 2 ] )
  126. )
  127. );
  128. } ).to.throw( CKEditorError, /selection-range-intersects/ );
  129. } );
  130. } );
  131. describe( 'collapse', () => {
  132. it( 'fires change:range', () => {
  133. const spy = sinon.spy();
  134. selection.on( 'change:range', spy );
  135. selection.collapse( root );
  136. expect( spy.calledOnce ).to.be.true;
  137. } );
  138. it( 'sets selection at the 0 offset if second parameter not passed', () => {
  139. selection.collapse( root );
  140. expect( selection ).to.have.property( 'isCollapsed', true );
  141. const focus = selection.focus;
  142. expect( focus ).to.have.property( 'parent', root );
  143. expect( focus ).to.have.property( 'offset', 0 );
  144. } );
  145. it( 'sets selection at given offset in given parent', () => {
  146. selection.collapse( root, 3 );
  147. expect( selection ).to.have.property( 'isCollapsed', true );
  148. const focus = selection.focus;
  149. expect( focus ).to.have.property( 'parent', root );
  150. expect( focus ).to.have.property( 'offset', 3 );
  151. } );
  152. it( 'sets selection at the end of the given parent', () => {
  153. selection.collapse( root, 'END' );
  154. expect( selection ).to.have.property( 'isCollapsed', true );
  155. const focus = selection.focus;
  156. expect( focus ).to.have.property( 'parent', root );
  157. expect( focus ).to.have.property( 'offset', root.getChildCount() );
  158. } );
  159. it( 'sets selection before the specified element', () => {
  160. selection.collapse( root.getChild( 1 ), 'BEFORE' );
  161. expect( selection ).to.have.property( 'isCollapsed', true );
  162. const focus = selection.focus;
  163. expect( focus ).to.have.property( 'parent', root );
  164. expect( focus ).to.have.property( 'offset', 1 );
  165. } );
  166. it( 'sets selection after the specified element', () => {
  167. selection.collapse( root.getChild( 1 ), 'AFTER' );
  168. expect( selection ).to.have.property( 'isCollapsed', true );
  169. const focus = selection.focus;
  170. expect( focus ).to.have.property( 'parent', root );
  171. expect( focus ).to.have.property( 'offset', 2 );
  172. } );
  173. it( 'sets selection at the specified position', () => {
  174. const pos = Position.createFromParentAndOffset( root, 3 );
  175. selection.collapse( pos );
  176. expect( selection ).to.have.property( 'isCollapsed', true );
  177. const focus = selection.focus;
  178. expect( focus ).to.have.property( 'parent', root );
  179. expect( focus ).to.have.property( 'offset', 3 );
  180. } );
  181. } );
  182. describe( 'focus', () => {
  183. it( 'should return first end start', () => {
  184. selection.addRange( range );
  185. expect( selection.focus.isEqual( range.end ) ).to.be.true;
  186. } );
  187. it( 'should return first range start when backward', () => {
  188. selection.addRange( range, true );
  189. expect( selection.focus.isEqual( range.start ) ).to.be.true;
  190. } );
  191. it( 'should return null if no ranges in selection', () => {
  192. expect( selection.focus ).to.be.null;
  193. } );
  194. } );
  195. describe( 'setFocus', () => {
  196. it( 'keeps all existing ranges and fires no change:range when no modifications needed', () => {
  197. selection.addRange( range );
  198. selection.addRange( liveRange );
  199. const spy = sinon.spy();
  200. selection.on( 'change:range', spy );
  201. selection.setFocus( selection.focus );
  202. expect( count( selection.getRanges() ) ).to.equal( 2 );
  203. expect( spy.callCount ).to.equal( 0 );
  204. } );
  205. it( 'fires change:range', () => {
  206. selection.addRange( range );
  207. const spy = sinon.spy();
  208. selection.on( 'change:range', spy );
  209. selection.setFocus( Position.createAt( root, 'END' ) );
  210. expect( spy.calledOnce ).to.be.true;
  211. } );
  212. it( 'throws if there are no ranges in selection', () => {
  213. const endPos = Position.createAt( root, 'END' );
  214. expect( () => {
  215. selection.setFocus( endPos );
  216. } ).to.throw( CKEditorError, /selection-setFocus-no-ranges/ );
  217. } );
  218. it( 'modifies existing collapsed selection', () => {
  219. const startPos = Position.createAt( root, 1 );
  220. const endPos = Position.createAt( root, 2 );
  221. selection.collapse( startPos );
  222. selection.setFocus( endPos );
  223. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'SAME' );
  224. expect( selection.focus.compareWith( endPos ) ).to.equal( 'SAME' );
  225. } );
  226. it( 'makes existing collapsed selection a backward selection', () => {
  227. const startPos = Position.createAt( root, 1 );
  228. const endPos = Position.createAt( root, 0 );
  229. selection.collapse( startPos );
  230. selection.setFocus( endPos );
  231. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'SAME' );
  232. expect( selection.focus.compareWith( endPos ) ).to.equal( 'SAME' );
  233. expect( selection.isBackward ).to.be.true;
  234. } );
  235. it( 'modifies existing non-collapsed selection', () => {
  236. const startPos = Position.createAt( root, 1 );
  237. const endPos = Position.createAt( root, 2 );
  238. const newEndPos = Position.createAt( root, 3 );
  239. selection.addRange( new Range( startPos, endPos ) );
  240. selection.setFocus( newEndPos );
  241. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'SAME' );
  242. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'SAME' );
  243. } );
  244. it( 'makes existing non-collapsed selection a backward selection', () => {
  245. const startPos = Position.createAt( root, 1 );
  246. const endPos = Position.createAt( root, 2 );
  247. const newEndPos = Position.createAt( root, 0 );
  248. selection.addRange( new Range( startPos, endPos ) );
  249. selection.setFocus( newEndPos );
  250. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'SAME' );
  251. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'SAME' );
  252. expect( selection.isBackward ).to.be.true;
  253. } );
  254. it( 'makes existing backward selection a forward selection', () => {
  255. const startPos = Position.createAt( root, 1 );
  256. const endPos = Position.createAt( root, 2 );
  257. const newEndPos = Position.createAt( root, 3 );
  258. selection.addRange( new Range( startPos, endPos ), true );
  259. selection.setFocus( newEndPos );
  260. expect( selection.anchor.compareWith( endPos ) ).to.equal( 'SAME' );
  261. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'SAME' );
  262. expect( selection.isBackward ).to.be.false;
  263. } );
  264. it( 'modifies existing backward selection', () => {
  265. const startPos = Position.createAt( root, 1 );
  266. const endPos = Position.createAt( root, 2 );
  267. const newEndPos = Position.createAt( root, 0 );
  268. selection.addRange( new Range( startPos, endPos ), true );
  269. selection.setFocus( newEndPos );
  270. expect( selection.anchor.compareWith( endPos ) ).to.equal( 'SAME' );
  271. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'SAME' );
  272. expect( selection.isBackward ).to.be.true;
  273. } );
  274. it( 'modifies only the last range', () => {
  275. // Offsets are chosen in this way that the order of adding ranges must count, not their document order.
  276. const startPos1 = Position.createAt( root, 4 );
  277. const endPos1 = Position.createAt( root, 5 );
  278. const startPos2 = Position.createAt( root, 1 );
  279. const endPos2 = Position.createAt( root, 2 );
  280. const newEndPos = Position.createAt( root, 0 );
  281. selection.addRange( new Range( startPos1, endPos1 ) );
  282. selection.addRange( new Range( startPos2, endPos2 ) );
  283. const spy = sinon.spy();
  284. selection.on( 'change:range', spy );
  285. selection.setFocus( newEndPos );
  286. const ranges = Array.from( selection.getRanges() );
  287. expect( ranges ).to.have.lengthOf( 2 );
  288. expect( ranges[ 0 ].start.compareWith( startPos1 ) ).to.equal( 'SAME' );
  289. expect( ranges[ 0 ].end.compareWith( endPos1 ) ).to.equal( 'SAME' );
  290. expect( selection.anchor.compareWith( startPos2 ) ).to.equal( 'SAME' );
  291. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'SAME' );
  292. expect( selection.isBackward ).to.be.true;
  293. expect( spy.calledOnce ).to.be.true;
  294. } );
  295. it( 'collapses the selection when extending to the anchor', () => {
  296. const startPos = Position.createAt( root, 1 );
  297. const endPos = Position.createAt( root, 2 );
  298. selection.addRange( new Range( startPos, endPos ) );
  299. selection.setFocus( startPos );
  300. expect( selection.focus.compareWith( startPos ) ).to.equal( 'SAME' );
  301. expect( selection.isCollapsed ).to.be.true;
  302. } );
  303. it( 'uses Position.createAt', () => {
  304. const startPos = Position.createAt( root, 1 );
  305. const endPos = Position.createAt( root, 2 );
  306. const newEndPos = Position.createAt( root, 4 );
  307. const spy = testUtils.sinon.stub( Position, 'createAt', () => newEndPos );
  308. selection.addRange( new Range( startPos, endPos ) );
  309. selection.setFocus( root, 'END' );
  310. expect( spy.calledOnce ).to.be.true;
  311. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'SAME' );
  312. } );
  313. } );
  314. describe( 'removeAllRanges', () => {
  315. let spy, ranges;
  316. beforeEach( () => {
  317. selection.addRange( liveRange );
  318. selection.addRange( range );
  319. spy = sinon.spy();
  320. selection.on( 'change:range', spy );
  321. ranges = selection._ranges;
  322. selection.removeAllRanges();
  323. } );
  324. it( 'should remove all stored ranges', () => {
  325. expect( Array.from( selection.getRanges() ).length ).to.equal( 0 );
  326. } );
  327. it( 'should fire exactly one update event', () => {
  328. expect( spy.calledOnce ).to.be.true;
  329. } );
  330. } );
  331. describe( 'setRanges', () => {
  332. let newRanges, spy, oldRanges;
  333. before( () => {
  334. newRanges = [
  335. new Range( new Position( root, [ 4 ] ), new Position( root, [ 5 ] ) ),
  336. new Range( new Position( root, [ 5, 0 ] ), new Position( root, [ 6, 0 ] ) )
  337. ];
  338. } );
  339. beforeEach( () => {
  340. selection.addRange( liveRange );
  341. selection.addRange( range );
  342. spy = sinon.spy();
  343. selection.on( 'change:range', spy );
  344. oldRanges = selection._ranges;
  345. } );
  346. it( 'should remove all ranges and add given ranges', () => {
  347. selection.setRanges( newRanges );
  348. let ranges = selection._ranges;
  349. expect( ranges.length ).to.equal( 2 );
  350. expect( ranges[ 0 ].isEqual( newRanges[ 0 ] ) ).to.be.true;
  351. expect( ranges[ 1 ].isEqual( newRanges[ 1 ] ) ).to.be.true;
  352. } );
  353. it( 'should use last range from given array to get anchor and focus position', () => {
  354. selection.setRanges( newRanges );
  355. expect( selection.anchor.path ).to.deep.equal( [ 5, 0 ] );
  356. expect( selection.focus.path ).to.deep.equal( [ 6, 0 ] );
  357. } );
  358. it( 'should acknowledge backward flag when setting anchor and focus', () => {
  359. selection.setRanges( newRanges, true );
  360. expect( selection.anchor.path ).to.deep.equal( [ 6, 0 ] );
  361. expect( selection.focus.path ).to.deep.equal( [ 5, 0 ] );
  362. } );
  363. it( 'should fire exactly one update event', () => {
  364. selection.setRanges( newRanges );
  365. expect( spy.calledOnce ).to.be.true;
  366. } );
  367. } );
  368. describe( 'getFirstRange', () => {
  369. it( 'should return null if no ranges were added', () => {
  370. expect( selection.getFirstRange() ).to.be.null;
  371. } );
  372. it( 'should return a range which start position is before all other ranges\' start positions', () => {
  373. // This will not be the first range despite being added as first
  374. selection.addRange( new Range( new Position( root, [ 4 ] ), new Position( root, [ 5 ] ) ) );
  375. // This should be the first range.
  376. selection.addRange( new Range( new Position( root, [ 1 ] ), new Position( root, [ 4 ] ) ) );
  377. // A random range that is not first.
  378. selection.addRange( new Range( new Position( root, [ 6 ] ), new Position( root, [ 7 ] ) ) );
  379. let range = selection.getFirstRange();
  380. expect( range.start.path ).to.deep.equal( [ 1 ] );
  381. expect( range.end.path ).to.deep.equal( [ 4 ] );
  382. } );
  383. } );
  384. describe( 'getFirstPosition', () => {
  385. it( 'should return null if no ranges were added', () => {
  386. expect( selection.getFirstPosition() ).to.be.null;
  387. } );
  388. it( 'should return a position that is in selection and is before any other position from the selection', () => {
  389. // This will not be a range containing the first position despite being added as first
  390. selection.addRange( new Range( new Position( root, [ 4 ] ), new Position( root, [ 5 ] ) ) );
  391. // This should be the first range.
  392. selection.addRange( new Range( new Position( root, [ 1 ] ), new Position( root, [ 4 ] ) ) );
  393. // A random range that is not first.
  394. selection.addRange( new Range( new Position( root, [ 6 ] ), new Position( root, [ 7 ] ) ) );
  395. let position = selection.getFirstPosition();
  396. expect( position.path ).to.deep.equal( [ 1 ] );
  397. } );
  398. } );
  399. describe( 'createFromSelection', () => {
  400. it( 'should return a Selection instance with same ranges and direction as given selection', () => {
  401. selection.addRange( liveRange );
  402. selection.addRange( range, true );
  403. const snapshot = Selection.createFromSelection( selection );
  404. expect( selection.isBackward ).to.equal( snapshot.isBackward );
  405. const selectionRanges = Array.from( selection.getRanges() );
  406. const snapshotRanges = Array.from( snapshot.getRanges() );
  407. expect( selectionRanges.length ).to.equal( snapshotRanges.length );
  408. for ( let i = 0; i < selectionRanges.length; i++ ) {
  409. expect( selectionRanges[ i ].isEqual( snapshotRanges[ i ] ) ).to.be.true;
  410. }
  411. } );
  412. } );
  413. describe( 'attributes interface', () => {
  414. let rangeInFullP;
  415. beforeEach( () => {
  416. root.insertChildren( 0, [
  417. new Element( 'p', [], 'foobar' ),
  418. new Element( 'p', [], [] )
  419. ] );
  420. rangeInFullP = new Range( new Position( root, [ 0, 4 ] ), new Position( root, [ 0, 4 ] ) );
  421. } );
  422. describe( 'setAttribute', () => {
  423. it( 'should set given attribute on the selection', () => {
  424. selection.setRanges( [ rangeInFullP ] );
  425. selection.setAttribute( 'foo', 'bar' );
  426. expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  427. } );
  428. it( 'should fire change:attribute event', () => {
  429. let spy = sinon.spy();
  430. selection.on( 'change:attribute', spy );
  431. selection.setAttribute( 'foo', 'bar' );
  432. expect( spy.called ).to.be.true;
  433. } );
  434. } );
  435. describe( 'getAttribute', () => {
  436. it( 'should return undefined if element does not contain given attribute', () => {
  437. expect( selection.getAttribute( 'abc' ) ).to.be.undefined;
  438. } );
  439. } );
  440. describe( 'getAttributes', () => {
  441. it( 'should return an iterator that iterates over all attributes set on the text fragment', () => {
  442. selection.setRanges( [ rangeInFullP ] );
  443. selection.setAttribute( 'foo', 'bar' );
  444. selection.setAttribute( 'abc', 'xyz' );
  445. let attrs = Array.from( selection.getAttributes() );
  446. expect( attrs ).to.deep.equal( [ [ 'foo', 'bar' ], [ 'abc', 'xyz' ] ] );
  447. } );
  448. } );
  449. describe( 'hasAttribute', () => {
  450. it( 'should return true if element contains attribute with given key', () => {
  451. selection.setRanges( [ rangeInFullP ] );
  452. selection.setAttribute( 'foo', 'bar' );
  453. expect( selection.hasAttribute( 'foo' ) ).to.be.true;
  454. } );
  455. it( 'should return false if element does not contain attribute with given key', () => {
  456. expect( selection.hasAttribute( 'abc' ) ).to.be.false;
  457. } );
  458. } );
  459. describe( 'clearAttributes', () => {
  460. it( 'should remove all attributes from the element', () => {
  461. selection.setRanges( [ rangeInFullP ] );
  462. selection.setAttribute( 'foo', 'bar' );
  463. selection.setAttribute( 'abc', 'xyz' );
  464. selection.clearAttributes();
  465. expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
  466. expect( selection.getAttribute( 'abc' ) ).to.be.undefined;
  467. } );
  468. it( 'should fire change:attribute event', () => {
  469. let spy = sinon.spy();
  470. selection.on( 'change:attribute', spy );
  471. selection.clearAttributes();
  472. expect( spy.called ).to.be.true;
  473. } );
  474. } );
  475. describe( 'removeAttribute', () => {
  476. it( 'should remove attribute', () => {
  477. selection.setRanges( [ rangeInFullP ] );
  478. selection.setAttribute( 'foo', 'bar' );
  479. selection.removeAttribute( 'foo' );
  480. expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
  481. } );
  482. it( 'should fire change:attribute event', () => {
  483. let spy = sinon.spy();
  484. selection.on( 'change:attribute', spy );
  485. selection.removeAttribute( 'foo' );
  486. expect( spy.called ).to.be.true;
  487. } );
  488. } );
  489. describe( 'setAttributesTo', () => {
  490. it( 'should remove all attributes set on element and set the given ones', () => {
  491. selection.setRanges( [ rangeInFullP ] );
  492. selection.setAttribute( 'abc', 'xyz' );
  493. selection.setAttributesTo( { foo: 'bar' } );
  494. expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  495. expect( selection.getAttribute( 'abc' ) ).to.be.undefined;
  496. } );
  497. } );
  498. } );
  499. } );