selection.js 24 KB

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