selection.js 41 KB

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