selection.js 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301
  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. describe( 'Selection', () => {
  18. let model, doc, root, selection, liveRange, range, range1, range2, range3;
  19. testUtils.createSinonSandbox();
  20. beforeEach( () => {
  21. model = new Model();
  22. doc = model.document;
  23. root = doc.createRoot();
  24. root._appendChild( [
  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._createAt( 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. // TODO: why this test?
  428. it.skip( 'uses Position._createAt', () => {
  429. const startPos = Position._createAt( root, 1 );
  430. const endPos = Position._createAt( root, 2 );
  431. const newEndPos = Position._createAt( root, 4 );
  432. const spy = testUtils.sinon.stub( Position, '_createAt' ).returns( newEndPos );
  433. selection.setTo( new Range( startPos, endPos ) );
  434. selection.setFocus( root, 'end' );
  435. expect( spy.calledOnce ).to.be.true;
  436. expect( selection.focus.compareWith( newEndPos ) ).to.equal( 'same' );
  437. } );
  438. } );
  439. describe( 'setTo - selection set to null', () => {
  440. let spy;
  441. it( 'should remove all stored ranges', () => {
  442. selection.setTo( [ liveRange, range ] );
  443. selection.setTo( null );
  444. expect( Array.from( selection.getRanges() ).length ).to.equal( 0 );
  445. } );
  446. it( 'should fire exactly one change:range event', () => {
  447. selection.setTo( [ liveRange, range ] );
  448. spy = sinon.spy();
  449. selection.on( 'change:range', spy );
  450. selection.setTo( null );
  451. expect( spy.calledOnce ).to.be.true;
  452. } );
  453. it( 'should not fire change:range event if there were no ranges', () => {
  454. spy = sinon.spy();
  455. selection.on( 'change:range', spy );
  456. selection.setTo( null );
  457. expect( spy.called ).to.be.false;
  458. } );
  459. } );
  460. describe( '_setRanges()', () => {
  461. let newRanges, spy;
  462. beforeEach( () => {
  463. newRanges = [
  464. new Range( new Position( root, [ 4 ] ), new Position( root, [ 5 ] ) ),
  465. new Range( new Position( root, [ 5, 0 ] ), new Position( root, [ 6, 0 ] ) )
  466. ];
  467. selection.setTo( [ liveRange, range ] );
  468. spy = sinon.spy();
  469. selection.on( 'change:range', spy );
  470. } );
  471. it( 'should throw an error when range is invalid', () => {
  472. expect( () => {
  473. selection._setRanges( [ { invalid: 'range' } ] );
  474. } ).to.throw( CKEditorError, /model-selection-set-ranges-not-range/ );
  475. } );
  476. it( 'should remove all ranges and add given ranges', () => {
  477. selection._setRanges( newRanges );
  478. const ranges = Array.from( selection.getRanges() );
  479. expect( ranges ).to.deep.equal( newRanges );
  480. } );
  481. it( 'should use last range from given array to get anchor and focus position', () => {
  482. selection._setRanges( newRanges );
  483. expect( selection.anchor.path ).to.deep.equal( [ 5, 0 ] );
  484. expect( selection.focus.path ).to.deep.equal( [ 6, 0 ] );
  485. } );
  486. it( 'should acknowledge backward flag when setting anchor and focus', () => {
  487. selection._setRanges( newRanges, { backward: true } );
  488. expect( selection.anchor.path ).to.deep.equal( [ 6, 0 ] );
  489. expect( selection.focus.path ).to.deep.equal( [ 5, 0 ] );
  490. } );
  491. it( 'should fire exactly one change:range event', () => {
  492. selection._setRanges( newRanges );
  493. expect( spy.calledOnce ).to.be.true;
  494. } );
  495. it( 'should fire change:range event with correct parameters', () => {
  496. selection.on( 'change:range', ( evt, data ) => {
  497. expect( data.directChange ).to.be.true;
  498. } );
  499. selection._setRanges( newRanges );
  500. } );
  501. it( 'should not fire change:range event if given ranges are the same', () => {
  502. selection._setRanges( [ liveRange, range ] );
  503. expect( spy.calledOnce ).to.be.false;
  504. } );
  505. it( 'should copy added ranges and store multiple ranges', () => {
  506. selection._setRanges( [ liveRange, range ] );
  507. const ranges = selection._ranges;
  508. expect( ranges.length ).to.equal( 2 );
  509. expect( ranges[ 0 ].isEqual( liveRange ) ).to.be.true;
  510. expect( ranges[ 1 ].isEqual( range ) ).to.be.true;
  511. expect( ranges[ 0 ] ).not.to.be.equal( liveRange );
  512. expect( ranges[ 1 ] ).not.to.be.equal( range );
  513. } );
  514. it( 'should set anchor and focus to the start and end of the last added range', () => {
  515. selection._setRanges( [ liveRange, range ] );
  516. expect( selection.anchor.path ).to.deep.equal( [ 2 ] );
  517. expect( selection.focus.path ).to.deep.equal( [ 2, 2 ] );
  518. } );
  519. it( 'should set anchor and focus to the end and start of the most recently added range if backward flag was used', () => {
  520. selection._setRanges( [ liveRange, range ], { backward: true } );
  521. expect( selection.anchor.path ).to.deep.equal( [ 2 ] );
  522. expect( selection.focus.path ).to.deep.equal( [ 2, 2 ] );
  523. } );
  524. } );
  525. describe( 'getFirstRange()', () => {
  526. it( 'should return null if no ranges were added', () => {
  527. expect( selection.getFirstRange() ).to.be.null;
  528. } );
  529. it( 'should return a range which start position is before all other ranges\' start positions', () => {
  530. selection.setTo( [
  531. // This will not be the first range despite being added as first
  532. range2,
  533. // This should be the first range.
  534. range1,
  535. // A random range that is not first.
  536. range3
  537. ] );
  538. const range = selection.getFirstRange();
  539. expect( range.start.path ).to.deep.equal( [ 1 ] );
  540. expect( range.end.path ).to.deep.equal( [ 4 ] );
  541. } );
  542. } );
  543. describe( 'getFirstPosition()', () => {
  544. it( 'should return null if no ranges were added', () => {
  545. expect( selection.getFirstPosition() ).to.be.null;
  546. } );
  547. it( 'should return a position that is in selection and is before any other position from the selection', () => {
  548. selection.setTo( [
  549. // This will not be the first range despite being added as first
  550. range2,
  551. // This should be the first range.
  552. range1,
  553. // A random range that is not first.
  554. range3
  555. ] );
  556. const position = selection.getFirstPosition();
  557. expect( position.path ).to.deep.equal( [ 1 ] );
  558. } );
  559. } );
  560. describe( 'getLastRange()', () => {
  561. it( 'should return null if no ranges were added', () => {
  562. expect( selection.getLastRange() ).to.be.null;
  563. } );
  564. it( 'should return a range which start position is before all other ranges\' start positions', () => {
  565. selection.setTo( [ range3, range1, range2 ] );
  566. const range = selection.getLastRange();
  567. expect( range.start.path ).to.deep.equal( [ 6 ] );
  568. expect( range.end.path ).to.deep.equal( [ 7 ] );
  569. } );
  570. } );
  571. describe( 'getLastPosition()', () => {
  572. it( 'should return null if no ranges were added', () => {
  573. expect( selection.getLastPosition() ).to.be.null;
  574. } );
  575. it( 'should return a position that is in selection and is before any other position from the selection', () => {
  576. selection.setTo( [ range3, range1, range2 ] );
  577. const position = selection.getLastPosition();
  578. expect( position.path ).to.deep.equal( [ 7 ] );
  579. } );
  580. } );
  581. describe( 'isEqual()', () => {
  582. it( 'should return true if selections equal', () => {
  583. selection.setTo( [ range1, range2 ] );
  584. const otherSelection = new Selection( [ range1, range2 ] );
  585. expect( selection.isEqual( otherSelection ) ).to.be.true;
  586. } );
  587. it( 'should return true if backward selections equal', () => {
  588. selection.setTo( [ range1 ], { backward: true } );
  589. const otherSelection = new Selection( [ range1 ], { backward: true } );
  590. expect( selection.isEqual( otherSelection ) ).to.be.true;
  591. } );
  592. it( 'should return true if both selections have no ranges', () => {
  593. const otherSelection = new Selection();
  594. expect( selection.isEqual( otherSelection ) ).to.be.true;
  595. } );
  596. it( 'should return false if ranges count does not equal', () => {
  597. selection.setTo( [ range1, range2 ] );
  598. const otherSelection = new Selection( [ range2 ] );
  599. expect( selection.isEqual( otherSelection ) ).to.be.false;
  600. } );
  601. it( 'should return false if ranges (other than the last added range) do not equal', () => {
  602. selection.setTo( [ range1, range3 ] );
  603. const otherSelection = new Selection( [ range2, range3 ] );
  604. expect( selection.isEqual( otherSelection ) ).to.be.false;
  605. } );
  606. it( 'should return false if directions do not equal', () => {
  607. selection.setTo( range1 );
  608. const otherSelection = new Selection( [ range1 ], { backward: true } );
  609. expect( selection.isEqual( otherSelection ) ).to.be.false;
  610. } );
  611. } );
  612. describe( 'setTo - used to collapse at start', () => {
  613. it( 'should collapse to start position and fire change event', () => {
  614. selection.setTo( [ range2, range1, range3 ] );
  615. const spy = sinon.spy();
  616. selection.on( 'change:range', spy );
  617. selection.setTo( selection.getFirstPosition() );
  618. expect( selection.rangeCount ).to.equal( 1 );
  619. expect( selection.isCollapsed ).to.be.true;
  620. expect( selection.getFirstPosition().isEqual( range1.start ) ).to.be.true;
  621. expect( spy.calledOnce ).to.be.true;
  622. } );
  623. it( 'should do nothing if selection was already collapsed', () => {
  624. selection.setTo( range1.start );
  625. const spy = sinon.spy( selection, 'fire' );
  626. selection.setTo( selection.getFirstPosition() );
  627. expect( spy.notCalled ).to.be.true;
  628. spy.restore();
  629. } );
  630. it( 'should do nothing if no ranges present', () => {
  631. const spy = sinon.spy( selection, 'fire' );
  632. selection.setTo( selection.getFirstPosition() );
  633. spy.restore();
  634. expect( spy.notCalled ).to.be.true;
  635. } );
  636. } );
  637. describe( 'setTo - used to collapse at end', () => {
  638. it( 'should collapse to start position and fire change:range event', () => {
  639. selection.setTo( [ range2, range3, range1 ] );
  640. const spy = sinon.spy();
  641. selection.on( 'change:range', spy );
  642. selection.setTo( selection.getLastPosition() );
  643. expect( selection.rangeCount ).to.equal( 1 );
  644. expect( selection.isCollapsed ).to.be.true;
  645. expect( selection.getLastPosition().isEqual( range3.end ) ).to.be.true;
  646. expect( spy.calledOnce ).to.be.true;
  647. } );
  648. it( 'should do nothing if selection was already collapsed', () => {
  649. selection.setTo( range1.start );
  650. const spy = sinon.spy( selection, 'fire' );
  651. selection.setTo( selection.getLastPosition() );
  652. expect( spy.notCalled ).to.be.true;
  653. spy.restore();
  654. } );
  655. it( 'should do nothing if selection has no ranges', () => {
  656. const spy = sinon.spy( selection, 'fire' );
  657. selection.setTo( selection.getLastPosition() );
  658. expect( spy.notCalled ).to.be.true;
  659. spy.restore();
  660. } );
  661. } );
  662. describe( 'getSelectedElement()', () => {
  663. let schema;
  664. beforeEach( () => {
  665. schema = new Schema();
  666. schema.register( '$root' );
  667. schema.register( 'p', { allowIn: '$root' } );
  668. schema.register( '$text', { allowIn: 'p' } );
  669. } );
  670. it( 'should return selected element', () => {
  671. const { selection, model } = parse( '<p>foo</p>[<p>bar</p>]<p>baz</p>', schema );
  672. const p = model.getChild( 1 );
  673. expect( selection.getSelectedElement() ).to.equal( p );
  674. } );
  675. it( 'should return null if there is more than one range', () => {
  676. const { selection } = parse( '[<p>foo</p>][<p>bar</p>]<p>baz</p>', schema );
  677. expect( selection.getSelectedElement() ).to.be.null;
  678. } );
  679. it( 'should return null if there is no selection', () => {
  680. expect( selection.getSelectedElement() ).to.be.null;
  681. } );
  682. it( 'should return null if selection is not over single element #1', () => {
  683. const { selection } = parse( '<p>foo</p>[<p>bar</p><p>baz}</p>', schema );
  684. expect( selection.getSelectedElement() ).to.be.null;
  685. } );
  686. it( 'should return null if selection is not over single element #2', () => {
  687. const { selection } = parse( '<p>{bar}</p>', schema );
  688. expect( selection.getSelectedElement() ).to.be.null;
  689. } );
  690. } );
  691. describe( 'getSelectedBlocks()', () => {
  692. beforeEach( () => {
  693. model.schema.register( 'p', { inheritAllFrom: '$block' } );
  694. model.schema.register( 'h', { inheritAllFrom: '$block' } );
  695. model.schema.register( 'blockquote' );
  696. model.schema.extend( 'blockquote', { allowIn: '$root' } );
  697. model.schema.extend( '$block', { allowIn: 'blockquote' } );
  698. model.schema.register( 'image', {
  699. allowIn: [ '$root', '$block' ]
  700. } );
  701. model.schema.extend( '$text', { allowIn: 'image' } );
  702. // Special block which can contain another blocks.
  703. model.schema.register( 'nestedBlock', { inheritAllFrom: '$block' } );
  704. model.schema.extend( 'nestedBlock', { allowIn: '$block' } );
  705. } );
  706. it( 'returns an iterator', () => {
  707. setData( model, '<p>a</p><p>[]b</p><p>c</p>' );
  708. expect( doc.selection.getSelectedBlocks().next ).to.be.a( 'function' );
  709. } );
  710. it( 'returns block for a collapsed selection', () => {
  711. setData( model, '<p>a</p><p>[]b</p><p>c</p>' );
  712. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'b' ] );
  713. } );
  714. it( 'returns block for a collapsed selection (empty block)', () => {
  715. setData( model, '<p>a</p><p>[]</p><p>c</p>' );
  716. const blocks = Array.from( doc.selection.getSelectedBlocks() );
  717. expect( blocks ).to.have.length( 1 );
  718. expect( blocks[ 0 ].childCount ).to.equal( 0 );
  719. } );
  720. it( 'returns block for a non collapsed selection', () => {
  721. setData( model, '<p>a</p><p>[b]</p><p>c</p>' );
  722. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'b' ] );
  723. } );
  724. it( 'returns two blocks for a non collapsed selection', () => {
  725. setData( model, '<p>a</p><h>[b</h><p>c]</p><p>d</p>' );
  726. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'b', 'c' ] );
  727. } );
  728. it( 'returns two blocks for a non collapsed selection (starts at block end)', () => {
  729. setData( model, '<p>a</p><h>b[</h><p>c]</p><p>d</p>' );
  730. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'b', 'c' ] );
  731. } );
  732. it( 'returns proper block for a multi-range selection', () => {
  733. setData( model, '<p>a</p><h>[b</h><p>c]</p><p>d</p><p>[e]</p>' );
  734. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'b', 'c', 'e' ] );
  735. } );
  736. it( 'does not return a block twice if two ranges are anchored in it', () => {
  737. setData( model, '<p>[a]b[c]</p>' );
  738. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'abc' ] );
  739. } );
  740. it( 'returns only blocks', () => {
  741. setData( model, '<p>[a</p><image>b</image><p>c]</p>' );
  742. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'a', 'c' ] );
  743. } );
  744. it( 'gets deeper into the tree', () => {
  745. setData( model, '<p>[a</p><blockquote><p>b</p><p>c</p></blockquote><p>d]</p>' );
  746. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'a', 'b', 'c', 'd' ] );
  747. } );
  748. it( 'gets deeper into the tree (end deeper)', () => {
  749. setData( model, '<p>[a</p><blockquote><p>b]</p><p>c</p></blockquote><p>d</p>' );
  750. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'a', 'b' ] );
  751. } );
  752. it( 'gets deeper into the tree (start deeper)', () => {
  753. setData( model, '<p>a</p><blockquote><p>b</p><p>[c</p></blockquote><p>d]</p>' );
  754. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'c', 'd' ] );
  755. } );
  756. it( 'returns an empty array if none of the selected elements is a block', () => {
  757. setData( model, '<p>a</p><image>[a</image><image>b]</image><p>b</p>' );
  758. expect( toText( doc.selection.getSelectedBlocks() ) ).to.be.empty;
  759. } );
  760. it( 'returns an empty array if the selected element is not a block', () => {
  761. setData( model, '<p>a</p><image>[]a</image><p>b</p>' );
  762. expect( toText( doc.selection.getSelectedBlocks() ) ).to.be.empty;
  763. } );
  764. // Super edge case – should not happen (blocks should never be nested),
  765. // but since the code handles it already it's worth testing.
  766. it( 'returns only the lowest block if blocks are nested', () => {
  767. setData( model, '<nestedBlock>a<nestedBlock>[]b</nestedBlock></nestedBlock>' );
  768. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'b' ] );
  769. } );
  770. // Like above but trickier.
  771. it( 'returns only the lowest block if blocks are nested', () => {
  772. setData(
  773. model,
  774. '<nestedBlock>a<nestedBlock>[b</nestedBlock></nestedBlock>' +
  775. '<nestedBlock>c<nestedBlock>d]</nestedBlock></nestedBlock>'
  776. );
  777. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'b', 'd' ] );
  778. } );
  779. it( 'returns nothing if directly in a root', () => {
  780. doc.createRoot( 'p', 'inlineOnlyRoot' );
  781. setData( model, 'a[b]c', { rootName: 'inlineOnlyRoot' } );
  782. expect( toText( doc.selection.getSelectedBlocks() ) ).to.be.empty;
  783. } );
  784. describe( '#984', () => {
  785. it( 'does not return the last block if none of its content is selected', () => {
  786. setData( model, '<p>[a</p><p>b</p><p>]c</p>' );
  787. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'a', 'b' ] );
  788. } );
  789. it( 'returns only the first block for a non collapsed selection if only end of selection is touching a block', () => {
  790. setData( model, '<p>a</p><h>b[</h><p>]c</p><p>d</p>' );
  791. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'b' ] );
  792. } );
  793. it( 'does not return the last block if none of its content is selected (nested case)', () => {
  794. setData( model, '<p>[a</p><nestedBlock><nestedBlock>]b</nestedBlock></nestedBlock>' );
  795. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'a' ] );
  796. } );
  797. // Like a super edge case, we can live with this behavior as I don't even know what we could expect here
  798. // since only the innermost block is considerd a block to return (so the <nB>b...</nB> needs to be ignored).
  799. it( 'does not return the last block if none of its content is selected (nested case, wrapper with a content)', () => {
  800. setData( model, '<p>[a</p><nestedBlock>b<nestedBlock>]c</nestedBlock></nestedBlock>' );
  801. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'a' ] );
  802. } );
  803. it( 'returns the last block if at least one of its child nodes is selected', () => {
  804. setData( model, '<p>[a</p><p>b</p><p><image></image>]c</p>' );
  805. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'a', 'b', 'c' ] );
  806. } );
  807. // I needed these last 2 cases to justify the use of isTouching() instead of simple `offset == 0` check.
  808. it( 'returns the last block if at least one of its child nodes is selected (end in an inline element)', () => {
  809. setData( model, '<p>[a</p><p>b</p><p><image>x]</image>c</p>' );
  810. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'a', 'b', 'c' ] );
  811. } );
  812. it(
  813. 'does not return the last block if at least one of its child nodes is selected ' +
  814. '(end in an inline element, no content selected)',
  815. () => {
  816. setData( model, '<p>[a</p><p>b</p><p><image>]x</image>c</p>' );
  817. expect( toText( doc.selection.getSelectedBlocks() ) ).to.deep.equal( [ 'a', 'b' ] );
  818. }
  819. );
  820. } );
  821. // Map all elements to data of its first child text node.
  822. function toText( elements ) {
  823. return Array.from( elements ).map( el => {
  824. return Array.from( el.getChildren() ).find( child => child.data ).data;
  825. } );
  826. }
  827. } );
  828. describe( 'attributes interface', () => {
  829. let rangeInFullP;
  830. beforeEach( () => {
  831. root._insertChild( 0, [
  832. new Element( 'p', [], new Text( 'foobar' ) ),
  833. new Element( 'p', [], [] )
  834. ] );
  835. rangeInFullP = new Range( new Position( root, [ 0, 4 ] ), new Position( root, [ 0, 4 ] ) );
  836. } );
  837. describe( 'setAttribute()', () => {
  838. it( 'should set given attribute on the selection', () => {
  839. selection.setTo( [ rangeInFullP ] );
  840. selection.setAttribute( 'foo', 'bar' );
  841. expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  842. } );
  843. it( 'should fire change:attribute event with correct parameters', () => {
  844. selection.on( 'change:attribute', ( evt, data ) => {
  845. expect( data.directChange ).to.be.true;
  846. expect( data.attributeKeys ).to.deep.equal( [ 'foo' ] );
  847. } );
  848. selection.setAttribute( 'foo', 'bar' );
  849. } );
  850. it( 'should not fire change:attribute event if attribute with same key and value was already set', () => {
  851. selection.setAttribute( 'foo', 'bar' );
  852. const spy = sinon.spy();
  853. selection.on( 'change:attribute', spy );
  854. selection.setAttribute( 'foo', 'bar' );
  855. expect( spy.called ).to.be.false;
  856. } );
  857. } );
  858. describe( 'getAttribute()', () => {
  859. it( 'should return undefined if element does not contain given attribute', () => {
  860. expect( selection.getAttribute( 'abc' ) ).to.be.undefined;
  861. } );
  862. } );
  863. describe( 'getAttributes()', () => {
  864. it( 'should return an iterator that iterates over all attributes set on selection', () => {
  865. selection.setTo( [ rangeInFullP ] );
  866. selection.setAttribute( 'foo', 'bar' );
  867. selection.setAttribute( 'abc', 'xyz' );
  868. const attrs = Array.from( selection.getAttributes() );
  869. expect( attrs ).to.deep.equal( [ [ 'foo', 'bar' ], [ 'abc', 'xyz' ] ] );
  870. } );
  871. } );
  872. describe( 'getAttributeKeys()', () => {
  873. it( 'should return iterator that iterates over all attribute keys set on selection', () => {
  874. selection.setTo( [ rangeInFullP ] );
  875. selection.setAttribute( 'foo', 'bar' );
  876. selection.setAttribute( 'abc', 'xyz' );
  877. const attrs = Array.from( selection.getAttributeKeys() );
  878. expect( attrs ).to.deep.equal( [ 'foo', 'abc' ] );
  879. } );
  880. } );
  881. describe( 'hasAttribute()', () => {
  882. it( 'should return true if element contains attribute with given key', () => {
  883. selection.setTo( [ rangeInFullP ] );
  884. selection.setAttribute( 'foo', 'bar' );
  885. expect( selection.hasAttribute( 'foo' ) ).to.be.true;
  886. } );
  887. it( 'should return false if element does not contain attribute with given key', () => {
  888. expect( selection.hasAttribute( 'abc' ) ).to.be.false;
  889. } );
  890. } );
  891. describe( 'removeAttribute()', () => {
  892. it( 'should remove attribute', () => {
  893. selection.setTo( [ rangeInFullP ] );
  894. selection.setAttribute( 'foo', 'bar' );
  895. selection.removeAttribute( 'foo' );
  896. expect( selection.getAttribute( 'foo' ) ).to.be.undefined;
  897. } );
  898. it( 'should fire change:attribute event with correct parameters', () => {
  899. selection.setAttribute( 'foo', 'bar' );
  900. selection.on( 'change:attribute', ( evt, data ) => {
  901. expect( data.directChange ).to.be.true;
  902. expect( data.attributeKeys ).to.deep.equal( [ 'foo' ] );
  903. } );
  904. selection.removeAttribute( 'foo' );
  905. } );
  906. it( 'should not fire change:attribute event if such attribute did not exist', () => {
  907. const spy = sinon.spy();
  908. selection.on( 'change:attribute', spy );
  909. selection.removeAttribute( 'foo' );
  910. expect( spy.called ).to.be.false;
  911. } );
  912. } );
  913. } );
  914. describe( 'containsEntireContent()', () => {
  915. beforeEach( () => {
  916. model.schema.register( 'p', { inheritAllFrom: '$block' } );
  917. model.schema.extend( 'p', { allowIn: '$root' } );
  918. } );
  919. it( 'returns true if the entire content in $root is selected', () => {
  920. setData( model, '<p>[Foo</p><p>Bom</p><p>Bar]</p>' );
  921. expect( doc.selection.containsEntireContent() ).to.equal( true );
  922. } );
  923. it( 'returns false when only a fragment of the content in $root is selected', () => {
  924. setData( model, '<p>Fo[o</p><p>Bom</p><p>Bar]</p>' );
  925. expect( doc.selection.containsEntireContent() ).to.equal( false );
  926. } );
  927. it( 'returns true if the entire content in specified element is selected', () => {
  928. setData( model, '<p>Foo</p><p>[Bom]</p><p>Bar</p>' );
  929. const root = doc.getRoot();
  930. const secondParagraph = root.getNodeByPath( [ 1 ] );
  931. expect( doc.selection.containsEntireContent( secondParagraph ) ).to.equal( true );
  932. } );
  933. it( 'returns false if the entire content in specified element is not selected', () => {
  934. setData( model, '<p>Foo</p><p>[Bom</p><p>B]ar</p>' );
  935. const root = doc.getRoot();
  936. const secondParagraph = root.getNodeByPath( [ 1 ] );
  937. expect( doc.selection.containsEntireContent( secondParagraph ) ).to.equal( false );
  938. } );
  939. it( 'returns false when the entire content except an empty element is selected', () => {
  940. model.schema.register( 'img', {
  941. allowIn: 'p'
  942. } );
  943. setData( model, '<p><img></img>[Foo]</p>' );
  944. expect( doc.selection.containsEntireContent() ).to.equal( false );
  945. } );
  946. it( 'returns true if the content is empty', () => {
  947. setData( model, '[]' );
  948. expect( doc.selection.containsEntireContent() ).to.equal( true );
  949. } );
  950. it( 'returns false if empty selection is at the end of non-empty content', () => {
  951. setData( model, '<p>Foo bar bom.</p>[]' );
  952. expect( doc.selection.containsEntireContent() ).to.equal( false );
  953. } );
  954. } );
  955. } );