8
0

documentselection.js 17 KB

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