8
0

documentselection.js 50 KB

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