selection.js 42 KB

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