8
0

selection.js 25 KB

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