documentselection.js 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270
  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.appendChildren( [
  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.insertChildren( 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.insertChildren( 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.insertChildren( 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.insertChildren( 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.insertChildren( 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 );
  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.appendChildren( [
  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. } );
  333. describe( '_getStoredAttributes()', () => {
  334. it( 'should return no values if there are no ranges in selection', () => {
  335. const values = Array.from( selection._getStoredAttributes() );
  336. expect( values ).to.deep.equal( [] );
  337. } );
  338. } );
  339. describe( 'are updated on a direct range change', () => {
  340. beforeEach( () => {
  341. root.insertChildren( 0, [
  342. new Element( 'p', { p: true } ),
  343. new Text( 'a', { a: true } ),
  344. new Element( 'p', { p: true } ),
  345. new Text( 'b', { b: true } ),
  346. new Text( 'c', { c: true } ),
  347. new Element( 'p', [], [
  348. new Text( 'd', { d: true } )
  349. ] ),
  350. new Element( 'p', { p: true } ),
  351. new Text( 'e', { e: true } )
  352. ] );
  353. } );
  354. it( 'if selection is a range, should find first character in it and copy it\'s attributes', () => {
  355. selection._setTo( [ new Range( new Position( root, [ 2 ] ), new Position( root, [ 5 ] ) ) ] );
  356. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'b', true ] ] );
  357. // Step into elements when looking for first character:
  358. selection._setTo( [ new Range( new Position( root, [ 5 ] ), new Position( root, [ 7 ] ) ) ] );
  359. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'd', true ] ] );
  360. } );
  361. it( 'if selection is collapsed it should seek a character to copy that character\'s attributes', () => {
  362. // Take styles from character before selection.
  363. selection._setTo( [ new Range( new Position( root, [ 2 ] ), new Position( root, [ 2 ] ) ) ] );
  364. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'a', true ] ] );
  365. // If there are none,
  366. // Take styles from character after selection.
  367. selection._setTo( [ new Range( new Position( root, [ 3 ] ), new Position( root, [ 3 ] ) ) ] );
  368. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'b', true ] ] );
  369. // If there are none,
  370. // Look from the selection position to the beginning of node looking for character to take attributes from.
  371. selection._setTo( [ new Range( new Position( root, [ 6 ] ), new Position( root, [ 6 ] ) ) ] );
  372. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'c', true ] ] );
  373. // If there are none,
  374. // Look from the selection position to the end of node looking for character to take attributes from.
  375. selection._setTo( [ new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) ) ] );
  376. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'a', true ] ] );
  377. // If there are no characters to copy attributes from, use stored attributes.
  378. selection._setTo( [ new Range( new Position( root, [ 0, 0 ] ), new Position( root, [ 0, 0 ] ) ) ] );
  379. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [] );
  380. } );
  381. it( 'should overwrite any previously set attributes', () => {
  382. selection._setTo( new Position( root, [ 5, 0 ] ) );
  383. selection._setAttribute( 'x', true );
  384. selection._setAttribute( 'y', true );
  385. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'd', true ], [ 'x', true ], [ 'y', true ] ] );
  386. selection._setTo( new Position( root, [ 1 ] ) );
  387. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'a', true ] ] );
  388. } );
  389. it( 'should fire change:attribute event', () => {
  390. const spy = sinon.spy();
  391. selection.on( 'change:attribute', spy );
  392. selection._setTo( [ new Range( new Position( root, [ 2 ] ), new Position( root, [ 5 ] ) ) ] );
  393. expect( spy.calledOnce ).to.be.true;
  394. } );
  395. it( 'should not fire change:attribute event if attributes did not change', () => {
  396. selection._setTo( new Position( root, [ 5, 0 ] ) );
  397. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'd', true ] ] );
  398. const spy = sinon.spy();
  399. selection.on( 'change:attribute', spy );
  400. selection._setTo( new Position( root, [ 5, 1 ] ) );
  401. expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [ [ 'd', true ] ] );
  402. expect( spy.called ).to.be.false;
  403. } );
  404. } );
  405. // #986
  406. describe( 'are not inherited from the inside of object elements', () => {
  407. beforeEach( () => {
  408. model.schema.register( 'image', {
  409. isObject: true
  410. } );
  411. model.schema.extend( 'image', { allowIn: '$root' } );
  412. model.schema.extend( 'image', { allowIn: '$block' } );
  413. model.schema.register( 'caption' );
  414. model.schema.extend( 'caption', { allowIn: 'image' } );
  415. model.schema.extend( '$text', {
  416. allowIn: [ 'image', 'caption' ],
  417. allowAttributes: 'bold'
  418. } );
  419. } );
  420. it( 'ignores attributes inside an object if selection contains that object', () => {
  421. setData( model, '<p>[<image><$text bold="true">Caption for the image.</$text></image>]</p>' );
  422. expect( selection.hasAttribute( 'bold' ) ).to.equal( false );
  423. } );
  424. it( 'ignores attributes inside an object if selection contains that object (deeper structure)', () => {
  425. setData( model, '<p>[<image><caption><$text bold="true">Caption for the image.</$text></caption></image>]</p>' );
  426. expect( selection.hasAttribute( 'bold' ) ).to.equal( false );
  427. } );
  428. it( 'ignores attributes inside an object if selection contains that object (block level)', () => {
  429. setData( model, '<p>foo</p>[<image><$text bold="true">Caption for the image.</$text></image>]<p>foo</p>' );
  430. expect( selection.hasAttribute( 'bold' ) ).to.equal( false );
  431. } );
  432. it( 'reads attributes from text even if the selection contains an object', () => {
  433. setData( model, '<p>x<$text bold="true">[bar</$text><image></image>foo]</p>' );
  434. expect( selection.getAttribute( 'bold' ) ).to.equal( true );
  435. } );
  436. it( 'reads attributes when the entire selection inside an object', () => {
  437. setData( model, '<p><image><caption><$text bold="true">[bar]</$text></caption></image></p>' );
  438. expect( selection.getAttribute( 'bold' ) ).to.equal( true );
  439. } );
  440. it( 'stops reading attributes if selection starts with an object', () => {
  441. setData( model, '<p>[<image></image><$text bold="true">bar]</$text></p>' );
  442. expect( selection.hasAttribute( 'bold' ) ).to.equal( false );
  443. } );
  444. } );
  445. describe( 'parent element\'s attributes', () => {
  446. it( 'are set using a normal batch', () => {
  447. const batchTypes = [];
  448. model.on( 'applyOperation', ( event, args ) => {
  449. const operation = args[ 0 ];
  450. const batch = operation.delta.batch;
  451. batchTypes.push( batch.type );
  452. } );
  453. selection._setTo( [ rangeInEmptyP ] );
  454. model.change( writer => {
  455. writer.setSelectionAttribute( 'foo', 'bar' );
  456. } );
  457. expect( batchTypes ).to.deep.equal( [ 'default' ] );
  458. expect( emptyP.getAttribute( fooStoreAttrKey ) ).to.equal( 'bar' );
  459. } );
  460. it( 'are removed when any content is inserted (reuses the same batch)', () => {
  461. // Dedupe batches by using a map (multiple change events will be fired).
  462. const batchTypes = new Map();
  463. selection._setTo( rangeInEmptyP );
  464. selection._setAttribute( 'foo', 'bar' );
  465. selection._setAttribute( 'abc', 'bar' );
  466. model.on( 'applyOperation', ( event, args ) => {
  467. const operation = args[ 0 ];
  468. const batch = operation.delta.batch;
  469. batchTypes.set( batch, batch.type );
  470. } );
  471. model.change( writer => {
  472. writer.insertText( 'x', rangeInEmptyP.start );
  473. } );
  474. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  475. expect( emptyP.hasAttribute( abcStoreAttrKey ) ).to.be.false;
  476. expect( Array.from( batchTypes.values() ) ).to.deep.equal( [ 'default' ] );
  477. } );
  478. it( 'are removed when any content is moved into', () => {
  479. selection._setTo( rangeInEmptyP );
  480. selection._setAttribute( 'foo', 'bar' );
  481. model.change( writer => {
  482. writer.move( Range.createOn( fullP.getChild( 0 ) ), rangeInEmptyP.start );
  483. } );
  484. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  485. } );
  486. it( 'are removed when containing element is merged with a non-empty element', () => {
  487. const emptyP2 = new Element( 'p', null, 'x' );
  488. root.appendChildren( emptyP2 );
  489. emptyP.setAttribute( fooStoreAttrKey, 'bar' );
  490. emptyP2.setAttribute( fooStoreAttrKey, 'bar' );
  491. model.change( writer => {
  492. // <emptyP>{}<emptyP2>
  493. writer.merge( Position.createAfter( emptyP ) );
  494. } );
  495. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  496. expect( emptyP.parent ).to.equal( root ); // Just to be sure we're checking the right element.
  497. } );
  498. it( 'are removed even when there is no selection in it', () => {
  499. emptyP.setAttribute( fooStoreAttrKey, 'bar' );
  500. selection._setTo( [ rangeInFullP ] );
  501. model.change( writer => {
  502. writer.insertText( 'x', rangeInEmptyP.start );
  503. } );
  504. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  505. } );
  506. it( 'are removed only once in case of multi-op deltas', () => {
  507. let batch;
  508. const emptyP2 = new Element( 'p', null, 'x' );
  509. root.appendChildren( emptyP2 );
  510. emptyP.setAttribute( fooStoreAttrKey, 'bar' );
  511. emptyP2.setAttribute( fooStoreAttrKey, 'bar' );
  512. model.change( writer => {
  513. batch = writer.batch;
  514. // <emptyP>{}<emptyP2>
  515. writer.merge( Position.createAfter( emptyP ) );
  516. } );
  517. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  518. // Attribute delta is only one.
  519. expect( Array.from( batch.deltas, delta => delta.type ) ).to.deep.equal( [ 'merge', 'attribute' ] );
  520. } );
  521. it( 'uses model change to clear attributes', () => {
  522. selection._setTo( [ rangeInEmptyP ] );
  523. model.change( writer => {
  524. writer.setSelectionAttribute( 'foo', 'bar' );
  525. writer.insertText( 'x', rangeInEmptyP.start );
  526. // `emptyP` still has the attribute, because attribute clearing is in enqueued block.
  527. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.true;
  528. } );
  529. // When the dust settles, `emptyP` should not have the attribute.
  530. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
  531. } );
  532. it( 'are not removed or merged when containing element is merged with another empty element', () => {
  533. const emptyP2 = new Element( 'p', null );
  534. root.appendChildren( emptyP2 );
  535. emptyP.setAttribute( fooStoreAttrKey, 'bar' );
  536. emptyP2.setAttribute( abcStoreAttrKey, 'bar' );
  537. expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.true;
  538. expect( emptyP.hasAttribute( abcStoreAttrKey ) ).to.be.false;
  539. model.change( writer => {
  540. // <emptyP>{}<emptyP2>
  541. writer.merge( Position.createAfter( emptyP ) );
  542. } );
  543. expect( emptyP.getAttribute( fooStoreAttrKey ) ).to.equal( 'bar' );
  544. expect( emptyP.parent ).to.equal( root ); // Just to be sure we're checking the right element.
  545. } );
  546. // Rename and some other deltas don't specify range in doc#change event.
  547. // So let's see if there's no crash or something.
  548. it( 'are not removed on rename', () => {
  549. model.change( writer => {
  550. writer.setSelection( rangeInEmptyP );
  551. writer.setSelectionAttribute( 'foo', 'bar' );
  552. } );
  553. sinon.spy( model, 'enqueueChange' );
  554. model.change( writer => {
  555. writer.rename( emptyP, 'pnew' );
  556. } );
  557. expect( model.enqueueChange.called ).to.be.false;
  558. expect( emptyP.getAttribute( fooStoreAttrKey ) ).to.equal( 'bar' );
  559. } );
  560. } );
  561. } );
  562. describe( '_overrideGravity()', () => {
  563. beforeEach( () => {
  564. model.schema.extend( '$text', {
  565. allowIn: '$root'
  566. } );
  567. } );
  568. it( 'should mark gravity as overridden', () => {
  569. expect( selection.isGravityOverridden ).to.false;
  570. selection._overrideGravity();
  571. expect( selection.isGravityOverridden ).to.true;
  572. } );
  573. it( 'should not inherit attributes from node before the caret', () => {
  574. setData( model, '<$text bold="true" italic="true">foo[]</$text>' );
  575. expect( Array.from( selection.getAttributeKeys() ) ).to.have.members( [ 'bold', 'italic' ] );
  576. selection._overrideGravity();
  577. expect( Array.from( selection.getAttributeKeys() ) ).to.length( 0 );
  578. } );
  579. it( 'should inherit attributes from node after the caret', () => {
  580. setData( model, '<$text>foo[]</$text><$text bold="true" italic="true">bar</$text>' );
  581. expect( Array.from( selection.getAttributeKeys() ) ).to.length( 0 );
  582. selection._overrideGravity();
  583. expect( Array.from( selection.getAttributeKeys() ) ).to.have.members( [ 'bold', 'italic' ] );
  584. } );
  585. it( 'should retain attributes that are set explicit', () => {
  586. setData( model, '<$text italic="true">foo[]</$text>' );
  587. selection._setAttribute( 'bold', true );
  588. expect( Array.from( selection.getAttributeKeys() ) ).to.have.members( [ 'bold', 'italic' ] );
  589. selection._overrideGravity();
  590. expect( Array.from( selection.getAttributeKeys() ) ).to.have.members( [ 'bold' ] );
  591. } );
  592. it( 'should retain overridden until selection will not change range by a direct change', () => {
  593. setData( model, '<$text bold="true" italic="true">foo[]</$text><$text italic="true">bar</$text>' );
  594. selection._overrideGravity();
  595. // Changed range but not directly.
  596. model.change( writer => writer.insertText( 'abc', new Position( root, [ 0 ] ) ) );
  597. expect( Array.from( selection.getAttributeKeys() ) ).to.have.members( [ 'italic' ] );
  598. // Changed range directly.
  599. model.change( writer => writer.setSelection( new Position( root, [ 5 ] ) ) );
  600. expect( Array.from( selection.getAttributeKeys() ) ).to.have.members( [ 'bold', 'italic' ] );
  601. } );
  602. } );
  603. describe( '_restoreGravity()', () => {
  604. beforeEach( () => {
  605. model.schema.extend( '$text', {
  606. allowIn: '$root'
  607. } );
  608. } );
  609. it( 'should revert default gravity when is overridden', () => {
  610. setData( model, '<$text bold="true" italic="true">foo[]</$text>' );
  611. selection._overrideGravity();
  612. expect( Array.from( selection.getAttributeKeys() ) ).to.length( 0 );
  613. expect( selection.isGravityOverridden ).to.true;
  614. selection._restoreGravity();
  615. expect( Array.from( selection.getAttributeKeys() ) ).to.have.members( [ 'bold', 'italic' ] );
  616. expect( selection.isGravityOverridden ).to.false;
  617. } );
  618. it( 'should do nothing when gravity is not overridden', () => {
  619. setData( model, '<$text bold="true" italic="true">foo[]</$text>' );
  620. expect( () => {
  621. selection._restoreGravity();
  622. } ).to.not.throw();
  623. expect( selection.isGravityOverridden ).to.false;
  624. } );
  625. it( 'should be called the same number of times as gravity is overridden to restore it', () => {
  626. setData( model, '<$text bold="true" italic="true">foo[]</$text>' );
  627. selection._overrideGravity();
  628. selection._overrideGravity();
  629. expect( selection.isGravityOverridden ).to.true;
  630. selection._restoreGravity();
  631. expect( selection.isGravityOverridden ).to.true;
  632. selection._restoreGravity();
  633. expect( selection.isGravityOverridden ).to.false;
  634. } );
  635. } );
  636. // DocumentSelection uses LiveRanges so here are only simple test to see if integration is
  637. // working well, without getting into complicated corner cases.
  638. describe( 'after applying an operation should get updated and fire events', () => {
  639. let spyRange;
  640. beforeEach( () => {
  641. root.removeChildren( 0, root.childCount );
  642. root.insertChildren( 0, [
  643. new Element( 'p', [], new Text( 'abcdef' ) ),
  644. new Element( 'p', [], new Text( 'foobar' ) ),
  645. new Text( 'xyz' )
  646. ] );
  647. selection._setTo( new Range( new Position( root, [ 0, 2 ] ), new Position( root, [ 1, 4 ] ) ) );
  648. spyRange = sinon.spy();
  649. selection.on( 'change:range', spyRange );
  650. } );
  651. describe( 'InsertOperation', () => {
  652. it( 'before selection', () => {
  653. selection.on( 'change:range', ( evt, data ) => {
  654. expect( data.directChange ).to.be.false;
  655. } );
  656. model.applyOperation( wrapInDelta(
  657. new InsertOperation(
  658. new Position( root, [ 0, 1 ] ),
  659. 'xyz',
  660. doc.version
  661. )
  662. ) );
  663. const range = selection.getFirstRange();
  664. expect( range.start.path ).to.deep.equal( [ 0, 5 ] );
  665. expect( range.end.path ).to.deep.equal( [ 1, 4 ] );
  666. expect( spyRange.calledOnce ).to.be.true;
  667. } );
  668. it( 'inside selection', () => {
  669. selection.on( 'change:range', ( evt, data ) => {
  670. expect( data.directChange ).to.be.false;
  671. } );
  672. model.applyOperation( wrapInDelta(
  673. new InsertOperation(
  674. new Position( root, [ 1, 0 ] ),
  675. 'xyz',
  676. doc.version
  677. )
  678. ) );
  679. const range = selection.getFirstRange();
  680. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  681. expect( range.end.path ).to.deep.equal( [ 1, 7 ] );
  682. expect( spyRange.calledOnce ).to.be.true;
  683. } );
  684. } );
  685. describe( 'MoveOperation', () => {
  686. it( 'move range from before a selection', () => {
  687. selection.on( 'change:range', ( evt, data ) => {
  688. expect( data.directChange ).to.be.false;
  689. } );
  690. model.applyOperation( wrapInDelta(
  691. new MoveOperation(
  692. new Position( root, [ 0, 0 ] ),
  693. 2,
  694. new Position( root, [ 2 ] ),
  695. doc.version
  696. )
  697. ) );
  698. const range = selection.getFirstRange();
  699. expect( range.start.path ).to.deep.equal( [ 0, 0 ] );
  700. expect( range.end.path ).to.deep.equal( [ 1, 4 ] );
  701. expect( spyRange.calledOnce ).to.be.true;
  702. } );
  703. it( 'moved into before a selection', () => {
  704. selection.on( 'change:range', ( evt, data ) => {
  705. expect( data.directChange ).to.be.false;
  706. } );
  707. model.applyOperation( wrapInDelta(
  708. new MoveOperation(
  709. new Position( root, [ 2 ] ),
  710. 2,
  711. new Position( root, [ 0, 0 ] ),
  712. doc.version
  713. )
  714. ) );
  715. const range = selection.getFirstRange();
  716. expect( range.start.path ).to.deep.equal( [ 0, 4 ] );
  717. expect( range.end.path ).to.deep.equal( [ 1, 4 ] );
  718. expect( spyRange.calledOnce ).to.be.true;
  719. } );
  720. it( 'move range from inside of selection', () => {
  721. selection.on( 'change:range', ( evt, data ) => {
  722. expect( data.directChange ).to.be.false;
  723. } );
  724. model.applyOperation( wrapInDelta(
  725. new MoveOperation(
  726. new Position( root, [ 1, 0 ] ),
  727. 2,
  728. new Position( root, [ 2 ] ),
  729. doc.version
  730. )
  731. ) );
  732. const range = selection.getFirstRange();
  733. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  734. expect( range.end.path ).to.deep.equal( [ 1, 2 ] );
  735. expect( spyRange.calledOnce ).to.be.true;
  736. } );
  737. it( 'moved range intersects with selection', () => {
  738. selection.on( 'change:range', ( evt, data ) => {
  739. expect( data.directChange ).to.be.false;
  740. } );
  741. model.applyOperation( wrapInDelta(
  742. new MoveOperation(
  743. new Position( root, [ 1, 3 ] ),
  744. 2,
  745. new Position( root, [ 4 ] ),
  746. doc.version
  747. )
  748. ) );
  749. const range = selection.getFirstRange();
  750. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  751. expect( range.end.path ).to.deep.equal( [ 5 ] );
  752. expect( spyRange.calledOnce ).to.be.true;
  753. } );
  754. it( 'split inside selection (do not break selection)', () => {
  755. selection.on( 'change:range', ( evt, data ) => {
  756. expect( data.directChange ).to.be.false;
  757. } );
  758. const batch = new Batch();
  759. const splitDelta = new SplitDelta();
  760. const insertOperation = new InsertOperation(
  761. new Position( root, [ 2 ] ),
  762. new Element( 'p' ),
  763. 0
  764. );
  765. const moveOperation = new MoveOperation(
  766. new Position( root, [ 1, 2 ] ),
  767. 4,
  768. new Position( root, [ 2, 0 ] ),
  769. 1
  770. );
  771. batch.addDelta( splitDelta );
  772. splitDelta.addOperation( insertOperation );
  773. splitDelta.addOperation( moveOperation );
  774. model.applyOperation( insertOperation );
  775. model.applyOperation( moveOperation );
  776. const range = selection.getFirstRange();
  777. expect( range.start.path ).to.deep.equal( [ 0, 2 ] );
  778. expect( range.end.path ).to.deep.equal( [ 2, 2 ] );
  779. expect( spyRange.calledOnce ).to.be.true;
  780. } );
  781. } );
  782. describe( 'AttributeOperation', () => {
  783. it( 'changed range includes selection anchor', () => {
  784. const spyAttribute = sinon.spy();
  785. selection.on( 'change:attribute', spyAttribute );
  786. selection.on( 'change:attribute', ( evt, data ) => {
  787. expect( data.directChange ).to.be.false;
  788. expect( data.attributeKeys ).to.deep.equal( [ 'foo' ] );
  789. } );
  790. model.change( writer => {
  791. const range = new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 5 ] ) );
  792. writer.setAttribute( 'foo', 'bar', range );
  793. } );
  794. expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  795. expect( spyAttribute.calledOnce ).to.be.true;
  796. } );
  797. it( 'should not overwrite previously set attributes', () => {
  798. selection._setAttribute( 'foo', 'xyz' );
  799. const spyAttribute = sinon.spy();
  800. selection.on( 'change:attribute', spyAttribute );
  801. model.applyOperation( wrapInDelta(
  802. new AttributeOperation(
  803. new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 5 ] ) ),
  804. 'foo',
  805. null,
  806. 'bar',
  807. doc.version
  808. )
  809. ) );
  810. expect( selection.getAttribute( 'foo' ) ).to.equal( 'xyz' );
  811. expect( spyAttribute.called ).to.be.false;
  812. } );
  813. it( 'should not overwrite previously set attributes with same values', () => {
  814. selection._setAttribute( 'foo', 'xyz' );
  815. const spyAttribute = sinon.spy();
  816. selection.on( 'change:attribute', spyAttribute );
  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. 'xyz',
  823. doc.version
  824. )
  825. ) );
  826. expect( selection.getAttribute( 'foo' ) ).to.equal( 'xyz' );
  827. expect( spyAttribute.called ).to.be.false;
  828. } );
  829. it( 'should not overwrite previously removed attributes', () => {
  830. selection._setAttribute( 'foo', 'xyz' );
  831. selection._removeAttribute( 'foo' );
  832. const spyAttribute = sinon.spy();
  833. selection.on( 'change:attribute', spyAttribute );
  834. model.applyOperation( wrapInDelta(
  835. new AttributeOperation(
  836. new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 5 ] ) ),
  837. 'foo',
  838. null,
  839. 'bar',
  840. doc.version
  841. )
  842. ) );
  843. expect( selection.hasAttribute( 'foo' ) ).to.be.false;
  844. expect( spyAttribute.called ).to.be.false;
  845. } );
  846. } );
  847. describe( 'RemoveOperation', () => {
  848. it( 'fix selection range if it ends up in graveyard #1', () => {
  849. selection._setTo( new Position( root, [ 1, 3 ] ) );
  850. model.applyOperation( wrapInDelta(
  851. new RemoveOperation(
  852. new Position( root, [ 1, 2 ] ),
  853. 2,
  854. new Position( doc.graveyard, [ 0 ] ),
  855. doc.version
  856. )
  857. ) );
  858. expect( selection.getFirstPosition().path ).to.deep.equal( [ 1, 2 ] );
  859. } );
  860. it( 'fix selection range if it ends up in graveyard #2', () => {
  861. selection._setTo( [ new Range( new Position( root, [ 1, 2 ] ), new Position( root, [ 1, 4 ] ) ) ] );
  862. model.applyOperation( wrapInDelta(
  863. new RemoveOperation(
  864. new Position( root, [ 1, 2 ] ),
  865. 2,
  866. new Position( doc.graveyard, [ 0 ] ),
  867. doc.version
  868. )
  869. ) );
  870. expect( selection.getFirstPosition().path ).to.deep.equal( [ 1, 2 ] );
  871. } );
  872. it( 'fix selection range if it ends up in graveyard #3', () => {
  873. selection._setTo( [ new Range( new Position( root, [ 1, 1 ] ), new Position( root, [ 1, 2 ] ) ) ] );
  874. model.applyOperation( wrapInDelta(
  875. new RemoveOperation(
  876. new Position( root, [ 1 ] ),
  877. 2,
  878. new Position( doc.graveyard, [ 0 ] ),
  879. doc.version
  880. )
  881. ) );
  882. expect( selection.getFirstPosition().path ).to.deep.equal( [ 0, 6 ] );
  883. } );
  884. it( 'fix selection range if it ends up in graveyard #4 - whole content removed', () => {
  885. model.applyOperation( wrapInDelta(
  886. new RemoveOperation(
  887. new Position( root, [ 0 ] ),
  888. 3,
  889. new Position( doc.graveyard, [ 0 ] ),
  890. doc.version
  891. )
  892. ) );
  893. expect( selection.getFirstPosition().path ).to.deep.equal( [ 0 ] );
  894. model.applyOperation( wrapInDelta(
  895. new InsertOperation(
  896. new Position( root, [ 0 ] ),
  897. new Element( 'p' ),
  898. doc.version
  899. )
  900. ) );
  901. // Now it's clear that it's the default range.
  902. expect( selection.getFirstPosition().path ).to.deep.equal( [ 0, 0 ] );
  903. } );
  904. } );
  905. it( '`DocumentSelection#change:range` event should be fire once even if selection contains multi-ranges', () => {
  906. root.removeChildren( 0, root.childCount );
  907. root.insertChildren( 0, [
  908. new Element( 'p', [], new Text( 'abcdef' ) ),
  909. new Element( 'p', [], new Text( 'foobar' ) ),
  910. new Text( 'xyz #2' )
  911. ] );
  912. selection._setTo( [
  913. Range.createIn( root.getNodeByPath( [ 0 ] ) ),
  914. Range.createIn( root.getNodeByPath( [ 1 ] ) )
  915. ] );
  916. spyRange = sinon.spy();
  917. selection.on( 'change:range', spyRange );
  918. model.applyOperation( wrapInDelta(
  919. new InsertOperation(
  920. new Position( root, [ 0 ] ),
  921. 'xyz #1',
  922. doc.version
  923. )
  924. ) );
  925. expect( spyRange.calledOnce ).to.be.true;
  926. } );
  927. } );
  928. it( 'should throw if one of ranges starts or ends inside surrogate pair', () => {
  929. root.removeChildren( 0, root.childCount );
  930. root.appendChildren( '\uD83D\uDCA9' );
  931. expect( () => {
  932. doc.selection._setTo( Range.createFromParentsAndOffsets( root, 0, root, 1 ) );
  933. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  934. expect( () => {
  935. doc.selection._setTo( Range.createFromParentsAndOffsets( root, 1, root, 2 ) );
  936. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  937. } );
  938. it( 'should throw if one of ranges starts or ends between base character and combining mark', () => {
  939. root.removeChildren( 0, root.childCount );
  940. root.appendChildren( 'foo̻̐ͩbar' );
  941. expect( () => {
  942. doc.selection._setTo( Range.createFromParentsAndOffsets( root, 3, root, 9 ) );
  943. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  944. expect( () => {
  945. doc.selection._setTo( Range.createFromParentsAndOffsets( root, 4, root, 9 ) );
  946. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  947. expect( () => {
  948. doc.selection._setTo( Range.createFromParentsAndOffsets( root, 5, root, 9 ) );
  949. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  950. expect( () => {
  951. doc.selection._setTo( Range.createFromParentsAndOffsets( root, 1, root, 3 ) );
  952. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  953. expect( () => {
  954. doc.selection._setTo( Range.createFromParentsAndOffsets( root, 1, root, 4 ) );
  955. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  956. expect( () => {
  957. doc.selection._setTo( Range.createFromParentsAndOffsets( root, 1, root, 5 ) );
  958. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  959. } );
  960. } );