selection.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  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. let r3;
  184. beforeEach( () => {
  185. const r1 = Range.createFromParentsAndOffsets( root, 2, root, 4 );
  186. const r2 = Range.createFromParentsAndOffsets( root, 4, root, 6 );
  187. r3 = Range.createFromParentsAndOffsets( root, 1, root, 2 );
  188. selection.addRange( r1 );
  189. selection.addRange( r2 );
  190. } );
  191. it( 'should return correct focus when last added range is not backward one', () => {
  192. selection.addRange( r3 );
  193. expect( selection.focus.isEqual( r3.end ) ).to.be.true;
  194. } );
  195. it( 'should return correct focus when last added range is backward one', () => {
  196. selection.addRange( r3, true );
  197. expect( selection.focus.isEqual( r3.start ) ).to.be.true;
  198. } );
  199. it( 'should return null if no ranges in selection', () => {
  200. selection.removeAllRanges();
  201. expect( selection.focus ).to.be.null;
  202. } );
  203. } );
  204. describe( 'setFocus', () => {
  205. it( 'keeps all existing ranges and fires no change:range when no modifications needed', () => {
  206. selection.addRange( range );
  207. selection.addRange( liveRange );
  208. const spy = sinon.spy();
  209. selection.on( 'change:range', spy );
  210. selection.setFocus( selection.focus );
  211. expect( count( selection.getRanges() ) ).to.equal( 2 );
  212. expect( spy.callCount ).to.equal( 0 );
  213. } );
  214. it( 'fires change:range', () => {
  215. selection.addRange( range );
  216. const spy = sinon.spy();
  217. selection.on( 'change:range', spy );
  218. selection.setFocus( Position.createAt( root, 'end' ) );
  219. expect( spy.calledOnce ).to.be.true;
  220. } );
  221. it( 'throws if there are no ranges in selection', () => {
  222. const endPos = Position.createAt( root, 'end' );
  223. expect( () => {
  224. selection.setFocus( endPos );
  225. } ).to.throw( CKEditorError, /selection-setFocus-no-ranges/ );
  226. } );
  227. it( 'modifies existing collapsed selection', () => {
  228. const startPos = Position.createAt( root, 1 );
  229. const endPos = Position.createAt( root, 2 );
  230. selection.collapse( startPos );
  231. selection.setFocus( endPos );
  232. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'SAME' );
  233. expect( selection.focus.compareWith( endPos ) ).to.equal( 'SAME' );
  234. } );
  235. it( 'makes existing collapsed selection a backward selection', () => {
  236. const startPos = Position.createAt( root, 1 );
  237. const endPos = Position.createAt( root, 0 );
  238. selection.collapse( startPos );
  239. selection.setFocus( endPos );
  240. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'SAME' );
  241. expect( selection.focus.compareWith( endPos ) ).to.equal( 'SAME' );
  242. expect( selection.isBackward ).to.be.true;
  243. } );
  244. it( 'modifies existing non-collapsed selection', () => {
  245. const startPos = Position.createAt( root, 1 );
  246. const endPos = Position.createAt( root, 2 );
  247. const newEndPos = Position.createAt( root, 3 );
  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. } );
  253. it( 'makes existing non-collapsed selection a backward selection', () => {
  254. const startPos = Position.createAt( root, 1 );
  255. const endPos = Position.createAt( root, 2 );
  256. const newEndPos = Position.createAt( root, 0 );
  257. selection.addRange( new Range( startPos, endPos ) );
  258. selection.setFocus( newEndPos );
  259. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'SAME' );
  260. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'SAME' );
  261. expect( selection.isBackward ).to.be.true;
  262. } );
  263. it( 'makes existing backward selection a forward selection', () => {
  264. const startPos = Position.createAt( root, 1 );
  265. const endPos = Position.createAt( root, 2 );
  266. const newEndPos = Position.createAt( root, 3 );
  267. selection.addRange( new Range( startPos, endPos ), true );
  268. selection.setFocus( newEndPos );
  269. expect( selection.anchor.compareWith( endPos ) ).to.equal( 'SAME' );
  270. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'SAME' );
  271. expect( selection.isBackward ).to.be.false;
  272. } );
  273. it( 'modifies existing backward selection', () => {
  274. const startPos = Position.createAt( root, 1 );
  275. const endPos = Position.createAt( root, 2 );
  276. const newEndPos = Position.createAt( root, 0 );
  277. selection.addRange( new Range( startPos, endPos ), true );
  278. selection.setFocus( newEndPos );
  279. expect( selection.anchor.compareWith( endPos ) ).to.equal( 'SAME' );
  280. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'SAME' );
  281. expect( selection.isBackward ).to.be.true;
  282. } );
  283. it( 'modifies only the last range', () => {
  284. // Offsets are chosen in this way that the order of adding ranges must count, not their document order.
  285. const startPos1 = Position.createAt( root, 4 );
  286. const endPos1 = Position.createAt( root, 5 );
  287. const startPos2 = Position.createAt( root, 1 );
  288. const endPos2 = Position.createAt( root, 2 );
  289. const newEndPos = Position.createAt( root, 0 );
  290. selection.addRange( new Range( startPos1, endPos1 ) );
  291. selection.addRange( new Range( startPos2, endPos2 ) );
  292. const spy = sinon.spy();
  293. selection.on( 'change:range', spy );
  294. selection.setFocus( newEndPos );
  295. const ranges = Array.from( selection.getRanges() );
  296. expect( ranges ).to.have.lengthOf( 2 );
  297. expect( ranges[ 0 ].start.compareWith( startPos1 ) ).to.equal( 'SAME' );
  298. expect( ranges[ 0 ].end.compareWith( endPos1 ) ).to.equal( 'SAME' );
  299. expect( selection.anchor.compareWith( startPos2 ) ).to.equal( 'SAME' );
  300. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'SAME' );
  301. expect( selection.isBackward ).to.be.true;
  302. expect( spy.calledOnce ).to.be.true;
  303. } );
  304. it( 'collapses the selection when extending to the anchor', () => {
  305. const startPos = Position.createAt( root, 1 );
  306. const endPos = Position.createAt( root, 2 );
  307. selection.addRange( new Range( startPos, endPos ) );
  308. selection.setFocus( startPos );
  309. expect( selection.focus.compareWith( startPos ) ).to.equal( 'SAME' );
  310. expect( selection.isCollapsed ).to.be.true;
  311. } );
  312. it( 'uses Position.createAt', () => {
  313. const startPos = Position.createAt( root, 1 );
  314. const endPos = Position.createAt( root, 2 );
  315. const newEndPos = Position.createAt( root, 4 );
  316. const spy = testUtils.sinon.stub( Position, 'createAt', () => newEndPos );
  317. selection.addRange( new Range( startPos, endPos ) );
  318. selection.setFocus( root, 'end' );
  319. expect( spy.calledOnce ).to.be.true;
  320. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'SAME' );
  321. } );
  322. } );
  323. describe( 'removeAllRanges', () => {
  324. let spy, ranges;
  325. beforeEach( () => {
  326. selection.addRange( liveRange );
  327. selection.addRange( range );
  328. spy = sinon.spy();
  329. selection.on( 'change:range', spy );
  330. ranges = selection._ranges;
  331. selection.removeAllRanges();
  332. } );
  333. it( 'should remove all stored ranges', () => {
  334. expect( Array.from( selection.getRanges() ).length ).to.equal( 0 );
  335. } );
  336. it( 'should fire exactly one update event', () => {
  337. expect( spy.calledOnce ).to.be.true;
  338. } );
  339. } );
  340. describe( 'setRanges', () => {
  341. let newRanges, spy, oldRanges;
  342. before( () => {
  343. newRanges = [
  344. new Range( new Position( root, [ 4 ] ), new Position( root, [ 5 ] ) ),
  345. new Range( new Position( root, [ 5, 0 ] ), new Position( root, [ 6, 0 ] ) )
  346. ];
  347. } );
  348. beforeEach( () => {
  349. selection.addRange( liveRange );
  350. selection.addRange( range );
  351. spy = sinon.spy();
  352. selection.on( 'change:range', spy );
  353. oldRanges = selection._ranges;
  354. } );
  355. it( 'should remove all ranges and add given ranges', () => {
  356. selection.setRanges( newRanges );
  357. let ranges = selection._ranges;
  358. expect( ranges.length ).to.equal( 2 );
  359. expect( ranges[ 0 ].isEqual( newRanges[ 0 ] ) ).to.be.true;
  360. expect( ranges[ 1 ].isEqual( newRanges[ 1 ] ) ).to.be.true;
  361. } );
  362. it( 'should use last range from given array to get anchor and focus position', () => {
  363. selection.setRanges( newRanges );
  364. expect( selection.anchor.path ).to.deep.equal( [ 5, 0 ] );
  365. expect( selection.focus.path ).to.deep.equal( [ 6, 0 ] );
  366. } );
  367. it( 'should acknowledge backward flag when setting anchor and focus', () => {
  368. selection.setRanges( newRanges, true );
  369. expect( selection.anchor.path ).to.deep.equal( [ 6, 0 ] );
  370. expect( selection.focus.path ).to.deep.equal( [ 5, 0 ] );
  371. } );
  372. it( 'should fire exactly one update event', () => {
  373. selection.setRanges( newRanges );
  374. expect( spy.calledOnce ).to.be.true;
  375. } );
  376. } );
  377. describe( 'getFirstRange', () => {
  378. it( 'should return null if no ranges were added', () => {
  379. expect( selection.getFirstRange() ).to.be.null;
  380. } );
  381. it( 'should return a range which start position is before all other ranges\' start positions', () => {
  382. // This will not be the first range despite being added as first
  383. selection.addRange( new Range( new Position( root, [ 4 ] ), new Position( root, [ 5 ] ) ) );
  384. // This should be the first range.
  385. selection.addRange( new Range( new Position( root, [ 1 ] ), new Position( root, [ 4 ] ) ) );
  386. // A random range that is not first.
  387. selection.addRange( new Range( new Position( root, [ 6 ] ), new Position( root, [ 7 ] ) ) );
  388. let range = selection.getFirstRange();
  389. expect( range.start.path ).to.deep.equal( [ 1 ] );
  390. expect( range.end.path ).to.deep.equal( [ 4 ] );
  391. } );
  392. } );
  393. describe( 'getFirstPosition', () => {
  394. it( 'should return null if no ranges were added', () => {
  395. expect( selection.getFirstPosition() ).to.be.null;
  396. } );
  397. it( 'should return a position that is in selection and is before any other position from the selection', () => {
  398. // This will not be a range containing the first position despite being added as first
  399. selection.addRange( new Range( new Position( root, [ 4 ] ), new Position( root, [ 5 ] ) ) );
  400. // This should be the first range.
  401. selection.addRange( new Range( new Position( root, [ 1 ] ), new Position( root, [ 4 ] ) ) );
  402. // A random range that is not first.
  403. selection.addRange( new Range( new Position( root, [ 6 ] ), new Position( root, [ 7 ] ) ) );
  404. let position = selection.getFirstPosition();
  405. expect( position.path ).to.deep.equal( [ 1 ] );
  406. } );
  407. } );
  408. describe( 'createFromSelection', () => {
  409. it( 'should return a Selection instance with same ranges and direction as given selection', () => {
  410. selection.addRange( liveRange );
  411. selection.addRange( range, true );
  412. const snapshot = Selection.createFromSelection( selection );
  413. expect( selection.isBackward ).to.equal( snapshot.isBackward );
  414. const selectionRanges = Array.from( selection.getRanges() );
  415. const snapshotRanges = Array.from( snapshot.getRanges() );
  416. expect( selectionRanges.length ).to.equal( snapshotRanges.length );
  417. for ( let i = 0; i < selectionRanges.length; i++ ) {
  418. expect( selectionRanges[ i ].isEqual( snapshotRanges[ i ] ) ).to.be.true;
  419. }
  420. } );
  421. } );
  422. describe( 'attributes interface', () => {
  423. let rangeInFullP;
  424. beforeEach( () => {
  425. root.insertChildren( 0, [
  426. new Element( 'p', [], 'foobar' ),
  427. new Element( 'p', [], [] )
  428. ] );
  429. rangeInFullP = new Range( new Position( root, [ 0, 4 ] ), new Position( root, [ 0, 4 ] ) );
  430. } );
  431. describe( 'setAttribute', () => {
  432. it( 'should set given attribute on the selection', () => {
  433. selection.setRanges( [ rangeInFullP ] );
  434. selection.setAttribute( 'foo', 'bar' );
  435. expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  436. } );
  437. it( 'should fire change:attribute event', () => {
  438. let spy = sinon.spy();
  439. selection.on( 'change:attribute', spy );
  440. selection.setAttribute( 'foo', 'bar' );
  441. expect( spy.called ).to.be.true;
  442. } );
  443. } );
  444. describe( 'getAttribute', () => {
  445. it( 'should return undefined if element does not contain given attribute', () => {
  446. expect( selection.getAttribute( 'abc' ) ).to.be.undefined;
  447. } );
  448. } );
  449. describe( 'getAttributes', () => {
  450. it( 'should return an iterator that iterates over all attributes set on the text fragment', () => {
  451. selection.setRanges( [ rangeInFullP ] );
  452. selection.setAttribute( 'foo', 'bar' );
  453. selection.setAttribute( 'abc', 'xyz' );
  454. let attrs = Array.from( selection.getAttributes() );
  455. expect( attrs ).to.deep.equal( [ [ 'foo', 'bar' ], [ 'abc', 'xyz' ] ] );
  456. } );
  457. } );
  458. describe( 'hasAttribute', () => {
  459. it( 'should return true if element contains attribute with given key', () => {
  460. selection.setRanges( [ rangeInFullP ] );
  461. selection.setAttribute( 'foo', 'bar' );
  462. expect( selection.hasAttribute( 'foo' ) ).to.be.true;
  463. } );
  464. it( 'should return false if element does not contain attribute with given key', () => {
  465. expect( selection.hasAttribute( 'abc' ) ).to.be.false;
  466. } );
  467. } );
  468. describe( 'clearAttributes', () => {
  469. it( 'should remove all attributes from the element', () => {
  470. selection.setRanges( [ rangeInFullP ] );
  471. selection.setAttribute( 'foo', 'bar' );
  472. selection.setAttribute( 'abc', 'xyz' );
  473. selection.clearAttributes();
  474. expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
  475. expect( selection.getAttribute( 'abc' ) ).to.be.undefined;
  476. } );
  477. it( 'should fire change:attribute event', () => {
  478. let spy = sinon.spy();
  479. selection.on( 'change:attribute', spy );
  480. selection.clearAttributes();
  481. expect( spy.called ).to.be.true;
  482. } );
  483. } );
  484. describe( 'removeAttribute', () => {
  485. it( 'should remove attribute', () => {
  486. selection.setRanges( [ rangeInFullP ] );
  487. selection.setAttribute( 'foo', 'bar' );
  488. selection.removeAttribute( 'foo' );
  489. expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
  490. } );
  491. it( 'should fire change:attribute event', () => {
  492. let spy = sinon.spy();
  493. selection.on( 'change:attribute', spy );
  494. selection.removeAttribute( 'foo' );
  495. expect( spy.called ).to.be.true;
  496. } );
  497. } );
  498. describe( 'setAttributesTo', () => {
  499. it( 'should remove all attributes set on element and set the given ones', () => {
  500. selection.setRanges( [ rangeInFullP ] );
  501. selection.setAttribute( 'abc', 'xyz' );
  502. selection.setAttributesTo( { foo: 'bar' } );
  503. expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  504. expect( selection.getAttribute( 'abc' ) ).to.be.undefined;
  505. } );
  506. } );
  507. } );
  508. } );