liveselection.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  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( 'getLastRange', () => {
  207. it( 'should return default range if no ranges were added', () => {
  208. const lastRange = selection.getLastRange();
  209. expect( lastRange.start.isEqual( new Position( root, [ 0, 0 ] ) ) );
  210. expect( lastRange.end.isEqual( new Position( root, [ 0, 0 ] ) ) );
  211. } );
  212. } );
  213. describe( 'createFromSelection', () => {
  214. it( 'should return a LiveSelection instance', () => {
  215. selection.addRange( range, true );
  216. expect( LiveSelection.createFromSelection( selection ) ).to.be.instanceof( LiveSelection );
  217. } );
  218. } );
  219. // LiveSelection uses LiveRanges so here are only simple test to see if integration is
  220. // working well, without getting into complicated corner cases.
  221. describe( 'after applying an operation should get updated and not fire update event', () => {
  222. let spy;
  223. beforeEach( () => {
  224. root.insertChildren( 0, [
  225. new Element( 'ul', [], new Text( 'abcdef' ) ),
  226. new Element( 'p', [], new Text( 'foobar' ) ),
  227. new Text( 'xyz' )
  228. ] );
  229. selection.addRange( new Range( new Position( root, [ 0, 2 ] ), new Position( root, [ 1, 4 ] ) ) );
  230. spy = sinon.spy();
  231. selection.on( 'change:range', spy );
  232. } );
  233. describe( 'InsertOperation', () => {
  234. it( 'before selection', () => {
  235. doc.applyOperation( wrapInDelta(
  236. new InsertOperation(
  237. new Position( root, [ 0, 1 ] ),
  238. 'xyz',
  239. doc.version
  240. )
  241. ) );
  242. let range = selection._ranges[ 0 ];
  243. expect( range.start.path ).to.deep.equal( [ 0, 5 ] );
  244. expect( range.end.path ).to.deep.equal( [ 1, 4 ] );
  245. expect( spy.called ).to.be.false;
  246. } );
  247. it( 'inside selection', () => {
  248. doc.applyOperation( wrapInDelta(
  249. new InsertOperation(
  250. new Position( root, [ 1, 0 ] ),
  251. 'xyz',
  252. doc.version
  253. )
  254. ) );
  255. let range = selection._ranges[ 0 ];
  256. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  257. expect( range.end.path ).to.deep.equal( [ 1, 7 ] );
  258. expect( spy.called ).to.be.false;
  259. } );
  260. } );
  261. describe( 'MoveOperation', () => {
  262. it( 'move range from before a selection', () => {
  263. doc.applyOperation( wrapInDelta(
  264. new MoveOperation(
  265. new Position( root, [ 0, 0 ] ),
  266. 2,
  267. new Position( root, [ 2 ] ),
  268. doc.version
  269. )
  270. ) );
  271. let range = selection._ranges[ 0 ];
  272. expect( range.start.path ).to.deep.equal( [ 0, 0 ] );
  273. expect( range.end.path ).to.deep.equal( [ 1, 4 ] );
  274. expect( spy.called ).to.be.false;
  275. } );
  276. it( 'moved into before a selection', () => {
  277. doc.applyOperation( wrapInDelta(
  278. new MoveOperation(
  279. new Position( root, [ 2 ] ),
  280. 2,
  281. new Position( root, [ 0, 0 ] ),
  282. doc.version
  283. )
  284. ) );
  285. let range = selection._ranges[ 0 ];
  286. expect( range.start.path ).to.deep.equal( [ 0, 4 ] );
  287. expect( range.end.path ).to.deep.equal( [ 1, 4 ] );
  288. expect( spy.called ).to.be.false;
  289. } );
  290. it( 'move range from inside of selection', () => {
  291. doc.applyOperation( wrapInDelta(
  292. new MoveOperation(
  293. new Position( root, [ 1, 0 ] ),
  294. 2,
  295. new Position( root, [ 2 ] ),
  296. doc.version
  297. )
  298. ) );
  299. let range = selection._ranges[ 0 ];
  300. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  301. expect( range.end.path ).to.deep.equal( [ 1, 2 ] );
  302. expect( spy.called ).to.be.false;
  303. } );
  304. it( 'moved range intersects with selection', () => {
  305. doc.applyOperation( wrapInDelta(
  306. new MoveOperation(
  307. new Position( root, [ 1, 3 ] ),
  308. 2,
  309. new Position( root, [ 4 ] ),
  310. doc.version
  311. )
  312. ) );
  313. let range = selection._ranges[ 0 ];
  314. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  315. expect( range.end.path ).to.deep.equal( [ 1, 3 ] );
  316. expect( spy.called ).to.be.false;
  317. } );
  318. it( 'split inside selection (do not break selection)', () => {
  319. doc.applyOperation( wrapInDelta(
  320. new InsertOperation(
  321. new Position( root, [ 2 ] ),
  322. new Element( 'p' ),
  323. doc.version
  324. )
  325. ) );
  326. doc.applyOperation( wrapInDelta(
  327. new MoveOperation(
  328. new Position( root, [ 1, 2 ] ),
  329. 4,
  330. new Position( root, [ 2, 0 ] ),
  331. doc.version
  332. )
  333. ) );
  334. let range = selection._ranges[ 0 ];
  335. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  336. expect( range.end.path ).to.deep.equal( [ 2, 2 ] );
  337. expect( spy.called ).to.be.false;
  338. } );
  339. } );
  340. } );
  341. describe( 'attributes interface', () => {
  342. let fullP, emptyP, rangeInFullP, rangeInEmptyP;
  343. beforeEach( () => {
  344. root.insertChildren( 0, [
  345. new Element( 'p', [], new Text( 'foobar' ) ),
  346. new Element( 'p', [], [] )
  347. ] );
  348. fullP = root.getChild( 0 );
  349. emptyP = root.getChild( 1 );
  350. rangeInFullP = new Range( new Position( root, [ 0, 4 ] ), new Position( root, [ 0, 4 ] ) );
  351. rangeInEmptyP = new Range( new Position( root, [ 1, 0 ] ), new Position( root, [ 1, 0 ] ) );
  352. } );
  353. describe( 'setAttribute', () => {
  354. it( 'should store attribute if the selection is in empty node', () => {
  355. selection.setRanges( [ rangeInEmptyP ] );
  356. selection.setAttribute( 'foo', 'bar' );
  357. expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  358. expect( emptyP.getAttribute( LiveSelection._getStoreAttributeKey( 'foo' ) ) ).to.equal( 'bar' );
  359. } );
  360. } );
  361. describe( 'setAttributesTo', () => {
  362. it( 'should remove all stored attributes and store the given ones if the selection is in empty node', () => {
  363. selection.setRanges( [ rangeInEmptyP ] );
  364. selection.setAttribute( 'abc', 'xyz' );
  365. selection.setAttributesTo( { foo: 'bar' } );
  366. expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  367. expect( selection.getAttribute( 'abc' ) ).to.be.undefined;
  368. expect( emptyP.getAttribute( LiveSelection._getStoreAttributeKey( 'foo' ) ) ).to.equal( 'bar' );
  369. expect( emptyP.hasAttribute( LiveSelection._getStoreAttributeKey( 'abc' ) ) ).to.be.false;
  370. } );
  371. } );
  372. describe( 'removeAttribute', () => {
  373. it( 'should remove attribute set on the text fragment', () => {
  374. selection.setRanges( [ rangeInFullP ] );
  375. selection.setAttribute( 'foo', 'bar' );
  376. selection.removeAttribute( 'foo' );
  377. expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
  378. expect( fullP.hasAttribute( LiveSelection._getStoreAttributeKey( 'foo' ) ) ).to.be.false;
  379. } );
  380. it( 'should remove stored attribute if the selection is in empty node', () => {
  381. selection.setRanges( [ rangeInEmptyP ] );
  382. selection.setAttribute( 'foo', 'bar' );
  383. selection.removeAttribute( 'foo' );
  384. expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
  385. expect( emptyP.hasAttribute( LiveSelection._getStoreAttributeKey( 'foo' ) ) ).to.be.false;
  386. } );
  387. } );
  388. describe( 'clearAttributes', () => {
  389. it( 'should remove all stored attributes if the selection is in empty node', () => {
  390. selection.setRanges( [ rangeInEmptyP ] );
  391. selection.setAttribute( 'foo', 'bar' );
  392. selection.setAttribute( 'abc', 'xyz' );
  393. selection.clearAttributes();
  394. expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
  395. expect( selection.getAttribute( 'abc' ) ).to.be.undefined;
  396. expect( emptyP.hasAttribute( LiveSelection._getStoreAttributeKey( 'foo' ) ) ).to.be.false;
  397. expect( emptyP.hasAttribute( LiveSelection._getStoreAttributeKey( 'abc' ) ) ).to.be.false;
  398. } );
  399. } );
  400. } );
  401. describe( '_updateAttributes', () => {
  402. beforeEach( () => {
  403. root.insertChildren( 0, [
  404. new Element( 'p', { p: true } ),
  405. new Text( 'a', { a: true } ),
  406. new Element( 'p', { p: true } ),
  407. new Text( 'b', { b: true } ),
  408. new Text( 'c', { c: true } ),
  409. new Element( 'p', [], [
  410. new Text( 'd', { d: true } )
  411. ] ),
  412. new Element( 'p', { p: true } ),
  413. new Text( 'e', { e: true } )
  414. ] );
  415. } );
  416. it( 'if selection is a range, should find first character in it and copy it\'s attributes', () => {
  417. selection.setRanges( [ new Range( new Position( root, [ 2 ] ), new Position( root, [ 5 ] ) ) ] );
  418. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'b', true ] ] );
  419. // Step into elements when looking for first character:
  420. selection.setRanges( [ new Range( new Position( root, [ 5 ] ), new Position( root, [ 7 ] ) ) ] );
  421. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'd', true ] ] );
  422. } );
  423. it( 'if selection is collapsed it should seek a character to copy that character\'s attributes', () => {
  424. // Take styles from character before selection.
  425. selection.setRanges( [ new Range( new Position( root, [ 2 ] ), new Position( root, [ 2 ] ) ) ] );
  426. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'a', true ] ] );
  427. // If there are none,
  428. // Take styles from character after selection.
  429. selection.setRanges( [ new Range( new Position( root, [ 3 ] ), new Position( root, [ 3 ] ) ) ] );
  430. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'b', true ] ] );
  431. // If there are none,
  432. // Look from the selection position to the beginning of node looking for character to take attributes from.
  433. selection.setRanges( [ new Range( new Position( root, [ 6 ] ), new Position( root, [ 6 ] ) ) ] );
  434. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'c', true ] ] );
  435. // If there are none,
  436. // Look from the selection position to the end of node looking for character to take attributes from.
  437. selection.setRanges( [ new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) ) ] );
  438. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'a', true ] ] );
  439. // If there are no characters to copy attributes from, use stored attributes.
  440. selection.setRanges( [ new Range( new Position( root, [ 0, 0 ] ), new Position( root, [ 0, 0 ] ) ) ] );
  441. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [] );
  442. } );
  443. it( 'should fire change:attribute event', () => {
  444. let spy = sinon.spy();
  445. selection.on( 'change:attribute', spy );
  446. selection.setRanges( [ new Range( new Position( root, [ 2 ] ), new Position( root, [ 5 ] ) ) ] );
  447. expect( spy.called ).to.be.true;
  448. } );
  449. } );
  450. describe( '_getStoredAttributes', () => {
  451. it( 'should return no values if there are no ranges in selection', () => {
  452. let values = Array.from( selection._getStoredAttributes() );
  453. expect( values ).to.deep.equal( [] );
  454. } );
  455. } );
  456. } );