8
0

selection.js 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371
  1. /**
  2. * @license Copyright (c) 2003-2017, 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.addRange( 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.addRange( new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) ) );
  70. selection.addRange( new Range( new Position( root, [ 2 ] ), new Position( root, [ 2 ] ) ) );
  71. expect( selection.isCollapsed ).to.be.false;
  72. } );
  73. it( 'should return false when there is not collapsed range', () => {
  74. selection.addRange( range );
  75. expect( selection.isCollapsed ).to.be.false;
  76. } );
  77. } );
  78. describe( 'rangeCount', () => {
  79. it( 'should return proper range count', () => {
  80. expect( selection.rangeCount ).to.equal( 0 );
  81. selection.addRange( new Range( new Position( root, [ 0 ] ), new Position( root, [ 0 ] ) ) );
  82. expect( selection.rangeCount ).to.equal( 1 );
  83. selection.addRange( new Range( new Position( root, [ 2 ] ), new Position( root, [ 2 ] ) ) );
  84. expect( selection.rangeCount ).to.equal( 2 );
  85. } );
  86. } );
  87. describe( 'isBackward', () => {
  88. it( 'is defined by the last added range', () => {
  89. selection.addRange( range, true );
  90. expect( selection ).to.have.property( 'isBackward', true );
  91. selection.addRange( liveRange );
  92. expect( selection ).to.have.property( 'isBackward', false );
  93. } );
  94. it( 'is false when last range is collapsed', () => {
  95. const pos = Position.createAt( root, 0 );
  96. selection.addRange( new Range( pos, pos ), true );
  97. expect( selection.isBackward ).to.be.false;
  98. } );
  99. } );
  100. describe( 'focus', () => {
  101. let r3;
  102. beforeEach( () => {
  103. const r1 = Range.createFromParentsAndOffsets( root, 2, root, 4 );
  104. const r2 = Range.createFromParentsAndOffsets( root, 4, root, 6 );
  105. r3 = Range.createFromParentsAndOffsets( root, 1, root, 2 );
  106. selection.addRange( r1 );
  107. selection.addRange( r2 );
  108. } );
  109. it( 'should return correct focus when last added range is not backward one', () => {
  110. selection.addRange( r3 );
  111. expect( selection.focus.isEqual( r3.end ) ).to.be.true;
  112. } );
  113. it( 'should return correct focus when last added range is backward one', () => {
  114. selection.addRange( r3, true );
  115. expect( selection.focus.isEqual( r3.start ) ).to.be.true;
  116. } );
  117. it( 'should return null if no ranges in selection', () => {
  118. selection.removeAllRanges();
  119. expect( selection.focus ).to.be.null;
  120. } );
  121. } );
  122. describe( 'addRange()', () => {
  123. it( 'should copy added ranges and store multiple ranges', () => {
  124. selection.addRange( liveRange );
  125. selection.addRange( range );
  126. const ranges = selection._ranges;
  127. expect( ranges.length ).to.equal( 2 );
  128. expect( ranges[ 0 ].isEqual( liveRange ) ).to.be.true;
  129. expect( ranges[ 1 ].isEqual( range ) ).to.be.true;
  130. expect( ranges[ 0 ] ).not.to.be.equal( liveRange );
  131. expect( ranges[ 1 ] ).not.to.be.equal( range );
  132. } );
  133. it( 'should set anchor and focus to the start and end of the most recently added range', () => {
  134. selection.addRange( liveRange );
  135. expect( selection.anchor.path ).to.deep.equal( [ 0 ] );
  136. expect( selection.focus.path ).to.deep.equal( [ 1 ] );
  137. selection.addRange( range );
  138. expect( selection.anchor.path ).to.deep.equal( [ 2 ] );
  139. expect( selection.focus.path ).to.deep.equal( [ 2, 2 ] );
  140. } );
  141. it( 'should set anchor and focus to the end and start of the most recently added range if backward flag was used', () => {
  142. selection.addRange( liveRange, true );
  143. expect( selection.anchor.path ).to.deep.equal( [ 1 ] );
  144. expect( selection.focus.path ).to.deep.equal( [ 0 ] );
  145. selection.addRange( range, true );
  146. expect( selection.anchor.path ).to.deep.equal( [ 2, 2 ] );
  147. expect( selection.focus.path ).to.deep.equal( [ 2 ] );
  148. } );
  149. it( 'should return a copy of (not a reference to) array of stored ranges', () => {
  150. selection.addRange( liveRange );
  151. const ranges = Array.from( selection.getRanges() );
  152. selection.addRange( range );
  153. expect( ranges.length ).to.equal( 1 );
  154. expect( ranges[ 0 ].isEqual( liveRange ) ).to.be.true;
  155. } );
  156. it( 'should fire change:range event when adding a range', () => {
  157. const spy = sinon.spy();
  158. selection.on( 'change:range', spy );
  159. selection.addRange( range );
  160. expect( spy.called ).to.be.true;
  161. } );
  162. it( 'should throw an error when range is invalid', () => {
  163. expect( () => {
  164. selection.addRange( { invalid: 'range' } );
  165. } ).to.throw( CKEditorError, /model-selection-added-not-range/ );
  166. } );
  167. it( 'should throw an error if added range intersects with already stored range', () => {
  168. selection.addRange( liveRange );
  169. expect( () => {
  170. selection.addRange(
  171. new Range(
  172. new Position( root, [ 0, 4 ] ),
  173. new Position( root, [ 1, 2 ] )
  174. )
  175. );
  176. } ).to.throw( CKEditorError, /model-selection-range-intersects/ );
  177. } );
  178. } );
  179. describe( 'setIn()', () => {
  180. it( 'should set selection inside an element', () => {
  181. const element = new Element( 'p', null, [ new Text( 'foo' ), new Text( 'bar' ) ] );
  182. selection.setIn( element );
  183. const ranges = Array.from( selection.getRanges() );
  184. expect( ranges.length ).to.equal( 1 );
  185. expect( ranges[ 0 ].start.parent ).to.equal( element );
  186. expect( ranges[ 0 ].start.offset ).to.deep.equal( 0 );
  187. expect( ranges[ 0 ].end.parent ).to.equal( element );
  188. expect( ranges[ 0 ].end.offset ).to.deep.equal( 6 );
  189. } );
  190. } );
  191. describe( 'setOn()', () => {
  192. it( 'should set selection on an item', () => {
  193. const textNode1 = new Text( 'foo' );
  194. const textNode2 = new Text( 'bar' );
  195. const textNode3 = new Text( 'baz' );
  196. const element = new Element( 'p', null, [ textNode1, textNode2, textNode3 ] );
  197. selection.setOn( textNode2 );
  198. const ranges = Array.from( selection.getRanges() );
  199. expect( ranges.length ).to.equal( 1 );
  200. expect( ranges[ 0 ].start.parent ).to.equal( element );
  201. expect( ranges[ 0 ].start.offset ).to.deep.equal( 3 );
  202. expect( ranges[ 0 ].end.parent ).to.equal( element );
  203. expect( ranges[ 0 ].end.offset ).to.deep.equal( 6 );
  204. } );
  205. } );
  206. describe( 'setCollapsedAt()', () => {
  207. it( 'fires change:range', () => {
  208. const spy = sinon.spy();
  209. selection.on( 'change:range', spy );
  210. selection.setCollapsedAt( root );
  211. expect( spy.calledOnce ).to.be.true;
  212. } );
  213. it( 'sets selection at the 0 offset if second parameter not passed', () => {
  214. selection.setCollapsedAt( root );
  215. expect( selection ).to.have.property( 'isCollapsed', true );
  216. const focus = selection.focus;
  217. expect( focus ).to.have.property( 'parent', root );
  218. expect( focus ).to.have.property( 'offset', 0 );
  219. } );
  220. it( 'sets selection at given offset in given parent', () => {
  221. selection.setCollapsedAt( root, 3 );
  222. expect( selection ).to.have.property( 'isCollapsed', true );
  223. const focus = selection.focus;
  224. expect( focus ).to.have.property( 'parent', root );
  225. expect( focus ).to.have.property( 'offset', 3 );
  226. } );
  227. it( 'sets selection at the end of the given parent', () => {
  228. selection.setCollapsedAt( root, 'end' );
  229. expect( selection ).to.have.property( 'isCollapsed', true );
  230. const focus = selection.focus;
  231. expect( focus ).to.have.property( 'parent', root );
  232. expect( focus ).to.have.property( 'offset', root.maxOffset );
  233. } );
  234. it( 'sets selection before the specified element', () => {
  235. selection.setCollapsedAt( root.getChild( 1 ), 'before' );
  236. expect( selection ).to.have.property( 'isCollapsed', true );
  237. const focus = selection.focus;
  238. expect( focus ).to.have.property( 'parent', root );
  239. expect( focus ).to.have.property( 'offset', 1 );
  240. } );
  241. it( 'sets selection after the specified element', () => {
  242. selection.setCollapsedAt( root.getChild( 1 ), 'after' );
  243. expect( selection ).to.have.property( 'isCollapsed', true );
  244. const focus = selection.focus;
  245. expect( focus ).to.have.property( 'parent', root );
  246. expect( focus ).to.have.property( 'offset', 2 );
  247. } );
  248. it( 'sets selection at the specified position', () => {
  249. const pos = Position.createFromParentAndOffset( root, 3 );
  250. selection.setCollapsedAt( pos );
  251. expect( selection ).to.have.property( 'isCollapsed', true );
  252. const focus = selection.focus;
  253. expect( focus ).to.have.property( 'parent', root );
  254. expect( focus ).to.have.property( 'offset', 3 );
  255. } );
  256. } );
  257. describe( 'moveFocusTo()', () => {
  258. it( 'keeps all existing ranges and fires no change:range when no modifications needed', () => {
  259. selection.addRange( range );
  260. selection.addRange( liveRange );
  261. const spy = sinon.spy();
  262. selection.on( 'change:range', spy );
  263. selection.moveFocusTo( selection.focus );
  264. expect( count( selection.getRanges() ) ).to.equal( 2 );
  265. expect( spy.callCount ).to.equal( 0 );
  266. } );
  267. it( 'fires change:range', () => {
  268. selection.addRange( range );
  269. const spy = sinon.spy();
  270. selection.on( 'change:range', spy );
  271. selection.moveFocusTo( Position.createAt( root, 'end' ) );
  272. expect( spy.calledOnce ).to.be.true;
  273. } );
  274. it( 'throws if there are no ranges in selection', () => {
  275. const endPos = Position.createAt( root, 'end' );
  276. expect( () => {
  277. selection.moveFocusTo( endPos );
  278. } ).to.throw( CKEditorError, /model-selection-moveFocusTo-no-ranges/ );
  279. } );
  280. it( 'modifies existing collapsed selection', () => {
  281. const startPos = Position.createAt( root, 1 );
  282. const endPos = Position.createAt( root, 2 );
  283. selection.setCollapsedAt( startPos );
  284. selection.moveFocusTo( endPos );
  285. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'same' );
  286. expect( selection.focus.compareWith( endPos ) ).to.equal( 'same' );
  287. } );
  288. it( 'makes existing collapsed selection a backward selection', () => {
  289. const startPos = Position.createAt( root, 1 );
  290. const endPos = Position.createAt( root, 0 );
  291. selection.setCollapsedAt( startPos );
  292. selection.moveFocusTo( endPos );
  293. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'same' );
  294. expect( selection.focus.compareWith( endPos ) ).to.equal( 'same' );
  295. expect( selection.isBackward ).to.be.true;
  296. } );
  297. it( 'modifies existing non-collapsed selection', () => {
  298. const startPos = Position.createAt( root, 1 );
  299. const endPos = Position.createAt( root, 2 );
  300. const newEndPos = Position.createAt( root, 3 );
  301. selection.addRange( new Range( startPos, endPos ) );
  302. selection.moveFocusTo( newEndPos );
  303. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'same' );
  304. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'same' );
  305. } );
  306. it( 'makes existing non-collapsed selection a backward selection', () => {
  307. const startPos = Position.createAt( root, 1 );
  308. const endPos = Position.createAt( root, 2 );
  309. const newEndPos = Position.createAt( root, 0 );
  310. selection.addRange( new Range( startPos, endPos ) );
  311. selection.moveFocusTo( newEndPos );
  312. expect( selection.anchor.compareWith( startPos ) ).to.equal( 'same' );
  313. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'same' );
  314. expect( selection.isBackward ).to.be.true;
  315. } );
  316. it( 'makes existing backward selection a forward selection', () => {
  317. const startPos = Position.createAt( root, 1 );
  318. const endPos = Position.createAt( root, 2 );
  319. const newEndPos = Position.createAt( root, 3 );
  320. selection.addRange( new Range( startPos, endPos ), true );
  321. selection.moveFocusTo( newEndPos );
  322. expect( selection.anchor.compareWith( endPos ) ).to.equal( 'same' );
  323. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'same' );
  324. expect( selection.isBackward ).to.be.false;
  325. } );
  326. it( 'modifies existing backward selection', () => {
  327. const startPos = Position.createAt( root, 1 );
  328. const endPos = Position.createAt( root, 2 );
  329. const newEndPos = Position.createAt( root, 0 );
  330. selection.addRange( new Range( startPos, endPos ), true );
  331. selection.moveFocusTo( newEndPos );
  332. expect( selection.anchor.compareWith( endPos ) ).to.equal( 'same' );
  333. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'same' );
  334. expect( selection.isBackward ).to.be.true;
  335. } );
  336. it( 'modifies only the last range', () => {
  337. // Offsets are chosen in this way that the order of adding ranges must count, not their document order.
  338. const startPos1 = Position.createAt( root, 4 );
  339. const endPos1 = Position.createAt( root, 5 );
  340. const startPos2 = Position.createAt( root, 1 );
  341. const endPos2 = Position.createAt( root, 2 );
  342. const newEndPos = Position.createAt( root, 0 );
  343. selection.addRange( new Range( startPos1, endPos1 ) );
  344. selection.addRange( new Range( startPos2, endPos2 ) );
  345. const spy = sinon.spy();
  346. selection.on( 'change:range', spy );
  347. selection.moveFocusTo( newEndPos );
  348. const ranges = Array.from( selection.getRanges() );
  349. expect( ranges ).to.have.lengthOf( 2 );
  350. expect( ranges[ 0 ].start.compareWith( startPos1 ) ).to.equal( 'same' );
  351. expect( ranges[ 0 ].end.compareWith( endPos1 ) ).to.equal( 'same' );
  352. expect( selection.anchor.compareWith( startPos2 ) ).to.equal( 'same' );
  353. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'same' );
  354. expect( selection.isBackward ).to.be.true;
  355. expect( spy.calledOnce ).to.be.true;
  356. } );
  357. it( 'collapses the selection when extending to the anchor', () => {
  358. const startPos = Position.createAt( root, 1 );
  359. const endPos = Position.createAt( root, 2 );
  360. selection.addRange( new Range( startPos, endPos ) );
  361. selection.moveFocusTo( startPos );
  362. expect( selection.focus.compareWith( startPos ) ).to.equal( 'same' );
  363. expect( selection.isCollapsed ).to.be.true;
  364. } );
  365. it( 'uses Position.createAt', () => {
  366. const startPos = Position.createAt( root, 1 );
  367. const endPos = Position.createAt( root, 2 );
  368. const newEndPos = Position.createAt( root, 4 );
  369. const spy = testUtils.sinon.stub( Position, 'createAt' ).returns( newEndPos );
  370. selection.addRange( new Range( startPos, endPos ) );
  371. selection.moveFocusTo( root, 'end' );
  372. expect( spy.calledOnce ).to.be.true;
  373. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'same' );
  374. } );
  375. } );
  376. describe( 'removeAllRanges()', () => {
  377. let spy;
  378. it( 'should remove all stored ranges', () => {
  379. selection.addRange( liveRange );
  380. selection.addRange( range );
  381. selection.removeAllRanges();
  382. expect( Array.from( selection.getRanges() ).length ).to.equal( 0 );
  383. } );
  384. it( 'should fire exactly one change:range event', () => {
  385. selection.addRange( liveRange );
  386. selection.addRange( range );
  387. spy = sinon.spy();
  388. selection.on( 'change:range', spy );
  389. selection.removeAllRanges();
  390. expect( spy.calledOnce ).to.be.true;
  391. } );
  392. it( 'should not fire change:range event if there were no ranges', () => {
  393. spy = sinon.spy();
  394. selection.on( 'change:range', spy );
  395. selection.removeAllRanges();
  396. expect( spy.called ).to.be.false;
  397. } );
  398. } );
  399. describe( 'setRanges()', () => {
  400. let newRanges, spy;
  401. beforeEach( () => {
  402. newRanges = [
  403. new Range( new Position( root, [ 4 ] ), new Position( root, [ 5 ] ) ),
  404. new Range( new Position( root, [ 5, 0 ] ), new Position( root, [ 6, 0 ] ) )
  405. ];
  406. selection.addRange( liveRange );
  407. selection.addRange( range );
  408. spy = sinon.spy();
  409. selection.on( 'change:range', spy );
  410. } );
  411. it( 'should throw an error when range is invalid', () => {
  412. expect( () => {
  413. selection.setRanges( [ { invalid: 'range' } ] );
  414. } ).to.throw( CKEditorError, /model-selection-added-not-range/ );
  415. } );
  416. it( 'should remove all ranges and add given ranges', () => {
  417. selection.setRanges( newRanges );
  418. const ranges = Array.from( selection.getRanges() );
  419. expect( ranges ).to.deep.equal( newRanges );
  420. } );
  421. it( 'should use last range from given array to get anchor and focus position', () => {
  422. selection.setRanges( newRanges );
  423. expect( selection.anchor.path ).to.deep.equal( [ 5, 0 ] );
  424. expect( selection.focus.path ).to.deep.equal( [ 6, 0 ] );
  425. } );
  426. it( 'should acknowledge backward flag when setting anchor and focus', () => {
  427. selection.setRanges( newRanges, true );
  428. expect( selection.anchor.path ).to.deep.equal( [ 6, 0 ] );
  429. expect( selection.focus.path ).to.deep.equal( [ 5, 0 ] );
  430. } );
  431. it( 'should fire exactly one change:range event', () => {
  432. selection.setRanges( newRanges );
  433. expect( spy.calledOnce ).to.be.true;
  434. } );
  435. it( 'should fire change:range event with correct parameters', () => {
  436. selection.on( 'change:range', ( evt, data ) => {
  437. expect( data.directChange ).to.be.true;
  438. } );
  439. selection.setRanges( newRanges );
  440. } );
  441. it( 'should not fire change:range event if given ranges are the same', () => {
  442. selection.setRanges( [ liveRange, range ] );
  443. expect( spy.calledOnce ).to.be.false;
  444. } );
  445. } );
  446. describe( 'setTo()', () => {
  447. it( 'should set selection to be same as given selection, using setRanges method', () => {
  448. const spy = sinon.spy( selection, 'setRanges' );
  449. const otherSelection = new Selection();
  450. otherSelection.addRange( range1 );
  451. otherSelection.addRange( range2, true );
  452. selection.setTo( otherSelection );
  453. expect( Array.from( selection.getRanges() ) ).to.deep.equal( [ range1, range2 ] );
  454. expect( selection.isBackward ).to.be.true;
  455. expect( selection.setRanges.calledOnce ).to.be.true;
  456. spy.restore();
  457. } );
  458. it( 'should set selection on the given Range using setRanges method', () => {
  459. const spy = sinon.spy( selection, 'setRanges' );
  460. selection.setTo( range1 );
  461. expect( Array.from( selection.getRanges() ) ).to.deep.equal( [ range1 ] );
  462. expect( selection.isBackward ).to.be.false;
  463. expect( selection.setRanges.calledOnce ).to.be.true;
  464. spy.restore();
  465. } );
  466. it( 'should set selection on the given iterable of Ranges using setRanges method', () => {
  467. const spy = sinon.spy( selection, 'setRanges' );
  468. selection.setTo( new Set( [ range1, range2 ] ) );
  469. expect( Array.from( selection.getRanges() ) ).to.deep.equal( [ range1, range2 ] );
  470. expect( selection.isBackward ).to.be.false;
  471. expect( selection.setRanges.calledOnce ).to.be.true;
  472. spy.restore();
  473. } );
  474. it( 'should set collapsed selection on the given Position using setRanges method', () => {
  475. const spy = sinon.spy( selection, 'setRanges' );
  476. const position = new Position( root, [ 4 ] );
  477. selection.setTo( position );
  478. expect( Array.from( selection.getRanges() ).length ).to.equal( 1 );
  479. expect( Array.from( selection.getRanges() )[ 0 ].start ).to.deep.equal( position );
  480. expect( selection.isBackward ).to.be.false;
  481. expect( selection.isCollapsed ).to.be.true;
  482. expect( selection.setRanges.calledOnce ).to.be.true;
  483. spy.restore();
  484. } );
  485. } );
  486. describe( 'getFirstRange()', () => {
  487. it( 'should return null if no ranges were added', () => {
  488. expect( selection.getFirstRange() ).to.be.null;
  489. } );
  490. it( 'should return a range which start position is before all other ranges\' start positions', () => {
  491. // This will not be the first range despite being added as first
  492. selection.addRange( range2 );
  493. // This should be the first range.
  494. selection.addRange( range1 );
  495. // A random range that is not first.
  496. selection.addRange( range3 );
  497. const range = selection.getFirstRange();
  498. expect( range.start.path ).to.deep.equal( [ 1 ] );
  499. expect( range.end.path ).to.deep.equal( [ 4 ] );
  500. } );
  501. } );
  502. describe( 'getFirstPosition()', () => {
  503. it( 'should return null if no ranges were added', () => {
  504. expect( selection.getFirstPosition() ).to.be.null;
  505. } );
  506. it( 'should return a position that is in selection and is before any other position from the selection', () => {
  507. // This will not be the first range despite being added as first
  508. selection.addRange( range2 );
  509. // This should be the first range.
  510. selection.addRange( range1 );
  511. // A random range that is not first.
  512. selection.addRange( range3 );
  513. const position = selection.getFirstPosition();
  514. expect( position.path ).to.deep.equal( [ 1 ] );
  515. } );
  516. } );
  517. describe( 'getLastRange()', () => {
  518. it( 'should return null if no ranges were added', () => {
  519. expect( selection.getLastRange() ).to.be.null;
  520. } );
  521. it( 'should return a range which start position is before all other ranges\' start positions', () => {
  522. selection.addRange( range3 );
  523. selection.addRange( range1 );
  524. selection.addRange( range2 );
  525. const range = selection.getLastRange();
  526. expect( range.start.path ).to.deep.equal( [ 6 ] );
  527. expect( range.end.path ).to.deep.equal( [ 7 ] );
  528. } );
  529. } );
  530. describe( 'getLastPosition()', () => {
  531. it( 'should return null if no ranges were added', () => {
  532. expect( selection.getLastPosition() ).to.be.null;
  533. } );
  534. it( 'should return a position that is in selection and is before any other position from the selection', () => {
  535. selection.addRange( range3 );
  536. selection.addRange( range1 );
  537. selection.addRange( range2 );
  538. const position = selection.getLastPosition();
  539. expect( position.path ).to.deep.equal( [ 7 ] );
  540. } );
  541. } );
  542. describe( 'isEqual()', () => {
  543. it( 'should return true if selections equal', () => {
  544. selection.addRange( range1 );
  545. selection.addRange( range2 );
  546. const otherSelection = new Selection();
  547. otherSelection.addRange( range1 );
  548. otherSelection.addRange( range2 );
  549. expect( selection.isEqual( otherSelection ) ).to.be.true;
  550. } );
  551. it( 'should return true if backward selections equal', () => {
  552. selection.addRange( range1, true );
  553. const otherSelection = new Selection();
  554. otherSelection.addRange( range1, true );
  555. expect( selection.isEqual( otherSelection ) ).to.be.true;
  556. } );
  557. it( 'should return true if both selections have no ranges', () => {
  558. const otherSelection = new Selection();
  559. expect( selection.isEqual( otherSelection ) ).to.be.true;
  560. } );
  561. it( 'should return false if ranges count does not equal', () => {
  562. selection.addRange( range1 );
  563. selection.addRange( range2 );
  564. const otherSelection = new Selection();
  565. otherSelection.addRange( range2 );
  566. expect( selection.isEqual( otherSelection ) ).to.be.false;
  567. } );
  568. it( 'should return false if ranges (other than the last added range) do not equal', () => {
  569. selection.addRange( range1 );
  570. selection.addRange( range3 );
  571. const otherSelection = new Selection();
  572. otherSelection.addRange( range2 );
  573. otherSelection.addRange( range3 );
  574. expect( selection.isEqual( otherSelection ) ).to.be.false;
  575. } );
  576. it( 'should return false if directions do not equal', () => {
  577. selection.addRange( range1 );
  578. const otherSelection = new Selection();
  579. otherSelection.addRange( range1, true );
  580. expect( selection.isEqual( otherSelection ) ).to.be.false;
  581. } );
  582. } );
  583. describe( 'collapseToStart()', () => {
  584. it( 'should collapse to start position and fire change event', () => {
  585. selection.setRanges( [ range2, range1, range3 ] );
  586. const spy = sinon.spy();
  587. selection.on( 'change:range', spy );
  588. selection.collapseToStart();
  589. expect( selection.rangeCount ).to.equal( 1 );
  590. expect( selection.isCollapsed ).to.be.true;
  591. expect( selection.getFirstPosition().isEqual( range1.start ) ).to.be.true;
  592. expect( spy.calledOnce ).to.be.true;
  593. } );
  594. it( 'should do nothing if selection was already collapsed', () => {
  595. selection.setCollapsedAt( range1.start );
  596. const spy = sinon.spy( selection, 'fire' );
  597. selection.collapseToStart();
  598. expect( spy.notCalled ).to.be.true;
  599. spy.restore();
  600. } );
  601. it( 'should do nothing if no ranges present', () => {
  602. const spy = sinon.spy( selection, 'fire' );
  603. selection.collapseToStart();
  604. spy.restore();
  605. expect( spy.notCalled ).to.be.true;
  606. } );
  607. } );
  608. describe( 'collapseToEnd()', () => {
  609. it( 'should collapse to start position and fire change:range event', () => {
  610. selection.setRanges( [ range2, range3, range1 ] );
  611. const spy = sinon.spy();
  612. selection.on( 'change:range', spy );
  613. selection.collapseToEnd();
  614. expect( selection.rangeCount ).to.equal( 1 );
  615. expect( selection.isCollapsed ).to.be.true;
  616. expect( selection.getLastPosition().isEqual( range3.end ) ).to.be.true;
  617. expect( spy.calledOnce ).to.be.true;
  618. } );
  619. it( 'should do nothing if selection was already collapsed', () => {
  620. selection.setCollapsedAt( range1.start );
  621. const spy = sinon.spy( selection, 'fire' );
  622. selection.collapseToEnd();
  623. expect( spy.notCalled ).to.be.true;
  624. spy.restore();
  625. } );
  626. it( 'should do nothing if selection has no ranges', () => {
  627. const spy = sinon.spy( selection, 'fire' );
  628. selection.collapseToEnd();
  629. expect( spy.notCalled ).to.be.true;
  630. spy.restore();
  631. } );
  632. } );
  633. describe( 'createFromSelection()', () => {
  634. it( 'should return a Selection instance with same ranges and direction as given selection', () => {
  635. selection.addRange( liveRange );
  636. selection.addRange( range, true );
  637. const snapshot = Selection.createFromSelection( selection );
  638. expect( selection.isBackward ).to.equal( snapshot.isBackward );
  639. const selectionRanges = Array.from( selection.getRanges() );
  640. const snapshotRanges = Array.from( snapshot.getRanges() );
  641. expect( selectionRanges.length ).to.equal( snapshotRanges.length );
  642. for ( let i = 0; i < selectionRanges.length; i++ ) {
  643. expect( selectionRanges[ i ].isEqual( snapshotRanges[ i ] ) ).to.be.true;
  644. }
  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.setRanges( [ 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.setRanges( [ 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.setRanges( [ 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.setRanges( [ 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( 'clearAttributes()', () => {
  877. it( 'should remove all attributes from the element', () => {
  878. selection.setRanges( [ rangeInFullP ] );
  879. selection.setAttribute( 'foo', 'bar' );
  880. selection.setAttribute( 'abc', 'xyz' );
  881. selection.clearAttributes();
  882. expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
  883. expect( selection.getAttribute( 'abc' ) ).to.be.undefined;
  884. } );
  885. it( 'should fire change:attribute event with correct parameters', () => {
  886. selection.setAttribute( 'foo', 'bar' );
  887. selection.on( 'change:attribute', ( evt, data ) => {
  888. expect( data.directChange ).to.be.true;
  889. expect( data.attributeKeys ).to.deep.equal( [ 'foo' ] );
  890. } );
  891. selection.clearAttributes();
  892. } );
  893. it( 'should not fire change:attribute event if there were no attributes', () => {
  894. const spy = sinon.spy();
  895. selection.on( 'change:attribute', spy );
  896. selection.clearAttributes();
  897. expect( spy.called ).to.be.false;
  898. } );
  899. } );
  900. describe( 'removeAttribute()', () => {
  901. it( 'should remove attribute', () => {
  902. selection.setRanges( [ rangeInFullP ] );
  903. selection.setAttribute( 'foo', 'bar' );
  904. selection.removeAttribute( 'foo' );
  905. expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
  906. } );
  907. it( 'should fire change:attribute event with correct parameters', () => {
  908. selection.setAttribute( 'foo', 'bar' );
  909. selection.on( 'change:attribute', ( evt, data ) => {
  910. expect( data.directChange ).to.be.true;
  911. expect( data.attributeKeys ).to.deep.equal( [ 'foo' ] );
  912. } );
  913. selection.removeAttribute( 'foo' );
  914. } );
  915. it( 'should not fire change:attribute event if such attribute did not exist', () => {
  916. const spy = sinon.spy();
  917. selection.on( 'change:attribute', spy );
  918. selection.removeAttribute( 'foo' );
  919. expect( spy.called ).to.be.false;
  920. } );
  921. } );
  922. describe( 'setAttributesTo()', () => {
  923. it( 'should remove all attributes set on element and set the given ones', () => {
  924. selection.setAttribute( 'abc', 'xyz' );
  925. selection.setAttributesTo( { foo: 'bar' } );
  926. expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  927. expect( selection.getAttribute( 'abc' ) ).to.be.undefined;
  928. } );
  929. it( 'should fire only one change:attribute event', () => {
  930. selection.setAttributesTo( { foo: 'bar', xxx: 'yyy' } );
  931. const spy = sinon.spy();
  932. selection.on( 'change:attribute', spy );
  933. selection.setAttributesTo( { foo: 'bar', abc: 'def' } );
  934. expect( spy.calledOnce ).to.be.true;
  935. } );
  936. it( 'should fire change:attribute event with correct parameters', () => {
  937. selection.setAttributesTo( { foo: 'bar', xxx: 'yyy' } );
  938. selection.on( 'change:attribute', ( evt, data ) => {
  939. expect( data.directChange ).to.be.true;
  940. expect( data.attributeKeys ).to.deep.equal( [ 'abc', 'xxx' ] );
  941. } );
  942. selection.setAttributesTo( { foo: 'bar', abc: 'def' } );
  943. } );
  944. it( 'should not fire change:attribute event if attributes had not changed', () => {
  945. selection.setRanges( [ rangeInFullP ] );
  946. selection.setAttributesTo( { foo: 'bar', xxx: 'yyy' } );
  947. const spy = sinon.spy();
  948. selection.on( 'change:attribute', spy );
  949. selection.setAttributesTo( { xxx: 'yyy', foo: 'bar' } );
  950. expect( spy.called ).to.be.false;
  951. } );
  952. } );
  953. } );
  954. describe( 'containsEntireContent()', () => {
  955. beforeEach( () => {
  956. model.schema.register( 'p', { inheritAllFrom: '$block' } );
  957. model.schema.extend( 'p', { allowIn: '$root' } );
  958. } );
  959. it( 'returns true if the entire content in $root is selected', () => {
  960. setData( model, '<p>[Foo</p><p>Bom</p><p>Bar]</p>' );
  961. expect( doc.selection.containsEntireContent() ).to.equal( true );
  962. } );
  963. it( 'returns false when only a fragment of the content in $root is selected', () => {
  964. setData( model, '<p>Fo[o</p><p>Bom</p><p>Bar]</p>' );
  965. expect( doc.selection.containsEntireContent() ).to.equal( false );
  966. } );
  967. it( 'returns true if the entire content in specified element is selected', () => {
  968. setData( model, '<p>Foo</p><p>[Bom]</p><p>Bar</p>' );
  969. const root = doc.getRoot();
  970. const secondParagraph = root.getNodeByPath( [ 1 ] );
  971. expect( doc.selection.containsEntireContent( secondParagraph ) ).to.equal( true );
  972. } );
  973. it( 'returns false if the entire content in specified element is not selected', () => {
  974. setData( model, '<p>Foo</p><p>[Bom</p><p>B]ar</p>' );
  975. const root = doc.getRoot();
  976. const secondParagraph = root.getNodeByPath( [ 1 ] );
  977. expect( doc.selection.containsEntireContent( secondParagraph ) ).to.equal( false );
  978. } );
  979. it( 'returns false when the entire content except an empty element is selected', () => {
  980. model.schema.register( 'img', {
  981. allowIn: 'p'
  982. } );
  983. setData( model, '<p><img></img>[Foo]</p>' );
  984. expect( doc.selection.containsEntireContent() ).to.equal( false );
  985. } );
  986. it( 'returns true if the content is empty', () => {
  987. setData( model, '[]' );
  988. expect( doc.selection.containsEntireContent() ).to.equal( true );
  989. } );
  990. it( 'returns false if empty selection is at the end of non-empty content', () => {
  991. setData( model, '<p>Foo bar bom.</p>[]' );
  992. expect( doc.selection.containsEntireContent() ).to.equal( false );
  993. } );
  994. } );
  995. } );