8
0

selection.js 40 KB

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