documentselection.js 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499
  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 AttributeOperation from '../../src/model/operation/attributeoperation';
  16. import SplitOperation from '../../src/model/operation/splitoperation';
  17. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  18. import Collection from '@ckeditor/ckeditor5-utils/src/collection';
  19. import count from '@ckeditor/ckeditor5-utils/src/count';
  20. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  21. import { setData, getData } from '../../src/dev-utils/model';
  22. describe( 'DocumentSelection', () => {
  23. let model, doc, root, selection, liveRange, range;
  24. testUtils.createSinonSandbox();
  25. const fooStoreAttrKey = DocumentSelection._getStoreAttributeKey( 'foo' );
  26. const abcStoreAttrKey = DocumentSelection._getStoreAttributeKey( 'abc' );
  27. beforeEach( () => {
  28. model = new Model();
  29. doc = model.document;
  30. root = doc.createRoot();
  31. root._appendChild( [
  32. new Element( 'p' ),
  33. new Element( 'p' ),
  34. new Element( 'p', [], new Text( 'foobar' ) ),
  35. new Element( 'p' ),
  36. new Element( 'p' ),
  37. new Element( 'p' ),
  38. new Element( 'p', [], new Text( 'foobar' ) )
  39. ] );
  40. selection = doc.selection;
  41. model.schema.register( 'p', { inheritAllFrom: '$block' } );
  42. model.schema.register( 'widget', {
  43. isObject: true
  44. } );
  45. liveRange = new LiveRange( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) );
  46. range = new Range( new Position( root, [ 2 ] ), new Position( root, [ 2, 2 ] ) );
  47. } );
  48. afterEach( () => {
  49. model.destroy();
  50. liveRange.detach();
  51. } );
  52. describe( 'default range', () => {
  53. it( 'should go to the first editable element', () => {
  54. const ranges = Array.from( selection.getRanges() );
  55. expect( ranges.length ).to.equal( 1 );
  56. expect( selection.anchor.isEqual( new Position( root, [ 0, 0 ] ) ) ).to.be.true;
  57. expect( selection.focus.isEqual( new Position( root, [ 0, 0 ] ) ) ).to.be.true;
  58. expect( selection ).to.have.property( 'isBackward', false );
  59. } );
  60. it( 'should be set to the beginning of the doc if there is no editable element', () => {
  61. model = new Model();
  62. doc = model.document;
  63. root = doc.createRoot();
  64. root._insertChild( 0, new Text( 'foobar' ) );
  65. selection = doc.selection;
  66. const ranges = Array.from( selection.getRanges() );
  67. expect( ranges.length ).to.equal( 1 );
  68. expect( selection.anchor.isEqual( new Position( root, [ 0 ] ) ) ).to.be.true;
  69. expect( selection.focus.isEqual( new Position( root, [ 0 ] ) ) ).to.be.true;
  70. expect( selection ).to.have.property( 'isBackward', false );
  71. expect( count( selection.getAttributes() ) ).to.equal( 0 );
  72. } );
  73. it( 'should skip element when you can not put selection', () => {
  74. model = new Model();
  75. doc = model.document;
  76. root = doc.createRoot();
  77. root._insertChild( 0, [
  78. new Element( 'img' ),
  79. new Element( 'p', [], new Text( 'foobar' ) )
  80. ] );
  81. model.schema.register( 'img' );
  82. model.schema.register( 'p', { inheritAllFrom: '$block' } );
  83. selection = doc.selection;
  84. const ranges = Array.from( selection.getRanges() );
  85. expect( ranges.length ).to.equal( 1 );
  86. expect( selection.anchor.isEqual( new Position( root, [ 1, 0 ] ) ) ).to.be.true;
  87. expect( selection.focus.isEqual( new Position( root, [ 1, 0 ] ) ) ).to.be.true;
  88. expect( selection ).to.have.property( 'isBackward', false );
  89. expect( count( selection.getAttributes() ) ).to.equal( 0 );
  90. } );
  91. } );
  92. describe( 'isCollapsed', () => {
  93. it( 'should be true for the default range (in text position)', () => {
  94. expect( selection.isCollapsed ).to.be.true;
  95. } );
  96. it( 'should be false for the default range (object selection) ', () => {
  97. root._insertChild( 0, new Element( 'widget' ) );
  98. expect( selection.isCollapsed ).to.be.false;
  99. } );
  100. it( 'should back off to the default algorithm if selection has ranges', () => {
  101. selection._setTo( range );
  102. expect( selection.isCollapsed ).to.be.false;
  103. } );
  104. } );
  105. describe( 'anchor', () => {
  106. it( 'should equal the default range\'s start (in text position)', () => {
  107. const expectedPos = new Position( root, [ 0, 0 ] );
  108. expect( selection.anchor.isEqual( expectedPos ) ).to.be.true;
  109. } );
  110. it( 'should equal the default range\'s start (object selection)', () => {
  111. root._insertChild( 0, new Element( 'widget' ) );
  112. const expectedPos = new Position( root, [ 0 ] );
  113. expect( selection.anchor.isEqual( expectedPos ) ).to.be.true;
  114. } );
  115. it( 'should back off to the default algorithm if selection has ranges', () => {
  116. selection._setTo( range );
  117. expect( selection.anchor.isEqual( range.start ) ).to.be.true;
  118. } );
  119. } );
  120. describe( 'focus', () => {
  121. it( 'should equal the default range\'s end (in text position)', () => {
  122. const expectedPos = new Position( root, [ 0, 0 ] );
  123. expect( selection.focus.isEqual( expectedPos ) ).to.be.true;
  124. } );
  125. it( 'should equal the default range\'s end (object selection)', () => {
  126. root._insertChild( 0, new Element( 'widget' ) );
  127. const expectedPos = new Position( root, [ 1 ] );
  128. expect( selection.focus.isEqual( expectedPos ) ).to.be.true;
  129. expect( selection.focus.isEqual( selection.getFirstRange().end ) ).to.be.true;
  130. } );
  131. it( 'should back off to the default algorithm if selection has ranges', () => {
  132. selection._setTo( range );
  133. expect( selection.focus.isEqual( range.end ) ).to.be.true;
  134. } );
  135. } );
  136. describe( 'rangeCount', () => {
  137. it( 'should return proper range count', () => {
  138. expect( selection.rangeCount ).to.equal( 1 );
  139. selection._setTo( new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) ) );
  140. expect( selection.rangeCount ).to.equal( 1 );
  141. selection._setTo( [
  142. new Range( new Position( root, [ 2 ] ), new Position( root, [ 2 ] ) ),
  143. new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) )
  144. ] );
  145. expect( selection.rangeCount ).to.equal( 2 );
  146. } );
  147. } );
  148. describe( 'hasOwnRange', () => {
  149. it( 'should return true if selection has any ranges set', () => {
  150. selection._setTo( new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) ) );
  151. expect( selection.hasOwnRange ).to.be.true;
  152. } );
  153. it( 'should return false if selection has a default range', () => {
  154. expect( selection.hasOwnRange ).to.be.false;
  155. } );
  156. } );
  157. describe( 'markers', () => {
  158. it( 'should implement #markers collection', () => {
  159. expect( selection.markers ).to.instanceof( Collection );
  160. expect( selection.markers ).to.length( 0 );
  161. } );
  162. it( 'should add markers to the collection when selection is inside the marker range', () => {
  163. model.change( writer => {
  164. writer.setSelection( writer.createRange(
  165. writer.createPositionFromPath( root, [ 2, 2 ] ),
  166. writer.createPositionFromPath( root, [ 2, 4 ] )
  167. ) );
  168. writer.addMarker( 'marker-1', {
  169. range: writer.createRange(
  170. writer.createPositionFromPath( root, [ 0, 0 ] ),
  171. writer.createPositionFromPath( root, [ 2, 2 ] )
  172. ),
  173. usingOperation: false
  174. } );
  175. writer.addMarker( 'marker-2', {
  176. range: writer.createRange(
  177. writer.createPositionFromPath( root, [ 2, 2 ] ),
  178. writer.createPositionFromPath( root, [ 2, 4 ] )
  179. ),
  180. usingOperation: false
  181. } );
  182. writer.addMarker( 'marker-3', {
  183. range: writer.createRange(
  184. writer.createPositionFromPath( root, [ 2, 1 ] ),
  185. writer.createPositionFromPath( root, [ 2, 5 ] )
  186. ),
  187. usingOperation: false
  188. } );
  189. writer.addMarker( 'marker-4', {
  190. range: writer.createRange(
  191. writer.createPositionFromPath( root, [ 2, 4 ] ),
  192. writer.createPositionFromPath( root, [ 3, 0 ] )
  193. ),
  194. usingOperation: false
  195. } );
  196. } );
  197. expect( selection.markers.map( marker => marker.name ) ).to.have.members( [ 'marker-2', 'marker-3' ] );
  198. } );
  199. it( 'should update markers after selection change', () => {
  200. model.change( writer => {
  201. writer.setSelection( writer.createRange(
  202. writer.createPositionFromPath( root, [ 2, 1 ] ),
  203. writer.createPositionFromPath( root, [ 2, 2 ] )
  204. ) );
  205. writer.addMarker( 'marker-1', {
  206. range: writer.createRange(
  207. writer.createPositionFromPath( root, [ 2, 0 ] ),
  208. writer.createPositionFromPath( root, [ 2, 6 ] )
  209. ),
  210. usingOperation: false
  211. } );
  212. writer.addMarker( 'marker-2', {
  213. range: writer.createRange(
  214. writer.createPositionFromPath( root, [ 2, 0 ] ),
  215. writer.createPositionFromPath( root, [ 2, 3 ] )
  216. ),
  217. usingOperation: false
  218. } );
  219. writer.addMarker( 'marker-3', {
  220. range: writer.createRange(
  221. writer.createPositionFromPath( root, [ 2, 3 ] ),
  222. writer.createPositionFromPath( root, [ 2, 6 ] )
  223. ),
  224. usingOperation: false
  225. } );
  226. } );
  227. expect( selection.markers.map( marker => marker.name ) ).to.have.members( [ 'marker-1', 'marker-2' ] );
  228. model.change( writer => {
  229. writer.setSelection( writer.createRange(
  230. writer.createPositionFromPath( root, [ 2, 4 ] ),
  231. writer.createPositionFromPath( root, [ 2, 5 ] )
  232. ) );
  233. } );
  234. expect( selection.markers.map( marker => marker.name ) ).to.have.members( [ 'marker-1', 'marker-3' ] );
  235. } );
  236. it( 'should update markers after markers change', () => {
  237. model.change( writer => {
  238. writer.setSelection( writer.createRange(
  239. writer.createPositionFromPath( root, [ 2, 1 ] ),
  240. writer.createPositionFromPath( root, [ 2, 2 ] )
  241. ) );
  242. writer.addMarker( 'marker-1', {
  243. range: writer.createRange(
  244. writer.createPositionFromPath( root, [ 2, 0 ] ),
  245. writer.createPositionFromPath( root, [ 2, 6 ] )
  246. ),
  247. usingOperation: false
  248. } );
  249. writer.addMarker( 'marker-2', {
  250. range: writer.createRange(
  251. writer.createPositionFromPath( root, [ 2, 0 ] ),
  252. writer.createPositionFromPath( root, [ 2, 3 ] )
  253. ),
  254. usingOperation: false
  255. } );
  256. writer.addMarker( 'marker-3', {
  257. range: writer.createRange(
  258. writer.createPositionFromPath( root, [ 2, 3 ] ),
  259. writer.createPositionFromPath( root, [ 2, 6 ] )
  260. ),
  261. usingOperation: false
  262. } );
  263. } );
  264. expect( selection.markers.map( marker => marker.name ), 1 ).to.have.members( [ 'marker-1', 'marker-2' ] );
  265. model.change( writer => {
  266. writer.removeMarker( 'marker-1' );
  267. writer.updateMarker( 'marker-2', {
  268. range: writer.createRange(
  269. writer.createPositionFromPath( root, [ 2, 3 ] ),
  270. writer.createPositionFromPath( root, [ 2, 6 ] )
  271. ),
  272. usingOperation: false
  273. } );
  274. writer.updateMarker( 'marker-3', {
  275. range: writer.createRange(
  276. writer.createPositionFromPath( root, [ 2, 0 ] ),
  277. writer.createPositionFromPath( root, [ 2, 3 ] )
  278. ),
  279. usingOperation: false
  280. } );
  281. } );
  282. expect( selection.markers.map( marker => marker.name ), 2 ).to.have.members( [ 'marker-3' ] );
  283. } );
  284. it( 'should not add marker when collapsed selection is on the marker left bound', () => {
  285. model.change( writer => {
  286. writer.setSelection( writer.createRange(
  287. writer.createPositionFromPath( root, [ 2, 2 ] ),
  288. writer.createPositionFromPath( root, [ 2, 4 ] )
  289. ) );
  290. writer.addMarker( 'marker', {
  291. range: writer.createRange(
  292. writer.createPositionFromPath( root, [ 2, 2 ] )
  293. ),
  294. usingOperation: false
  295. } );
  296. } );
  297. expect( selection.markers ).to.length( 0 );
  298. } );
  299. it( 'should not add marker when collapsed selection is on the marker right bound', () => {
  300. model.change( writer => {
  301. writer.setSelection( writer.createRange(
  302. writer.createPositionFromPath( root, [ 2, 4 ] )
  303. ) );
  304. writer.addMarker( 'marker', {
  305. range: writer.createRange(
  306. writer.createPositionFromPath( root, [ 2, 2 ] ),
  307. writer.createPositionFromPath( root, [ 2, 4 ] )
  308. ),
  309. usingOperation: false
  310. } );
  311. } );
  312. expect( selection.markers ).to.length( 0 );
  313. } );
  314. it( 'should add marker when non-collapsed selection is inside a marker and touches the left bound', () => {
  315. model.change( writer => {
  316. writer.setSelection( writer.createRange(
  317. writer.createPositionFromPath( root, [ 2, 1 ] ),
  318. writer.createPositionFromPath( root, [ 2, 3 ] )
  319. ) );
  320. writer.addMarker( 'marker', {
  321. range: writer.createRange(
  322. writer.createPositionFromPath( root, [ 2, 1 ] ),
  323. writer.createPositionFromPath( root, [ 2, 5 ] )
  324. ),
  325. usingOperation: false
  326. } );
  327. } );
  328. expect( selection.markers.map( marker => marker.name ) ).to.have.members( [ 'marker' ] );
  329. } );
  330. it( 'should add marker when non-collapsed selection is inside a marker and touches the right bound', () => {
  331. model.change( writer => {
  332. writer.setSelection( writer.createRange(
  333. writer.createPositionFromPath( root, [ 2, 2 ] ),
  334. writer.createPositionFromPath( root, [ 2, 5 ] )
  335. ) );
  336. writer.addMarker( 'marker', {
  337. range: writer.createRange(
  338. writer.createPositionFromPath( root, [ 2, 1 ] ),
  339. writer.createPositionFromPath( root, [ 2, 5 ] )
  340. ),
  341. usingOperation: false
  342. } );
  343. } );
  344. expect( selection.markers.map( marker => marker.name ) ).to.have.members( [ 'marker' ] );
  345. } );
  346. } );
  347. describe( 'destroy()', () => {
  348. it( 'should unbind all events', () => {
  349. selection._setTo( [ range, liveRange ] );
  350. const ranges = Array.from( selection._selection._ranges );
  351. sinon.spy( ranges[ 0 ], 'detach' );
  352. sinon.spy( ranges[ 1 ], 'detach' );
  353. selection.destroy();
  354. expect( ranges[ 0 ].detach.called ).to.be.true;
  355. expect( ranges[ 1 ].detach.called ).to.be.true;
  356. ranges[ 0 ].detach.restore();
  357. ranges[ 1 ].detach.restore();
  358. } );
  359. } );
  360. describe( 'getFirstRange()', () => {
  361. it( 'should return default range if no ranges were added', () => {
  362. const firstRange = selection.getFirstRange();
  363. expect( firstRange.start.isEqual( new Position( root, [ 0, 0 ] ) ) );
  364. expect( firstRange.end.isEqual( new Position( root, [ 0, 0 ] ) ) );
  365. } );
  366. } );
  367. describe( 'getLastRange()', () => {
  368. it( 'should return default range if no ranges were added', () => {
  369. const lastRange = selection.getLastRange();
  370. expect( lastRange.start.isEqual( new Position( root, [ 0, 0 ] ) ) );
  371. expect( lastRange.end.isEqual( new Position( root, [ 0, 0 ] ) ) );
  372. } );
  373. } );
  374. describe( 'getSelectedElement()', () => {
  375. it( 'should return selected element', () => {
  376. selection._setTo( liveRange );
  377. const p = root.getChild( 0 );
  378. expect( selection.getSelectedElement() ).to.equal( p );
  379. } );
  380. } );
  381. describe( '_setTo() - set collapsed at', () => {
  382. it( 'detaches all existing ranges', () => {
  383. selection._setTo( [ range, liveRange ] );
  384. const spy = testUtils.sinon.spy( LiveRange.prototype, 'detach' );
  385. selection._setTo( root, 0 );
  386. expect( spy.calledTwice ).to.be.true;
  387. } );
  388. } );
  389. describe( '_setFocus()', () => {
  390. it( 'modifies default range', () => {
  391. const startPos = selection.getFirstPosition();
  392. const endPos = Position._createAt( root, 'end' );
  393. selection._setFocus( endPos );
  394. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'same' );
  395. expect( selection.focus.compareWith( endPos ) ).to.equal( 'same' );
  396. } );
  397. it( 'detaches the range it replaces', () => {
  398. const startPos = Position._createAt( root, 1 );
  399. const endPos = Position._createAt( root, 2 );
  400. const newEndPos = Position._createAt( root, 4 );
  401. const spy = testUtils.sinon.spy( LiveRange.prototype, 'detach' );
  402. selection._setTo( new Range( startPos, endPos ) );
  403. selection._setFocus( newEndPos );
  404. expect( spy.calledOnce ).to.be.true;
  405. } );
  406. it( 'refreshes attributes', () => {
  407. const spy = sinon.spy( selection._selection, '_updateAttributes' );
  408. selection._setFocus( Position._createAt( root, 1 ) );
  409. expect( spy.called ).to.be.true;
  410. } );
  411. } );
  412. describe( '_setTo() - remove all ranges', () => {
  413. let spy, ranges;
  414. beforeEach( () => {
  415. selection._setTo( [ liveRange, range ] );
  416. spy = sinon.spy();
  417. selection.on( 'change:range', spy );
  418. ranges = Array.from( selection._selection._ranges );
  419. sinon.spy( ranges[ 0 ], 'detach' );
  420. sinon.spy( ranges[ 1 ], 'detach' );
  421. selection._setTo( null );
  422. } );
  423. afterEach( () => {
  424. ranges[ 0 ].detach.restore();
  425. ranges[ 1 ].detach.restore();
  426. } );
  427. it( 'should remove all stored ranges (and reset to default range)', () => {
  428. expect( Array.from( selection.getRanges() ).length ).to.equal( 1 );
  429. expect( selection.anchor.isEqual( new Position( root, [ 0, 0 ] ) ) ).to.be.true;
  430. expect( selection.focus.isEqual( new Position( root, [ 0, 0 ] ) ) ).to.be.true;
  431. } );
  432. it( 'should detach ranges', () => {
  433. expect( ranges[ 0 ].detach.called ).to.be.true;
  434. expect( ranges[ 1 ].detach.called ).to.be.true;
  435. } );
  436. it( 'should refresh attributes', () => {
  437. const spy = sinon.spy( selection._selection, '_updateAttributes' );
  438. selection._setTo( null );
  439. expect( spy.called ).to.be.true;
  440. } );
  441. } );
  442. describe( '_setTo()', () => {
  443. it( 'should throw an error when range is invalid', () => {
  444. expect( () => {
  445. selection._setTo( [ { invalid: 'range' } ] );
  446. } ).to.throw( CKEditorError, /model-selection-set-ranges-not-range/ );
  447. } );
  448. it( 'should log a warning when trying to set selection to the graveyard', () => {
  449. // eslint-disable-next-line no-undef
  450. const warnStub = sinon.stub( console, 'warn' );
  451. const range = new Range( new Position( model.document.graveyard, [ 0 ] ) );
  452. selection._setTo( range );
  453. expect( warnStub.calledOnce ).to.equal( true );
  454. expect( warnStub.getCall( 0 ).args[ 0 ] ).to.match( /model-selection-range-in-graveyard/ );
  455. expect( selection._ranges ).to.deep.equal( [] );
  456. warnStub.restore();
  457. } );
  458. it( 'should detach removed ranges', () => {
  459. selection._setTo( [ liveRange, range ] );
  460. const oldRanges = Array.from( selection._selection._ranges );
  461. sinon.spy( oldRanges[ 0 ], 'detach' );
  462. sinon.spy( oldRanges[ 1 ], 'detach' );
  463. selection._setTo( [] );
  464. expect( oldRanges[ 0 ].detach.called ).to.be.true;
  465. expect( oldRanges[ 1 ].detach.called ).to.be.true;
  466. } );
  467. it( 'should refresh attributes', () => {
  468. const spy = sinon.spy( selection._selection, '_updateAttributes' );
  469. selection._setTo( [ range ] );
  470. expect( spy.called ).to.be.true;
  471. } );
  472. // See #630.
  473. it( 'should refresh attributes – integration test for #630', () => {
  474. model.schema.extend( '$text', { allowIn: '$root' } );
  475. setData( model, 'f<$text italic="true">[o</$text><$text bold="true">ob]a</$text>r' );
  476. selection._setTo( [ Range._createFromPositionAndShift( selection.getLastRange().end, 0 ) ] );
  477. expect( selection.getAttribute( 'bold' ) ).to.equal( true );
  478. expect( selection.hasAttribute( 'italic' ) ).to.equal( false );
  479. expect( getData( model ) )
  480. .to.equal( 'f<$text italic="true">o</$text><$text bold="true">ob[]a</$text>r' );
  481. } );
  482. } );
  483. describe( '_isStoreAttributeKey', () => {
  484. it( 'should return true if given key is a key of an attribute stored in element by DocumentSelection', () => {
  485. expect( DocumentSelection._isStoreAttributeKey( fooStoreAttrKey ) ).to.be.true;
  486. } );
  487. it( 'should return false if given key is not a key of an attribute stored in element by DocumentSelection', () => {
  488. expect( DocumentSelection._isStoreAttributeKey( 'foo' ) ).to.be.false;
  489. } );
  490. } );
  491. describe( 'attributes', () => {
  492. let fullP, emptyP, rangeInFullP, rangeInEmptyP;
  493. beforeEach( () => {
  494. root._removeChildren( 0, root.childCount );
  495. root._appendChild( [
  496. new Element( 'p', [], new Text( 'foobar' ) ),
  497. new Element( 'p', [], [] )
  498. ] );
  499. fullP = root.getChild( 0 );
  500. emptyP = root.getChild( 1 );
  501. rangeInFullP = new Range( new Position( root, [ 0, 4 ] ), new Position( root, [ 0, 4 ] ) );
  502. rangeInEmptyP = new Range( new Position( root, [ 1, 0 ] ), new Position( root, [ 1, 0 ] ) );
  503. // I've lost 30 mins debugging why my tests fail and that was due to the above code reusing
  504. // a root which wasn't empty (so the ranges didn't actually make much sense).
  505. expect( root.childCount ).to.equal( 2 );
  506. } );
  507. describe( '_setAttribute()', () => {
  508. it( 'should set attribute', () => {
  509. selection._setTo( [ rangeInEmptyP ] );
  510. selection._setAttribute( 'foo', 'bar' );
  511. expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  512. } );
  513. } );
  514. describe( '_removeAttribute()', () => {
  515. it( 'should remove attribute set on the text fragment', () => {
  516. selection._setTo( [ rangeInFullP ] );
  517. selection._setAttribute( 'foo', 'bar' );
  518. selection._removeAttribute( 'foo' );
  519. expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
  520. } );
  521. it( 'should prevent auto update of the attribute even if attribute is not preset yet', () => {
  522. selection._setTo( new Position( root, [ 0, 1 ] ) );
  523. // Remove "foo" attribute that is not present in selection yet.
  524. expect( selection.hasAttribute( 'foo' ) ).to.be.false;
  525. selection._removeAttribute( 'foo' );
  526. // Trigger selecton auto update on document change. It should not get attribute from surrounding text;
  527. model.change( writer => {
  528. writer.setAttribute( 'foo', 'bar', Range._createIn( fullP ) );
  529. } );
  530. expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
  531. } );
  532. } );
  533. describe( '_getStoredAttributes()', () => {
  534. it( 'should return no values if there are no ranges in selection', () => {
  535. const values = Array.from( selection._getStoredAttributes() );
  536. expect( values ).to.deep.equal( [] );
  537. } );
  538. } );
  539. describe( 'are updated on a direct range change', () => {
  540. beforeEach( () => {
  541. root._insertChild( 0, [
  542. new Element( 'p', { p: true } ),
  543. new Text( 'a', { a: true } ),
  544. new Element( 'p', { p: true } ),
  545. new Text( 'b', { b: true } ),
  546. new Text( 'c', { c: true } ),
  547. new Element( 'p', [], [
  548. new Text( 'd', { d: true } )
  549. ] ),
  550. new Element( 'p', { p: true } ),
  551. new Text( 'e', { e: true } )
  552. ] );
  553. } );
  554. it( 'if selection is a range, should find first character in it and copy it\'s attributes', () => {
  555. selection._setTo( [ new Range( new Position( root, [ 2 ] ), new Position( root, [ 5 ] ) ) ] );
  556. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'b', true ] ] );
  557. // Step into elements when looking for first character:
  558. selection._setTo( [ new Range( new Position( root, [ 5 ] ), new Position( root, [ 7 ] ) ) ] );
  559. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'd', true ] ] );
  560. } );
  561. it( 'if selection is collapsed it should seek a character to copy that character\'s attributes', () => {
  562. // Take styles from character before selection.
  563. selection._setTo( [ new Range( new Position( root, [ 2 ] ), new Position( root, [ 2 ] ) ) ] );
  564. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'a', true ] ] );
  565. // If there are none,
  566. // Take styles from character after selection.
  567. selection._setTo( [ new Range( new Position( root, [ 3 ] ), new Position( root, [ 3 ] ) ) ] );
  568. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'b', true ] ] );
  569. // If there are none,
  570. // Look from the selection position to the beginning of node looking for character to take attributes from.
  571. selection._setTo( [ new Range( new Position( root, [ 6 ] ), new Position( root, [ 6 ] ) ) ] );
  572. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'c', true ] ] );
  573. // If there are none,
  574. // Look from the selection position to the end of node looking for character to take attributes from.
  575. selection._setTo( [ new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) ) ] );
  576. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'a', true ] ] );
  577. // If there are no characters to copy attributes from, use stored attributes.
  578. selection._setTo( [ new Range( new Position( root, [ 0, 0 ] ), new Position( root, [ 0, 0 ] ) ) ] );
  579. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [] );
  580. } );
  581. it( 'should overwrite any previously set attributes', () => {
  582. selection._setTo( new Position( root, [ 5, 0 ] ) );
  583. selection._setAttribute( 'x', true );
  584. selection._setAttribute( 'y', true );
  585. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'd', true ], [ 'x', true ], [ 'y', true ] ] );
  586. selection._setTo( new Position( root, [ 1 ] ) );
  587. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'a', true ] ] );
  588. } );
  589. it( 'should fire change:attribute event', () => {
  590. const spy = sinon.spy();
  591. selection.on( 'change:attribute', spy );
  592. selection._setTo( [ new Range( new Position( root, [ 2 ] ), new Position( root, [ 5 ] ) ) ] );
  593. expect( spy.calledOnce ).to.be.true;
  594. } );
  595. it( 'should not fire change:attribute event if attributes did not change', () => {
  596. selection._setTo( new Position( root, [ 5, 0 ] ) );
  597. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'd', true ] ] );
  598. const spy = sinon.spy();
  599. selection.on( 'change:attribute', spy );
  600. selection._setTo( new Position( root, [ 5, 1 ] ) );
  601. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'd', true ] ] );
  602. expect( spy.called ).to.be.false;
  603. } );
  604. } );
  605. // #986
  606. describe( 'are not inherited from the inside of object elements', () => {
  607. beforeEach( () => {
  608. model.schema.register( 'image', {
  609. isObject: true
  610. } );
  611. model.schema.extend( 'image', { allowIn: '$root' } );
  612. model.schema.extend( 'image', { allowIn: '$block' } );
  613. model.schema.register( 'caption' );
  614. model.schema.extend( 'caption', { allowIn: 'image' } );
  615. model.schema.extend( '$text', {
  616. allowIn: [ 'image', 'caption' ],
  617. allowAttributes: 'bold'
  618. } );
  619. } );
  620. it( 'ignores attributes inside an object if selection contains that object', () => {
  621. setData( model, '<p>[<image><$text bold="true">Caption for the image.</$text></image>]</p>' );
  622. expect( selection.hasAttribute( 'bold' ) ).to.equal( false );
  623. } );
  624. it( 'ignores attributes inside an object if selection contains that object (deeper structure)', () => {
  625. setData( model, '<p>[<image><caption><$text bold="true">Caption for the image.</$text></caption></image>]</p>' );
  626. expect( selection.hasAttribute( 'bold' ) ).to.equal( false );
  627. } );
  628. it( 'ignores attributes inside an object if selection contains that object (block level)', () => {
  629. setData( model, '<p>foo</p>[<image><$text bold="true">Caption for the image.</$text></image>]<p>foo</p>' );
  630. expect( selection.hasAttribute( 'bold' ) ).to.equal( false );
  631. } );
  632. it( 'reads attributes from text even if the selection contains an object', () => {
  633. setData( model, '<p>x<$text bold="true">[bar</$text><image></image>foo]</p>' );
  634. expect( selection.getAttribute( 'bold' ) ).to.equal( true );
  635. } );
  636. it( 'reads attributes when the entire selection inside an object', () => {
  637. setData( model, '<p><image><caption><$text bold="true">[bar]</$text></caption></image></p>' );
  638. expect( selection.getAttribute( 'bold' ) ).to.equal( true );
  639. } );
  640. it( 'stops reading attributes if selection starts with an object', () => {
  641. setData( model, '<p>[<image></image><$text bold="true">bar]</$text></p>' );
  642. expect( selection.hasAttribute( 'bold' ) ).to.equal( false );
  643. } );
  644. } );
  645. describe( 'parent element\'s attributes', () => {
  646. it( 'are set using a normal batch', () => {
  647. const batchTypes = [];
  648. model.on( 'applyOperation', ( event, args ) => {
  649. const operation = args[ 0 ];
  650. const batch = operation.batch;
  651. batchTypes.push( batch.type );
  652. } );
  653. selection._setTo( [ rangeInEmptyP ] );
  654. model.change( writer => {
  655. writer.setSelectionAttribute( 'foo', 'bar' );
  656. } );
  657. expect( batchTypes ).to.deep.equal( [ 'default' ] );
  658. expect( emptyP.getAttribute( fooStoreAttrKey ) ).to.equal( 'bar' );
  659. } );
  660. it( 'are removed when any content is inserted (reuses the same batch)', () => {
  661. // Dedupe batches by using a map (multiple change events will be fired).
  662. const batchTypes = new Map();
  663. selection._setTo( rangeInEmptyP );
  664. selection._setAttribute( 'foo', 'bar' );
  665. selection._setAttribute( 'abc', 'bar' );
  666. model.on( 'applyOperation', ( event, args ) => {
  667. const operation = args[ 0 ];
  668. const batch = operation.batch;
  669. batchTypes.set( batch, batch.type );
  670. } );
  671. model.change( writer => {
  672. writer.insertText( 'x', rangeInEmptyP.start );
  673. } );
  674. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  675. expect( emptyP.hasAttribute( abcStoreAttrKey ) ).to.be.false;
  676. expect( Array.from( batchTypes.values() ) ).to.deep.equal( [ 'default' ] );
  677. } );
  678. it( 'are removed when any content is moved into', () => {
  679. selection._setTo( rangeInEmptyP );
  680. selection._setAttribute( 'foo', 'bar' );
  681. model.change( writer => {
  682. writer.move( writer.createRangeOn( fullP.getChild( 0 ) ), rangeInEmptyP.start );
  683. } );
  684. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  685. } );
  686. it( 'are removed when containing element is merged with a non-empty element', () => {
  687. const emptyP2 = new Element( 'p', null, 'x' );
  688. root._appendChild( emptyP2 );
  689. emptyP._setAttribute( fooStoreAttrKey, 'bar' );
  690. emptyP2._setAttribute( fooStoreAttrKey, 'bar' );
  691. model.change( writer => {
  692. // <emptyP>{}<emptyP2>
  693. writer.merge( writer.createPositionAfter( emptyP ) );
  694. } );
  695. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  696. expect( emptyP.parent ).to.equal( root ); // Just to be sure we're checking the right element.
  697. } );
  698. it( 'are removed even when there is no selection in it', () => {
  699. emptyP._setAttribute( fooStoreAttrKey, 'bar' );
  700. selection._setTo( [ rangeInFullP ] );
  701. model.change( writer => {
  702. writer.insertText( 'x', rangeInEmptyP.start );
  703. } );
  704. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  705. } );
  706. it( 'uses model change to clear attributes', () => {
  707. selection._setTo( [ rangeInEmptyP ] );
  708. model.change( writer => {
  709. writer.setSelectionAttribute( 'foo', 'bar' );
  710. writer.insertText( 'x', rangeInEmptyP.start );
  711. // `emptyP` still has the attribute, because attribute clearing is in enqueued block.
  712. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.true;
  713. } );
  714. // When the dust settles, `emptyP` should not have the attribute.
  715. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  716. } );
  717. it( 'are not removed or merged when containing element is merged with another empty element', () => {
  718. const emptyP2 = new Element( 'p', null );
  719. root._appendChild( emptyP2 );
  720. emptyP._setAttribute( fooStoreAttrKey, 'bar' );
  721. emptyP2._setAttribute( abcStoreAttrKey, 'bar' );
  722. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.true;
  723. expect( emptyP.hasAttribute( abcStoreAttrKey ) ).to.be.false;
  724. model.change( writer => {
  725. // <emptyP>{}<emptyP2>
  726. writer.merge( writer.createPositionAfter( emptyP ) );
  727. } );
  728. expect( emptyP.getAttribute( fooStoreAttrKey ) ).to.equal( 'bar' );
  729. expect( emptyP.parent ).to.equal( root ); // Just to be sure we're checking the right element.
  730. } );
  731. // Rename and some other operations don't specify range in doc#change event.
  732. // So let's see if there's no crash or something.
  733. it( 'are not removed on rename', () => {
  734. model.change( writer => {
  735. writer.setSelection( rangeInEmptyP );
  736. writer.setSelectionAttribute( 'foo', 'bar' );
  737. } );
  738. sinon.spy( model, 'enqueueChange' );
  739. model.change( writer => {
  740. writer.rename( emptyP, 'pnew' );
  741. } );
  742. expect( model.enqueueChange.called ).to.be.false;
  743. expect( emptyP.getAttribute( fooStoreAttrKey ) ).to.equal( 'bar' );
  744. } );
  745. } );
  746. } );
  747. describe( '_overrideGravity()', () => {
  748. beforeEach( () => {
  749. model.schema.extend( '$text', {
  750. allowIn: '$root'
  751. } );
  752. } );
  753. it( 'should return the UID', () => {
  754. const overrideUidA = selection._overrideGravity();
  755. const overrideUidB = selection._overrideGravity();
  756. expect( overrideUidA ).to.be.a( 'string' );
  757. expect( overrideUidB ).to.be.a( 'string' );
  758. expect( overrideUidA ).to.not.equal( overrideUidB );
  759. } );
  760. it( 'should mark gravity as overridden', () => {
  761. expect( selection.isGravityOverridden ).to.false;
  762. selection._overrideGravity();
  763. expect( selection.isGravityOverridden ).to.true;
  764. } );
  765. it( 'should not inherit attributes from node before the caret', () => {
  766. setData( model, '<$text bold="true" italic="true">foo[]</$text>' );
  767. expect( Array.from( selection.getAttributeKeys() ) ).to.have.members( [ 'bold', 'italic' ] );
  768. selection._overrideGravity();
  769. expect( Array.from( selection.getAttributeKeys() ) ).to.length( 0 );
  770. } );
  771. it( 'should inherit attributes from node after the caret', () => {
  772. setData( model, '<$text>foo[]</$text><$text bold="true" italic="true">bar</$text>' );
  773. expect( Array.from( selection.getAttributeKeys() ) ).to.length( 0 );
  774. selection._overrideGravity();
  775. expect( Array.from( selection.getAttributeKeys() ) ).to.have.members( [ 'bold', 'italic' ] );
  776. } );
  777. it( 'should not retain attributes that are set explicitly', () => {
  778. setData( model, '<$text italic="true">foo[]</$text>' );
  779. selection._setAttribute( 'bold', true );
  780. expect( Array.from( selection.getAttributeKeys() ) ).to.have.members( [ 'bold', 'italic' ] );
  781. selection._overrideGravity();
  782. expect( Array.from( selection.getAttributeKeys() ) ).to.have.members( [] );
  783. } );
  784. it( 'should retain overridden until selection will not change range by a direct change', () => {
  785. setData( model, '<$text bold="true" italic="true">foo[]</$text><$text italic="true">bar</$text>' );
  786. selection._overrideGravity();
  787. // Changed range but not directly.
  788. model.change( writer => writer.insertText( 'abc', new Position( root, [ 0 ] ) ) );
  789. expect( Array.from( selection.getAttributeKeys() ) ).to.have.members( [ 'italic' ] );
  790. // Changed range directly.
  791. model.change( writer => writer.setSelection( new Position( root, [ 5 ] ) ) );
  792. expect( Array.from( selection.getAttributeKeys() ) ).to.have.members( [ 'bold', 'italic' ] );
  793. } );
  794. } );
  795. describe( '_restoreGravity()', () => {
  796. beforeEach( () => {
  797. model.schema.extend( '$text', {
  798. allowIn: '$root'
  799. } );
  800. } );
  801. it( 'should throw when a wrong, already restored, or no UID provided', () => {
  802. const overrideUid = selection._overrideGravity();
  803. selection._restoreGravity( overrideUid );
  804. // Wrong UID
  805. expect( () => {
  806. selection._restoreGravity( 'foo' );
  807. } ).to.throw( CKEditorError, /^document-selection-gravity-wrong-restore/ );
  808. // Already restored
  809. expect( () => {
  810. selection._restoreGravity( overrideUid );
  811. } ).to.throw( CKEditorError, /^document-selection-gravity-wrong-restore/ );
  812. // No UID
  813. expect( () => {
  814. selection._restoreGravity();
  815. } ).to.throw( CKEditorError, /^document-selection-gravity-wrong-restore/ );
  816. } );
  817. it( 'should revert default gravity when is overridden', () => {
  818. setData( model, '<$text bold="true" italic="true">foo[]</$text>' );
  819. const overrideUid = selection._overrideGravity();
  820. expect( Array.from( selection.getAttributeKeys() ) ).to.length( 0 );
  821. expect( selection.isGravityOverridden ).to.true;
  822. selection._restoreGravity( overrideUid );
  823. expect( Array.from( selection.getAttributeKeys() ) ).to.have.members( [ 'bold', 'italic' ] );
  824. expect( selection.isGravityOverridden ).to.false;
  825. } );
  826. it( 'should be called the same number of times as gravity is overridden to restore it', () => {
  827. setData( model, '<$text bold="true" italic="true">foo[]</$text>' );
  828. const overrideUidA = selection._overrideGravity();
  829. const overrideUidB = selection._overrideGravity();
  830. expect( selection.isGravityOverridden ).to.true;
  831. selection._restoreGravity( overrideUidA );
  832. expect( selection.isGravityOverridden ).to.true;
  833. selection._restoreGravity( overrideUidB );
  834. expect( selection.isGravityOverridden ).to.false;
  835. } );
  836. } );
  837. // DocumentSelection uses LiveRanges so here are only simple test to see if integration is
  838. // working well, without getting into complicated corner cases.
  839. describe( 'after applying an operation should get updated and fire events', () => {
  840. let spyRange;
  841. beforeEach( () => {
  842. root._removeChildren( 0, root.childCount );
  843. root._insertChild( 0, [
  844. new Element( 'p', [], new Text( 'abcdef' ) ),
  845. new Element( 'p', [], new Text( 'foobar' ) ),
  846. new Text( 'xyz' )
  847. ] );
  848. selection._setTo( new Range( new Position( root, [ 0, 2 ] ), new Position( root, [ 1, 4 ] ) ) );
  849. spyRange = sinon.spy();
  850. selection.on( 'change:range', spyRange );
  851. } );
  852. describe( 'InsertOperation', () => {
  853. it( 'before selection', () => {
  854. selection.on( 'change:range', ( evt, data ) => {
  855. expect( data.directChange ).to.be.false;
  856. } );
  857. model.applyOperation(
  858. new InsertOperation(
  859. new Position( root, [ 0, 1 ] ),
  860. 'xyz',
  861. doc.version
  862. )
  863. );
  864. const range = selection.getFirstRange();
  865. expect( range.start.path ).to.deep.equal( [ 0, 5 ] );
  866. expect( range.end.path ).to.deep.equal( [ 1, 4 ] );
  867. expect( spyRange.calledOnce ).to.be.true;
  868. } );
  869. it( 'inside selection', () => {
  870. selection.on( 'change:range', ( evt, data ) => {
  871. expect( data.directChange ).to.be.false;
  872. } );
  873. model.applyOperation(
  874. new InsertOperation(
  875. new Position( root, [ 1, 0 ] ),
  876. 'xyz',
  877. doc.version
  878. )
  879. );
  880. const range = selection.getFirstRange();
  881. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  882. expect( range.end.path ).to.deep.equal( [ 1, 7 ] );
  883. expect( spyRange.calledOnce ).to.be.true;
  884. } );
  885. } );
  886. describe( 'MoveOperation', () => {
  887. it( 'move range from before a selection', () => {
  888. selection.on( 'change:range', ( evt, data ) => {
  889. expect( data.directChange ).to.be.false;
  890. } );
  891. model.applyOperation(
  892. new MoveOperation(
  893. new Position( root, [ 0, 0 ] ),
  894. 2,
  895. new Position( root, [ 2 ] ),
  896. doc.version
  897. )
  898. );
  899. const range = selection.getFirstRange();
  900. expect( range.start.path ).to.deep.equal( [ 0, 0 ] );
  901. expect( range.end.path ).to.deep.equal( [ 1, 4 ] );
  902. expect( spyRange.calledOnce ).to.be.true;
  903. } );
  904. it( 'moved into before a selection', () => {
  905. selection.on( 'change:range', ( evt, data ) => {
  906. expect( data.directChange ).to.be.false;
  907. } );
  908. model.applyOperation(
  909. new MoveOperation(
  910. new Position( root, [ 2 ] ),
  911. 2,
  912. new Position( root, [ 0, 0 ] ),
  913. doc.version
  914. )
  915. );
  916. const range = selection.getFirstRange();
  917. expect( range.start.path ).to.deep.equal( [ 0, 4 ] );
  918. expect( range.end.path ).to.deep.equal( [ 1, 4 ] );
  919. expect( spyRange.calledOnce ).to.be.true;
  920. } );
  921. it( 'move range from inside of selection', () => {
  922. selection.on( 'change:range', ( evt, data ) => {
  923. expect( data.directChange ).to.be.false;
  924. } );
  925. model.applyOperation(
  926. new MoveOperation(
  927. new Position( root, [ 1, 0 ] ),
  928. 2,
  929. new Position( root, [ 2 ] ),
  930. doc.version
  931. )
  932. );
  933. const range = selection.getFirstRange();
  934. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  935. expect( range.end.path ).to.deep.equal( [ 1, 2 ] );
  936. expect( spyRange.calledOnce ).to.be.true;
  937. } );
  938. it( 'moved range intersects with selection', () => {
  939. selection.on( 'change:range', ( evt, data ) => {
  940. expect( data.directChange ).to.be.false;
  941. } );
  942. model.applyOperation(
  943. new MoveOperation(
  944. new Position( root, [ 1, 3 ] ),
  945. 2,
  946. new Position( root, [ 4 ] ),
  947. doc.version
  948. )
  949. );
  950. const range = selection.getFirstRange();
  951. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  952. expect( range.end.path ).to.deep.equal( [ 1, 3 ] );
  953. expect( spyRange.calledOnce ).to.be.true;
  954. } );
  955. it( 'split inside selection (do not break selection)', () => {
  956. selection.on( 'change:range', ( evt, data ) => {
  957. expect( data.directChange ).to.be.false;
  958. } );
  959. const batch = new Batch();
  960. const splitOperation = new SplitOperation( new Position( root, [ 1, 2 ] ), 4, null, 0 );
  961. batch.addOperation( splitOperation );
  962. model.applyOperation( splitOperation );
  963. const range = selection.getFirstRange();
  964. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  965. expect( range.end.path ).to.deep.equal( [ 2, 2 ] );
  966. expect( spyRange.calledOnce ).to.be.true;
  967. } );
  968. } );
  969. describe( 'AttributeOperation', () => {
  970. it( 'changed range includes selection anchor', () => {
  971. const spyAttribute = sinon.spy();
  972. selection.on( 'change:attribute', spyAttribute );
  973. selection.on( 'change:attribute', ( evt, data ) => {
  974. expect( data.directChange ).to.be.false;
  975. expect( data.attributeKeys ).to.deep.equal( [ 'foo' ] );
  976. } );
  977. model.applyOperation(
  978. new AttributeOperation(
  979. new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 5 ] ) ),
  980. 'foo',
  981. null,
  982. 'bar',
  983. doc.version
  984. )
  985. );
  986. // Attributes are auto updated on document change.
  987. model.change( () => {} );
  988. expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  989. expect( spyAttribute.calledOnce ).to.be.true;
  990. } );
  991. it( 'should not overwrite previously set attributes', () => {
  992. selection._setAttribute( 'foo', 'xyz' );
  993. const spyAttribute = sinon.spy();
  994. selection.on( 'change:attribute', spyAttribute );
  995. model.applyOperation(
  996. new AttributeOperation(
  997. new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 5 ] ) ),
  998. 'foo',
  999. null,
  1000. 'bar',
  1001. doc.version
  1002. )
  1003. );
  1004. expect( selection.getAttribute( 'foo' ) ).to.equal( 'xyz' );
  1005. expect( spyAttribute.called ).to.be.false;
  1006. } );
  1007. it( 'should not overwrite previously set attributes with same values', () => {
  1008. selection._setAttribute( 'foo', 'xyz' );
  1009. const spyAttribute = sinon.spy();
  1010. selection.on( 'change:attribute', spyAttribute );
  1011. model.applyOperation(
  1012. new AttributeOperation(
  1013. new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 5 ] ) ),
  1014. 'foo',
  1015. null,
  1016. 'xyz',
  1017. doc.version
  1018. )
  1019. );
  1020. expect( selection.getAttribute( 'foo' ) ).to.equal( 'xyz' );
  1021. expect( spyAttribute.called ).to.be.false;
  1022. } );
  1023. it( 'should not overwrite previously removed attributes', () => {
  1024. selection._setAttribute( 'foo', 'xyz' );
  1025. selection._removeAttribute( 'foo' );
  1026. const spyAttribute = sinon.spy();
  1027. selection.on( 'change:attribute', spyAttribute );
  1028. model.applyOperation(
  1029. new AttributeOperation(
  1030. new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 5 ] ) ),
  1031. 'foo',
  1032. null,
  1033. 'bar',
  1034. doc.version
  1035. )
  1036. );
  1037. expect( selection.hasAttribute( 'foo' ) ).to.be.false;
  1038. expect( spyAttribute.called ).to.be.false;
  1039. } );
  1040. } );
  1041. describe( 'MoveOperation to graveyard', () => {
  1042. it( 'fix selection range if it ends up in graveyard #1', () => {
  1043. selection._setTo( new Position( root, [ 1, 3 ] ) );
  1044. model.applyOperation(
  1045. new MoveOperation(
  1046. new Position( root, [ 1, 2 ] ),
  1047. 2,
  1048. new Position( doc.graveyard, [ 0 ] ),
  1049. doc.version
  1050. )
  1051. );
  1052. expect( selection.getFirstPosition().path ).to.deep.equal( [ 1, 2 ] );
  1053. } );
  1054. it( 'fix selection range if it ends up in graveyard #2', () => {
  1055. selection._setTo( [ new Range( new Position( root, [ 1, 2 ] ), new Position( root, [ 1, 4 ] ) ) ] );
  1056. model.applyOperation(
  1057. new MoveOperation(
  1058. new Position( root, [ 1, 2 ] ),
  1059. 2,
  1060. new Position( doc.graveyard, [ 0 ] ),
  1061. doc.version
  1062. )
  1063. );
  1064. expect( selection.getFirstPosition().path ).to.deep.equal( [ 1, 2 ] );
  1065. } );
  1066. it( 'fix selection range if it ends up in graveyard #3', () => {
  1067. selection._setTo( [ new Range( new Position( root, [ 1, 1 ] ), new Position( root, [ 1, 2 ] ) ) ] );
  1068. model.applyOperation(
  1069. new MoveOperation(
  1070. new Position( root, [ 1 ] ),
  1071. 2,
  1072. new Position( doc.graveyard, [ 0 ] ),
  1073. doc.version
  1074. )
  1075. );
  1076. expect( selection.getFirstPosition().path ).to.deep.equal( [ 0, 6 ] );
  1077. } );
  1078. it( 'fix selection range if it ends up in graveyard #4 - whole content removed', () => {
  1079. model.applyOperation(
  1080. new MoveOperation(
  1081. new Position( root, [ 0 ] ),
  1082. 3,
  1083. new Position( doc.graveyard, [ 0 ] ),
  1084. doc.version
  1085. )
  1086. );
  1087. expect( selection.getFirstPosition().path ).to.deep.equal( [ 0 ] );
  1088. model.applyOperation(
  1089. new InsertOperation(
  1090. new Position( root, [ 0 ] ),
  1091. new Element( 'p' ),
  1092. doc.version
  1093. )
  1094. );
  1095. // Now it's clear that it's the default range.
  1096. expect( selection.getFirstPosition().path ).to.deep.equal( [ 0, 0 ] );
  1097. } );
  1098. } );
  1099. it( '`DocumentSelection#change:range` event should be fire once even if selection contains multi-ranges', () => {
  1100. root._removeChildren( 0, root.childCount );
  1101. root._insertChild( 0, [
  1102. new Element( 'p', [], new Text( 'abcdef' ) ),
  1103. new Element( 'p', [], new Text( 'foobar' ) ),
  1104. new Text( 'xyz #2' )
  1105. ] );
  1106. selection._setTo( [
  1107. Range._createIn( root.getNodeByPath( [ 0 ] ) ),
  1108. Range._createIn( root.getNodeByPath( [ 1 ] ) )
  1109. ] );
  1110. spyRange = sinon.spy();
  1111. selection.on( 'change:range', spyRange );
  1112. model.applyOperation(
  1113. new InsertOperation(
  1114. new Position( root, [ 0 ] ),
  1115. 'xyz #1',
  1116. doc.version
  1117. )
  1118. );
  1119. expect( spyRange.calledOnce ).to.be.true;
  1120. } );
  1121. } );
  1122. it( 'should throw if one of ranges starts or ends inside surrogate pair', () => {
  1123. root._removeChildren( 0, root.childCount );
  1124. root._appendChild( '\uD83D\uDCA9' );
  1125. expect( () => {
  1126. doc.selection._setTo( new Range( Position._createAt( root, 0 ), Position._createAt( root, 1 ) ) );
  1127. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  1128. expect( () => {
  1129. doc.selection._setTo( new Range( Position._createAt( root, 1 ), Position._createAt( root, 2 ) ) );
  1130. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  1131. } );
  1132. it( 'should throw if one of ranges starts or ends between base character and combining mark', () => {
  1133. root._removeChildren( 0, root.childCount );
  1134. root._appendChild( 'foo̻̐ͩbar' );
  1135. expect( () => {
  1136. doc.selection._setTo( new Range( Position._createAt( root, 3 ), Position._createAt( root, 9 ) ) );
  1137. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  1138. expect( () => {
  1139. doc.selection._setTo( new Range( Position._createAt( root, 4 ), Position._createAt( root, 9 ) ) );
  1140. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  1141. expect( () => {
  1142. doc.selection._setTo( new Range( Position._createAt( root, 5 ), Position._createAt( root, 9 ) ) );
  1143. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  1144. expect( () => {
  1145. doc.selection._setTo( new Range( Position._createAt( root, 1 ), Position._createAt( root, 3 ) ) );
  1146. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  1147. expect( () => {
  1148. doc.selection._setTo( new Range( Position._createAt( root, 1 ), Position._createAt( root, 4 ) ) );
  1149. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  1150. expect( () => {
  1151. doc.selection._setTo( new Range( Position._createAt( root, 1 ), Position._createAt( root, 5 ) ) );
  1152. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  1153. } );
  1154. } );