8
0

selection.js 40 KB

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