selection.js 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import Model from '../../src/model/model';
  6. import Element from '../../src/model/element';
  7. import Text from '../../src/model/text';
  8. import Range from '../../src/model/range';
  9. import Position from '../../src/model/position';
  10. import LiveRange from '../../src/model/liverange';
  11. import Selection from '../../src/model/selection';
  12. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  13. import count from '@ckeditor/ckeditor5-utils/src/count';
  14. import { parse, setData } from '../../src/dev-utils/model';
  15. import Schema from '../../src/model/schema';
  16. import { expectToThrowCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  17. describe( 'Selection', () => {
  18. let model, doc, root, selection, liveRange, range, range1, range2, range3;
  19. testUtils.createSinonSandbox();
  20. beforeEach( () => {
  21. model = new Model();
  22. doc = model.document;
  23. root = doc.createRoot();
  24. root._appendChild( [
  25. new Element( 'p' ),
  26. new Element( 'p' ),
  27. new Element( 'p', [], new Text( 'foobar' ) ),
  28. new Element( 'p' ),
  29. new Element( 'p' ),
  30. new Element( 'p' ),
  31. new Element( 'p', [], new Text( 'foobar' ) )
  32. ] );
  33. selection = new Selection();
  34. liveRange = new LiveRange( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) );
  35. range = new Range( new Position( root, [ 2 ] ), new Position( root, [ 2, 2 ] ) );
  36. range1 = new Range( new Position( root, [ 1 ] ), new Position( root, [ 4 ] ) );
  37. range2 = new Range( new Position( root, [ 4 ] ), new Position( root, [ 5 ] ) );
  38. range3 = new Range( new Position( root, [ 6 ] ), new Position( root, [ 7 ] ) );
  39. } );
  40. afterEach( () => {
  41. model.destroy();
  42. liveRange.detach();
  43. } );
  44. describe( 'constructor()', () => {
  45. it( 'should be able to create an empty selection', () => {
  46. const selection = new Selection();
  47. expect( Array.from( selection.getRanges() ) ).to.deep.equal( [] );
  48. } );
  49. it( 'should be able to create a selection from the given ranges', () => {
  50. const ranges = [ range1, range2, range3 ];
  51. const selection = new Selection( ranges );
  52. expect( Array.from( selection.getRanges() ) ).to.deep.equal( ranges );
  53. } );
  54. it( 'should be able to create a selection from the given range and isLastBackward flag', () => {
  55. const selection = new Selection( range1, { backward: true } );
  56. expect( selection.isBackward ).to.be.true;
  57. expect( Array.from( selection.getRanges() ) ).to.deep.equal( [ range1 ] );
  58. } );
  59. it( 'should be able to create a selection from the given ranges and isLastBackward flag', () => {
  60. const ranges = new Set( [ range1, range2, range3 ] );
  61. const selection = new Selection( ranges, { backward: true } );
  62. expect( selection.isBackward ).to.be.true;
  63. expect( Array.from( selection.getRanges() ) ).to.deep.equal( [ range1, range2, range3 ] );
  64. } );
  65. it( 'should be able to create a selection from the other selection', () => {
  66. const ranges = [ range1, range2, range3 ];
  67. const otherSelection = new Selection( ranges, { backward: true } );
  68. const selection = new Selection( otherSelection );
  69. expect( selection.isBackward ).to.be.true;
  70. expect( Array.from( selection.getRanges() ) ).to.deep.equal( [ range1, range2, range3 ] );
  71. } );
  72. it( 'should be able to create a selection at the start position of an item', () => {
  73. const selection = new Selection( root, 0 );
  74. const focus = selection.focus;
  75. expect( selection ).to.have.property( 'isCollapsed', true );
  76. expect( focus ).to.have.property( 'parent', root );
  77. expect( focus ).to.have.property( 'offset', 0 );
  78. } );
  79. it( 'should be able to create a selection before the specified element', () => {
  80. const selection = new Selection( root.getChild( 1 ), 'before' );
  81. const focus = selection.focus;
  82. expect( selection ).to.have.property( 'isCollapsed', true );
  83. expect( focus ).to.have.property( 'parent', root );
  84. expect( focus ).to.have.property( 'offset', 1 );
  85. } );
  86. it( 'should throw an error if added ranges intersects', () => {
  87. expectToThrowCKEditorError( () => {
  88. // eslint-disable-next-line no-new
  89. new Selection( [
  90. liveRange,
  91. new Range(
  92. new Position( root, [ 0, 4 ] ),
  93. new Position( root, [ 1, 2 ] )
  94. )
  95. ] );
  96. }, /model-selection-range-intersects/, model );
  97. } );
  98. it( 'should throw an error when trying to set selection to not selectable', () => {
  99. expectToThrowCKEditorError( () => {
  100. // eslint-disable-next-line no-new
  101. new Selection( {} );
  102. }, 'model-selection-setto-not-selectable' );
  103. } );
  104. } );
  105. describe( 'is()', () => {
  106. let selection;
  107. beforeEach( () => {
  108. selection = new Selection();
  109. } );
  110. it( 'should return true for "selection"', () => {
  111. expect( selection.is( 'selection' ) ).to.be.true;
  112. expect( selection.is( 'model:selection' ) ).to.be.true;
  113. } );
  114. it( 'should return false for incorrect values', () => {
  115. expect( selection.is( 'model' ) ).to.be.false;
  116. expect( selection.is( 'model:node' ) ).to.be.false;
  117. expect( selection.is( '$text' ) ).to.be.false;
  118. expect( selection.is( '$textProxy' ) ).to.be.false;
  119. expect( selection.is( 'element' ) ).to.be.false;
  120. expect( selection.is( 'element', 'paragraph' ) ).to.be.false;
  121. expect( selection.is( 'documentSelection' ) ).to.be.false;
  122. expect( selection.is( 'node' ) ).to.be.false;
  123. expect( selection.is( 'rootElement' ) ).to.be.false;
  124. } );
  125. } );
  126. describe( 'isCollapsed', () => {
  127. it( 'should return false for empty selection', () => {
  128. expect( selection.isCollapsed ).to.be.false;
  129. } );
  130. it( 'should return true when there is single collapsed ranges', () => {
  131. selection.setTo( new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) ) );
  132. expect( selection.isCollapsed ).to.be.true;
  133. } );
  134. it( 'should return false when there are multiple ranges', () => {
  135. selection.setTo( [
  136. new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) ),
  137. new Range( new Position( root, [ 2 ] ), new Position( root, [ 2 ] ) )
  138. ] );
  139. expect( selection.isCollapsed ).to.be.false;
  140. } );
  141. it( 'should return false when there is not collapsed range', () => {
  142. selection.setTo( range );
  143. expect( selection.isCollapsed ).to.be.false;
  144. } );
  145. } );
  146. describe( 'rangeCount', () => {
  147. it( 'should return proper range count', () => {
  148. expect( selection.rangeCount ).to.equal( 0 );
  149. selection.setTo( new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) ) );
  150. expect( selection.rangeCount ).to.equal( 1 );
  151. selection.setTo( [
  152. new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) ),
  153. new Range( new Position( root, [ 2 ] ), new Position( root, [ 2 ] ) )
  154. ] );
  155. expect( selection.rangeCount ).to.equal( 2 );
  156. } );
  157. } );
  158. describe( 'isBackward', () => {
  159. it( 'is defined by the last added range', () => {
  160. selection.setTo( [ range ], { backward: true } );
  161. expect( selection ).to.have.property( 'isBackward', true );
  162. selection.setTo( liveRange );
  163. expect( selection ).to.have.property( 'isBackward', false );
  164. } );
  165. it( 'is false when last range is collapsed', () => {
  166. const pos = Position._createAt( root, 0 );
  167. selection.setTo( pos );
  168. expect( selection.isBackward ).to.be.false;
  169. } );
  170. } );
  171. describe( 'focus', () => {
  172. let r1, r2, r3;
  173. beforeEach( () => {
  174. r1 = new Range( Position._createAt( root, 2 ), Position._createAt( root, 4 ) );
  175. r2 = new Range( Position._createAt( root, 4 ), Position._createAt( root, 6 ) );
  176. r3 = new Range( Position._createAt( root, 1 ), Position._createAt( root, 2 ) );
  177. selection.setTo( [ r1, r2 ] );
  178. } );
  179. it( 'should return correct focus when last added range is not backward one', () => {
  180. selection.setTo( [ r1, r2, r3 ] );
  181. expect( selection.focus.isEqual( r3.end ) ).to.be.true;
  182. } );
  183. it( 'should return correct focus when last added range is backward one', () => {
  184. selection.setTo( [ r1, r2, r3 ], { backward: true } );
  185. expect( selection.focus.isEqual( r3.start ) ).to.be.true;
  186. } );
  187. it( 'should return null if no ranges in selection', () => {
  188. selection.setTo( null );
  189. expect( selection.focus ).to.be.null;
  190. } );
  191. } );
  192. describe( 'setTo()', () => {
  193. it( 'should set selection to be same as given selection, using _setRanges method', () => {
  194. const spy = sinon.spy( selection, '_setRanges' );
  195. const otherSelection = new Selection( [ range1, range2 ], { backward: true } );
  196. selection.setTo( otherSelection );
  197. expect( Array.from( selection.getRanges() ) ).to.deep.equal( [ range1, range2 ] );
  198. expect( selection.isBackward ).to.be.true;
  199. expect( selection._setRanges.calledOnce ).to.be.true;
  200. spy.restore();
  201. } );
  202. it( 'should set selection on the given Range using _setRanges method', () => {
  203. const spy = sinon.spy( selection, '_setRanges' );
  204. selection.setTo( range1 );
  205. expect( Array.from( selection.getRanges() ) ).to.deep.equal( [ range1 ] );
  206. expect( selection.isBackward ).to.be.false;
  207. expect( selection._setRanges.calledOnce ).to.be.true;
  208. spy.restore();
  209. } );
  210. it( 'should set selection on the given iterable of Ranges using _setRanges method', () => {
  211. const spy = sinon.spy( selection, '_setRanges' );
  212. selection.setTo( new Set( [ range1, range2 ] ) );
  213. expect( Array.from( selection.getRanges() ) ).to.deep.equal( [ range1, range2 ] );
  214. expect( selection.isBackward ).to.be.false;
  215. expect( selection._setRanges.calledOnce ).to.be.true;
  216. spy.restore();
  217. } );
  218. it( 'should set collapsed selection on the given Position using _setRanges method', () => {
  219. const spy = sinon.spy( selection, '_setRanges' );
  220. const position = new Position( root, [ 4 ] );
  221. selection.setTo( position );
  222. expect( Array.from( selection.getRanges() ).length ).to.equal( 1 );
  223. expect( Array.from( selection.getRanges() )[ 0 ].start ).to.deep.equal( position );
  224. expect( selection.isBackward ).to.be.false;
  225. expect( selection.isCollapsed ).to.be.true;
  226. expect( selection._setRanges.calledOnce ).to.be.true;
  227. spy.restore();
  228. } );
  229. it( 'should throw an error if added ranges intersects', () => {
  230. expectToThrowCKEditorError( () => {
  231. selection.setTo( [
  232. liveRange,
  233. new Range(
  234. new Position( root, [ 0, 4 ] ),
  235. new Position( root, [ 1, 2 ] )
  236. )
  237. ] );
  238. }, /model-selection-range-intersects/, model );
  239. } );
  240. it( 'should throw an error when trying to set selection to not selectable', () => {
  241. expectToThrowCKEditorError( () => {
  242. selection.setTo( {} );
  243. }, 'model-selection-setto-not-selectable' );
  244. } );
  245. it( 'should throw an error when trying to set selection to not selectable #2', () => {
  246. expectToThrowCKEditorError( () => {
  247. selection.setTo();
  248. }, 'model-selection-setto-not-selectable' );
  249. } );
  250. it( 'should allow setting selection inside an element', () => {
  251. const element = new Element( 'p', null, [ new Text( 'foo' ), new Text( 'bar' ) ] );
  252. selection.setTo( element, 'in' );
  253. const ranges = Array.from( selection.getRanges() );
  254. expect( ranges.length ).to.equal( 1 );
  255. expect( ranges[ 0 ].start.parent ).to.equal( element );
  256. expect( ranges[ 0 ].start.offset ).to.deep.equal( 0 );
  257. expect( ranges[ 0 ].end.parent ).to.equal( element );
  258. expect( ranges[ 0 ].end.offset ).to.deep.equal( 6 );
  259. } );
  260. it( 'should allow setting selection on an item', () => {
  261. const textNode1 = new Text( 'foo' );
  262. const textNode2 = new Text( 'bar' );
  263. const textNode3 = new Text( 'baz' );
  264. const element = new Element( 'p', null, [ textNode1, textNode2, textNode3 ] );
  265. selection.setTo( textNode2, 'on' );
  266. const ranges = Array.from( selection.getRanges() );
  267. expect( ranges.length ).to.equal( 1 );
  268. expect( ranges[ 0 ].start.parent ).to.equal( element );
  269. expect( ranges[ 0 ].start.offset ).to.deep.equal( 3 );
  270. expect( ranges[ 0 ].end.parent ).to.equal( element );
  271. expect( ranges[ 0 ].end.offset ).to.deep.equal( 6 );
  272. } );
  273. it( 'should allow setting backward selection on an item', () => {
  274. const textNode1 = new Text( 'foo' );
  275. const textNode2 = new Text( 'bar' );
  276. const textNode3 = new Text( 'baz' );
  277. const element = new Element( 'p', null, [ textNode1, textNode2, textNode3 ] );
  278. selection.setTo( textNode2, 'on', { backward: true } );
  279. const ranges = Array.from( selection.getRanges() );
  280. expect( ranges.length ).to.equal( 1 );
  281. expect( ranges[ 0 ].start.parent ).to.equal( element );
  282. expect( ranges[ 0 ].start.offset ).to.deep.equal( 3 );
  283. expect( ranges[ 0 ].end.parent ).to.equal( element );
  284. expect( ranges[ 0 ].end.offset ).to.deep.equal( 6 );
  285. expect( selection.isBackward ).to.be.true;
  286. } );
  287. // TODO - backward
  288. // TODO - throwing
  289. describe( 'setting selection to position or item', () => {
  290. it( 'should fire change:range', () => {
  291. const spy = sinon.spy();
  292. selection.on( 'change:range', spy );
  293. selection.setTo( root, 0 );
  294. expect( spy.calledOnce ).to.be.true;
  295. } );
  296. it( 'should throw if second parameter is not passed', () => {
  297. expectToThrowCKEditorError( () => {
  298. selection.setTo( root );
  299. }, 'model-selection-setto-required-second-parameter', model );
  300. } );
  301. it( 'should set selection at given offset in given parent', () => {
  302. selection.setTo( root, 3 );
  303. expect( selection ).to.have.property( 'isCollapsed', true );
  304. const focus = selection.focus;
  305. expect( focus ).to.have.property( 'parent', root );
  306. expect( focus ).to.have.property( 'offset', 3 );
  307. } );
  308. it( 'should set selection at the end of the given parent', () => {
  309. selection.setTo( root, 'end' );
  310. expect( selection ).to.have.property( 'isCollapsed', true );
  311. const focus = selection.focus;
  312. expect( focus ).to.have.property( 'parent', root );
  313. expect( focus ).to.have.property( 'offset', root.maxOffset );
  314. } );
  315. it( 'should set selection before the specified element', () => {
  316. selection.setTo( root.getChild( 1 ), 'before' );
  317. expect( selection ).to.have.property( 'isCollapsed', true );
  318. const focus = selection.focus;
  319. expect( focus ).to.have.property( 'parent', root );
  320. expect( focus ).to.have.property( 'offset', 1 );
  321. } );
  322. it( 'should set selection after the specified element', () => {
  323. selection.setTo( root.getChild( 1 ), 'after' );
  324. expect( selection ).to.have.property( 'isCollapsed', true );
  325. const focus = selection.focus;
  326. expect( focus ).to.have.property( 'parent', root );
  327. expect( focus ).to.have.property( 'offset', 2 );
  328. } );
  329. it( 'should set selection at the specified position', () => {
  330. const pos = Position._createAt( root, 3 );
  331. selection.setTo( pos );
  332. expect( selection ).to.have.property( 'isCollapsed', true );
  333. const focus = selection.focus;
  334. expect( focus ).to.have.property( 'parent', root );
  335. expect( focus ).to.have.property( 'offset', 3 );
  336. } );
  337. } );
  338. } );
  339. describe( 'setFocus()', () => {
  340. it( 'keeps all existing ranges and fires no change:range when no modifications needed', () => {
  341. selection.setTo( [ range, liveRange ] );
  342. const spy = sinon.spy();
  343. selection.on( 'change:range', spy );
  344. selection.setFocus( selection.focus );
  345. expect( count( selection.getRanges() ) ).to.equal( 2 );
  346. expect( spy.callCount ).to.equal( 0 );
  347. } );
  348. it( 'fires change:range', () => {
  349. selection.setTo( range );
  350. const spy = sinon.spy();
  351. selection.on( 'change:range', spy );
  352. selection.setFocus( Position._createAt( root, 'end' ) );
  353. expect( spy.calledOnce ).to.be.true;
  354. } );
  355. it( 'throws if there are no ranges in selection', () => {
  356. const endPos = Position._createAt( root, 'end' );
  357. expectToThrowCKEditorError( () => {
  358. selection.setFocus( endPos );
  359. }, 'model-selection-setfocus-no-ranges', model );
  360. } );
  361. it( 'modifies existing collapsed selection', () => {
  362. const startPos = Position._createAt( root, 1 );
  363. const endPos = Position._createAt( root, 2 );
  364. selection.setTo( startPos );
  365. selection.setFocus( endPos );
  366. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'same' );
  367. expect( selection.focus.compareWith( endPos ) ).to.equal( 'same' );
  368. } );
  369. it( 'makes existing collapsed selection a backward selection', () => {
  370. const startPos = Position._createAt( root, 1 );
  371. const endPos = Position._createAt( root, 0 );
  372. selection.setTo( startPos );
  373. selection.setFocus( endPos );
  374. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'same' );
  375. expect( selection.focus.compareWith( endPos ) ).to.equal( 'same' );
  376. expect( selection.isBackward ).to.be.true;
  377. } );
  378. it( 'modifies existing non-collapsed selection', () => {
  379. const startPos = Position._createAt( root, 1 );
  380. const endPos = Position._createAt( root, 2 );
  381. const newEndPos = Position._createAt( root, 3 );
  382. selection.setTo( new Range( startPos, endPos ) );
  383. selection.setFocus( newEndPos );
  384. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'same' );
  385. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'same' );
  386. } );
  387. it( 'makes existing non-collapsed selection a backward selection', () => {
  388. const startPos = Position._createAt( root, 1 );
  389. const endPos = Position._createAt( root, 2 );
  390. const newEndPos = Position._createAt( root, 0 );
  391. selection.setTo( new Range( startPos, endPos ) );
  392. selection.setFocus( newEndPos );
  393. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'same' );
  394. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'same' );
  395. expect( selection.isBackward ).to.be.true;
  396. } );
  397. it( 'makes existing backward selection a forward selection', () => {
  398. const startPos = Position._createAt( root, 1 );
  399. const endPos = Position._createAt( root, 2 );
  400. const newEndPos = Position._createAt( root, 3 );
  401. selection.setTo( new Range( startPos, endPos ), { backward: true } );
  402. selection.setFocus( newEndPos );
  403. expect( selection.anchor.compareWith( endPos ) ).to.equal( 'same' );
  404. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'same' );
  405. expect( selection.isBackward ).to.be.false;
  406. } );
  407. it( 'modifies existing backward selection', () => {
  408. const startPos = Position._createAt( root, 1 );
  409. const endPos = Position._createAt( root, 2 );
  410. const newEndPos = Position._createAt( root, 0 );
  411. selection.setTo( new Range( startPos, endPos ), { backward: true } );
  412. selection.setFocus( newEndPos );
  413. expect( selection.anchor.compareWith( endPos ) ).to.equal( 'same' );
  414. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'same' );
  415. expect( selection.isBackward ).to.be.true;
  416. } );
  417. it( 'modifies only the last range', () => {
  418. // Offsets are chosen in this way that the order of adding ranges must count, not their document order.
  419. const startPos1 = Position._createAt( root, 4 );
  420. const endPos1 = Position._createAt( root, 5 );
  421. const startPos2 = Position._createAt( root, 1 );
  422. const endPos2 = Position._createAt( root, 2 );
  423. const newEndPos = Position._createAt( root, 0 );
  424. selection.setTo( [
  425. new Range( startPos1, endPos1 ),
  426. new Range( startPos2, endPos2 )
  427. ] );
  428. const spy = sinon.spy();
  429. selection.on( 'change:range', spy );
  430. selection.setFocus( newEndPos );
  431. const ranges = Array.from( selection.getRanges() );
  432. expect( ranges ).to.have.lengthOf( 2 );
  433. expect( ranges[ 0 ].start.compareWith( startPos1 ) ).to.equal( 'same' );
  434. expect( ranges[ 0 ].end.compareWith( endPos1 ) ).to.equal( 'same' );
  435. expect( selection.anchor.compareWith( startPos2 ) ).to.equal( 'same' );
  436. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'same' );
  437. expect( selection.isBackward ).to.be.true;
  438. expect( spy.calledOnce ).to.be.true;
  439. } );
  440. it( 'collapses the selection when extending to the anchor', () => {
  441. const startPos = Position._createAt( root, 1 );
  442. const endPos = Position._createAt( root, 2 );
  443. selection.setTo( new Range( startPos, endPos ) );
  444. selection.setFocus( startPos );
  445. expect( selection.focus.compareWith( startPos ) ).to.equal( 'same' );
  446. expect( selection.isCollapsed ).to.be.true;
  447. } );
  448. } );
  449. describe( 'setTo - selection set to null', () => {
  450. let spy;
  451. it( 'should remove all stored ranges', () => {
  452. selection.setTo( [ liveRange, range ] );
  453. selection.setTo( null );
  454. expect( Array.from( selection.getRanges() ).length ).to.equal( 0 );
  455. } );
  456. it( 'should fire exactly one change:range event', () => {
  457. selection.setTo( [ liveRange, range ] );
  458. spy = sinon.spy();
  459. selection.on( 'change:range', spy );
  460. selection.setTo( null );
  461. expect( spy.calledOnce ).to.be.true;
  462. } );
  463. it( 'should not fire change:range event if there were no ranges', () => {
  464. spy = sinon.spy();
  465. selection.on( 'change:range', spy );
  466. selection.setTo( null );
  467. expect( spy.called ).to.be.false;
  468. } );
  469. } );
  470. describe( '_setRanges()', () => {
  471. let newRanges, spy;
  472. beforeEach( () => {
  473. newRanges = [
  474. new Range( new Position( root, [ 4 ] ), new Position( root, [ 5 ] ) ),
  475. new Range( new Position( root, [ 5, 0 ] ), new Position( root, [ 6, 0 ] ) )
  476. ];
  477. selection.setTo( [ liveRange, range ] );
  478. spy = sinon.spy();
  479. selection.on( 'change:range', spy );
  480. } );
  481. it( 'should throw an error when range is invalid', () => {
  482. expectToThrowCKEditorError( () => {
  483. selection._setRanges( [ { invalid: 'range' } ] );
  484. }, /model-selection-set-ranges-not-range/, model );
  485. } );
  486. it( 'should remove all ranges and add given ranges', () => {
  487. selection._setRanges( newRanges );
  488. const ranges = Array.from( selection.getRanges() );
  489. expect( ranges ).to.deep.equal( newRanges );
  490. } );
  491. it( 'should use last range from given array to get anchor and focus position', () => {
  492. selection._setRanges( newRanges );
  493. expect( selection.anchor.path ).to.deep.equal( [ 5, 0 ] );
  494. expect( selection.focus.path ).to.deep.equal( [ 6, 0 ] );
  495. } );
  496. it( 'should acknowledge backward flag when setting anchor and focus', () => {
  497. selection._setRanges( newRanges, { backward: true } );
  498. expect( selection.anchor.path ).to.deep.equal( [ 6, 0 ] );
  499. expect( selection.focus.path ).to.deep.equal( [ 5, 0 ] );
  500. } );
  501. it( 'should fire exactly one change:range event', () => {
  502. selection._setRanges( newRanges );
  503. expect( spy.calledOnce ).to.be.true;
  504. } );
  505. it( 'should fire change:range event with correct parameters', () => {
  506. selection.on( 'change:range', ( evt, data ) => {
  507. expect( data.directChange ).to.be.true;
  508. } );
  509. selection._setRanges( newRanges );
  510. } );
  511. it( 'should not fire change:range event if given ranges are the same', () => {
  512. selection._setRanges( [ liveRange, range ] );
  513. expect( spy.calledOnce ).to.be.false;
  514. } );
  515. it( 'should copy added ranges and store multiple ranges', () => {
  516. selection._setRanges( [ liveRange, range ] );
  517. const ranges = selection._ranges;
  518. expect( ranges.length ).to.equal( 2 );
  519. expect( ranges[ 0 ].isEqual( liveRange ) ).to.be.true;
  520. expect( ranges[ 1 ].isEqual( range ) ).to.be.true;
  521. expect( ranges[ 0 ] ).not.to.equal( liveRange );
  522. expect( ranges[ 1 ] ).not.to.equal( range );
  523. } );
  524. it( 'should set anchor and focus to the start and end of the last added range', () => {
  525. selection._setRanges( [ liveRange, range ] );
  526. expect( selection.anchor.path ).to.deep.equal( [ 2 ] );
  527. expect( selection.focus.path ).to.deep.equal( [ 2, 2 ] );
  528. } );
  529. it( 'should set anchor and focus to the end and start of the most recently added range if backward flag was used', () => {
  530. selection._setRanges( [ liveRange, range ], { backward: true } );
  531. expect( selection.anchor.path ).to.deep.equal( [ 2 ] );
  532. expect( selection.focus.path ).to.deep.equal( [ 2, 2 ] );
  533. } );
  534. } );
  535. describe( 'getFirstRange()', () => {
  536. it( 'should return null if no ranges were added', () => {
  537. expect( selection.getFirstRange() ).to.be.null;
  538. } );
  539. it( 'should return a range which start position is before all other ranges\' start positions', () => {
  540. selection.setTo( [
  541. // This will not be the first range despite being added as first
  542. range2,
  543. // This should be the first range.
  544. range1,
  545. // A random range that is not first.
  546. range3
  547. ] );
  548. const range = selection.getFirstRange();
  549. expect( range.start.path ).to.deep.equal( [ 1 ] );
  550. expect( range.end.path ).to.deep.equal( [ 4 ] );
  551. } );
  552. } );
  553. describe( 'getFirstPosition()', () => {
  554. it( 'should return null if no ranges were added', () => {
  555. expect( selection.getFirstPosition() ).to.be.null;
  556. } );
  557. it( 'should return a position that is in selection and is before any other position from the selection', () => {
  558. selection.setTo( [
  559. // This will not be the first range despite being added as first
  560. range2,
  561. // This should be the first range.
  562. range1,
  563. // A random range that is not first.
  564. range3
  565. ] );
  566. const position = selection.getFirstPosition();
  567. expect( position.path ).to.deep.equal( [ 1 ] );
  568. } );
  569. } );
  570. describe( 'getLastRange()', () => {
  571. it( 'should return null if no ranges were added', () => {
  572. expect( selection.getLastRange() ).to.be.null;
  573. } );
  574. it( 'should return a range which start position is before all other ranges\' start positions', () => {
  575. selection.setTo( [ range3, range1, range2 ] );
  576. const range = selection.getLastRange();
  577. expect( range.start.path ).to.deep.equal( [ 6 ] );
  578. expect( range.end.path ).to.deep.equal( [ 7 ] );
  579. } );
  580. } );
  581. describe( 'getLastPosition()', () => {
  582. it( 'should return null if no ranges were added', () => {
  583. expect( selection.getLastPosition() ).to.be.null;
  584. } );
  585. it( 'should return a position that is in selection and is before any other position from the selection', () => {
  586. selection.setTo( [ range3, range1, range2 ] );
  587. const position = selection.getLastPosition();
  588. expect( position.path ).to.deep.equal( [ 7 ] );
  589. } );
  590. } );
  591. describe( 'isEqual()', () => {
  592. it( 'should return true if selections equal', () => {
  593. selection.setTo( [ range1, range2 ] );
  594. const otherSelection = new Selection( [ range1, range2 ] );
  595. expect( selection.isEqual( otherSelection ) ).to.be.true;
  596. } );
  597. it( 'should return true if backward selections equal', () => {
  598. selection.setTo( [ range1 ], { backward: true } );
  599. const otherSelection = new Selection( [ range1 ], { backward: true } );
  600. expect( selection.isEqual( otherSelection ) ).to.be.true;
  601. } );
  602. it( 'should return true if both selections have no ranges', () => {
  603. const otherSelection = new Selection();
  604. expect( selection.isEqual( otherSelection ) ).to.be.true;
  605. } );
  606. it( 'should return false if ranges count does not equal', () => {
  607. selection.setTo( [ range1, range2 ] );
  608. const otherSelection = new Selection( [ range2 ] );
  609. expect( selection.isEqual( otherSelection ) ).to.be.false;
  610. } );
  611. it( 'should return false if ranges (other than the last added range) do not equal', () => {
  612. selection.setTo( [ range1, range3 ] );
  613. const otherSelection = new Selection( [ range2, range3 ] );
  614. expect( selection.isEqual( otherSelection ) ).to.be.false;
  615. } );
  616. it( 'should return false if directions do not equal', () => {
  617. selection.setTo( range1 );
  618. const otherSelection = new Selection( [ range1 ], { backward: true } );
  619. expect( selection.isEqual( otherSelection ) ).to.be.false;
  620. } );
  621. } );
  622. describe( 'setTo - used to collapse at start', () => {
  623. it( 'should collapse to start position and fire change event', () => {
  624. selection.setTo( [ range2, range1, range3 ] );
  625. const spy = sinon.spy();
  626. selection.on( 'change:range', spy );
  627. selection.setTo( selection.getFirstPosition() );
  628. expect( selection.rangeCount ).to.equal( 1 );
  629. expect( selection.isCollapsed ).to.be.true;
  630. expect( selection.getFirstPosition().isEqual( range1.start ) ).to.be.true;
  631. expect( spy.calledOnce ).to.be.true;
  632. } );
  633. it( 'should do nothing if selection was already collapsed', () => {
  634. selection.setTo( range1.start );
  635. const spy = sinon.spy( selection, 'fire' );
  636. selection.setTo( selection.getFirstPosition() );
  637. expect( spy.notCalled ).to.be.true;
  638. spy.restore();
  639. } );
  640. it( 'should do nothing if no ranges present', () => {
  641. const spy = sinon.spy( selection, 'fire' );
  642. selection.setTo( selection.getFirstPosition() );
  643. spy.restore();
  644. expect( spy.notCalled ).to.be.true;
  645. } );
  646. } );
  647. describe( 'setTo - used to collapse at end', () => {
  648. it( 'should collapse to start position and fire change:range event', () => {
  649. selection.setTo( [ range2, range3, range1 ] );
  650. const spy = sinon.spy();
  651. selection.on( 'change:range', spy );
  652. selection.setTo( selection.getLastPosition() );
  653. expect( selection.rangeCount ).to.equal( 1 );
  654. expect( selection.isCollapsed ).to.be.true;
  655. expect( selection.getLastPosition().isEqual( range3.end ) ).to.be.true;
  656. expect( spy.calledOnce ).to.be.true;
  657. } );
  658. it( 'should do nothing if selection was already collapsed', () => {
  659. selection.setTo( range1.start );
  660. const spy = sinon.spy( selection, 'fire' );
  661. selection.setTo( selection.getLastPosition() );
  662. expect( spy.notCalled ).to.be.true;
  663. spy.restore();
  664. } );
  665. it( 'should do nothing if selection has no ranges', () => {
  666. const spy = sinon.spy( selection, 'fire' );
  667. selection.setTo( selection.getLastPosition() );
  668. expect( spy.notCalled ).to.be.true;
  669. spy.restore();
  670. } );
  671. } );
  672. describe( 'getSelectedElement()', () => {
  673. let schema;
  674. beforeEach( () => {
  675. schema = new Schema();
  676. schema.register( '$root' );
  677. schema.register( 'p', { allowIn: '$root' } );
  678. schema.register( '$text', { allowIn: 'p' } );
  679. } );
  680. it( 'should return selected element', () => {
  681. const { selection, model } = parse( '<p>foo</p>[<p>bar</p>]<p>baz</p>', schema );
  682. const p = model.getChild( 1 );
  683. expect( selection.getSelectedElement() ).to.equal( p );
  684. } );
  685. it( 'should return null if there is more than one range', () => {
  686. const { selection } = parse( '[<p>foo</p>][<p>bar</p>]<p>baz</p>', schema );
  687. expect( selection.getSelectedElement() ).to.be.null;
  688. } );
  689. it( 'should return null if there is no selection', () => {
  690. expect( selection.getSelectedElement() ).to.be.null;
  691. } );
  692. it( 'should return null if selection is not over single element #1', () => {
  693. const { selection } = parse( '<p>foo</p>[<p>bar</p><p>baz}</p>', schema );
  694. expect( selection.getSelectedElement() ).to.be.null;
  695. } );
  696. it( 'should return null if selection is not over single element #2', () => {
  697. const { selection } = parse( '<p>{bar}</p>', schema );
  698. expect( selection.getSelectedElement() ).to.be.null;
  699. } );
  700. } );
  701. describe( 'getSelectedBlocks()', () => {
  702. beforeEach( () => {
  703. model.schema.register( 'p', { inheritAllFrom: '$block' } );
  704. model.schema.register( 'h', { inheritAllFrom: '$block' } );
  705. model.schema.register( 'blockquote' );
  706. model.schema.extend( 'blockquote', { allowIn: '$root' } );
  707. model.schema.extend( '$block', { allowIn: 'blockquote' } );
  708. model.schema.register( 'image', {
  709. allowIn: [ '$root', '$block' ]
  710. } );
  711. model.schema.extend( '$text', { allowIn: 'image' } );
  712. // Special block which can contain another blocks.
  713. model.schema.register( 'nestedBlock', { inheritAllFrom: '$block' } );
  714. model.schema.extend( 'nestedBlock', { allowIn: '$block' } );
  715. model.schema.register( 'table', { isBlock: true, isLimit: true, isObject: true, allowIn: '$root' } );
  716. model.schema.register( 'tableRow', { allowIn: 'table', isLimit: true } );
  717. model.schema.register( 'tableCell', { allowIn: 'tableRow', isLimit: true, isSelectable: true } );
  718. model.schema.extend( 'p', { allowIn: 'tableCell' } );
  719. } );
  720. it( 'returns an iterator', () => {
  721. setData( model, '<p>a</p><p>[]b</p><p>c</p>' );
  722. expect( doc.selection.getSelectedBlocks().next ).to.be.a( 'function' );
  723. } );
  724. it( 'returns block for a collapsed selection', () => {
  725. setData( model, '<p>a</p><p>[]b</p><p>c</p>' );
  726. expect( stringifyBlocks( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'p#b' ] );
  727. } );
  728. it( 'returns block for a collapsed selection (empty block)', () => {
  729. setData( model, '<p>a</p><p>[]</p><p>c</p>' );
  730. const blocks = Array.from( doc.selection.getSelectedBlocks() );
  731. expect( blocks ).to.have.length( 1 );
  732. expect( blocks[ 0 ].childCount ).to.equal( 0 );
  733. } );
  734. it( 'returns block for a non collapsed selection', () => {
  735. setData( model, '<p>a</p><p>[b]</p><p>c</p>' );
  736. expect( stringifyBlocks( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'p#b' ] );
  737. } );
  738. it( 'returns two blocks for a non collapsed selection', () => {
  739. setData( model, '<p>a</p><h>[b</h><p>c]</p><p>d</p>' );
  740. expect( stringifyBlocks( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'h#b', 'p#c' ] );
  741. } );
  742. it( 'returns two blocks for a non collapsed selection (starts at block end)', () => {
  743. setData( model, '<p>a</p><h>b[</h><p>c]</p><p>d</p>' );
  744. expect( stringifyBlocks( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'h#b', 'p#c' ] );
  745. } );
  746. it( 'returns proper block for a multi-range selection', () => {
  747. setData( model, '<p>a</p><h>[b</h><p>c]</p><p>d</p><p>[e]</p>' );
  748. expect( stringifyBlocks( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'h#b', 'p#c', 'p#e' ] );
  749. } );
  750. it( 'does not return a block twice if two ranges are anchored in it', () => {
  751. setData( model, '<p>[a]b[c]</p>' );
  752. expect( stringifyBlocks( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'p#abc' ] );
  753. } );
  754. it( 'returns only blocks', () => {
  755. setData( model, '<p>[a</p><image>b</image><p>c]</p>' );
  756. expect( stringifyBlocks( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'p#a', 'p#c' ] );
  757. } );
  758. it( 'gets deeper into the tree', () => {
  759. setData( model, '<p>[a</p><blockquote><p>b</p><p>c</p></blockquote><p>d]</p>' );
  760. expect( stringifyBlocks( doc.selection.getSelectedBlocks() ) )
  761. .to.deep.equal( [ 'p#a', 'p#b', 'p#c', 'p#d' ] );
  762. } );
  763. it( 'gets deeper into the tree (end deeper)', () => {
  764. setData( model, '<p>[a</p><blockquote><p>b]</p><p>c</p></blockquote><p>d</p>' );
  765. expect( stringifyBlocks( doc.selection.getSelectedBlocks() ) )
  766. .to.deep.equal( [ 'p#a', 'p#b' ] );
  767. } );
  768. it( 'gets deeper into the tree (start deeper)', () => {
  769. setData( model, '<p>a</p><blockquote><p>b</p><p>[c</p></blockquote><p>d]</p>' );
  770. expect( stringifyBlocks( doc.selection.getSelectedBlocks() ) )
  771. .to.deep.equal( [ 'p#c', 'p#d' ] );
  772. } );
  773. it( 'returns an empty array if none of the selected elements is a block', () => {
  774. setData( model, '<p>a</p><image>[a</image><image>b]</image><p>b</p>' );
  775. expect( stringifyBlocks( doc.selection.getSelectedBlocks() ) ).to.be.empty;
  776. } );
  777. it( 'returns an empty array if the selected element is not a block', () => {
  778. setData( model, '<p>a</p><image>[]a</image><p>b</p>' );
  779. expect( stringifyBlocks( doc.selection.getSelectedBlocks() ) ).to.be.empty;
  780. } );
  781. // Super edge case – should not happen (blocks should never be nested),
  782. // but since the code handles it already it's worth testing.
  783. it( 'returns only the lowest block if blocks are nested (case #1)', () => {
  784. setData( model, '<nestedBlock>a<nestedBlock>[]b</nestedBlock></nestedBlock>' );
  785. expect( stringifyBlocks( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'nestedBlock#b' ] );
  786. } );
  787. // Like above but - with multiple ranges.
  788. it( 'returns only the lowest block if blocks are nested (case #2)', () => {
  789. setData(
  790. model,
  791. '<nestedBlock>a<nestedBlock>[b</nestedBlock></nestedBlock>' +
  792. '<nestedBlock>c<nestedBlock>d]</nestedBlock></nestedBlock>'
  793. );
  794. expect( stringifyBlocks( doc.selection.getSelectedBlocks() ) )
  795. .to.deep.equal( [ 'nestedBlock#b', 'nestedBlock#d' ] );
  796. } );
  797. // Like above but - with multiple collapsed ranges.
  798. it( 'returns only the lowest block if blocks are nested (case #3)', () => {
  799. setData(
  800. model,
  801. '<nestedBlock>a<nestedBlock>[]b</nestedBlock></nestedBlock>' +
  802. '<nestedBlock>c<nestedBlock>d[]</nestedBlock></nestedBlock>'
  803. );
  804. expect( stringifyBlocks( doc.selection.getSelectedBlocks() ) )
  805. .to.deep.equal( [ 'nestedBlock#b', 'nestedBlock#d' ] );
  806. } );
  807. it( 'returns nothing if directly in a root', () => {
  808. doc.createRoot( 'p', 'inlineOnlyRoot' );
  809. setData( model, 'a[b]c', { rootName: 'inlineOnlyRoot' } );
  810. expect( stringifyBlocks( doc.selection.getSelectedBlocks() ) ).to.be.empty;
  811. } );
  812. it( 'does not go cross limit elements', () => {
  813. model.schema.register( 'blk', { allowIn: [ '$root', 'tableCell' ], isObject: true, isBlock: true } );
  814. setData( model, '<table><tableRow><tableCell><p>foo</p>[<blk></blk><p>bar]</p></tableCell></tableRow></table>' );
  815. expect( stringifyBlocks( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'blk', 'p#bar' ] );
  816. } );
  817. it( 'returns only top most blocks (multiple selected)', () => {
  818. setData( model, '<p>[foo</p><table><tableRow><tableCell><p>bar</p></tableCell></tableRow></table><p>baz]</p>' );
  819. expect( stringifyBlocks( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'p#foo', 'table', 'p#baz' ] );
  820. } );
  821. it( 'returns only top most block (one selected)', () => {
  822. setData( model, '[<table><tableRow><tableCell><p>bar</p></tableCell></tableRow></table>]' );
  823. expect( stringifyBlocks( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'table' ] );
  824. } );
  825. it( 'returns only selected blocks even if nested in other blocks', () => {
  826. setData( model, '<p>foo</p><table><tableRow><tableCell><p>[b]ar</p></tableCell></tableRow></table><p>baz</p>' );
  827. expect( stringifyBlocks( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'p#bar' ] );
  828. } );
  829. it( 'returns only selected blocks even if nested in other blocks (selection on the block)', () => {
  830. model.schema.register( 'blk', { allowIn: [ '$root', 'tableCell' ], isObject: true, isBlock: true } );
  831. setData( model, '<table><tableRow><tableCell><p>foo</p>[<blk></blk><p>bar]</p></tableCell></tableRow></table>' );
  832. expect( stringifyBlocks( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'blk', 'p#bar' ] );
  833. } );
  834. describe( '#984', () => {
  835. it( 'does not return the last block if none of its content is selected', () => {
  836. setData( model, '<p>[a</p><p>b</p><p>]c</p>' );
  837. expect( stringifyBlocks( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'p#a', 'p#b' ] );
  838. } );
  839. it( 'returns only the first block for a non collapsed selection if only end of selection is touching a block', () => {
  840. setData( model, '<p>a</p><h>b[</h><p>]c</p><p>d</p>' );
  841. expect( stringifyBlocks( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'h#b' ] );
  842. } );
  843. it( 'does not return the last block if none of its content is selected (nested case)', () => {
  844. setData( model, '<p>[a</p><nestedBlock><nestedBlock>]b</nestedBlock></nestedBlock>' );
  845. expect( stringifyBlocks( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'p#a' ] );
  846. } );
  847. // Like a super edge case, we can live with this behavior as I don't even know what we could expect here
  848. // since only the innermost block is considerd a block to return (so the <nB>b...</nB> needs to be ignored).
  849. it( 'does not return the last block if none of its content is selected (nested case, wrapper with a content)', () => {
  850. setData( model, '<p>[a</p><nestedBlock>b<nestedBlock>]c</nestedBlock></nestedBlock>' );
  851. expect( stringifyBlocks( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'p#a' ] );
  852. } );
  853. it( 'returns the last block if at least one of its child nodes is selected', () => {
  854. setData( model, '<p>[a</p><p>b</p><p><image></image>]c</p>' );
  855. expect( stringifyBlocks( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'p#a', 'p#b', 'p#c' ] );
  856. } );
  857. // I needed these last 2 cases to justify the use of isTouching() instead of simple `offset == 0` check.
  858. it( 'returns the last block if at least one of its child nodes is selected (end in an inline element)', () => {
  859. setData( model, '<p>[a</p><p>b</p><p><image>x]</image>c</p>' );
  860. expect( stringifyBlocks( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'p#a', 'p#b', 'p#c' ] );
  861. } );
  862. it(
  863. 'does not return the last block if at least one of its child nodes is selected ' +
  864. '(end in an inline element, no content selected)',
  865. () => {
  866. setData( model, '<p>[a</p><p>b</p><p><image>]x</image>c</p>' );
  867. expect( stringifyBlocks( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'p#a', 'p#b' ] );
  868. }
  869. );
  870. } );
  871. } );
  872. describe( 'attributes interface', () => {
  873. let rangeInFullP;
  874. beforeEach( () => {
  875. root._insertChild( 0, [
  876. new Element( 'p', [], new Text( 'foobar' ) ),
  877. new Element( 'p', [], [] )
  878. ] );
  879. rangeInFullP = new Range( new Position( root, [ 0, 4 ] ), new Position( root, [ 0, 4 ] ) );
  880. } );
  881. describe( 'setAttribute()', () => {
  882. it( 'should set given attribute on the selection', () => {
  883. selection.setTo( [ rangeInFullP ] );
  884. selection.setAttribute( 'foo', 'bar' );
  885. expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  886. } );
  887. it( 'should fire change:attribute event with correct parameters', () => {
  888. selection.on( 'change:attribute', ( evt, data ) => {
  889. expect( data.directChange ).to.be.true;
  890. expect( data.attributeKeys ).to.deep.equal( [ 'foo' ] );
  891. } );
  892. selection.setAttribute( 'foo', 'bar' );
  893. } );
  894. it( 'should not fire change:attribute event if attribute with same key and value was already set', () => {
  895. selection.setAttribute( 'foo', 'bar' );
  896. const spy = sinon.spy();
  897. selection.on( 'change:attribute', spy );
  898. selection.setAttribute( 'foo', 'bar' );
  899. expect( spy.called ).to.be.false;
  900. } );
  901. } );
  902. describe( 'getAttribute()', () => {
  903. it( 'should return undefined if element does not contain given attribute', () => {
  904. expect( selection.getAttribute( 'abc' ) ).to.be.undefined;
  905. } );
  906. } );
  907. describe( 'getAttributes()', () => {
  908. it( 'should return an iterator that iterates over all attributes set on selection', () => {
  909. selection.setTo( [ rangeInFullP ] );
  910. selection.setAttribute( 'foo', 'bar' );
  911. selection.setAttribute( 'abc', 'xyz' );
  912. const attrs = Array.from( selection.getAttributes() );
  913. expect( attrs ).to.deep.equal( [ [ 'foo', 'bar' ], [ 'abc', 'xyz' ] ] );
  914. } );
  915. } );
  916. describe( 'getAttributeKeys()', () => {
  917. it( 'should return iterator that iterates over all attribute keys set on selection', () => {
  918. selection.setTo( [ rangeInFullP ] );
  919. selection.setAttribute( 'foo', 'bar' );
  920. selection.setAttribute( 'abc', 'xyz' );
  921. const attrs = Array.from( selection.getAttributeKeys() );
  922. expect( attrs ).to.deep.equal( [ 'foo', 'abc' ] );
  923. } );
  924. } );
  925. describe( 'hasAttribute()', () => {
  926. it( 'should return true if element contains attribute with given key', () => {
  927. selection.setTo( [ rangeInFullP ] );
  928. selection.setAttribute( 'foo', 'bar' );
  929. expect( selection.hasAttribute( 'foo' ) ).to.be.true;
  930. } );
  931. it( 'should return false if element does not contain attribute with given key', () => {
  932. expect( selection.hasAttribute( 'abc' ) ).to.be.false;
  933. } );
  934. } );
  935. describe( 'removeAttribute()', () => {
  936. it( 'should remove attribute', () => {
  937. selection.setTo( [ rangeInFullP ] );
  938. selection.setAttribute( 'foo', 'bar' );
  939. selection.removeAttribute( 'foo' );
  940. expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
  941. } );
  942. it( 'should fire change:attribute event with correct parameters', () => {
  943. selection.setAttribute( 'foo', 'bar' );
  944. selection.on( 'change:attribute', ( evt, data ) => {
  945. expect( data.directChange ).to.be.true;
  946. expect( data.attributeKeys ).to.deep.equal( [ 'foo' ] );
  947. } );
  948. selection.removeAttribute( 'foo' );
  949. } );
  950. it( 'should not fire change:attribute event if such attribute did not exist', () => {
  951. const spy = sinon.spy();
  952. selection.on( 'change:attribute', spy );
  953. selection.removeAttribute( 'foo' );
  954. expect( spy.called ).to.be.false;
  955. } );
  956. } );
  957. } );
  958. describe( 'containsEntireContent()', () => {
  959. beforeEach( () => {
  960. model.schema.register( 'p', { inheritAllFrom: '$block' } );
  961. model.schema.extend( 'p', { allowIn: '$root' } );
  962. } );
  963. it( 'returns true if the entire content in $root is selected', () => {
  964. setData( model, '<p>[Foo</p><p>Bom</p><p>Bar]</p>' );
  965. expect( doc.selection.containsEntireContent() ).to.equal( true );
  966. } );
  967. it( 'returns false when only a fragment of the content in $root is selected', () => {
  968. setData( model, '<p>Fo[o</p><p>Bom</p><p>Bar]</p>' );
  969. expect( doc.selection.containsEntireContent() ).to.equal( false );
  970. } );
  971. it( 'returns true if the entire content in specified element is selected', () => {
  972. setData( model, '<p>Foo</p><p>[Bom]</p><p>Bar</p>' );
  973. const root = doc.getRoot();
  974. const secondParagraph = root.getNodeByPath( [ 1 ] );
  975. expect( doc.selection.containsEntireContent( secondParagraph ) ).to.equal( true );
  976. } );
  977. it( 'returns false if the entire content in specified element is not selected', () => {
  978. setData( model, '<p>Foo</p><p>[Bom</p><p>B]ar</p>' );
  979. const root = doc.getRoot();
  980. const secondParagraph = root.getNodeByPath( [ 1 ] );
  981. expect( doc.selection.containsEntireContent( secondParagraph ) ).to.equal( false );
  982. } );
  983. it( 'returns false when the entire content except an empty element is selected', () => {
  984. model.schema.register( 'img', {
  985. allowIn: 'p'
  986. } );
  987. setData( model, '<p><img></img>[Foo]</p>' );
  988. expect( doc.selection.containsEntireContent() ).to.equal( false );
  989. } );
  990. it( 'returns true if the content is empty', () => {
  991. setData( model, '[]' );
  992. expect( doc.selection.containsEntireContent() ).to.equal( true );
  993. } );
  994. it( 'returns false if empty selection is at the end of non-empty content', () => {
  995. setData( model, '<p>Foo bar bom.</p>[]' );
  996. expect( doc.selection.containsEntireContent() ).to.equal( false );
  997. } );
  998. } );
  999. // Map all elements to names. If element contains child text node it will be appended to name with '#'.
  1000. function stringifyBlocks( elements ) {
  1001. return Array.from( elements ).map( el => {
  1002. const name = el.name;
  1003. let innerText = '';
  1004. for ( const child of el.getChildren() ) {
  1005. if ( child.is( '$text' ) ) {
  1006. innerText += child.data;
  1007. }
  1008. }
  1009. return innerText.length ? `${ name }#${ innerText }` : name;
  1010. } );
  1011. }
  1012. } );