documentselection.js 40 KB

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