8
0

documentselection.js 42 KB

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