documentselection.js 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520
  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. it( 'should add marker of selected widget', () => {
  347. root._insertChild( 0, new Element( 'widget' ) );
  348. model.change( writer => {
  349. writer.setSelection( writer.createRange(
  350. writer.createPositionFromPath( root, [ 0 ] ),
  351. writer.createPositionFromPath( root, [ 1 ] )
  352. ) );
  353. writer.addMarker( 'marker', {
  354. range: writer.createRange(
  355. writer.createPositionFromPath( root, [ 0 ] ),
  356. writer.createPositionFromPath( root, [ 1 ] )
  357. ),
  358. usingOperation: false
  359. } );
  360. } );
  361. expect( selection.markers.map( marker => marker.name ) ).to.have.members( [ 'marker' ] );
  362. } );
  363. } );
  364. describe( 'destroy()', () => {
  365. it( 'should unbind all events', () => {
  366. selection._setTo( [ range, liveRange ] );
  367. const ranges = Array.from( selection._selection._ranges );
  368. sinon.spy( ranges[ 0 ], 'detach' );
  369. sinon.spy( ranges[ 1 ], 'detach' );
  370. selection.destroy();
  371. expect( ranges[ 0 ].detach.called ).to.be.true;
  372. expect( ranges[ 1 ].detach.called ).to.be.true;
  373. ranges[ 0 ].detach.restore();
  374. ranges[ 1 ].detach.restore();
  375. } );
  376. } );
  377. describe( 'getFirstRange()', () => {
  378. it( 'should return default range if no ranges were added', () => {
  379. const firstRange = selection.getFirstRange();
  380. expect( firstRange.start.isEqual( new Position( root, [ 0, 0 ] ) ) );
  381. expect( firstRange.end.isEqual( new Position( root, [ 0, 0 ] ) ) );
  382. } );
  383. } );
  384. describe( 'getLastRange()', () => {
  385. it( 'should return default range if no ranges were added', () => {
  386. const lastRange = selection.getLastRange();
  387. expect( lastRange.start.isEqual( new Position( root, [ 0, 0 ] ) ) );
  388. expect( lastRange.end.isEqual( new Position( root, [ 0, 0 ] ) ) );
  389. } );
  390. } );
  391. describe( 'getSelectedElement()', () => {
  392. it( 'should return selected element', () => {
  393. selection._setTo( liveRange );
  394. const p = root.getChild( 0 );
  395. expect( selection.getSelectedElement() ).to.equal( p );
  396. } );
  397. } );
  398. describe( '_setTo() - set collapsed at', () => {
  399. it( 'detaches all existing ranges', () => {
  400. selection._setTo( [ range, liveRange ] );
  401. const spy = testUtils.sinon.spy( LiveRange.prototype, 'detach' );
  402. selection._setTo( root, 0 );
  403. expect( spy.calledTwice ).to.be.true;
  404. } );
  405. } );
  406. describe( '_setFocus()', () => {
  407. it( 'modifies default range', () => {
  408. const startPos = selection.getFirstPosition();
  409. const endPos = Position._createAt( root, 'end' );
  410. selection._setFocus( endPos );
  411. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'same' );
  412. expect( selection.focus.compareWith( endPos ) ).to.equal( 'same' );
  413. } );
  414. it( 'detaches the range it replaces', () => {
  415. const startPos = Position._createAt( root, 1 );
  416. const endPos = Position._createAt( root, 2 );
  417. const newEndPos = Position._createAt( root, 4 );
  418. const spy = testUtils.sinon.spy( LiveRange.prototype, 'detach' );
  419. selection._setTo( new Range( startPos, endPos ) );
  420. selection._setFocus( newEndPos );
  421. expect( spy.calledOnce ).to.be.true;
  422. } );
  423. it( 'refreshes attributes', () => {
  424. const spy = sinon.spy( selection._selection, '_updateAttributes' );
  425. selection._setFocus( Position._createAt( root, 1 ) );
  426. expect( spy.called ).to.be.true;
  427. } );
  428. } );
  429. describe( '_setTo() - remove all ranges', () => {
  430. let spy, ranges;
  431. beforeEach( () => {
  432. selection._setTo( [ liveRange, range ] );
  433. spy = sinon.spy();
  434. selection.on( 'change:range', spy );
  435. ranges = Array.from( selection._selection._ranges );
  436. sinon.spy( ranges[ 0 ], 'detach' );
  437. sinon.spy( ranges[ 1 ], 'detach' );
  438. selection._setTo( null );
  439. } );
  440. afterEach( () => {
  441. ranges[ 0 ].detach.restore();
  442. ranges[ 1 ].detach.restore();
  443. } );
  444. it( 'should remove all stored ranges (and reset to default range)', () => {
  445. expect( Array.from( selection.getRanges() ).length ).to.equal( 1 );
  446. expect( selection.anchor.isEqual( new Position( root, [ 0, 0 ] ) ) ).to.be.true;
  447. expect( selection.focus.isEqual( new Position( root, [ 0, 0 ] ) ) ).to.be.true;
  448. } );
  449. it( 'should detach ranges', () => {
  450. expect( ranges[ 0 ].detach.called ).to.be.true;
  451. expect( ranges[ 1 ].detach.called ).to.be.true;
  452. } );
  453. it( 'should refresh attributes', () => {
  454. const spy = sinon.spy( selection._selection, '_updateAttributes' );
  455. selection._setTo( null );
  456. expect( spy.called ).to.be.true;
  457. } );
  458. } );
  459. describe( '_setTo()', () => {
  460. it( 'should throw an error when range is invalid', () => {
  461. expect( () => {
  462. selection._setTo( [ { invalid: 'range' } ] );
  463. } ).to.throw( CKEditorError, /model-selection-set-ranges-not-range/ );
  464. } );
  465. it( 'should log a warning when trying to set selection to the graveyard', () => {
  466. // eslint-disable-next-line no-undef
  467. const warnStub = sinon.stub( console, 'warn' );
  468. const range = new Range( new Position( model.document.graveyard, [ 0 ] ) );
  469. selection._setTo( range );
  470. expect( warnStub.calledOnce ).to.equal( true );
  471. expect( warnStub.getCall( 0 ).args[ 0 ] ).to.match( /model-selection-range-in-graveyard/ );
  472. expect( selection._ranges ).to.deep.equal( [] );
  473. warnStub.restore();
  474. } );
  475. it( 'should detach removed ranges', () => {
  476. selection._setTo( [ liveRange, range ] );
  477. const oldRanges = Array.from( selection._selection._ranges );
  478. sinon.spy( oldRanges[ 0 ], 'detach' );
  479. sinon.spy( oldRanges[ 1 ], 'detach' );
  480. selection._setTo( [] );
  481. expect( oldRanges[ 0 ].detach.called ).to.be.true;
  482. expect( oldRanges[ 1 ].detach.called ).to.be.true;
  483. } );
  484. it( 'should refresh attributes', () => {
  485. const spy = sinon.spy( selection._selection, '_updateAttributes' );
  486. selection._setTo( [ range ] );
  487. expect( spy.called ).to.be.true;
  488. } );
  489. // See #630.
  490. it( 'should refresh attributes – integration test for #630', () => {
  491. model.schema.extend( '$text', { allowIn: '$root' } );
  492. setData( model, 'f<$text italic="true">[o</$text><$text bold="true">ob]a</$text>r' );
  493. selection._setTo( [ Range._createFromPositionAndShift( selection.getLastRange().end, 0 ) ] );
  494. expect( selection.getAttribute( 'bold' ) ).to.equal( true );
  495. expect( selection.hasAttribute( 'italic' ) ).to.equal( false );
  496. expect( getData( model ) )
  497. .to.equal( 'f<$text italic="true">o</$text><$text bold="true">ob[]a</$text>r' );
  498. } );
  499. } );
  500. describe( '_isStoreAttributeKey', () => {
  501. it( 'should return true if given key is a key of an attribute stored in element by DocumentSelection', () => {
  502. expect( DocumentSelection._isStoreAttributeKey( fooStoreAttrKey ) ).to.be.true;
  503. } );
  504. it( 'should return false if given key is not a key of an attribute stored in element by DocumentSelection', () => {
  505. expect( DocumentSelection._isStoreAttributeKey( 'foo' ) ).to.be.false;
  506. } );
  507. } );
  508. describe( 'attributes', () => {
  509. let fullP, emptyP, rangeInFullP, rangeInEmptyP;
  510. beforeEach( () => {
  511. root._removeChildren( 0, root.childCount );
  512. root._appendChild( [
  513. new Element( 'p', [], new Text( 'foobar' ) ),
  514. new Element( 'p', [], [] )
  515. ] );
  516. fullP = root.getChild( 0 );
  517. emptyP = root.getChild( 1 );
  518. rangeInFullP = new Range( new Position( root, [ 0, 4 ] ), new Position( root, [ 0, 4 ] ) );
  519. rangeInEmptyP = new Range( new Position( root, [ 1, 0 ] ), new Position( root, [ 1, 0 ] ) );
  520. // I've lost 30 mins debugging why my tests fail and that was due to the above code reusing
  521. // a root which wasn't empty (so the ranges didn't actually make much sense).
  522. expect( root.childCount ).to.equal( 2 );
  523. } );
  524. describe( '_setAttribute()', () => {
  525. it( 'should set attribute', () => {
  526. selection._setTo( [ rangeInEmptyP ] );
  527. selection._setAttribute( 'foo', 'bar' );
  528. expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  529. } );
  530. } );
  531. describe( '_removeAttribute()', () => {
  532. it( 'should remove attribute set on the text fragment', () => {
  533. selection._setTo( [ rangeInFullP ] );
  534. selection._setAttribute( 'foo', 'bar' );
  535. selection._removeAttribute( 'foo' );
  536. expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
  537. } );
  538. it( 'should prevent auto update of the attribute even if attribute is not preset yet', () => {
  539. selection._setTo( new Position( root, [ 0, 1 ] ) );
  540. // Remove "foo" attribute that is not present in selection yet.
  541. expect( selection.hasAttribute( 'foo' ) ).to.be.false;
  542. selection._removeAttribute( 'foo' );
  543. // Trigger selecton auto update on document change. It should not get attribute from surrounding text;
  544. model.change( writer => {
  545. writer.setAttribute( 'foo', 'bar', Range._createIn( fullP ) );
  546. } );
  547. expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
  548. } );
  549. } );
  550. describe( '_getStoredAttributes()', () => {
  551. it( 'should return no values if there are no ranges in selection', () => {
  552. const values = Array.from( selection._getStoredAttributes() );
  553. expect( values ).to.deep.equal( [] );
  554. } );
  555. } );
  556. describe( 'are updated on a direct range change', () => {
  557. beforeEach( () => {
  558. root._insertChild( 0, [
  559. new Element( 'p', { p: true } ),
  560. new Text( 'a', { a: true } ),
  561. new Element( 'p', { p: true } ),
  562. new Text( 'b', { b: true } ),
  563. new Text( 'c', { c: true } ),
  564. new Element( 'p', [], [
  565. new Text( 'd', { d: true } )
  566. ] ),
  567. new Element( 'p', { p: true } ),
  568. new Text( 'e', { e: true } )
  569. ] );
  570. } );
  571. it( 'if selection is a range, should find first character in it and copy it\'s attributes', () => {
  572. selection._setTo( [ new Range( new Position( root, [ 2 ] ), new Position( root, [ 5 ] ) ) ] );
  573. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'b', true ] ] );
  574. // Step into elements when looking for first character:
  575. selection._setTo( [ new Range( new Position( root, [ 5 ] ), new Position( root, [ 7 ] ) ) ] );
  576. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'd', true ] ] );
  577. } );
  578. it( 'if selection is collapsed it should seek a character to copy that character\'s attributes', () => {
  579. // Take styles from character before selection.
  580. selection._setTo( [ new Range( new Position( root, [ 2 ] ), new Position( root, [ 2 ] ) ) ] );
  581. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'a', true ] ] );
  582. // If there are none,
  583. // Take styles from character after selection.
  584. selection._setTo( [ new Range( new Position( root, [ 3 ] ), new Position( root, [ 3 ] ) ) ] );
  585. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'b', true ] ] );
  586. // If there are none,
  587. // Look from the selection position to the beginning of node looking for character to take attributes from.
  588. selection._setTo( [ new Range( new Position( root, [ 6 ] ), new Position( root, [ 6 ] ) ) ] );
  589. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'c', true ] ] );
  590. // If there are none,
  591. // Look from the selection position to the end of node looking for character to take attributes from.
  592. selection._setTo( [ new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) ) ] );
  593. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'a', true ] ] );
  594. // If there are no characters to copy attributes from, use stored attributes.
  595. selection._setTo( [ new Range( new Position( root, [ 0, 0 ] ), new Position( root, [ 0, 0 ] ) ) ] );
  596. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [] );
  597. } );
  598. it( 'should overwrite any previously set attributes', () => {
  599. selection._setTo( new Position( root, [ 5, 0 ] ) );
  600. selection._setAttribute( 'x', true );
  601. selection._setAttribute( 'y', true );
  602. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'd', true ], [ 'x', true ], [ 'y', true ] ] );
  603. selection._setTo( new Position( root, [ 1 ] ) );
  604. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'a', true ] ] );
  605. } );
  606. it( 'should fire change:attribute event', () => {
  607. const spy = sinon.spy();
  608. selection.on( 'change:attribute', spy );
  609. selection._setTo( [ new Range( new Position( root, [ 2 ] ), new Position( root, [ 5 ] ) ) ] );
  610. expect( spy.calledOnce ).to.be.true;
  611. } );
  612. it( 'should not fire change:attribute event if attributes did not change', () => {
  613. selection._setTo( new Position( root, [ 5, 0 ] ) );
  614. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'd', true ] ] );
  615. const spy = sinon.spy();
  616. selection.on( 'change:attribute', spy );
  617. selection._setTo( new Position( root, [ 5, 1 ] ) );
  618. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'd', true ] ] );
  619. expect( spy.called ).to.be.false;
  620. } );
  621. } );
  622. // #986
  623. describe( 'are not inherited from the inside of object elements', () => {
  624. beforeEach( () => {
  625. model.schema.register( 'image', {
  626. isObject: true
  627. } );
  628. model.schema.extend( 'image', { allowIn: '$root' } );
  629. model.schema.extend( 'image', { allowIn: '$block' } );
  630. model.schema.register( 'caption' );
  631. model.schema.extend( 'caption', { allowIn: 'image' } );
  632. model.schema.extend( '$text', {
  633. allowIn: [ 'image', 'caption' ],
  634. allowAttributes: 'bold'
  635. } );
  636. } );
  637. it( 'ignores attributes inside an object if selection contains that object', () => {
  638. setData( model, '<p>[<image><$text bold="true">Caption for the image.</$text></image>]</p>' );
  639. expect( selection.hasAttribute( 'bold' ) ).to.equal( false );
  640. } );
  641. it( 'ignores attributes inside an object if selection contains that object (deeper structure)', () => {
  642. setData( model, '<p>[<image><caption><$text bold="true">Caption for the image.</$text></caption></image>]</p>' );
  643. expect( selection.hasAttribute( 'bold' ) ).to.equal( false );
  644. } );
  645. it( 'ignores attributes inside an object if selection contains that object (block level)', () => {
  646. setData( model, '<p>foo</p>[<image><$text bold="true">Caption for the image.</$text></image>]<p>foo</p>' );
  647. expect( selection.hasAttribute( 'bold' ) ).to.equal( false );
  648. } );
  649. it( 'reads attributes from text even if the selection contains an object', () => {
  650. setData( model, '<p>x<$text bold="true">[bar</$text><image></image>foo]</p>' );
  651. expect( selection.getAttribute( 'bold' ) ).to.equal( true );
  652. } );
  653. it( 'reads attributes when the entire selection inside an object', () => {
  654. setData( model, '<p><image><caption><$text bold="true">[bar]</$text></caption></image></p>' );
  655. expect( selection.getAttribute( 'bold' ) ).to.equal( true );
  656. } );
  657. it( 'stops reading attributes if selection starts with an object', () => {
  658. setData( model, '<p>[<image></image><$text bold="true">bar]</$text></p>' );
  659. expect( selection.hasAttribute( 'bold' ) ).to.equal( false );
  660. } );
  661. } );
  662. describe( 'parent element\'s attributes', () => {
  663. it( 'are set using a normal batch', () => {
  664. const batchTypes = [];
  665. model.on( 'applyOperation', ( event, args ) => {
  666. const operation = args[ 0 ];
  667. const batch = operation.batch;
  668. batchTypes.push( batch.type );
  669. } );
  670. selection._setTo( [ rangeInEmptyP ] );
  671. model.change( writer => {
  672. writer.setSelectionAttribute( 'foo', 'bar' );
  673. } );
  674. expect( batchTypes ).to.deep.equal( [ 'default' ] );
  675. expect( emptyP.getAttribute( fooStoreAttrKey ) ).to.equal( 'bar' );
  676. } );
  677. it( 'are removed when any content is inserted (reuses the same batch)', () => {
  678. // Dedupe batches by using a map (multiple change events will be fired).
  679. const batchTypes = new Map();
  680. selection._setTo( rangeInEmptyP );
  681. selection._setAttribute( 'foo', 'bar' );
  682. selection._setAttribute( 'abc', 'bar' );
  683. model.on( 'applyOperation', ( event, args ) => {
  684. const operation = args[ 0 ];
  685. const batch = operation.batch;
  686. batchTypes.set( batch, batch.type );
  687. } );
  688. model.change( writer => {
  689. writer.insertText( 'x', rangeInEmptyP.start );
  690. } );
  691. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  692. expect( emptyP.hasAttribute( abcStoreAttrKey ) ).to.be.false;
  693. expect( Array.from( batchTypes.values() ) ).to.deep.equal( [ 'default' ] );
  694. } );
  695. it( 'are removed when any content is moved into', () => {
  696. selection._setTo( rangeInEmptyP );
  697. selection._setAttribute( 'foo', 'bar' );
  698. model.change( writer => {
  699. writer.move( writer.createRangeOn( fullP.getChild( 0 ) ), rangeInEmptyP.start );
  700. } );
  701. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  702. } );
  703. it( 'are removed when containing element is merged with a non-empty element', () => {
  704. const emptyP2 = new Element( 'p', null, 'x' );
  705. root._appendChild( emptyP2 );
  706. emptyP._setAttribute( fooStoreAttrKey, 'bar' );
  707. emptyP2._setAttribute( fooStoreAttrKey, 'bar' );
  708. model.change( writer => {
  709. // <emptyP>{}<emptyP2>
  710. writer.merge( writer.createPositionAfter( emptyP ) );
  711. } );
  712. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  713. expect( emptyP.parent ).to.equal( root ); // Just to be sure we're checking the right element.
  714. } );
  715. it( 'are removed even when there is no selection in it', () => {
  716. emptyP._setAttribute( fooStoreAttrKey, 'bar' );
  717. selection._setTo( [ rangeInFullP ] );
  718. model.change( writer => {
  719. writer.insertText( 'x', rangeInEmptyP.start );
  720. } );
  721. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  722. } );
  723. it( 'uses model change to clear attributes', () => {
  724. selection._setTo( [ rangeInEmptyP ] );
  725. model.change( writer => {
  726. writer.setSelectionAttribute( 'foo', 'bar' );
  727. writer.insertText( 'x', rangeInEmptyP.start );
  728. // `emptyP` still has the attribute, because attribute clearing is in enqueued block.
  729. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.true;
  730. } );
  731. // When the dust settles, `emptyP` should not have the attribute.
  732. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  733. } );
  734. it( 'are not removed or merged when containing element is merged with another empty element', () => {
  735. const emptyP2 = new Element( 'p', null );
  736. root._appendChild( emptyP2 );
  737. emptyP._setAttribute( fooStoreAttrKey, 'bar' );
  738. emptyP2._setAttribute( abcStoreAttrKey, 'bar' );
  739. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.true;
  740. expect( emptyP.hasAttribute( abcStoreAttrKey ) ).to.be.false;
  741. model.change( writer => {
  742. // <emptyP>{}<emptyP2>
  743. writer.merge( writer.createPositionAfter( emptyP ) );
  744. } );
  745. expect( emptyP.getAttribute( fooStoreAttrKey ) ).to.equal( 'bar' );
  746. expect( emptyP.parent ).to.equal( root ); // Just to be sure we're checking the right element.
  747. } );
  748. // Rename and some other operations don't specify range in doc#change event.
  749. // So let's see if there's no crash or something.
  750. it( 'are not removed on rename', () => {
  751. model.change( writer => {
  752. writer.setSelection( rangeInEmptyP );
  753. writer.setSelectionAttribute( 'foo', 'bar' );
  754. } );
  755. sinon.spy( model, 'enqueueChange' );
  756. model.change( writer => {
  757. writer.rename( emptyP, 'pnew' );
  758. } );
  759. expect( model.enqueueChange.called ).to.be.false;
  760. expect( emptyP.getAttribute( fooStoreAttrKey ) ).to.equal( 'bar' );
  761. } );
  762. } );
  763. } );
  764. describe( '_overrideGravity()', () => {
  765. beforeEach( () => {
  766. model.schema.extend( '$text', {
  767. allowIn: '$root'
  768. } );
  769. } );
  770. it( 'should return the UID', () => {
  771. const overrideUidA = selection._overrideGravity();
  772. const overrideUidB = selection._overrideGravity();
  773. expect( overrideUidA ).to.be.a( 'string' );
  774. expect( overrideUidB ).to.be.a( 'string' );
  775. expect( overrideUidA ).to.not.equal( overrideUidB );
  776. } );
  777. it( 'should mark gravity as overridden', () => {
  778. expect( selection.isGravityOverridden ).to.false;
  779. selection._overrideGravity();
  780. expect( selection.isGravityOverridden ).to.true;
  781. } );
  782. it( 'should not inherit attributes from node before the caret', () => {
  783. setData( model, '<$text bold="true" italic="true">foo[]</$text>' );
  784. expect( Array.from( selection.getAttributeKeys() ) ).to.have.members( [ 'bold', 'italic' ] );
  785. selection._overrideGravity();
  786. expect( Array.from( selection.getAttributeKeys() ) ).to.length( 0 );
  787. } );
  788. it( 'should inherit attributes from node after the caret', () => {
  789. setData( model, '<$text>foo[]</$text><$text bold="true" italic="true">bar</$text>' );
  790. expect( Array.from( selection.getAttributeKeys() ) ).to.length( 0 );
  791. selection._overrideGravity();
  792. expect( Array.from( selection.getAttributeKeys() ) ).to.have.members( [ 'bold', 'italic' ] );
  793. } );
  794. it( 'should not retain attributes that are set explicitly', () => {
  795. setData( model, '<$text italic="true">foo[]</$text>' );
  796. selection._setAttribute( 'bold', true );
  797. expect( Array.from( selection.getAttributeKeys() ) ).to.have.members( [ 'bold', 'italic' ] );
  798. selection._overrideGravity();
  799. expect( Array.from( selection.getAttributeKeys() ) ).to.have.members( [] );
  800. } );
  801. it( 'should retain overridden until selection will not change range by a direct change', () => {
  802. setData( model, '<$text bold="true" italic="true">foo[]</$text><$text italic="true">bar</$text>' );
  803. selection._overrideGravity();
  804. // Changed range but not directly.
  805. model.change( writer => writer.insertText( 'abc', new Position( root, [ 0 ] ) ) );
  806. expect( Array.from( selection.getAttributeKeys() ) ).to.have.members( [ 'italic' ] );
  807. // Changed range directly.
  808. model.change( writer => writer.setSelection( new Position( root, [ 5 ] ) ) );
  809. expect( Array.from( selection.getAttributeKeys() ) ).to.have.members( [ 'bold', 'italic' ] );
  810. } );
  811. } );
  812. describe( '_restoreGravity()', () => {
  813. beforeEach( () => {
  814. model.schema.extend( '$text', {
  815. allowIn: '$root'
  816. } );
  817. } );
  818. it( 'should throw when a wrong, already restored, or no UID provided', () => {
  819. const overrideUid = selection._overrideGravity();
  820. selection._restoreGravity( overrideUid );
  821. // Wrong UID
  822. expect( () => {
  823. selection._restoreGravity( 'foo' );
  824. } ).to.throw( CKEditorError, /^document-selection-gravity-wrong-restore/ );
  825. // Already restored
  826. expect( () => {
  827. selection._restoreGravity( overrideUid );
  828. } ).to.throw( CKEditorError, /^document-selection-gravity-wrong-restore/ );
  829. // No UID
  830. expect( () => {
  831. selection._restoreGravity();
  832. } ).to.throw( CKEditorError, /^document-selection-gravity-wrong-restore/ );
  833. } );
  834. it( 'should revert default gravity when is overridden', () => {
  835. setData( model, '<$text bold="true" italic="true">foo[]</$text>' );
  836. const overrideUid = selection._overrideGravity();
  837. expect( Array.from( selection.getAttributeKeys() ) ).to.length( 0 );
  838. expect( selection.isGravityOverridden ).to.true;
  839. selection._restoreGravity( overrideUid );
  840. expect( Array.from( selection.getAttributeKeys() ) ).to.have.members( [ 'bold', 'italic' ] );
  841. expect( selection.isGravityOverridden ).to.false;
  842. } );
  843. it( 'should be called the same number of times as gravity is overridden to restore it', () => {
  844. setData( model, '<$text bold="true" italic="true">foo[]</$text>' );
  845. const overrideUidA = selection._overrideGravity();
  846. const overrideUidB = selection._overrideGravity();
  847. expect( selection.isGravityOverridden ).to.true;
  848. selection._restoreGravity( overrideUidA );
  849. expect( selection.isGravityOverridden ).to.true;
  850. selection._restoreGravity( overrideUidB );
  851. expect( selection.isGravityOverridden ).to.false;
  852. } );
  853. } );
  854. // DocumentSelection uses LiveRanges so here are only simple test to see if integration is
  855. // working well, without getting into complicated corner cases.
  856. describe( 'after applying an operation should get updated and fire events', () => {
  857. let spyRange;
  858. beforeEach( () => {
  859. root._removeChildren( 0, root.childCount );
  860. root._insertChild( 0, [
  861. new Element( 'p', [], new Text( 'abcdef' ) ),
  862. new Element( 'p', [], new Text( 'foobar' ) ),
  863. new Text( 'xyz' )
  864. ] );
  865. selection._setTo( new Range( new Position( root, [ 0, 2 ] ), new Position( root, [ 1, 4 ] ) ) );
  866. spyRange = sinon.spy();
  867. selection.on( 'change:range', spyRange );
  868. } );
  869. describe( 'InsertOperation', () => {
  870. it( 'before selection', () => {
  871. selection.on( 'change:range', ( evt, data ) => {
  872. expect( data.directChange ).to.be.false;
  873. } );
  874. model.applyOperation(
  875. new InsertOperation(
  876. new Position( root, [ 0, 1 ] ),
  877. 'xyz',
  878. doc.version
  879. )
  880. );
  881. const range = selection.getFirstRange();
  882. expect( range.start.path ).to.deep.equal( [ 0, 5 ] );
  883. expect( range.end.path ).to.deep.equal( [ 1, 4 ] );
  884. expect( spyRange.calledOnce ).to.be.true;
  885. } );
  886. it( 'inside selection', () => {
  887. selection.on( 'change:range', ( evt, data ) => {
  888. expect( data.directChange ).to.be.false;
  889. } );
  890. model.applyOperation(
  891. new InsertOperation(
  892. new Position( root, [ 1, 0 ] ),
  893. 'xyz',
  894. doc.version
  895. )
  896. );
  897. const range = selection.getFirstRange();
  898. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  899. expect( range.end.path ).to.deep.equal( [ 1, 7 ] );
  900. expect( spyRange.calledOnce ).to.be.true;
  901. } );
  902. } );
  903. describe( 'MoveOperation', () => {
  904. it( 'move range from 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, [ 0, 0 ] ),
  911. 2,
  912. new Position( root, [ 2 ] ),
  913. doc.version
  914. )
  915. );
  916. const range = selection.getFirstRange();
  917. expect( range.start.path ).to.deep.equal( [ 0, 0 ] );
  918. expect( range.end.path ).to.deep.equal( [ 1, 4 ] );
  919. expect( spyRange.calledOnce ).to.be.true;
  920. } );
  921. it( 'moved into before a 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, [ 2 ] ),
  928. 2,
  929. new Position( root, [ 0, 0 ] ),
  930. doc.version
  931. )
  932. );
  933. const range = selection.getFirstRange();
  934. expect( range.start.path ).to.deep.equal( [ 0, 4 ] );
  935. expect( range.end.path ).to.deep.equal( [ 1, 4 ] );
  936. expect( spyRange.calledOnce ).to.be.true;
  937. } );
  938. it( 'move range from inside of 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, 0 ] ),
  945. 2,
  946. new Position( root, [ 2 ] ),
  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, 2 ] );
  953. expect( spyRange.calledOnce ).to.be.true;
  954. } );
  955. it( 'moved range intersects with selection', () => {
  956. selection.on( 'change:range', ( evt, data ) => {
  957. expect( data.directChange ).to.be.false;
  958. } );
  959. model.applyOperation(
  960. new MoveOperation(
  961. new Position( root, [ 1, 3 ] ),
  962. 2,
  963. new Position( root, [ 4 ] ),
  964. doc.version
  965. )
  966. );
  967. const range = selection.getFirstRange();
  968. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  969. expect( range.end.path ).to.deep.equal( [ 1, 3 ] );
  970. expect( spyRange.calledOnce ).to.be.true;
  971. } );
  972. it( 'split inside selection (do not break selection)', () => {
  973. selection.on( 'change:range', ( evt, data ) => {
  974. expect( data.directChange ).to.be.false;
  975. } );
  976. const batch = new Batch();
  977. const splitOperation = new SplitOperation( new Position( root, [ 1, 2 ] ), 4, null, 0 );
  978. batch.addOperation( splitOperation );
  979. model.applyOperation( splitOperation );
  980. const range = selection.getFirstRange();
  981. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  982. expect( range.end.path ).to.deep.equal( [ 2, 2 ] );
  983. expect( spyRange.calledOnce ).to.be.true;
  984. } );
  985. } );
  986. describe( 'AttributeOperation', () => {
  987. it( 'changed range includes selection anchor', () => {
  988. const spyAttribute = sinon.spy();
  989. selection.on( 'change:attribute', spyAttribute );
  990. selection.on( 'change:attribute', ( evt, data ) => {
  991. expect( data.directChange ).to.be.false;
  992. expect( data.attributeKeys ).to.deep.equal( [ 'foo' ] );
  993. } );
  994. model.applyOperation(
  995. new AttributeOperation(
  996. new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 5 ] ) ),
  997. 'foo',
  998. null,
  999. 'bar',
  1000. doc.version
  1001. )
  1002. );
  1003. // Attributes are auto updated on document change.
  1004. model.change( () => {} );
  1005. expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  1006. expect( spyAttribute.calledOnce ).to.be.true;
  1007. } );
  1008. it( 'should not overwrite previously set attributes', () => {
  1009. selection._setAttribute( 'foo', 'xyz' );
  1010. const spyAttribute = sinon.spy();
  1011. selection.on( 'change:attribute', spyAttribute );
  1012. model.applyOperation(
  1013. new AttributeOperation(
  1014. new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 5 ] ) ),
  1015. 'foo',
  1016. null,
  1017. 'bar',
  1018. doc.version
  1019. )
  1020. );
  1021. expect( selection.getAttribute( 'foo' ) ).to.equal( 'xyz' );
  1022. expect( spyAttribute.called ).to.be.false;
  1023. } );
  1024. it( 'should not overwrite previously set attributes with same values', () => {
  1025. selection._setAttribute( 'foo', 'xyz' );
  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. 'xyz',
  1034. doc.version
  1035. )
  1036. );
  1037. expect( selection.getAttribute( 'foo' ) ).to.equal( 'xyz' );
  1038. expect( spyAttribute.called ).to.be.false;
  1039. } );
  1040. it( 'should not overwrite previously removed attributes', () => {
  1041. selection._setAttribute( 'foo', 'xyz' );
  1042. selection._removeAttribute( 'foo' );
  1043. const spyAttribute = sinon.spy();
  1044. selection.on( 'change:attribute', spyAttribute );
  1045. model.applyOperation(
  1046. new AttributeOperation(
  1047. new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 5 ] ) ),
  1048. 'foo',
  1049. null,
  1050. 'bar',
  1051. doc.version
  1052. )
  1053. );
  1054. expect( selection.hasAttribute( 'foo' ) ).to.be.false;
  1055. expect( spyAttribute.called ).to.be.false;
  1056. } );
  1057. } );
  1058. describe( 'MoveOperation to graveyard', () => {
  1059. it( 'fix selection range if it ends up in graveyard #1', () => {
  1060. selection._setTo( new Position( root, [ 1, 3 ] ) );
  1061. model.applyOperation(
  1062. new MoveOperation(
  1063. new Position( root, [ 1, 2 ] ),
  1064. 2,
  1065. new Position( doc.graveyard, [ 0 ] ),
  1066. doc.version
  1067. )
  1068. );
  1069. expect( selection.getFirstPosition().path ).to.deep.equal( [ 1, 2 ] );
  1070. } );
  1071. it( 'fix selection range if it ends up in graveyard #2', () => {
  1072. selection._setTo( [ new Range( new Position( root, [ 1, 2 ] ), new Position( root, [ 1, 4 ] ) ) ] );
  1073. model.applyOperation(
  1074. new MoveOperation(
  1075. new Position( root, [ 1, 2 ] ),
  1076. 2,
  1077. new Position( doc.graveyard, [ 0 ] ),
  1078. doc.version
  1079. )
  1080. );
  1081. expect( selection.getFirstPosition().path ).to.deep.equal( [ 1, 2 ] );
  1082. } );
  1083. it( 'fix selection range if it ends up in graveyard #3', () => {
  1084. selection._setTo( [ new Range( new Position( root, [ 1, 1 ] ), new Position( root, [ 1, 2 ] ) ) ] );
  1085. model.applyOperation(
  1086. new MoveOperation(
  1087. new Position( root, [ 1 ] ),
  1088. 2,
  1089. new Position( doc.graveyard, [ 0 ] ),
  1090. doc.version
  1091. )
  1092. );
  1093. expect( selection.getFirstPosition().path ).to.deep.equal( [ 0, 6 ] );
  1094. } );
  1095. it( 'fix selection range if it ends up in graveyard #4 - whole content removed', () => {
  1096. model.applyOperation(
  1097. new MoveOperation(
  1098. new Position( root, [ 0 ] ),
  1099. 3,
  1100. new Position( doc.graveyard, [ 0 ] ),
  1101. doc.version
  1102. )
  1103. );
  1104. expect( selection.getFirstPosition().path ).to.deep.equal( [ 0 ] );
  1105. model.applyOperation(
  1106. new InsertOperation(
  1107. new Position( root, [ 0 ] ),
  1108. new Element( 'p' ),
  1109. doc.version
  1110. )
  1111. );
  1112. // Now it's clear that it's the default range.
  1113. expect( selection.getFirstPosition().path ).to.deep.equal( [ 0, 0 ] );
  1114. } );
  1115. } );
  1116. it( '`DocumentSelection#change:range` event should be fire once even if selection contains multi-ranges', () => {
  1117. root._removeChildren( 0, root.childCount );
  1118. root._insertChild( 0, [
  1119. new Element( 'p', [], new Text( 'abcdef' ) ),
  1120. new Element( 'p', [], new Text( 'foobar' ) ),
  1121. new Text( 'xyz #2' )
  1122. ] );
  1123. selection._setTo( [
  1124. Range._createIn( root.getNodeByPath( [ 0 ] ) ),
  1125. Range._createIn( root.getNodeByPath( [ 1 ] ) )
  1126. ] );
  1127. spyRange = sinon.spy();
  1128. selection.on( 'change:range', spyRange );
  1129. model.applyOperation(
  1130. new InsertOperation(
  1131. new Position( root, [ 0 ] ),
  1132. 'xyz #1',
  1133. doc.version
  1134. )
  1135. );
  1136. expect( spyRange.calledOnce ).to.be.true;
  1137. } );
  1138. } );
  1139. it( 'should throw if one of ranges starts or ends inside surrogate pair', () => {
  1140. root._removeChildren( 0, root.childCount );
  1141. root._appendChild( '\uD83D\uDCA9' );
  1142. expect( () => {
  1143. doc.selection._setTo( new Range( Position._createAt( root, 0 ), Position._createAt( root, 1 ) ) );
  1144. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  1145. expect( () => {
  1146. doc.selection._setTo( new Range( Position._createAt( root, 1 ), Position._createAt( root, 2 ) ) );
  1147. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  1148. } );
  1149. it( 'should throw if one of ranges starts or ends between base character and combining mark', () => {
  1150. root._removeChildren( 0, root.childCount );
  1151. root._appendChild( 'foo̻̐ͩbar' );
  1152. expect( () => {
  1153. doc.selection._setTo( new Range( Position._createAt( root, 3 ), Position._createAt( root, 9 ) ) );
  1154. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  1155. expect( () => {
  1156. doc.selection._setTo( new Range( Position._createAt( root, 4 ), Position._createAt( root, 9 ) ) );
  1157. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  1158. expect( () => {
  1159. doc.selection._setTo( new Range( Position._createAt( root, 5 ), Position._createAt( root, 9 ) ) );
  1160. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  1161. expect( () => {
  1162. doc.selection._setTo( new Range( Position._createAt( root, 1 ), Position._createAt( root, 3 ) ) );
  1163. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  1164. expect( () => {
  1165. doc.selection._setTo( new Range( Position._createAt( root, 1 ), Position._createAt( root, 4 ) ) );
  1166. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  1167. expect( () => {
  1168. doc.selection._setTo( new Range( Position._createAt( root, 1 ), Position._createAt( root, 5 ) ) );
  1169. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  1170. } );
  1171. } );