8
0

selection.js 41 KB

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