8
0

liveselection.js 18 KB

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