selection.js 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369
  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( 'p', { inheritAllFrom: '$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.register( 'p', { inheritAllFrom: '$block' } );
  677. model.schema.register( 'h', { inheritAllFrom: '$block' } );
  678. model.schema.register( 'blockquote' );
  679. model.schema.extend( 'blockquote', { allowIn: '$root' } );
  680. model.schema.extend( '$block', { allowIn: 'blockquote' } );
  681. model.schema.register( 'image' );
  682. model.schema.extend( 'image', { allowIn: '$root' } );
  683. model.schema.extend( 'image', { allowIn: '$block' } );
  684. model.schema.extend( '$text', { allowIn: 'image' } );
  685. // Special block which can contain another blocks.
  686. model.schema.register( 'nestedBlock', { inheritAllFrom: '$block' } );
  687. model.schema.extend( 'nestedBlock', { allowIn: '$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.register( 'p', { inheritAllFrom: '$block' } );
  955. model.schema.extend( 'p', { allowIn: '$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.register( 'img', {
  979. allowIn: 'p'
  980. } );
  981. setData( model, '<p><img></img>[Foo]</p>' );
  982. expect( doc.selection.containsEntireContent() ).to.equal( false );
  983. } );
  984. it( 'returns true if the content is empty', () => {
  985. setData( model, '[]' );
  986. expect( doc.selection.containsEntireContent() ).to.equal( true );
  987. } );
  988. it( 'returns false if empty selection is at the end of non-empty content', () => {
  989. setData( model, '<p>Foo bar bom.</p>[]' );
  990. expect( doc.selection.containsEntireContent() ).to.equal( false );
  991. } );
  992. } );
  993. } );