documentselection.js 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100
  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. } );
  197. describe( 'setTo - removeAllRanges', () => {
  198. let spy, ranges;
  199. beforeEach( () => {
  200. selection._setTo( [ liveRange, range ] );
  201. spy = sinon.spy();
  202. selection.on( 'change:range', spy );
  203. ranges = Array.from( selection._selection._ranges );
  204. sinon.spy( ranges[ 0 ], 'detach' );
  205. sinon.spy( ranges[ 1 ], 'detach' );
  206. selection._setTo( null );
  207. } );
  208. afterEach( () => {
  209. ranges[ 0 ].detach.restore();
  210. ranges[ 1 ].detach.restore();
  211. } );
  212. it( 'should remove all stored ranges (and reset to default range)', () => {
  213. expect( Array.from( selection.getRanges() ).length ).to.equal( 1 );
  214. expect( selection.anchor.isEqual( new Position( root, [ 0, 0 ] ) ) ).to.be.true;
  215. expect( selection.focus.isEqual( new Position( root, [ 0, 0 ] ) ) ).to.be.true;
  216. } );
  217. it( 'should detach ranges', () => {
  218. expect( ranges[ 0 ].detach.called ).to.be.true;
  219. expect( ranges[ 1 ].detach.called ).to.be.true;
  220. } );
  221. it( 'should refresh attributes', () => {
  222. const spy = sinon.spy( selection._selection, '_updateAttributes' );
  223. selection._setTo( null );
  224. expect( spy.called ).to.be.true;
  225. } );
  226. } );
  227. // TODO - merge with other setTo's
  228. describe( 'setTo()', () => {
  229. it( 'should throw an error when range is invalid', () => {
  230. expect( () => {
  231. selection._setTo( [ { invalid: 'range' } ] );
  232. } ).to.throw( CKEditorError, /model-selection-added-not-range/ );
  233. } );
  234. it( 'should detach removed ranges', () => {
  235. selection._setTo( [ liveRange, range ] );
  236. const oldRanges = Array.from( selection._selection._ranges );
  237. sinon.spy( oldRanges[ 0 ], 'detach' );
  238. sinon.spy( oldRanges[ 1 ], 'detach' );
  239. selection._setTo( [] );
  240. expect( oldRanges[ 0 ].detach.called ).to.be.true;
  241. expect( oldRanges[ 1 ].detach.called ).to.be.true;
  242. } );
  243. it( 'should refresh attributes', () => {
  244. const spy = sinon.spy( selection._selection, '_updateAttributes' );
  245. selection._setTo( [ range ] );
  246. expect( spy.called ).to.be.true;
  247. } );
  248. // See #630.
  249. it( 'should refresh attributes – integration test for #630', () => {
  250. model.schema.extend( '$text', { allowIn: '$root' } );
  251. setData( model, 'f<$text italic="true">[o</$text><$text bold="true">ob]a</$text>r' );
  252. selection._setTo( [ Range.createFromPositionAndShift( selection.getLastRange().end, 0 ) ] );
  253. expect( selection.getAttribute( 'bold' ) ).to.equal( true );
  254. expect( selection.hasAttribute( 'italic' ) ).to.equal( false );
  255. expect( getData( model ) )
  256. .to.equal( 'f<$text italic="true">o</$text><$text bold="true">ob[]a</$text>r' );
  257. } );
  258. } );
  259. describe( 'getFirstRange()', () => {
  260. it( 'should return default range if no ranges were added', () => {
  261. const firstRange = selection.getFirstRange();
  262. expect( firstRange.start.isEqual( new Position( root, [ 0, 0 ] ) ) );
  263. expect( firstRange.end.isEqual( new Position( root, [ 0, 0 ] ) ) );
  264. } );
  265. } );
  266. describe( 'getLastRange()', () => {
  267. it( 'should return default range if no ranges were added', () => {
  268. const lastRange = selection.getLastRange();
  269. expect( lastRange.start.isEqual( new Position( root, [ 0, 0 ] ) ) );
  270. expect( lastRange.end.isEqual( new Position( root, [ 0, 0 ] ) ) );
  271. } );
  272. } );
  273. describe( '_isStoreAttributeKey', () => {
  274. it( 'should return true if given key is a key of an attribute stored in element by DocumentSelection', () => {
  275. expect( DocumentSelection._isStoreAttributeKey( fooStoreAttrKey ) ).to.be.true;
  276. } );
  277. it( 'should return false if given key is not a key of an attribute stored in element by DocumentSelection', () => {
  278. expect( DocumentSelection._isStoreAttributeKey( 'foo' ) ).to.be.false;
  279. } );
  280. } );
  281. // DocumentSelection uses LiveRanges so here are only simple test to see if integration is
  282. // working well, without getting into complicated corner cases.
  283. describe( 'after applying an operation should get updated and fire events', () => {
  284. let spyRange;
  285. beforeEach( () => {
  286. root.removeChildren( 0, root.childCount );
  287. root.insertChildren( 0, [
  288. new Element( 'p', [], new Text( 'abcdef' ) ),
  289. new Element( 'p', [], new Text( 'foobar' ) ),
  290. new Text( 'xyz' )
  291. ] );
  292. selection._setTo( new Range( new Position( root, [ 0, 2 ] ), new Position( root, [ 1, 4 ] ) ) );
  293. spyRange = sinon.spy();
  294. selection.on( 'change:range', spyRange );
  295. } );
  296. describe( 'InsertOperation', () => {
  297. it( 'before selection', () => {
  298. selection.on( 'change:range', ( evt, data ) => {
  299. expect( data.directChange ).to.be.false;
  300. } );
  301. model.applyOperation( wrapInDelta(
  302. new InsertOperation(
  303. new Position( root, [ 0, 1 ] ),
  304. 'xyz',
  305. doc.version
  306. )
  307. ) );
  308. const range = selection.getFirstRange();
  309. expect( range.start.path ).to.deep.equal( [ 0, 5 ] );
  310. expect( range.end.path ).to.deep.equal( [ 1, 4 ] );
  311. expect( spyRange.calledOnce ).to.be.true;
  312. } );
  313. it( 'inside selection', () => {
  314. selection.on( 'change:range', ( evt, data ) => {
  315. expect( data.directChange ).to.be.false;
  316. } );
  317. model.applyOperation( wrapInDelta(
  318. new InsertOperation(
  319. new Position( root, [ 1, 0 ] ),
  320. 'xyz',
  321. doc.version
  322. )
  323. ) );
  324. const range = selection.getFirstRange();
  325. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  326. expect( range.end.path ).to.deep.equal( [ 1, 7 ] );
  327. expect( spyRange.calledOnce ).to.be.true;
  328. } );
  329. } );
  330. describe( 'MoveOperation', () => {
  331. it( 'move range from before a selection', () => {
  332. selection.on( 'change:range', ( evt, data ) => {
  333. expect( data.directChange ).to.be.false;
  334. } );
  335. model.applyOperation( wrapInDelta(
  336. new MoveOperation(
  337. new Position( root, [ 0, 0 ] ),
  338. 2,
  339. new Position( root, [ 2 ] ),
  340. doc.version
  341. )
  342. ) );
  343. const range = selection.getFirstRange();
  344. expect( range.start.path ).to.deep.equal( [ 0, 0 ] );
  345. expect( range.end.path ).to.deep.equal( [ 1, 4 ] );
  346. expect( spyRange.calledOnce ).to.be.true;
  347. } );
  348. it( 'moved into before a selection', () => {
  349. selection.on( 'change:range', ( evt, data ) => {
  350. expect( data.directChange ).to.be.false;
  351. } );
  352. model.applyOperation( wrapInDelta(
  353. new MoveOperation(
  354. new Position( root, [ 2 ] ),
  355. 2,
  356. new Position( root, [ 0, 0 ] ),
  357. doc.version
  358. )
  359. ) );
  360. const range = selection.getFirstRange();
  361. expect( range.start.path ).to.deep.equal( [ 0, 4 ] );
  362. expect( range.end.path ).to.deep.equal( [ 1, 4 ] );
  363. expect( spyRange.calledOnce ).to.be.true;
  364. } );
  365. it( 'move range from inside of selection', () => {
  366. selection.on( 'change:range', ( evt, data ) => {
  367. expect( data.directChange ).to.be.false;
  368. } );
  369. model.applyOperation( wrapInDelta(
  370. new MoveOperation(
  371. new Position( root, [ 1, 0 ] ),
  372. 2,
  373. new Position( root, [ 2 ] ),
  374. doc.version
  375. )
  376. ) );
  377. const range = selection.getFirstRange();
  378. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  379. expect( range.end.path ).to.deep.equal( [ 1, 2 ] );
  380. expect( spyRange.calledOnce ).to.be.true;
  381. } );
  382. it( 'moved range intersects with selection', () => {
  383. selection.on( 'change:range', ( evt, data ) => {
  384. expect( data.directChange ).to.be.false;
  385. } );
  386. model.applyOperation( wrapInDelta(
  387. new MoveOperation(
  388. new Position( root, [ 1, 3 ] ),
  389. 2,
  390. new Position( root, [ 4 ] ),
  391. doc.version
  392. )
  393. ) );
  394. const range = selection.getFirstRange();
  395. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  396. expect( range.end.path ).to.deep.equal( [ 5 ] );
  397. expect( spyRange.calledOnce ).to.be.true;
  398. } );
  399. it( 'split inside selection (do not break selection)', () => {
  400. selection.on( 'change:range', ( evt, data ) => {
  401. expect( data.directChange ).to.be.false;
  402. } );
  403. const batch = new Batch();
  404. const splitDelta = new SplitDelta();
  405. const insertOperation = new InsertOperation(
  406. new Position( root, [ 2 ] ),
  407. new Element( 'p' ),
  408. 0
  409. );
  410. const moveOperation = new MoveOperation(
  411. new Position( root, [ 1, 2 ] ),
  412. 4,
  413. new Position( root, [ 2, 0 ] ),
  414. 1
  415. );
  416. batch.addDelta( splitDelta );
  417. splitDelta.addOperation( insertOperation );
  418. splitDelta.addOperation( moveOperation );
  419. model.applyOperation( insertOperation );
  420. model.applyOperation( moveOperation );
  421. const range = selection.getFirstRange();
  422. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  423. expect( range.end.path ).to.deep.equal( [ 2, 2 ] );
  424. expect( spyRange.calledOnce ).to.be.true;
  425. } );
  426. } );
  427. describe( 'AttributeOperation', () => {
  428. it( 'changed range includes selection anchor', () => {
  429. const spyAttribute = sinon.spy();
  430. selection.on( 'change:attribute', spyAttribute );
  431. selection.on( 'change:attribute', ( evt, data ) => {
  432. expect( data.directChange ).to.be.false;
  433. expect( data.attributeKeys ).to.deep.equal( [ 'foo' ] );
  434. } );
  435. model.applyOperation( wrapInDelta(
  436. new AttributeOperation(
  437. new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 5 ] ) ),
  438. 'foo',
  439. null,
  440. 'bar',
  441. doc.version
  442. )
  443. ) );
  444. expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  445. expect( spyAttribute.calledOnce ).to.be.true;
  446. } );
  447. it( 'should not overwrite previously set attributes', () => {
  448. selection._setAttribute( 'foo', 'xyz' );
  449. const spyAttribute = sinon.spy();
  450. selection.on( 'change:attribute', spyAttribute );
  451. model.applyOperation( wrapInDelta(
  452. new AttributeOperation(
  453. new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 5 ] ) ),
  454. 'foo',
  455. null,
  456. 'bar',
  457. doc.version
  458. )
  459. ) );
  460. expect( selection.getAttribute( 'foo' ) ).to.equal( 'xyz' );
  461. expect( spyAttribute.called ).to.be.false;
  462. } );
  463. it( 'should not overwrite previously removed attributes', () => {
  464. selection._setAttribute( 'foo', 'xyz' );
  465. selection._removeAttribute( 'foo' );
  466. const spyAttribute = sinon.spy();
  467. selection.on( 'change:attribute', spyAttribute );
  468. model.applyOperation( wrapInDelta(
  469. new AttributeOperation(
  470. new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 5 ] ) ),
  471. 'foo',
  472. null,
  473. 'bar',
  474. doc.version
  475. )
  476. ) );
  477. expect( selection.hasAttribute( 'foo' ) ).to.be.false;
  478. expect( spyAttribute.called ).to.be.false;
  479. } );
  480. } );
  481. describe( 'RemoveOperation', () => {
  482. it( 'fix selection range if it ends up in graveyard #1', () => {
  483. selection._setTo( new Position( root, [ 1, 3 ] ) );
  484. model.applyOperation( wrapInDelta(
  485. new RemoveOperation(
  486. new Position( root, [ 1, 2 ] ),
  487. 2,
  488. new Position( doc.graveyard, [ 0 ] ),
  489. doc.version
  490. )
  491. ) );
  492. expect( selection.getFirstPosition().path ).to.deep.equal( [ 1, 2 ] );
  493. } );
  494. it( 'fix selection range if it ends up in graveyard #2', () => {
  495. selection._setTo( [ new Range( new Position( root, [ 1, 2 ] ), new Position( root, [ 1, 4 ] ) ) ] );
  496. model.applyOperation( wrapInDelta(
  497. new RemoveOperation(
  498. new Position( root, [ 1, 2 ] ),
  499. 2,
  500. new Position( doc.graveyard, [ 0 ] ),
  501. doc.version
  502. )
  503. ) );
  504. expect( selection.getFirstPosition().path ).to.deep.equal( [ 1, 2 ] );
  505. } );
  506. it( 'fix selection range if it ends up in graveyard #3', () => {
  507. selection._setTo( [ new Range( new Position( root, [ 1, 1 ] ), new Position( root, [ 1, 2 ] ) ) ] );
  508. model.applyOperation( wrapInDelta(
  509. new RemoveOperation(
  510. new Position( root, [ 1 ] ),
  511. 2,
  512. new Position( doc.graveyard, [ 0 ] ),
  513. doc.version
  514. )
  515. ) );
  516. expect( selection.getFirstPosition().path ).to.deep.equal( [ 0, 6 ] );
  517. } );
  518. it( 'fix selection range if it ends up in graveyard #4 - whole content removed', () => {
  519. model.applyOperation( wrapInDelta(
  520. new RemoveOperation(
  521. new Position( root, [ 0 ] ),
  522. 3,
  523. new Position( doc.graveyard, [ 0 ] ),
  524. doc.version
  525. )
  526. ) );
  527. expect( selection.getFirstPosition().path ).to.deep.equal( [ 0 ] );
  528. model.applyOperation( wrapInDelta(
  529. new InsertOperation(
  530. new Position( root, [ 0 ] ),
  531. new Element( 'p' ),
  532. doc.version
  533. )
  534. ) );
  535. // Now it's clear that it's the default range.
  536. expect( selection.getFirstPosition().path ).to.deep.equal( [ 0, 0 ] );
  537. } );
  538. } );
  539. } );
  540. describe( 'attributes', () => {
  541. let fullP, emptyP, rangeInFullP, rangeInEmptyP;
  542. beforeEach( () => {
  543. root.removeChildren( 0, root.childCount );
  544. root.appendChildren( [
  545. new Element( 'p', [], new Text( 'foobar' ) ),
  546. new Element( 'p', [], [] )
  547. ] );
  548. fullP = root.getChild( 0 );
  549. emptyP = root.getChild( 1 );
  550. rangeInFullP = new Range( new Position( root, [ 0, 4 ] ), new Position( root, [ 0, 4 ] ) );
  551. rangeInEmptyP = new Range( new Position( root, [ 1, 0 ] ), new Position( root, [ 1, 0 ] ) );
  552. // I've lost 30 mins debugging why my tests fail and that was due to the above code reusing
  553. // a root which wasn't empty (so the ranges didn't actualy make much sense).
  554. expect( root.childCount ).to.equal( 2 );
  555. } );
  556. describe( '_setAttribute()', () => {
  557. it( 'should set attribute', () => {
  558. selection._setTo( [ rangeInEmptyP ] );
  559. selection._setAttribute( 'foo', 'bar' );
  560. expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  561. } );
  562. } );
  563. describe( 'removeAttribute()', () => {
  564. it( 'should remove attribute set on the text fragment', () => {
  565. selection._setTo( [ rangeInFullP ] );
  566. selection._setAttribute( 'foo', 'bar' );
  567. selection._removeAttribute( 'foo' );
  568. expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
  569. } );
  570. } );
  571. describe( 'clearAttributes()', () => {
  572. it( 'should remove all stored attributes if the selection is in empty node', () => {
  573. selection._setTo( [ rangeInEmptyP ] );
  574. selection._setAttribute( 'foo', 'bar' );
  575. selection._setAttribute( 'abc', 'xyz' );
  576. selection._clearAttributes();
  577. expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
  578. expect( selection.getAttribute( 'abc' ) ).to.be.undefined;
  579. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  580. expect( emptyP.hasAttribute( abcStoreAttrKey ) ).to.be.false;
  581. } );
  582. } );
  583. describe( '_getStoredAttributes()', () => {
  584. it( 'should return no values if there are no ranges in selection', () => {
  585. const values = Array.from( selection._getStoredAttributes() );
  586. expect( values ).to.deep.equal( [] );
  587. } );
  588. } );
  589. describe( 'are updated on a direct range change', () => {
  590. beforeEach( () => {
  591. root.insertChildren( 0, [
  592. new Element( 'p', { p: true } ),
  593. new Text( 'a', { a: true } ),
  594. new Element( 'p', { p: true } ),
  595. new Text( 'b', { b: true } ),
  596. new Text( 'c', { c: true } ),
  597. new Element( 'p', [], [
  598. new Text( 'd', { d: true } )
  599. ] ),
  600. new Element( 'p', { p: true } ),
  601. new Text( 'e', { e: true } )
  602. ] );
  603. } );
  604. it( 'if selection is a range, should find first character in it and copy it\'s attributes', () => {
  605. selection._setTo( [ new Range( new Position( root, [ 2 ] ), new Position( root, [ 5 ] ) ) ] );
  606. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'b', true ] ] );
  607. // Step into elements when looking for first character:
  608. selection._setTo( [ new Range( new Position( root, [ 5 ] ), new Position( root, [ 7 ] ) ) ] );
  609. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'd', true ] ] );
  610. } );
  611. it( 'if selection is collapsed it should seek a character to copy that character\'s attributes', () => {
  612. // Take styles from character before selection.
  613. selection._setTo( [ new Range( new Position( root, [ 2 ] ), new Position( root, [ 2 ] ) ) ] );
  614. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'a', true ] ] );
  615. // If there are none,
  616. // Take styles from character after selection.
  617. selection._setTo( [ new Range( new Position( root, [ 3 ] ), new Position( root, [ 3 ] ) ) ] );
  618. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'b', true ] ] );
  619. // If there are none,
  620. // Look from the selection position to the beginning of node looking for character to take attributes from.
  621. selection._setTo( [ new Range( new Position( root, [ 6 ] ), new Position( root, [ 6 ] ) ) ] );
  622. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'c', true ] ] );
  623. // If there are none,
  624. // Look from the selection position to the end of node looking for character to take attributes from.
  625. selection._setTo( [ new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) ) ] );
  626. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'a', true ] ] );
  627. // If there are no characters to copy attributes from, use stored attributes.
  628. selection._setTo( [ new Range( new Position( root, [ 0, 0 ] ), new Position( root, [ 0, 0 ] ) ) ] );
  629. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [] );
  630. } );
  631. it( 'should overwrite any previously set attributes', () => {
  632. selection._setTo( new Position( root, [ 5, 0 ] ) );
  633. selection._setAttribute( 'x', true );
  634. selection._setAttribute( 'y', true );
  635. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'd', true ], [ 'x', true ], [ 'y', true ] ] );
  636. selection._setTo( new Position( root, [ 1 ] ) );
  637. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'a', true ] ] );
  638. } );
  639. it( 'should fire change:attribute event', () => {
  640. const spy = sinon.spy();
  641. selection.on( 'change:attribute', spy );
  642. selection._setTo( [ new Range( new Position( root, [ 2 ] ), new Position( root, [ 5 ] ) ) ] );
  643. expect( spy.calledOnce ).to.be.true;
  644. } );
  645. it( 'should not fire change:attribute event if attributes did not change', () => {
  646. selection._setTo( new Position( root, [ 5, 0 ] ) );
  647. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'd', true ] ] );
  648. const spy = sinon.spy();
  649. selection.on( 'change:attribute', spy );
  650. selection._setTo( new Position( root, [ 5, 1 ] ) );
  651. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'd', true ] ] );
  652. expect( spy.called ).to.be.false;
  653. } );
  654. } );
  655. // #986
  656. describe( 'are not inherited from the inside of object elements', () => {
  657. beforeEach( () => {
  658. model.schema.register( 'image', {
  659. isObject: true
  660. } );
  661. model.schema.extend( 'image', { allowIn: '$root' } );
  662. model.schema.extend( 'image', { allowIn: '$block' } );
  663. model.schema.register( 'caption' );
  664. model.schema.extend( 'caption', { allowIn: 'image' } );
  665. model.schema.extend( '$text', {
  666. allowIn: [ 'image', 'caption' ],
  667. allowAttributes: 'bold'
  668. } );
  669. } );
  670. it( 'ignores attributes inside an object if selection contains that object', () => {
  671. setData( model, '<p>[<image><$text bold="true">Caption for the image.</$text></image>]</p>' );
  672. expect( selection.hasAttribute( 'bold' ) ).to.equal( false );
  673. } );
  674. it( 'ignores attributes inside an object if selection contains that object (deeper structure)', () => {
  675. setData( model, '<p>[<image><caption><$text bold="true">Caption for the image.</$text></caption></image>]</p>' );
  676. expect( selection.hasAttribute( 'bold' ) ).to.equal( false );
  677. } );
  678. it( 'ignores attributes inside an object if selection contains that object (block level)', () => {
  679. setData( model, '<p>foo</p>[<image><$text bold="true">Caption for the image.</$text></image>]<p>foo</p>' );
  680. expect( selection.hasAttribute( 'bold' ) ).to.equal( false );
  681. } );
  682. it( 'reads attributes from text even if the selection contains an object', () => {
  683. setData( model, '<p>x<$text bold="true">[bar</$text><image></image>foo]</p>' );
  684. expect( selection.getAttribute( 'bold' ) ).to.equal( true );
  685. } );
  686. it( 'reads attributes when the entire selection inside an object', () => {
  687. setData( model, '<p><image><caption><$text bold="true">[bar]</$text></caption></image></p>' );
  688. expect( selection.getAttribute( 'bold' ) ).to.equal( true );
  689. } );
  690. it( 'stops reading attributes if selection starts with an object', () => {
  691. setData( model, '<p>[<image></image><$text bold="true">bar]</$text></p>' );
  692. expect( selection.hasAttribute( 'bold' ) ).to.equal( false );
  693. } );
  694. } );
  695. describe( 'parent element\'s attributes', () => {
  696. it( 'are set using a normal batch', () => {
  697. const batchTypes = [];
  698. model.on( 'applyOperation', ( event, args ) => {
  699. const operation = args[ 0 ];
  700. const batch = operation.delta.batch;
  701. batchTypes.push( batch.type );
  702. } );
  703. selection._setTo( [ rangeInEmptyP ] );
  704. model.change( writer => {
  705. writer.setSelectionAttribute( 'foo', 'bar' );
  706. } );
  707. expect( batchTypes ).to.deep.equal( [ 'default' ] );
  708. expect( emptyP.getAttribute( fooStoreAttrKey ) ).to.equal( 'bar' );
  709. } );
  710. it( 'are removed when any content is inserted (reuses the same batch)', () => {
  711. // Dedupe batches by using a map (multiple change events will be fired).
  712. const batchTypes = new Map();
  713. selection._setTo( rangeInEmptyP );
  714. selection._setAttribute( 'foo', 'bar' );
  715. selection._setAttribute( 'abc', 'bar' );
  716. model.on( 'applyOperation', ( event, args ) => {
  717. const operation = args[ 0 ];
  718. const batch = operation.delta.batch;
  719. batchTypes.set( batch, batch.type );
  720. } );
  721. model.change( writer => {
  722. writer.insertText( 'x', rangeInEmptyP.start );
  723. } );
  724. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  725. expect( emptyP.hasAttribute( abcStoreAttrKey ) ).to.be.false;
  726. expect( Array.from( batchTypes.values() ) ).to.deep.equal( [ 'default' ] );
  727. } );
  728. it( 'are removed when any content is moved into', () => {
  729. selection._setTo( rangeInEmptyP );
  730. selection._setAttribute( 'foo', 'bar' );
  731. model.change( writer => {
  732. writer.move( Range.createOn( fullP.getChild( 0 ) ), rangeInEmptyP.start );
  733. } );
  734. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  735. } );
  736. it( 'are removed when containing element is merged with a non-empty element', () => {
  737. const emptyP2 = new Element( 'p', null, 'x' );
  738. root.appendChildren( emptyP2 );
  739. emptyP.setAttribute( fooStoreAttrKey, 'bar' );
  740. emptyP2.setAttribute( fooStoreAttrKey, 'bar' );
  741. model.change( writer => {
  742. // <emptyP>{}<emptyP2>
  743. writer.merge( Position.createAfter( emptyP ) );
  744. } );
  745. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  746. expect( emptyP.parent ).to.equal( root ); // Just to be sure we're checking the right element.
  747. } );
  748. it( 'are removed even when there is no selection in it', () => {
  749. emptyP.setAttribute( fooStoreAttrKey, 'bar' );
  750. selection._setTo( [ rangeInFullP ] );
  751. model.change( writer => {
  752. writer.insertText( 'x', rangeInEmptyP.start );
  753. } );
  754. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  755. } );
  756. it( 'are removed only once in case of multi-op deltas', () => {
  757. let batch;
  758. const emptyP2 = new Element( 'p', null, 'x' );
  759. root.appendChildren( emptyP2 );
  760. emptyP.setAttribute( fooStoreAttrKey, 'bar' );
  761. emptyP2.setAttribute( fooStoreAttrKey, 'bar' );
  762. model.change( writer => {
  763. batch = writer.batch;
  764. // <emptyP>{}<emptyP2>
  765. writer.merge( Position.createAfter( emptyP ) );
  766. } );
  767. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  768. // Attribute delta is only one.
  769. expect( Array.from( batch.deltas, delta => delta.type ) ).to.deep.equal( [ 'merge', 'attribute' ] );
  770. } );
  771. it( 'uses model change to clear attributes', () => {
  772. selection._setTo( [ rangeInEmptyP ] );
  773. model.change( writer => {
  774. writer.setSelectionAttribute( 'foo', 'bar' );
  775. writer.insertText( 'x', rangeInEmptyP.start );
  776. // `emptyP` still has the attribute, because attribute clearing is in enqueued block.
  777. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.true;
  778. } );
  779. // When the dust settles, `emptyP` should not have the attribute.
  780. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  781. } );
  782. it( 'are not removed or merged when containing element is merged with another empty element', () => {
  783. const emptyP2 = new Element( 'p', null );
  784. root.appendChildren( emptyP2 );
  785. emptyP.setAttribute( fooStoreAttrKey, 'bar' );
  786. emptyP2.setAttribute( abcStoreAttrKey, 'bar' );
  787. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.true;
  788. expect( emptyP.hasAttribute( abcStoreAttrKey ) ).to.be.false;
  789. model.change( writer => {
  790. // <emptyP>{}<emptyP2>
  791. writer.merge( Position.createAfter( emptyP ) );
  792. } );
  793. expect( emptyP.getAttribute( fooStoreAttrKey ) ).to.equal( 'bar' );
  794. expect( emptyP.parent ).to.equal( root ); // Just to be sure we're checking the right element.
  795. } );
  796. // Rename and some other deltas don't specify range in doc#change event.
  797. // So let's see if there's no crash or something.
  798. it( 'are not removed on rename', () => {
  799. model.change( writer => {
  800. writer.setSelection( rangeInEmptyP );
  801. writer.setSelectionAttribute( 'foo', 'bar' );
  802. } );
  803. sinon.spy( model, 'enqueueChange' );
  804. model.change( writer => {
  805. writer.rename( emptyP, 'pnew' );
  806. } );
  807. expect( model.enqueueChange.called ).to.be.false;
  808. expect( emptyP.getAttribute( fooStoreAttrKey ) ).to.equal( 'bar' );
  809. } );
  810. } );
  811. } );
  812. it( 'should throw if one of ranges starts or ends inside surrogate pair', () => {
  813. root.removeChildren( 0, root.childCount );
  814. root.appendChildren( '\uD83D\uDCA9' );
  815. expect( () => {
  816. doc.selection._setTo( Range.createFromParentsAndOffsets( root, 0, root, 1 ) );
  817. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  818. expect( () => {
  819. doc.selection._setTo( Range.createFromParentsAndOffsets( root, 1, root, 2 ) );
  820. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  821. } );
  822. it( 'should throw if one of ranges starts or ends between base character and combining mark', () => {
  823. root.removeChildren( 0, root.childCount );
  824. root.appendChildren( 'foo̻̐ͩbar' );
  825. expect( () => {
  826. doc.selection._setTo( Range.createFromParentsAndOffsets( root, 3, root, 9 ) );
  827. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  828. expect( () => {
  829. doc.selection._setTo( Range.createFromParentsAndOffsets( root, 4, root, 9 ) );
  830. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  831. expect( () => {
  832. doc.selection._setTo( Range.createFromParentsAndOffsets( root, 5, root, 9 ) );
  833. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  834. expect( () => {
  835. doc.selection._setTo( Range.createFromParentsAndOffsets( root, 1, root, 3 ) );
  836. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  837. expect( () => {
  838. doc.selection._setTo( Range.createFromParentsAndOffsets( root, 1, root, 4 ) );
  839. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  840. expect( () => {
  841. doc.selection._setTo( Range.createFromParentsAndOffsets( root, 1, root, 5 ) );
  842. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  843. } );
  844. } );