8
0

liveselection.js 18 KB

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