liveselection.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  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. import Document from '/ckeditor5/engine/model/document.js';
  7. import Element from '/ckeditor5/engine/model/element.js';
  8. import Text from '/ckeditor5/engine/model/text.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 LiveSelection from '/ckeditor5/engine/model/liveselection.js';
  13. import InsertOperation from '/ckeditor5/engine/model/operation/insertoperation.js';
  14. import MoveOperation from '/ckeditor5/engine/model/operation/moveoperation.js';
  15. import count from '/ckeditor5/utils/count.js';
  16. import testUtils from '/tests/core/_utils/utils.js';
  17. import { wrapInDelta } from '/tests/engine/model/_utils/utils.js';
  18. testUtils.createSinonSandbox();
  19. describe( 'LiveSelection', () => {
  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();
  28. root.appendChildren( [
  29. new Element( 'p' ),
  30. new Element( 'p' ),
  31. new Element( 'p', [], new Text( 'foobar' ) ),
  32. new Element( 'p' ),
  33. new Element( 'p' ),
  34. new Element( 'p' ),
  35. new Element( 'p', [], new Text( 'foobar' ) )
  36. ] );
  37. selection = doc.selection;
  38. doc.schema.registerItem( 'p', '$block' );
  39. liveRange = new LiveRange( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) );
  40. range = new Range( new Position( root, [ 2 ] ), new Position( root, [ 2, 2 ] ) );
  41. } );
  42. afterEach( () => {
  43. doc.destroy();
  44. liveRange.detach();
  45. } );
  46. describe( 'default range', () => {
  47. it( 'should go to the first editable element', () => {
  48. const ranges = Array.from( selection.getRanges() );
  49. expect( ranges.length ).to.equal( 1 );
  50. expect( selection.anchor.isEqual( new Position( root, [ 0, 0 ] ) ) ).to.be.true;
  51. expect( selection.focus.isEqual( new Position( root, [ 0, 0 ] ) ) ).to.be.true;
  52. expect( selection ).to.have.property( 'isBackward', false );
  53. } );
  54. it( 'should be set to the beginning of the doc if there is no editable element', () => {
  55. doc = new Document();
  56. root = doc.createRoot();
  57. root.insertChildren( 0, new Text( 'foobar' ) );
  58. selection = doc.selection;
  59. const ranges = Array.from( selection.getRanges() );
  60. expect( ranges.length ).to.equal( 1 );
  61. expect( selection.anchor.isEqual( new Position( root, [ 0 ] ) ) ).to.be.true;
  62. expect( selection.focus.isEqual( new Position( root, [ 0 ] ) ) ).to.be.true;
  63. expect( selection ).to.have.property( 'isBackward', false );
  64. expect( count( selection.getAttributes() ) ).to.equal( 0 );
  65. } );
  66. it( 'should skip element when you can not put selection', () => {
  67. doc = new Document();
  68. root = doc.createRoot();
  69. root.insertChildren( 0, [
  70. new Element( 'img' ),
  71. new Element( 'p', [], new Text( 'foobar' ) )
  72. ] );
  73. doc.schema.registerItem( 'img' );
  74. doc.schema.registerItem( 'p', '$block' );
  75. selection = doc.selection;
  76. const ranges = Array.from( selection.getRanges() );
  77. expect( ranges.length ).to.equal( 1 );
  78. expect( selection.anchor.isEqual( new Position( root, [ 1, 0 ] ) ) ).to.be.true;
  79. expect( selection.focus.isEqual( new Position( root, [ 1, 0 ] ) ) ).to.be.true;
  80. expect( selection ).to.have.property( 'isBackward', false );
  81. expect( count( selection.getAttributes() ) ).to.equal( 0 );
  82. } );
  83. } );
  84. describe( 'isCollapsed', () => {
  85. it( 'should return true for default range', () => {
  86. expect( selection.isCollapsed ).to.be.true;
  87. } );
  88. } );
  89. describe( 'rangeCount', () => {
  90. it( 'should return proper range count', () => {
  91. expect( selection.rangeCount ).to.equal( 1 );
  92. selection.addRange( new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) ) );
  93. expect( selection.rangeCount ).to.equal( 1 );
  94. selection.addRange( new Range( new Position( root, [ 2 ] ), new Position( root, [ 2 ] ) ) );
  95. expect( selection.rangeCount ).to.equal( 2 );
  96. } );
  97. } );
  98. describe( 'addRange', () => {
  99. it( 'should convert added Range to LiveRange', () => {
  100. selection.addRange( range );
  101. const ranges = selection._ranges;
  102. expect( ranges[ 0 ] ).to.be.instanceof( LiveRange );
  103. } );
  104. } );
  105. describe( 'collapse', () => {
  106. it( 'detaches all existing ranges', () => {
  107. selection.addRange( range );
  108. selection.addRange( liveRange );
  109. const spy = testUtils.sinon.spy( LiveRange.prototype, 'detach' );
  110. selection.collapse( root );
  111. expect( spy.calledTwice ).to.be.true;
  112. } );
  113. } );
  114. describe( 'destroy', () => {
  115. it( 'should unbind all events', () => {
  116. selection.addRange( liveRange );
  117. selection.addRange( range );
  118. const ranges = selection._ranges;
  119. sinon.spy( ranges[ 0 ], 'detach' );
  120. sinon.spy( ranges[ 1 ], 'detach' );
  121. selection.destroy();
  122. expect( ranges[ 0 ].detach.called ).to.be.true;
  123. expect( ranges[ 1 ].detach.called ).to.be.true;
  124. ranges[ 0 ].detach.restore();
  125. ranges[ 1 ].detach.restore();
  126. } );
  127. } );
  128. describe( 'setFocus', () => {
  129. it( 'modifies default range', () => {
  130. const startPos = selection.getFirstPosition();
  131. const endPos = Position.createAt( root, 'end' );
  132. selection.setFocus( endPos );
  133. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'same' );
  134. expect( selection.focus.compareWith( endPos ) ).to.equal( 'same' );
  135. } );
  136. it( 'detaches the range it replaces', () => {
  137. const startPos = Position.createAt( root, 1 );
  138. const endPos = Position.createAt( root, 2 );
  139. const newEndPos = Position.createAt( root, 4 );
  140. const spy = testUtils.sinon.spy( LiveRange.prototype, 'detach' );
  141. selection.addRange( new Range( startPos, endPos ) );
  142. selection.setFocus( newEndPos );
  143. expect( spy.calledOnce ).to.be.true;
  144. } );
  145. } );
  146. describe( 'removeAllRanges', () => {
  147. let spy, ranges;
  148. beforeEach( () => {
  149. selection.addRange( liveRange );
  150. selection.addRange( range );
  151. spy = sinon.spy();
  152. selection.on( 'change:range', spy );
  153. ranges = selection._ranges;
  154. sinon.spy( ranges[ 0 ], 'detach' );
  155. sinon.spy( ranges[ 1 ], 'detach' );
  156. selection.removeAllRanges();
  157. } );
  158. afterEach( () => {
  159. ranges[ 0 ].detach.restore();
  160. ranges[ 1 ].detach.restore();
  161. } );
  162. it( 'should remove all stored ranges (and reset to default range)', () => {
  163. expect( Array.from( selection.getRanges() ).length ).to.equal( 1 );
  164. expect( selection.anchor.isEqual( new Position( root, [ 0, 0 ] ) ) ).to.be.true;
  165. expect( selection.focus.isEqual( new Position( root, [ 0, 0 ] ) ) ).to.be.true;
  166. } );
  167. it( 'should detach ranges', () => {
  168. expect( ranges[ 0 ].detach.called ).to.be.true;
  169. expect( ranges[ 1 ].detach.called ).to.be.true;
  170. } );
  171. } );
  172. describe( 'setRanges', () => {
  173. let newRanges, spy, oldRanges;
  174. before( () => {
  175. newRanges = [
  176. new Range( new Position( root, [ 4 ] ), new Position( root, [ 5 ] ) ),
  177. new Range( new Position( root, [ 5, 0 ] ), new Position( root, [ 6, 0 ] ) )
  178. ];
  179. } );
  180. beforeEach( () => {
  181. selection.addRange( liveRange );
  182. selection.addRange( range );
  183. spy = sinon.spy();
  184. selection.on( 'change:range', spy );
  185. oldRanges = selection._ranges;
  186. sinon.spy( oldRanges[ 0 ], 'detach' );
  187. sinon.spy( oldRanges[ 1 ], 'detach' );
  188. } );
  189. afterEach( () => {
  190. oldRanges[ 0 ].detach.restore();
  191. oldRanges[ 1 ].detach.restore();
  192. } );
  193. it( 'should detach removed ranges', () => {
  194. selection.setRanges( newRanges );
  195. expect( oldRanges[ 0 ].detach.called ).to.be.true;
  196. expect( oldRanges[ 1 ].detach.called ).to.be.true;
  197. } );
  198. } );
  199. describe( 'getFirstRange', () => {
  200. it( 'should return default range if no ranges were added', () => {
  201. const firstRange = selection.getFirstRange();
  202. expect( firstRange.start.isEqual( new Position( root, [ 0, 0 ] ) ) );
  203. expect( firstRange.end.isEqual( new Position( root, [ 0, 0 ] ) ) );
  204. } );
  205. } );
  206. describe( 'getFirstPosition', () => {
  207. it( 'should return start position of default range if no ranges were added', () => {
  208. const firstPosition = selection.getFirstPosition();
  209. expect( firstPosition.isEqual( new Position( root, [ 0, 0 ] ) ) );
  210. } );
  211. } );
  212. describe( 'createFromSelection', () => {
  213. it( 'should return a LiveSelection instance', () => {
  214. selection.addRange( range, true );
  215. expect( LiveSelection.createFromSelection( selection ) ).to.be.instanceof( LiveSelection );
  216. } );
  217. } );
  218. // LiveSelection uses LiveRanges so here are only simple test to see if integration is
  219. // working well, without getting into complicated corner cases.
  220. describe( 'after applying an operation should get updated and not fire update event', () => {
  221. let spy;
  222. beforeEach( () => {
  223. root.insertChildren( 0, [
  224. new Element( 'ul', [], new Text( 'abcdef' ) ),
  225. new Element( 'p', [], new Text( 'foobar' ) ),
  226. new Text( 'xyz' )
  227. ] );
  228. selection.addRange( new Range( new Position( root, [ 0, 2 ] ), new Position( root, [ 1, 4 ] ) ) );
  229. spy = sinon.spy();
  230. selection.on( 'change:range', spy );
  231. } );
  232. describe( 'InsertOperation', () => {
  233. it( 'before selection', () => {
  234. doc.applyOperation( wrapInDelta(
  235. new InsertOperation(
  236. new Position( root, [ 0, 1 ] ),
  237. 'xyz',
  238. doc.version
  239. )
  240. ) );
  241. let range = selection._ranges[ 0 ];
  242. expect( range.start.path ).to.deep.equal( [ 0, 5 ] );
  243. expect( range.end.path ).to.deep.equal( [ 1, 4 ] );
  244. expect( spy.called ).to.be.false;
  245. } );
  246. it( 'inside selection', () => {
  247. doc.applyOperation( wrapInDelta(
  248. new InsertOperation(
  249. new Position( root, [ 1, 0 ] ),
  250. 'xyz',
  251. doc.version
  252. )
  253. ) );
  254. let range = selection._ranges[ 0 ];
  255. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  256. expect( range.end.path ).to.deep.equal( [ 1, 7 ] );
  257. expect( spy.called ).to.be.false;
  258. } );
  259. } );
  260. describe( 'MoveOperation', () => {
  261. it( 'move range from before a selection', () => {
  262. doc.applyOperation( wrapInDelta(
  263. new MoveOperation(
  264. new Position( root, [ 0, 0 ] ),
  265. 2,
  266. new Position( root, [ 2 ] ),
  267. doc.version
  268. )
  269. ) );
  270. let range = selection._ranges[ 0 ];
  271. expect( range.start.path ).to.deep.equal( [ 0, 0 ] );
  272. expect( range.end.path ).to.deep.equal( [ 1, 4 ] );
  273. expect( spy.called ).to.be.false;
  274. } );
  275. it( 'moved into before a selection', () => {
  276. doc.applyOperation( wrapInDelta(
  277. new MoveOperation(
  278. new Position( root, [ 2 ] ),
  279. 2,
  280. new Position( root, [ 0, 0 ] ),
  281. doc.version
  282. )
  283. ) );
  284. let range = selection._ranges[ 0 ];
  285. expect( range.start.path ).to.deep.equal( [ 0, 4 ] );
  286. expect( range.end.path ).to.deep.equal( [ 1, 4 ] );
  287. expect( spy.called ).to.be.false;
  288. } );
  289. it( 'move range from inside of selection', () => {
  290. doc.applyOperation( wrapInDelta(
  291. new MoveOperation(
  292. new Position( root, [ 1, 0 ] ),
  293. 2,
  294. new Position( root, [ 2 ] ),
  295. doc.version
  296. )
  297. ) );
  298. let range = selection._ranges[ 0 ];
  299. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  300. expect( range.end.path ).to.deep.equal( [ 1, 2 ] );
  301. expect( spy.called ).to.be.false;
  302. } );
  303. it( 'moved range intersects with selection', () => {
  304. doc.applyOperation( wrapInDelta(
  305. new MoveOperation(
  306. new Position( root, [ 1, 3 ] ),
  307. 2,
  308. new Position( root, [ 4 ] ),
  309. doc.version
  310. )
  311. ) );
  312. let range = selection._ranges[ 0 ];
  313. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  314. expect( range.end.path ).to.deep.equal( [ 1, 3 ] );
  315. expect( spy.called ).to.be.false;
  316. } );
  317. it( 'split inside selection (do not break selection)', () => {
  318. doc.applyOperation( wrapInDelta(
  319. new InsertOperation(
  320. new Position( root, [ 2 ] ),
  321. new Element( 'p' ),
  322. doc.version
  323. )
  324. ) );
  325. doc.applyOperation( wrapInDelta(
  326. new MoveOperation(
  327. new Position( root, [ 1, 2 ] ),
  328. 4,
  329. new Position( root, [ 2, 0 ] ),
  330. doc.version
  331. )
  332. ) );
  333. let range = selection._ranges[ 0 ];
  334. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  335. expect( range.end.path ).to.deep.equal( [ 2, 2 ] );
  336. expect( spy.called ).to.be.false;
  337. } );
  338. } );
  339. } );
  340. describe( 'attributes interface', () => {
  341. let fullP, emptyP, rangeInFullP, rangeInEmptyP;
  342. beforeEach( () => {
  343. root.insertChildren( 0, [
  344. new Element( 'p', [], new Text( 'foobar' ) ),
  345. new Element( 'p', [], [] )
  346. ] );
  347. fullP = root.getChild( 0 );
  348. emptyP = root.getChild( 1 );
  349. rangeInFullP = new Range( new Position( root, [ 0, 4 ] ), new Position( root, [ 0, 4 ] ) );
  350. rangeInEmptyP = new Range( new Position( root, [ 1, 0 ] ), new Position( root, [ 1, 0 ] ) );
  351. } );
  352. describe( 'setAttribute', () => {
  353. it( 'should store attribute if the selection is in empty node', () => {
  354. selection.setRanges( [ rangeInEmptyP ] );
  355. selection.setAttribute( 'foo', 'bar' );
  356. expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  357. expect( emptyP.getAttribute( LiveSelection._getStoreAttributeKey( 'foo' ) ) ).to.equal( 'bar' );
  358. } );
  359. } );
  360. describe( 'setAttributesTo', () => {
  361. it( 'should remove all stored attributes and store the given ones if the selection is in empty node', () => {
  362. selection.setRanges( [ rangeInEmptyP ] );
  363. selection.setAttribute( 'abc', 'xyz' );
  364. selection.setAttributesTo( { foo: 'bar' } );
  365. expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  366. expect( selection.getAttribute( 'abc' ) ).to.be.undefined;
  367. expect( emptyP.getAttribute( LiveSelection._getStoreAttributeKey( 'foo' ) ) ).to.equal( 'bar' );
  368. expect( emptyP.hasAttribute( LiveSelection._getStoreAttributeKey( 'abc' ) ) ).to.be.false;
  369. } );
  370. } );
  371. describe( 'removeAttribute', () => {
  372. it( 'should remove attribute set on the text fragment', () => {
  373. selection.setRanges( [ rangeInFullP ] );
  374. selection.setAttribute( 'foo', 'bar' );
  375. selection.removeAttribute( 'foo' );
  376. expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
  377. expect( fullP.hasAttribute( LiveSelection._getStoreAttributeKey( 'foo' ) ) ).to.be.false;
  378. } );
  379. it( 'should remove stored attribute if the selection is in empty node', () => {
  380. selection.setRanges( [ rangeInEmptyP ] );
  381. selection.setAttribute( 'foo', 'bar' );
  382. selection.removeAttribute( 'foo' );
  383. expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
  384. expect( emptyP.hasAttribute( LiveSelection._getStoreAttributeKey( 'foo' ) ) ).to.be.false;
  385. } );
  386. } );
  387. describe( 'clearAttributes', () => {
  388. it( 'should remove all stored attributes if the selection is in empty node', () => {
  389. selection.setRanges( [ rangeInEmptyP ] );
  390. selection.setAttribute( 'foo', 'bar' );
  391. selection.setAttribute( 'abc', 'xyz' );
  392. selection.clearAttributes();
  393. expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
  394. expect( selection.getAttribute( 'abc' ) ).to.be.undefined;
  395. expect( emptyP.hasAttribute( LiveSelection._getStoreAttributeKey( 'foo' ) ) ).to.be.false;
  396. expect( emptyP.hasAttribute( LiveSelection._getStoreAttributeKey( 'abc' ) ) ).to.be.false;
  397. } );
  398. } );
  399. } );
  400. describe( '_updateAttributes', () => {
  401. beforeEach( () => {
  402. root.insertChildren( 0, [
  403. new Element( 'p', { p: true } ),
  404. new Text( 'a', { a: true } ),
  405. new Element( 'p', { p: true } ),
  406. new Text( 'b', { b: true } ),
  407. new Text( 'c', { c: true } ),
  408. new Element( 'p', [], [
  409. new Text( 'd', { d: true } )
  410. ] ),
  411. new Element( 'p', { p: true } ),
  412. new Text( 'e', { e: true } )
  413. ] );
  414. } );
  415. it( 'if selection is a range, should find first character in it and copy it\'s attributes', () => {
  416. selection.setRanges( [ new Range( new Position( root, [ 2 ] ), new Position( root, [ 5 ] ) ) ] );
  417. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'b', true ] ] );
  418. // Step into elements when looking for first character:
  419. selection.setRanges( [ new Range( new Position( root, [ 5 ] ), new Position( root, [ 7 ] ) ) ] );
  420. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'd', true ] ] );
  421. } );
  422. it( 'if selection is collapsed it should seek a character to copy that character\'s attributes', () => {
  423. // Take styles from character before selection.
  424. selection.setRanges( [ new Range( new Position( root, [ 2 ] ), new Position( root, [ 2 ] ) ) ] );
  425. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'a', true ] ] );
  426. // If there are none,
  427. // Take styles from character after selection.
  428. selection.setRanges( [ new Range( new Position( root, [ 3 ] ), new Position( root, [ 3 ] ) ) ] );
  429. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'b', true ] ] );
  430. // If there are none,
  431. // Look from the selection position to the beginning of node looking for character to take attributes from.
  432. selection.setRanges( [ new Range( new Position( root, [ 6 ] ), new Position( root, [ 6 ] ) ) ] );
  433. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'c', true ] ] );
  434. // If there are none,
  435. // Look from the selection position to the end of node looking for character to take attributes from.
  436. selection.setRanges( [ new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) ) ] );
  437. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'a', true ] ] );
  438. // If there are no characters to copy attributes from, use stored attributes.
  439. selection.setRanges( [ new Range( new Position( root, [ 0, 0 ] ), new Position( root, [ 0, 0 ] ) ) ] );
  440. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [] );
  441. } );
  442. it( 'should fire change:attribute event', () => {
  443. let spy = sinon.spy();
  444. selection.on( 'change:attribute', spy );
  445. selection.setRanges( [ new Range( new Position( root, [ 2 ] ), new Position( root, [ 5 ] ) ) ] );
  446. expect( spy.called ).to.be.true;
  447. } );
  448. } );
  449. describe( '_getStoredAttributes', () => {
  450. it( 'should return no values if there are no ranges in selection', () => {
  451. let values = Array.from( selection._getStoredAttributes() );
  452. expect( values ).to.deep.equal( [] );
  453. } );
  454. } );
  455. } );