8
0

documentselection.js 52 KB

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