selection.js 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368
  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.registerItem( 'p', '$block' );
  652. } );
  653. it( 'should return selected element', () => {
  654. const { selection, model } = parse( '<p>foo</p>[<p>bar</p>]<p>baz</p>', schema );
  655. const p = model.getChild( 1 );
  656. expect( selection.getSelectedElement() ).to.equal( p );
  657. } );
  658. it( 'should return null if there is more than one range', () => {
  659. const { selection } = parse( '[<p>foo</p>][<p>bar</p>]<p>baz</p>', schema );
  660. expect( selection.getSelectedElement() ).to.be.null;
  661. } );
  662. it( 'should return null if there is no selection', () => {
  663. expect( selection.getSelectedElement() ).to.be.null;
  664. } );
  665. it( 'should return null if selection is not over single element #1', () => {
  666. const { selection } = parse( '<p>foo</p>[<p>bar</p><p>baz}</p>', schema );
  667. expect( selection.getSelectedElement() ).to.be.null;
  668. } );
  669. it( 'should return null if selection is not over single element #2', () => {
  670. const { selection } = parse( '<p>{bar}</p>', schema );
  671. expect( selection.getSelectedElement() ).to.be.null;
  672. } );
  673. } );
  674. describe( 'getSelectedBlocks()', () => {
  675. beforeEach( () => {
  676. model.schema.registerItem( 'p', '$block' );
  677. model.schema.registerItem( 'h', '$block' );
  678. model.schema.registerItem( 'blockquote' );
  679. model.schema.allow( { name: 'blockquote', inside: '$root' } );
  680. model.schema.allow( { name: '$block', inside: 'blockquote' } );
  681. model.schema.registerItem( 'image' );
  682. model.schema.allow( { name: 'image', inside: '$root' } );
  683. model.schema.allow( { name: 'image', inside: '$block' } );
  684. model.schema.allow( { name: '$text', inside: 'image' } );
  685. // Special block which can contain another blocks.
  686. model.schema.registerItem( 'nestedBlock', '$block' );
  687. model.schema.allow( { name: 'nestedBlock', inside: '$block' } );
  688. } );
  689. it( 'returns an iterator', () => {
  690. setData( model, '<p>a</p><p>[]b</p><p>c</p>' );
  691. expect( doc.selection.getSelectedBlocks().next ).to.be.a( 'function' );
  692. } );
  693. it( 'returns block for a collapsed selection', () => {
  694. setData( model, '<p>a</p><p>[]b</p><p>c</p>' );
  695. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'b' ] );
  696. } );
  697. it( 'returns block for a collapsed selection (empty block)', () => {
  698. setData( model, '<p>a</p><p>[]</p><p>c</p>' );
  699. const blocks = Array.from( doc.selection.getSelectedBlocks() );
  700. expect( blocks ).to.have.length( 1 );
  701. expect( blocks[ 0 ].childCount ).to.equal( 0 );
  702. } );
  703. it( 'returns block for a non collapsed selection', () => {
  704. setData( model, '<p>a</p><p>[b]</p><p>c</p>' );
  705. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'b' ] );
  706. } );
  707. it( 'returns two blocks for a non collapsed selection', () => {
  708. setData( model, '<p>a</p><h>[b</h><p>c]</p><p>d</p>' );
  709. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'b', 'c' ] );
  710. } );
  711. it( 'returns two blocks for a non collapsed selection (starts at block end)', () => {
  712. setData( model, '<p>a</p><h>b[</h><p>c]</p><p>d</p>' );
  713. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'b', 'c' ] );
  714. } );
  715. it( 'returns proper block for a multi-range selection', () => {
  716. setData( model, '<p>a</p><h>[b</h><p>c]</p><p>d</p><p>[e]</p>' );
  717. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'b', 'c', 'e' ] );
  718. } );
  719. it( 'does not return a block twice if two ranges are anchored in it', () => {
  720. setData( model, '<p>[a]b[c]</p>' );
  721. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'abc' ] );
  722. } );
  723. it( 'returns only blocks', () => {
  724. setData( model, '<p>[a</p><image>b</image><p>c]</p>' );
  725. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'a', 'c' ] );
  726. } );
  727. it( 'gets deeper into the tree', () => {
  728. setData( model, '<p>[a</p><blockquote><p>b</p><p>c</p></blockquote><p>d]</p>' );
  729. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'a', 'b', 'c', 'd' ] );
  730. } );
  731. it( 'gets deeper into the tree (end deeper)', () => {
  732. setData( model, '<p>[a</p><blockquote><p>b]</p><p>c</p></blockquote><p>d</p>' );
  733. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'a', 'b' ] );
  734. } );
  735. it( 'gets deeper into the tree (start deeper)', () => {
  736. setData( model, '<p>a</p><blockquote><p>b</p><p>[c</p></blockquote><p>d]</p>' );
  737. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'c', 'd' ] );
  738. } );
  739. it( 'returns an empty array if none of the selected elements is a block', () => {
  740. setData( model, '<p>a</p><image>[a</image><image>b]</image><p>b</p>' );
  741. expect( toText( doc.selection.getSelectedBlocks() ) ).to.be.empty;
  742. } );
  743. it( 'returns an empty array if the selected element is not a block', () => {
  744. setData( model, '<p>a</p><image>[]a</image><p>b</p>' );
  745. expect( toText( doc.selection.getSelectedBlocks() ) ).to.be.empty;
  746. } );
  747. // Super edge case – should not happen (blocks should never be nested),
  748. // but since the code handles it already it's worth testing.
  749. it( 'returns only the lowest block if blocks are nested', () => {
  750. setData( model, '<nestedBlock>a<nestedBlock>[]b</nestedBlock></nestedBlock>' );
  751. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'b' ] );
  752. } );
  753. // Like above but trickier.
  754. it( 'returns only the lowest block if blocks are nested', () => {
  755. setData(
  756. model,
  757. '<nestedBlock>a<nestedBlock>[b</nestedBlock></nestedBlock>' +
  758. '<nestedBlock>c<nestedBlock>d]</nestedBlock></nestedBlock>'
  759. );
  760. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'b', 'd' ] );
  761. } );
  762. it( 'returns nothing if directly in a root', () => {
  763. doc.createRoot( 'p', 'inlineOnlyRoot' );
  764. setData( model, 'a[b]c', { rootName: 'inlineOnlyRoot' } );
  765. expect( toText( doc.selection.getSelectedBlocks() ) ).to.be.empty;
  766. } );
  767. describe( '#984', () => {
  768. it( 'does not return the last block if none of its content is selected', () => {
  769. setData( model, '<p>[a</p><p>b</p><p>]c</p>' );
  770. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'a', 'b' ] );
  771. } );
  772. it( 'returns only the first block for a non collapsed selection if only end of selection is touching a block', () => {
  773. setData( model, '<p>a</p><h>b[</h><p>]c</p><p>d</p>' );
  774. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'b' ] );
  775. } );
  776. it( 'does not return the last block if none of its content is selected (nested case)', () => {
  777. setData( model, '<p>[a</p><nestedBlock><nestedBlock>]b</nestedBlock></nestedBlock>' );
  778. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'a' ] );
  779. } );
  780. // Like a super edge case, we can live with this behavior as I don't even know what we could expect here
  781. // since only the innermost block is considerd a block to return (so the <nB>b...</nB> needs to be ignored).
  782. it( 'does not return the last block if none of its content is selected (nested case, wrapper with a content)', () => {
  783. setData( model, '<p>[a</p><nestedBlock>b<nestedBlock>]c</nestedBlock></nestedBlock>' );
  784. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'a' ] );
  785. } );
  786. it( 'returns the last block if at least one of its child nodes is selected', () => {
  787. setData( model, '<p>[a</p><p>b</p><p><image></image>]c</p>' );
  788. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'a', 'b', 'c' ] );
  789. } );
  790. // I needed these last 2 cases to justify the use of isTouching() instead of simple `offset == 0` check.
  791. it( 'returns the last block if at least one of its child nodes is selected (end in an inline element)', () => {
  792. setData( model, '<p>[a</p><p>b</p><p><image>x]</image>c</p>' );
  793. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'a', 'b', 'c' ] );
  794. } );
  795. it(
  796. 'does not return the last block if at least one of its child nodes is selected ' +
  797. '(end in an inline element, no content selected)',
  798. () => {
  799. setData( model, '<p>[a</p><p>b</p><p><image>]x</image>c</p>' );
  800. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'a', 'b' ] );
  801. }
  802. );
  803. } );
  804. // Map all elements to data of its first child text node.
  805. function toText( elements ) {
  806. return Array.from( elements ).map( el => {
  807. return Array.from( el.getChildren() ).find( child => child.data ).data;
  808. } );
  809. }
  810. } );
  811. describe( 'attributes interface', () => {
  812. let rangeInFullP;
  813. beforeEach( () => {
  814. root.insertChildren( 0, [
  815. new Element( 'p', [], new Text( 'foobar' ) ),
  816. new Element( 'p', [], [] )
  817. ] );
  818. rangeInFullP = new Range( new Position( root, [ 0, 4 ] ), new Position( root, [ 0, 4 ] ) );
  819. } );
  820. describe( 'setAttribute()', () => {
  821. it( 'should set given attribute on the selection', () => {
  822. selection.setRanges( [ rangeInFullP ] );
  823. selection.setAttribute( 'foo', 'bar' );
  824. expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  825. } );
  826. it( 'should fire change:attribute event with correct parameters', () => {
  827. selection.on( 'change:attribute', ( evt, data ) => {
  828. expect( data.directChange ).to.be.true;
  829. expect( data.attributeKeys ).to.deep.equal( [ 'foo' ] );
  830. } );
  831. selection.setAttribute( 'foo', 'bar' );
  832. } );
  833. it( 'should not fire change:attribute event if attribute with same key and value was already set', () => {
  834. selection.setAttribute( 'foo', 'bar' );
  835. const spy = sinon.spy();
  836. selection.on( 'change:attribute', spy );
  837. selection.setAttribute( 'foo', 'bar' );
  838. expect( spy.called ).to.be.false;
  839. } );
  840. } );
  841. describe( 'getAttribute()', () => {
  842. it( 'should return undefined if element does not contain given attribute', () => {
  843. expect( selection.getAttribute( 'abc' ) ).to.be.undefined;
  844. } );
  845. } );
  846. describe( 'getAttributes()', () => {
  847. it( 'should return an iterator that iterates over all attributes set on selection', () => {
  848. selection.setRanges( [ rangeInFullP ] );
  849. selection.setAttribute( 'foo', 'bar' );
  850. selection.setAttribute( 'abc', 'xyz' );
  851. const attrs = Array.from( selection.getAttributes() );
  852. expect( attrs ).to.deep.equal( [ [ 'foo', 'bar' ], [ 'abc', 'xyz' ] ] );
  853. } );
  854. } );
  855. describe( 'getAttributeKeys()', () => {
  856. it( 'should return iterator that iterates over all attribute keys set on selection', () => {
  857. selection.setRanges( [ rangeInFullP ] );
  858. selection.setAttribute( 'foo', 'bar' );
  859. selection.setAttribute( 'abc', 'xyz' );
  860. const attrs = Array.from( selection.getAttributeKeys() );
  861. expect( attrs ).to.deep.equal( [ 'foo', 'abc' ] );
  862. } );
  863. } );
  864. describe( 'hasAttribute()', () => {
  865. it( 'should return true if element contains attribute with given key', () => {
  866. selection.setRanges( [ rangeInFullP ] );
  867. selection.setAttribute( 'foo', 'bar' );
  868. expect( selection.hasAttribute( 'foo' ) ).to.be.true;
  869. } );
  870. it( 'should return false if element does not contain attribute with given key', () => {
  871. expect( selection.hasAttribute( 'abc' ) ).to.be.false;
  872. } );
  873. } );
  874. describe( 'clearAttributes()', () => {
  875. it( 'should remove all attributes from the element', () => {
  876. selection.setRanges( [ rangeInFullP ] );
  877. selection.setAttribute( 'foo', 'bar' );
  878. selection.setAttribute( 'abc', 'xyz' );
  879. selection.clearAttributes();
  880. expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
  881. expect( selection.getAttribute( 'abc' ) ).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.clearAttributes();
  890. } );
  891. it( 'should not fire change:attribute event if there were no attributes', () => {
  892. const spy = sinon.spy();
  893. selection.on( 'change:attribute', spy );
  894. selection.clearAttributes();
  895. expect( spy.called ).to.be.false;
  896. } );
  897. } );
  898. describe( 'removeAttribute()', () => {
  899. it( 'should remove attribute', () => {
  900. selection.setRanges( [ rangeInFullP ] );
  901. selection.setAttribute( 'foo', 'bar' );
  902. selection.removeAttribute( 'foo' );
  903. expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
  904. } );
  905. it( 'should fire change:attribute event with correct parameters', () => {
  906. selection.setAttribute( 'foo', 'bar' );
  907. selection.on( 'change:attribute', ( evt, data ) => {
  908. expect( data.directChange ).to.be.true;
  909. expect( data.attributeKeys ).to.deep.equal( [ 'foo' ] );
  910. } );
  911. selection.removeAttribute( 'foo' );
  912. } );
  913. it( 'should not fire change:attribute event if such attribute did not exist', () => {
  914. const spy = sinon.spy();
  915. selection.on( 'change:attribute', spy );
  916. selection.removeAttribute( 'foo' );
  917. expect( spy.called ).to.be.false;
  918. } );
  919. } );
  920. describe( 'setAttributesTo()', () => {
  921. it( 'should remove all attributes set on element and set the given ones', () => {
  922. selection.setAttribute( 'abc', 'xyz' );
  923. selection.setAttributesTo( { foo: 'bar' } );
  924. expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  925. expect( selection.getAttribute( 'abc' ) ).to.be.undefined;
  926. } );
  927. it( 'should fire only one change:attribute event', () => {
  928. selection.setAttributesTo( { foo: 'bar', xxx: 'yyy' } );
  929. const spy = sinon.spy();
  930. selection.on( 'change:attribute', spy );
  931. selection.setAttributesTo( { foo: 'bar', abc: 'def' } );
  932. expect( spy.calledOnce ).to.be.true;
  933. } );
  934. it( 'should fire change:attribute event with correct parameters', () => {
  935. selection.setAttributesTo( { foo: 'bar', xxx: 'yyy' } );
  936. selection.on( 'change:attribute', ( evt, data ) => {
  937. expect( data.directChange ).to.be.true;
  938. expect( data.attributeKeys ).to.deep.equal( [ 'abc', 'xxx' ] );
  939. } );
  940. selection.setAttributesTo( { foo: 'bar', abc: 'def' } );
  941. } );
  942. it( 'should not fire change:attribute event if attributes had not changed', () => {
  943. selection.setRanges( [ rangeInFullP ] );
  944. selection.setAttributesTo( { foo: 'bar', xxx: 'yyy' } );
  945. const spy = sinon.spy();
  946. selection.on( 'change:attribute', spy );
  947. selection.setAttributesTo( { xxx: 'yyy', foo: 'bar' } );
  948. expect( spy.called ).to.be.false;
  949. } );
  950. } );
  951. } );
  952. describe( 'containsEntireContent()', () => {
  953. beforeEach( () => {
  954. model.schema.registerItem( 'p', '$block' );
  955. model.schema.allow( { name: 'p', inside: '$root' } );
  956. } );
  957. it( 'returns true if the entire content in $root is selected', () => {
  958. setData( model, '<p>[Foo</p><p>Bom</p><p>Bar]</p>' );
  959. expect( doc.selection.containsEntireContent() ).to.equal( true );
  960. } );
  961. it( 'returns false when only a fragment of the content in $root is selected', () => {
  962. setData( model, '<p>Fo[o</p><p>Bom</p><p>Bar]</p>' );
  963. expect( doc.selection.containsEntireContent() ).to.equal( false );
  964. } );
  965. it( 'returns true if the entire content in specified element is selected', () => {
  966. setData( model, '<p>Foo</p><p>[Bom]</p><p>Bar</p>' );
  967. const root = doc.getRoot();
  968. const secondParagraph = root.getNodeByPath( [ 1 ] );
  969. expect( doc.selection.containsEntireContent( secondParagraph ) ).to.equal( true );
  970. } );
  971. it( 'returns false if the entire content in specified element is not selected', () => {
  972. setData( model, '<p>Foo</p><p>[Bom</p><p>B]ar</p>' );
  973. const root = doc.getRoot();
  974. const secondParagraph = root.getNodeByPath( [ 1 ] );
  975. expect( doc.selection.containsEntireContent( secondParagraph ) ).to.equal( false );
  976. } );
  977. it( 'returns false when the entire content except an empty element is selected', () => {
  978. model.schema.registerItem( 'img', '$inline' );
  979. model.schema.allow( { name: 'img', inside: 'p' } );
  980. setData( model, '<p><img></img>[Foo]</p>' );
  981. expect( doc.selection.containsEntireContent() ).to.equal( false );
  982. } );
  983. it( 'returns true if the content is empty', () => {
  984. setData( model, '[]' );
  985. expect( doc.selection.containsEntireContent() ).to.equal( true );
  986. } );
  987. it( 'returns false if empty selection is at the end of non-empty content', () => {
  988. setData( model, '<p>Foo bar bom.</p>[]' );
  989. expect( doc.selection.containsEntireContent() ).to.equal( false );
  990. } );
  991. } );
  992. } );