documentselection.js 40 KB

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