documentselection.js 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Model from '../../src/model/model';
  6. import Batch from '../../src/model/batch';
  7. import Element from '../../src/model/element';
  8. import Text from '../../src/model/text';
  9. import Range from '../../src/model/range';
  10. import Position from '../../src/model/position';
  11. import LiveRange from '../../src/model/liverange';
  12. import DocumentSelection from '../../src/model/documentselection';
  13. import InsertOperation from '../../src/model/operation/insertoperation';
  14. import MoveOperation from '../../src/model/operation/moveoperation';
  15. import RemoveOperation from '../../src/model/operation/removeoperation';
  16. import AttributeOperation from '../../src/model/operation/attributeoperation';
  17. import SplitDelta from '../../src/model/delta/splitdelta';
  18. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  19. import count from '@ckeditor/ckeditor5-utils/src/count';
  20. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  21. import { wrapInDelta } from '../../tests/model/_utils/utils';
  22. import { setData, getData } from '../../src/dev-utils/model';
  23. testUtils.createSinonSandbox();
  24. describe( 'DocumentSelection', () => {
  25. let model, doc, root, selection, liveRange, range;
  26. const fooStoreAttrKey = DocumentSelection._getStoreAttributeKey( 'foo' );
  27. const abcStoreAttrKey = DocumentSelection._getStoreAttributeKey( 'abc' );
  28. beforeEach( () => {
  29. model = new Model();
  30. doc = model.document;
  31. root = doc.createRoot();
  32. root.appendChildren( [
  33. new Element( 'p' ),
  34. new Element( 'p' ),
  35. new Element( 'p', [], new Text( 'foobar' ) ),
  36. new Element( 'p' ),
  37. new Element( 'p' ),
  38. new Element( 'p' ),
  39. new Element( 'p', [], new Text( 'foobar' ) )
  40. ] );
  41. selection = doc.selection;
  42. model.schema.register( 'p', { inheritAllFrom: '$block' } );
  43. model.schema.register( 'widget', {
  44. isObject: true
  45. } );
  46. liveRange = new LiveRange( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) );
  47. range = new Range( new Position( root, [ 2 ] ), new Position( root, [ 2, 2 ] ) );
  48. } );
  49. afterEach( () => {
  50. model.destroy();
  51. liveRange.detach();
  52. } );
  53. describe( 'default range', () => {
  54. it( 'should go to the first editable element', () => {
  55. const ranges = Array.from( selection.getRanges() );
  56. expect( ranges.length ).to.equal( 1 );
  57. expect( selection.anchor.isEqual( new Position( root, [ 0, 0 ] ) ) ).to.be.true;
  58. expect( selection.focus.isEqual( new Position( root, [ 0, 0 ] ) ) ).to.be.true;
  59. expect( selection ).to.have.property( 'isBackward', false );
  60. } );
  61. it( 'should be set to the beginning of the doc if there is no editable element', () => {
  62. model = new Model();
  63. doc = model.document;
  64. root = doc.createRoot();
  65. root.insertChildren( 0, new Text( 'foobar' ) );
  66. selection = doc.selection;
  67. const ranges = Array.from( selection.getRanges() );
  68. expect( ranges.length ).to.equal( 1 );
  69. expect( selection.anchor.isEqual( new Position( root, [ 0 ] ) ) ).to.be.true;
  70. expect( selection.focus.isEqual( new Position( root, [ 0 ] ) ) ).to.be.true;
  71. expect( selection ).to.have.property( 'isBackward', false );
  72. expect( count( selection.getAttributes() ) ).to.equal( 0 );
  73. } );
  74. it( 'should skip element when you can not put selection', () => {
  75. model = new Model();
  76. doc = model.document;
  77. root = doc.createRoot();
  78. root.insertChildren( 0, [
  79. new Element( 'img' ),
  80. new Element( 'p', [], new Text( 'foobar' ) )
  81. ] );
  82. model.schema.register( 'img' );
  83. model.schema.register( 'p', { inheritAllFrom: '$block' } );
  84. selection = doc.selection;
  85. const ranges = Array.from( selection.getRanges() );
  86. expect( ranges.length ).to.equal( 1 );
  87. expect( selection.anchor.isEqual( new Position( root, [ 1, 0 ] ) ) ).to.be.true;
  88. expect( selection.focus.isEqual( new Position( root, [ 1, 0 ] ) ) ).to.be.true;
  89. expect( selection ).to.have.property( 'isBackward', false );
  90. expect( count( selection.getAttributes() ) ).to.equal( 0 );
  91. } );
  92. } );
  93. describe( 'isCollapsed', () => {
  94. it( 'should be true for the default range (in text position)', () => {
  95. expect( selection.isCollapsed ).to.be.true;
  96. } );
  97. it( 'should be false for the default range (object selection) ', () => {
  98. root.insertChildren( 0, new Element( 'widget' ) );
  99. expect( selection.isCollapsed ).to.be.false;
  100. } );
  101. it( 'should back off to the default algorithm if selection has ranges', () => {
  102. selection._setTo( range );
  103. expect( selection.isCollapsed ).to.be.false;
  104. } );
  105. } );
  106. describe( 'anchor', () => {
  107. it( 'should equal the default range\'s start (in text position)', () => {
  108. const expectedPos = new Position( root, [ 0, 0 ] );
  109. expect( selection.anchor.isEqual( expectedPos ) ).to.be.true;
  110. } );
  111. it( 'should equal the default range\'s start (object selection)', () => {
  112. root.insertChildren( 0, new Element( 'widget' ) );
  113. const expectedPos = new Position( root, [ 0 ] );
  114. expect( selection.anchor.isEqual( expectedPos ) ).to.be.true;
  115. } );
  116. it( 'should back off to the default algorithm if selection has ranges', () => {
  117. selection._setTo( range );
  118. expect( selection.anchor.isEqual( range.start ) ).to.be.true;
  119. } );
  120. } );
  121. describe( 'focus', () => {
  122. it( 'should equal the default range\'s end (in text position)', () => {
  123. const expectedPos = new Position( root, [ 0, 0 ] );
  124. expect( selection.focus.isEqual( expectedPos ) ).to.be.true;
  125. } );
  126. it( 'should equal the default range\'s end (object selection)', () => {
  127. root.insertChildren( 0, new Element( 'widget' ) );
  128. const expectedPos = new Position( root, [ 1 ] );
  129. expect( selection.focus.isEqual( expectedPos ) ).to.be.true;
  130. expect( selection.focus.isEqual( selection.getFirstRange().end ) ).to.be.true;
  131. } );
  132. it( 'should back off to the default algorithm if selection has ranges', () => {
  133. selection._setTo( range );
  134. expect( selection.focus.isEqual( range.end ) ).to.be.true;
  135. } );
  136. } );
  137. describe( 'rangeCount', () => {
  138. it( 'should return proper range count', () => {
  139. expect( selection.rangeCount ).to.equal( 1 );
  140. selection._setTo( new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) ) );
  141. expect( selection.rangeCount ).to.equal( 1 );
  142. selection._setTo( [
  143. new Range( new Position( root, [ 2 ] ), new Position( root, [ 2 ] ) ),
  144. new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) )
  145. ] );
  146. expect( selection.rangeCount ).to.equal( 2 );
  147. } );
  148. } );
  149. describe( 'hasOwnRange', () => {
  150. it( 'should return true if selection has any ranges set', () => {
  151. selection._setTo( new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) ) );
  152. expect( selection.hasOwnRange ).to.be.true;
  153. } );
  154. it( 'should return false if selection has a default range', () => {
  155. expect( selection.hasOwnRange ).to.be.false;
  156. } );
  157. } );
  158. describe( 'setTo - set collapsed at', () => {
  159. it( 'detaches all existing ranges', () => {
  160. selection._setTo( [ range, liveRange ] );
  161. const spy = testUtils.sinon.spy( LiveRange.prototype, 'detach' );
  162. selection._setTo( root );
  163. expect( spy.calledTwice ).to.be.true;
  164. } );
  165. } );
  166. describe( 'destroy()', () => {
  167. it( 'should unbind all events', () => {
  168. selection._setTo( [ range, liveRange ] );
  169. const ranges = Array.from( selection._selection._ranges );
  170. sinon.spy( ranges[ 0 ], 'detach' );
  171. sinon.spy( ranges[ 1 ], 'detach' );
  172. selection.destroy();
  173. expect( ranges[ 0 ].detach.called ).to.be.true;
  174. expect( ranges[ 1 ].detach.called ).to.be.true;
  175. ranges[ 0 ].detach.restore();
  176. ranges[ 1 ].detach.restore();
  177. } );
  178. } );
  179. describe( 'setFocus()', () => {
  180. it( 'modifies default range', () => {
  181. const startPos = selection.getFirstPosition();
  182. const endPos = Position.createAt( root, 'end' );
  183. selection._setFocus( endPos );
  184. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'same' );
  185. expect( selection.focus.compareWith( endPos ) ).to.equal( 'same' );
  186. } );
  187. it( 'detaches the range it replaces', () => {
  188. const startPos = Position.createAt( root, 1 );
  189. const endPos = Position.createAt( root, 2 );
  190. const newEndPos = Position.createAt( root, 4 );
  191. const spy = testUtils.sinon.spy( LiveRange.prototype, 'detach' );
  192. selection._setTo( new Range( startPos, endPos ) );
  193. selection._setFocus( newEndPos );
  194. expect( spy.calledOnce ).to.be.true;
  195. } );
  196. it( 'refreshes attributes', () => {
  197. const spy = sinon.spy( selection._selection, '_updateAttributes' );
  198. selection._setFocus( Position.createAt( root, 1 ) );
  199. expect( spy.called ).to.be.true;
  200. } );
  201. } );
  202. describe( 'setTo - removeAllRanges', () => {
  203. let spy, ranges;
  204. beforeEach( () => {
  205. selection._setTo( [ liveRange, range ] );
  206. spy = sinon.spy();
  207. selection.on( 'change:range', spy );
  208. ranges = Array.from( selection._selection._ranges );
  209. sinon.spy( ranges[ 0 ], 'detach' );
  210. sinon.spy( ranges[ 1 ], 'detach' );
  211. selection._setTo( null );
  212. } );
  213. afterEach( () => {
  214. ranges[ 0 ].detach.restore();
  215. ranges[ 1 ].detach.restore();
  216. } );
  217. it( 'should remove all stored ranges (and reset to default range)', () => {
  218. expect( Array.from( selection.getRanges() ).length ).to.equal( 1 );
  219. expect( selection.anchor.isEqual( new Position( root, [ 0, 0 ] ) ) ).to.be.true;
  220. expect( selection.focus.isEqual( new Position( root, [ 0, 0 ] ) ) ).to.be.true;
  221. } );
  222. it( 'should detach ranges', () => {
  223. expect( ranges[ 0 ].detach.called ).to.be.true;
  224. expect( ranges[ 1 ].detach.called ).to.be.true;
  225. } );
  226. it( 'should refresh attributes', () => {
  227. const spy = sinon.spy( selection._selection, '_updateAttributes' );
  228. selection._setTo( null );
  229. expect( spy.called ).to.be.true;
  230. } );
  231. } );
  232. describe( 'setTo()', () => {
  233. it( 'should throw an error when range is invalid', () => {
  234. expect( () => {
  235. selection._setTo( [ { invalid: 'range' } ] );
  236. } ).to.throw( CKEditorError, /model-selection-added-not-range/ );
  237. } );
  238. it( 'should log a warning when trying to set selection to the graveyard', () => {
  239. // eslint-disable-next-line no-undef
  240. const warnStub = sinon.stub( console, 'warn' );
  241. const range = new Range( new Position( model.document.graveyard, [ 0 ] ) );
  242. selection._setTo( range );
  243. expect( warnStub.calledOnce ).to.equal( true );
  244. expect( warnStub.getCall( 0 ).args[ 0 ] ).to.match( /model-selection-range-in-graveyard/ );
  245. expect( selection._ranges ).to.deep.equal( [] );
  246. warnStub.restore();
  247. } );
  248. it( 'should detach removed ranges', () => {
  249. selection._setTo( [ liveRange, range ] );
  250. const oldRanges = Array.from( selection._selection._ranges );
  251. sinon.spy( oldRanges[ 0 ], 'detach' );
  252. sinon.spy( oldRanges[ 1 ], 'detach' );
  253. selection._setTo( [] );
  254. expect( oldRanges[ 0 ].detach.called ).to.be.true;
  255. expect( oldRanges[ 1 ].detach.called ).to.be.true;
  256. } );
  257. it( 'should refresh attributes', () => {
  258. const spy = sinon.spy( selection._selection, '_updateAttributes' );
  259. selection._setTo( [ range ] );
  260. expect( spy.called ).to.be.true;
  261. } );
  262. // See #630.
  263. it( 'should refresh attributes – integration test for #630', () => {
  264. model.schema.extend( '$text', { allowIn: '$root' } );
  265. setData( model, 'f<$text italic="true">[o</$text><$text bold="true">ob]a</$text>r' );
  266. selection._setTo( [ Range.createFromPositionAndShift( selection.getLastRange().end, 0 ) ] );
  267. expect( selection.getAttribute( 'bold' ) ).to.equal( true );
  268. expect( selection.hasAttribute( 'italic' ) ).to.equal( false );
  269. expect( getData( model ) )
  270. .to.equal( 'f<$text italic="true">o</$text><$text bold="true">ob[]a</$text>r' );
  271. } );
  272. } );
  273. describe( 'getFirstRange()', () => {
  274. it( 'should return default range if no ranges were added', () => {
  275. const firstRange = selection.getFirstRange();
  276. expect( firstRange.start.isEqual( new Position( root, [ 0, 0 ] ) ) );
  277. expect( firstRange.end.isEqual( new Position( root, [ 0, 0 ] ) ) );
  278. } );
  279. } );
  280. describe( 'getLastRange()', () => {
  281. it( 'should return default range if no ranges were added', () => {
  282. const lastRange = selection.getLastRange();
  283. expect( lastRange.start.isEqual( new Position( root, [ 0, 0 ] ) ) );
  284. expect( lastRange.end.isEqual( new Position( root, [ 0, 0 ] ) ) );
  285. } );
  286. } );
  287. describe( '_isStoreAttributeKey', () => {
  288. it( 'should return true if given key is a key of an attribute stored in element by DocumentSelection', () => {
  289. expect( DocumentSelection._isStoreAttributeKey( fooStoreAttrKey ) ).to.be.true;
  290. } );
  291. it( 'should return false if given key is not a key of an attribute stored in element by DocumentSelection', () => {
  292. expect( DocumentSelection._isStoreAttributeKey( 'foo' ) ).to.be.false;
  293. } );
  294. } );
  295. // DocumentSelection uses LiveRanges so here are only simple test to see if integration is
  296. // working well, without getting into complicated corner cases.
  297. describe( 'after applying an operation should get updated and fire events', () => {
  298. let spyRange;
  299. beforeEach( () => {
  300. root.removeChildren( 0, root.childCount );
  301. root.insertChildren( 0, [
  302. new Element( 'p', [], new Text( 'abcdef' ) ),
  303. new Element( 'p', [], new Text( 'foobar' ) ),
  304. new Text( 'xyz' )
  305. ] );
  306. selection._setTo( new Range( new Position( root, [ 0, 2 ] ), new Position( root, [ 1, 4 ] ) ) );
  307. spyRange = sinon.spy();
  308. selection.on( 'change:range', spyRange );
  309. } );
  310. describe( 'InsertOperation', () => {
  311. it( 'before selection', () => {
  312. selection.on( 'change:range', ( evt, data ) => {
  313. expect( data.directChange ).to.be.false;
  314. } );
  315. model.applyOperation( wrapInDelta(
  316. new InsertOperation(
  317. new Position( root, [ 0, 1 ] ),
  318. 'xyz',
  319. doc.version
  320. )
  321. ) );
  322. const range = selection.getFirstRange();
  323. expect( range.start.path ).to.deep.equal( [ 0, 5 ] );
  324. expect( range.end.path ).to.deep.equal( [ 1, 4 ] );
  325. expect( spyRange.calledOnce ).to.be.true;
  326. } );
  327. it( 'inside selection', () => {
  328. selection.on( 'change:range', ( evt, data ) => {
  329. expect( data.directChange ).to.be.false;
  330. } );
  331. model.applyOperation( wrapInDelta(
  332. new InsertOperation(
  333. new Position( root, [ 1, 0 ] ),
  334. 'xyz',
  335. doc.version
  336. )
  337. ) );
  338. const range = selection.getFirstRange();
  339. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  340. expect( range.end.path ).to.deep.equal( [ 1, 7 ] );
  341. expect( spyRange.calledOnce ).to.be.true;
  342. } );
  343. } );
  344. describe( 'MoveOperation', () => {
  345. it( 'move range from before a selection', () => {
  346. selection.on( 'change:range', ( evt, data ) => {
  347. expect( data.directChange ).to.be.false;
  348. } );
  349. model.applyOperation( wrapInDelta(
  350. new MoveOperation(
  351. new Position( root, [ 0, 0 ] ),
  352. 2,
  353. new Position( root, [ 2 ] ),
  354. doc.version
  355. )
  356. ) );
  357. const range = selection.getFirstRange();
  358. expect( range.start.path ).to.deep.equal( [ 0, 0 ] );
  359. expect( range.end.path ).to.deep.equal( [ 1, 4 ] );
  360. expect( spyRange.calledOnce ).to.be.true;
  361. } );
  362. it( 'moved into before a selection', () => {
  363. selection.on( 'change:range', ( evt, data ) => {
  364. expect( data.directChange ).to.be.false;
  365. } );
  366. model.applyOperation( wrapInDelta(
  367. new MoveOperation(
  368. new Position( root, [ 2 ] ),
  369. 2,
  370. new Position( root, [ 0, 0 ] ),
  371. doc.version
  372. )
  373. ) );
  374. const range = selection.getFirstRange();
  375. expect( range.start.path ).to.deep.equal( [ 0, 4 ] );
  376. expect( range.end.path ).to.deep.equal( [ 1, 4 ] );
  377. expect( spyRange.calledOnce ).to.be.true;
  378. } );
  379. it( 'move range from inside of selection', () => {
  380. selection.on( 'change:range', ( evt, data ) => {
  381. expect( data.directChange ).to.be.false;
  382. } );
  383. model.applyOperation( wrapInDelta(
  384. new MoveOperation(
  385. new Position( root, [ 1, 0 ] ),
  386. 2,
  387. new Position( root, [ 2 ] ),
  388. doc.version
  389. )
  390. ) );
  391. const range = selection.getFirstRange();
  392. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  393. expect( range.end.path ).to.deep.equal( [ 1, 2 ] );
  394. expect( spyRange.calledOnce ).to.be.true;
  395. } );
  396. it( 'moved range intersects with selection', () => {
  397. selection.on( 'change:range', ( evt, data ) => {
  398. expect( data.directChange ).to.be.false;
  399. } );
  400. model.applyOperation( wrapInDelta(
  401. new MoveOperation(
  402. new Position( root, [ 1, 3 ] ),
  403. 2,
  404. new Position( root, [ 4 ] ),
  405. doc.version
  406. )
  407. ) );
  408. const range = selection.getFirstRange();
  409. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  410. expect( range.end.path ).to.deep.equal( [ 5 ] );
  411. expect( spyRange.calledOnce ).to.be.true;
  412. } );
  413. it( 'split inside selection (do not break selection)', () => {
  414. selection.on( 'change:range', ( evt, data ) => {
  415. expect( data.directChange ).to.be.false;
  416. } );
  417. const batch = new Batch();
  418. const splitDelta = new SplitDelta();
  419. const insertOperation = new InsertOperation(
  420. new Position( root, [ 2 ] ),
  421. new Element( 'p' ),
  422. 0
  423. );
  424. const moveOperation = new MoveOperation(
  425. new Position( root, [ 1, 2 ] ),
  426. 4,
  427. new Position( root, [ 2, 0 ] ),
  428. 1
  429. );
  430. batch.addDelta( splitDelta );
  431. splitDelta.addOperation( insertOperation );
  432. splitDelta.addOperation( moveOperation );
  433. model.applyOperation( insertOperation );
  434. model.applyOperation( moveOperation );
  435. const range = selection.getFirstRange();
  436. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  437. expect( range.end.path ).to.deep.equal( [ 2, 2 ] );
  438. expect( spyRange.calledOnce ).to.be.true;
  439. } );
  440. } );
  441. describe( 'AttributeOperation', () => {
  442. it( 'changed range includes selection anchor', () => {
  443. const spyAttribute = sinon.spy();
  444. selection.on( 'change:attribute', spyAttribute );
  445. selection.on( 'change:attribute', ( evt, data ) => {
  446. expect( data.directChange ).to.be.false;
  447. expect( data.attributeKeys ).to.deep.equal( [ 'foo' ] );
  448. } );
  449. model.applyOperation( wrapInDelta(
  450. new AttributeOperation(
  451. new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 5 ] ) ),
  452. 'foo',
  453. null,
  454. 'bar',
  455. doc.version
  456. )
  457. ) );
  458. expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  459. expect( spyAttribute.calledOnce ).to.be.true;
  460. } );
  461. it( 'should not overwrite previously set attributes', () => {
  462. selection._setAttribute( 'foo', 'xyz' );
  463. const spyAttribute = sinon.spy();
  464. selection.on( 'change:attribute', spyAttribute );
  465. model.applyOperation( wrapInDelta(
  466. new AttributeOperation(
  467. new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 5 ] ) ),
  468. 'foo',
  469. null,
  470. 'bar',
  471. doc.version
  472. )
  473. ) );
  474. expect( selection.getAttribute( 'foo' ) ).to.equal( 'xyz' );
  475. expect( spyAttribute.called ).to.be.false;
  476. } );
  477. it( 'should not overwrite previously removed attributes', () => {
  478. selection._setAttribute( 'foo', 'xyz' );
  479. selection._removeAttribute( 'foo' );
  480. const spyAttribute = sinon.spy();
  481. selection.on( 'change:attribute', spyAttribute );
  482. model.applyOperation( wrapInDelta(
  483. new AttributeOperation(
  484. new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 5 ] ) ),
  485. 'foo',
  486. null,
  487. 'bar',
  488. doc.version
  489. )
  490. ) );
  491. expect( selection.hasAttribute( 'foo' ) ).to.be.false;
  492. expect( spyAttribute.called ).to.be.false;
  493. } );
  494. } );
  495. describe( 'RemoveOperation', () => {
  496. it( 'fix selection range if it ends up in graveyard #1', () => {
  497. selection._setTo( new Position( root, [ 1, 3 ] ) );
  498. model.applyOperation( wrapInDelta(
  499. new RemoveOperation(
  500. new Position( root, [ 1, 2 ] ),
  501. 2,
  502. new Position( doc.graveyard, [ 0 ] ),
  503. doc.version
  504. )
  505. ) );
  506. expect( selection.getFirstPosition().path ).to.deep.equal( [ 1, 2 ] );
  507. } );
  508. it( 'fix selection range if it ends up in graveyard #2', () => {
  509. selection._setTo( [ new Range( new Position( root, [ 1, 2 ] ), new Position( root, [ 1, 4 ] ) ) ] );
  510. model.applyOperation( wrapInDelta(
  511. new RemoveOperation(
  512. new Position( root, [ 1, 2 ] ),
  513. 2,
  514. new Position( doc.graveyard, [ 0 ] ),
  515. doc.version
  516. )
  517. ) );
  518. expect( selection.getFirstPosition().path ).to.deep.equal( [ 1, 2 ] );
  519. } );
  520. it( 'fix selection range if it ends up in graveyard #3', () => {
  521. selection._setTo( [ new Range( new Position( root, [ 1, 1 ] ), new Position( root, [ 1, 2 ] ) ) ] );
  522. model.applyOperation( wrapInDelta(
  523. new RemoveOperation(
  524. new Position( root, [ 1 ] ),
  525. 2,
  526. new Position( doc.graveyard, [ 0 ] ),
  527. doc.version
  528. )
  529. ) );
  530. expect( selection.getFirstPosition().path ).to.deep.equal( [ 0, 6 ] );
  531. } );
  532. it( 'fix selection range if it ends up in graveyard #4 - whole content removed', () => {
  533. model.applyOperation( wrapInDelta(
  534. new RemoveOperation(
  535. new Position( root, [ 0 ] ),
  536. 3,
  537. new Position( doc.graveyard, [ 0 ] ),
  538. doc.version
  539. )
  540. ) );
  541. expect( selection.getFirstPosition().path ).to.deep.equal( [ 0 ] );
  542. model.applyOperation( wrapInDelta(
  543. new InsertOperation(
  544. new Position( root, [ 0 ] ),
  545. new Element( 'p' ),
  546. doc.version
  547. )
  548. ) );
  549. // Now it's clear that it's the default range.
  550. expect( selection.getFirstPosition().path ).to.deep.equal( [ 0, 0 ] );
  551. } );
  552. } );
  553. } );
  554. describe( 'attributes', () => {
  555. let fullP, emptyP, rangeInFullP, rangeInEmptyP;
  556. beforeEach( () => {
  557. root.removeChildren( 0, root.childCount );
  558. root.appendChildren( [
  559. new Element( 'p', [], new Text( 'foobar' ) ),
  560. new Element( 'p', [], [] )
  561. ] );
  562. fullP = root.getChild( 0 );
  563. emptyP = root.getChild( 1 );
  564. rangeInFullP = new Range( new Position( root, [ 0, 4 ] ), new Position( root, [ 0, 4 ] ) );
  565. rangeInEmptyP = new Range( new Position( root, [ 1, 0 ] ), new Position( root, [ 1, 0 ] ) );
  566. // I've lost 30 mins debugging why my tests fail and that was due to the above code reusing
  567. // a root which wasn't empty (so the ranges didn't actualy make much sense).
  568. expect( root.childCount ).to.equal( 2 );
  569. } );
  570. describe( '_setAttribute()', () => {
  571. it( 'should set attribute', () => {
  572. selection._setTo( [ rangeInEmptyP ] );
  573. selection._setAttribute( 'foo', 'bar' );
  574. expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  575. } );
  576. } );
  577. describe( 'removeAttribute()', () => {
  578. it( 'should remove attribute set on the text fragment', () => {
  579. selection._setTo( [ rangeInFullP ] );
  580. selection._setAttribute( 'foo', 'bar' );
  581. selection._removeAttribute( 'foo' );
  582. expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
  583. } );
  584. } );
  585. describe( '_getStoredAttributes()', () => {
  586. it( 'should return no values if there are no ranges in selection', () => {
  587. const values = Array.from( selection._getStoredAttributes() );
  588. expect( values ).to.deep.equal( [] );
  589. } );
  590. } );
  591. describe( 'are updated on a direct range change', () => {
  592. beforeEach( () => {
  593. root.insertChildren( 0, [
  594. new Element( 'p', { p: true } ),
  595. new Text( 'a', { a: true } ),
  596. new Element( 'p', { p: true } ),
  597. new Text( 'b', { b: true } ),
  598. new Text( 'c', { c: true } ),
  599. new Element( 'p', [], [
  600. new Text( 'd', { d: true } )
  601. ] ),
  602. new Element( 'p', { p: true } ),
  603. new Text( 'e', { e: true } )
  604. ] );
  605. } );
  606. it( 'if selection is a range, should find first character in it and copy it\'s attributes', () => {
  607. selection._setTo( [ new Range( new Position( root, [ 2 ] ), new Position( root, [ 5 ] ) ) ] );
  608. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'b', true ] ] );
  609. // Step into elements when looking for first character:
  610. selection._setTo( [ new Range( new Position( root, [ 5 ] ), new Position( root, [ 7 ] ) ) ] );
  611. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'd', true ] ] );
  612. } );
  613. it( 'if selection is collapsed it should seek a character to copy that character\'s attributes', () => {
  614. // Take styles from character before selection.
  615. selection._setTo( [ new Range( new Position( root, [ 2 ] ), new Position( root, [ 2 ] ) ) ] );
  616. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'a', true ] ] );
  617. // If there are none,
  618. // Take styles from character after selection.
  619. selection._setTo( [ new Range( new Position( root, [ 3 ] ), new Position( root, [ 3 ] ) ) ] );
  620. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'b', true ] ] );
  621. // If there are none,
  622. // Look from the selection position to the beginning of node looking for character to take attributes from.
  623. selection._setTo( [ new Range( new Position( root, [ 6 ] ), new Position( root, [ 6 ] ) ) ] );
  624. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'c', true ] ] );
  625. // If there are none,
  626. // Look from the selection position to the end of node looking for character to take attributes from.
  627. selection._setTo( [ new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) ) ] );
  628. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'a', true ] ] );
  629. // If there are no characters to copy attributes from, use stored attributes.
  630. selection._setTo( [ new Range( new Position( root, [ 0, 0 ] ), new Position( root, [ 0, 0 ] ) ) ] );
  631. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [] );
  632. } );
  633. it( 'should overwrite any previously set attributes', () => {
  634. selection._setTo( new Position( root, [ 5, 0 ] ) );
  635. selection._setAttribute( 'x', true );
  636. selection._setAttribute( 'y', true );
  637. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'd', true ], [ 'x', true ], [ 'y', true ] ] );
  638. selection._setTo( new Position( root, [ 1 ] ) );
  639. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'a', true ] ] );
  640. } );
  641. it( 'should fire change:attribute event', () => {
  642. const spy = sinon.spy();
  643. selection.on( 'change:attribute', spy );
  644. selection._setTo( [ new Range( new Position( root, [ 2 ] ), new Position( root, [ 5 ] ) ) ] );
  645. expect( spy.calledOnce ).to.be.true;
  646. } );
  647. it( 'should not fire change:attribute event if attributes did not change', () => {
  648. selection._setTo( new Position( root, [ 5, 0 ] ) );
  649. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'd', true ] ] );
  650. const spy = sinon.spy();
  651. selection.on( 'change:attribute', spy );
  652. selection._setTo( new Position( root, [ 5, 1 ] ) );
  653. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'd', true ] ] );
  654. expect( spy.called ).to.be.false;
  655. } );
  656. } );
  657. // #986
  658. describe( 'are not inherited from the inside of object elements', () => {
  659. beforeEach( () => {
  660. model.schema.register( 'image', {
  661. isObject: true
  662. } );
  663. model.schema.extend( 'image', { allowIn: '$root' } );
  664. model.schema.extend( 'image', { allowIn: '$block' } );
  665. model.schema.register( 'caption' );
  666. model.schema.extend( 'caption', { allowIn: 'image' } );
  667. model.schema.extend( '$text', {
  668. allowIn: [ 'image', 'caption' ],
  669. allowAttributes: 'bold'
  670. } );
  671. } );
  672. it( 'ignores attributes inside an object if selection contains that object', () => {
  673. setData( model, '<p>[<image><$text bold="true">Caption for the image.</$text></image>]</p>' );
  674. expect( selection.hasAttribute( 'bold' ) ).to.equal( false );
  675. } );
  676. it( 'ignores attributes inside an object if selection contains that object (deeper structure)', () => {
  677. setData( model, '<p>[<image><caption><$text bold="true">Caption for the image.</$text></caption></image>]</p>' );
  678. expect( selection.hasAttribute( 'bold' ) ).to.equal( false );
  679. } );
  680. it( 'ignores attributes inside an object if selection contains that object (block level)', () => {
  681. setData( model, '<p>foo</p>[<image><$text bold="true">Caption for the image.</$text></image>]<p>foo</p>' );
  682. expect( selection.hasAttribute( 'bold' ) ).to.equal( false );
  683. } );
  684. it( 'reads attributes from text even if the selection contains an object', () => {
  685. setData( model, '<p>x<$text bold="true">[bar</$text><image></image>foo]</p>' );
  686. expect( selection.getAttribute( 'bold' ) ).to.equal( true );
  687. } );
  688. it( 'reads attributes when the entire selection inside an object', () => {
  689. setData( model, '<p><image><caption><$text bold="true">[bar]</$text></caption></image></p>' );
  690. expect( selection.getAttribute( 'bold' ) ).to.equal( true );
  691. } );
  692. it( 'stops reading attributes if selection starts with an object', () => {
  693. setData( model, '<p>[<image></image><$text bold="true">bar]</$text></p>' );
  694. expect( selection.hasAttribute( 'bold' ) ).to.equal( false );
  695. } );
  696. } );
  697. describe( 'parent element\'s attributes', () => {
  698. it( 'are set using a normal batch', () => {
  699. const batchTypes = [];
  700. model.on( 'applyOperation', ( event, args ) => {
  701. const operation = args[ 0 ];
  702. const batch = operation.delta.batch;
  703. batchTypes.push( batch.type );
  704. } );
  705. selection._setTo( [ rangeInEmptyP ] );
  706. model.change( writer => {
  707. writer.setSelectionAttribute( 'foo', 'bar' );
  708. } );
  709. expect( batchTypes ).to.deep.equal( [ 'default' ] );
  710. expect( emptyP.getAttribute( fooStoreAttrKey ) ).to.equal( 'bar' );
  711. } );
  712. it( 'are removed when any content is inserted (reuses the same batch)', () => {
  713. // Dedupe batches by using a map (multiple change events will be fired).
  714. const batchTypes = new Map();
  715. selection._setTo( rangeInEmptyP );
  716. selection._setAttribute( 'foo', 'bar' );
  717. selection._setAttribute( 'abc', 'bar' );
  718. model.on( 'applyOperation', ( event, args ) => {
  719. const operation = args[ 0 ];
  720. const batch = operation.delta.batch;
  721. batchTypes.set( batch, batch.type );
  722. } );
  723. model.change( writer => {
  724. writer.insertText( 'x', rangeInEmptyP.start );
  725. } );
  726. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  727. expect( emptyP.hasAttribute( abcStoreAttrKey ) ).to.be.false;
  728. expect( Array.from( batchTypes.values() ) ).to.deep.equal( [ 'default' ] );
  729. } );
  730. it( 'are removed when any content is moved into', () => {
  731. selection._setTo( rangeInEmptyP );
  732. selection._setAttribute( 'foo', 'bar' );
  733. model.change( writer => {
  734. writer.move( Range.createOn( fullP.getChild( 0 ) ), rangeInEmptyP.start );
  735. } );
  736. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  737. } );
  738. it( 'are removed when containing element is merged with a non-empty element', () => {
  739. const emptyP2 = new Element( 'p', null, 'x' );
  740. root.appendChildren( emptyP2 );
  741. emptyP.setAttribute( fooStoreAttrKey, 'bar' );
  742. emptyP2.setAttribute( fooStoreAttrKey, 'bar' );
  743. model.change( writer => {
  744. // <emptyP>{}<emptyP2>
  745. writer.merge( Position.createAfter( emptyP ) );
  746. } );
  747. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  748. expect( emptyP.parent ).to.equal( root ); // Just to be sure we're checking the right element.
  749. } );
  750. it( 'are removed even when there is no selection in it', () => {
  751. emptyP.setAttribute( fooStoreAttrKey, 'bar' );
  752. selection._setTo( [ rangeInFullP ] );
  753. model.change( writer => {
  754. writer.insertText( 'x', rangeInEmptyP.start );
  755. } );
  756. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  757. } );
  758. it( 'are removed only once in case of multi-op deltas', () => {
  759. let batch;
  760. const emptyP2 = new Element( 'p', null, 'x' );
  761. root.appendChildren( emptyP2 );
  762. emptyP.setAttribute( fooStoreAttrKey, 'bar' );
  763. emptyP2.setAttribute( fooStoreAttrKey, 'bar' );
  764. model.change( writer => {
  765. batch = writer.batch;
  766. // <emptyP>{}<emptyP2>
  767. writer.merge( Position.createAfter( emptyP ) );
  768. } );
  769. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  770. // Attribute delta is only one.
  771. expect( Array.from( batch.deltas, delta => delta.type ) ).to.deep.equal( [ 'merge', 'attribute' ] );
  772. } );
  773. it( 'uses model change to clear attributes', () => {
  774. selection._setTo( [ rangeInEmptyP ] );
  775. model.change( writer => {
  776. writer.setSelectionAttribute( 'foo', 'bar' );
  777. writer.insertText( 'x', rangeInEmptyP.start );
  778. // `emptyP` still has the attribute, because attribute clearing is in enqueued block.
  779. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.true;
  780. } );
  781. // When the dust settles, `emptyP` should not have the attribute.
  782. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  783. } );
  784. it( 'are not removed or merged when containing element is merged with another empty element', () => {
  785. const emptyP2 = new Element( 'p', null );
  786. root.appendChildren( emptyP2 );
  787. emptyP.setAttribute( fooStoreAttrKey, 'bar' );
  788. emptyP2.setAttribute( abcStoreAttrKey, 'bar' );
  789. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.true;
  790. expect( emptyP.hasAttribute( abcStoreAttrKey ) ).to.be.false;
  791. model.change( writer => {
  792. // <emptyP>{}<emptyP2>
  793. writer.merge( Position.createAfter( emptyP ) );
  794. } );
  795. expect( emptyP.getAttribute( fooStoreAttrKey ) ).to.equal( 'bar' );
  796. expect( emptyP.parent ).to.equal( root ); // Just to be sure we're checking the right element.
  797. } );
  798. // Rename and some other deltas don't specify range in doc#change event.
  799. // So let's see if there's no crash or something.
  800. it( 'are not removed on rename', () => {
  801. model.change( writer => {
  802. writer.setSelection( rangeInEmptyP );
  803. writer.setSelectionAttribute( 'foo', 'bar' );
  804. } );
  805. sinon.spy( model, 'enqueueChange' );
  806. model.change( writer => {
  807. writer.rename( emptyP, 'pnew' );
  808. } );
  809. expect( model.enqueueChange.called ).to.be.false;
  810. expect( emptyP.getAttribute( fooStoreAttrKey ) ).to.equal( 'bar' );
  811. } );
  812. } );
  813. } );
  814. it( 'should throw if one of ranges starts or ends inside surrogate pair', () => {
  815. root.removeChildren( 0, root.childCount );
  816. root.appendChildren( '\uD83D\uDCA9' );
  817. expect( () => {
  818. doc.selection._setTo( Range.createFromParentsAndOffsets( root, 0, root, 1 ) );
  819. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  820. expect( () => {
  821. doc.selection._setTo( Range.createFromParentsAndOffsets( root, 1, root, 2 ) );
  822. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  823. } );
  824. it( 'should throw if one of ranges starts or ends between base character and combining mark', () => {
  825. root.removeChildren( 0, root.childCount );
  826. root.appendChildren( 'foo̻̐ͩbar' );
  827. expect( () => {
  828. doc.selection._setTo( Range.createFromParentsAndOffsets( root, 3, root, 9 ) );
  829. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  830. expect( () => {
  831. doc.selection._setTo( Range.createFromParentsAndOffsets( root, 4, root, 9 ) );
  832. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  833. expect( () => {
  834. doc.selection._setTo( Range.createFromParentsAndOffsets( root, 5, root, 9 ) );
  835. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  836. expect( () => {
  837. doc.selection._setTo( Range.createFromParentsAndOffsets( root, 1, root, 3 ) );
  838. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  839. expect( () => {
  840. doc.selection._setTo( Range.createFromParentsAndOffsets( root, 1, root, 4 ) );
  841. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  842. expect( () => {
  843. doc.selection._setTo( Range.createFromParentsAndOffsets( root, 1, root, 5 ) );
  844. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  845. } );
  846. } );