selection.js 21 KB

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