8
0

documentselection.js 38 KB

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