selection-post-fixer.js 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import Model from '../../../src/model/model';
  6. import { injectSelectionPostFixer } from '../../../src/model/utils/selection-post-fixer';
  7. import { getData as getModelData, setData as setModelData } from '../../../src/dev-utils/model';
  8. import { assertEqualMarkup } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  9. describe.only( 'Selection post-fixer', () => {
  10. describe( 'injectSelectionPostFixer()', () => {
  11. it( 'is a function', () => {
  12. expect( injectSelectionPostFixer ).to.be.a( 'function' );
  13. } );
  14. } );
  15. describe( 'injected behavior', () => {
  16. let model, modelRoot;
  17. beforeEach( () => {
  18. model = new Model();
  19. modelRoot = model.document.createRoot();
  20. model.schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  21. model.schema.register( 'table', {
  22. allowWhere: '$block',
  23. allowAttributes: [ 'headingRows', 'headingColumns' ],
  24. isLimit: true,
  25. isObject: true
  26. } );
  27. model.schema.register( 'tableRow', {
  28. allowIn: 'table',
  29. isLimit: true
  30. } );
  31. model.schema.register( 'tableCell', {
  32. allowIn: 'tableRow',
  33. allowAttributes: [ 'colspan', 'rowspan' ],
  34. isObject: true
  35. } );
  36. model.schema.extend( '$block', { allowIn: 'tableCell' } );
  37. model.schema.register( 'image', {
  38. isObject: true,
  39. isBlock: true,
  40. allowWhere: '$block'
  41. } );
  42. model.schema.extend( '$block', { allowIn: 'tableCell' } );
  43. model.schema.register( 'caption', {
  44. allowIn: 'image',
  45. allowContentOf: '$block',
  46. isLimit: true
  47. } );
  48. model.schema.register( 'inlineWidget', {
  49. isObject: true,
  50. allowIn: [ '$block', '$clipboardHolder' ]
  51. } );
  52. model.schema.register( 'figure', {
  53. allowIn: '$root',
  54. allowAttributes: [ 'name', 'title' ]
  55. } );
  56. } );
  57. it( 'should not crash if there is no correct position for model selection', () => {
  58. setModelData( model, '' );
  59. expect( getModelData( model ) ).to.equal( '[]' );
  60. } );
  61. it( 'should react to structure changes', () => {
  62. setModelData( model, '<paragraph>[]foo</paragraph><image></image>' );
  63. model.change( writer => {
  64. writer.remove( modelRoot.getChild( 0 ) );
  65. } );
  66. expect( getModelData( model ) ).to.equal( '[<image></image>]' );
  67. } );
  68. it( 'should react to selection changes', () => {
  69. setModelData( model, '<paragraph>[]foo</paragraph><image></image>' );
  70. // <paragraph>foo</paragraph>[]<image></image>
  71. model.change( writer => {
  72. writer.setSelection(
  73. writer.createRange( writer.createPositionAt( modelRoot, 1 ), writer.createPositionAt( modelRoot, 1 ) )
  74. );
  75. } );
  76. expect( getModelData( model ) ).to.equal( '<paragraph>foo[]</paragraph><image></image>' );
  77. } );
  78. describe( 'non-collapsed selection - table scenarios', () => {
  79. beforeEach( () => {
  80. setModelData( model,
  81. '<paragraph>[]foo</paragraph>' +
  82. '<table>' +
  83. '<tableRow>' +
  84. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  85. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  86. '</tableRow>' +
  87. '</table>' +
  88. '<paragraph>bar</paragraph>'
  89. );
  90. } );
  91. it( 'should fix #1 - range start outside table, end on table cell', () => {
  92. // <paragraph>f[oo</paragraph><table><tableRow><tableCell></tableCell>]<tableCell>...
  93. model.change( writer => {
  94. writer.setSelection( writer.createRange(
  95. writer.createPositionAt( modelRoot.getChild( 0 ), 1 ),
  96. writer.createPositionAt( modelRoot.getChild( 1 ).getChild( 0 ), 1 )
  97. ) );
  98. } );
  99. expect( getModelData( model ) ).to.equal(
  100. '<paragraph>f[oo</paragraph>' +
  101. '<table>' +
  102. '<tableRow>' +
  103. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  104. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  105. '</tableRow>' +
  106. '</table>]' +
  107. '<paragraph>bar</paragraph>'
  108. );
  109. } );
  110. it( 'should fix #2 - range start on table cell, end outside table', () => {
  111. // ...<table><tableRow><tableCell></tableCell>[<tableCell></tableCell></tableRow></table><paragraph>b]ar</paragraph>
  112. model.change( writer => {
  113. writer.setSelection( writer.createRange(
  114. writer.createPositionAt( modelRoot.getChild( 1 ).getChild( 0 ), 1 ),
  115. writer.createPositionAt( modelRoot.getChild( 2 ), 1 )
  116. ) );
  117. } );
  118. expect( getModelData( model ) ).to.equal(
  119. '<paragraph>foo</paragraph>' +
  120. '[<table>' +
  121. '<tableRow>' +
  122. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  123. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  124. '</tableRow>' +
  125. '</table>' +
  126. '<paragraph>b]ar</paragraph>'
  127. );
  128. } );
  129. it( 'should fix #3', () => {
  130. // <paragraph>f[oo</paragraph><table>]<tableRow>...
  131. model.change( writer => {
  132. writer.setSelection( writer.createRange(
  133. writer.createPositionAt( modelRoot.getChild( 0 ), 1 ),
  134. writer.createPositionAt( modelRoot.getChild( 1 ), 0 )
  135. ) );
  136. } );
  137. expect( getModelData( model ) ).to.equal(
  138. '<paragraph>f[oo</paragraph>' +
  139. '<table>' +
  140. '<tableRow>' +
  141. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  142. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  143. '</tableRow>' +
  144. '</table>]' +
  145. '<paragraph>bar</paragraph>'
  146. );
  147. } );
  148. it( 'should fix #4', () => {
  149. // <paragraph>foo</paragraph><table><tableRow><tableCell>a[aa</tableCell><tableCell>b]bb</tableCell>
  150. model.change( writer => {
  151. writer.setSelection( writer.createRange(
  152. writer.createPositionAt( modelRoot.getNodeByPath( [ 1, 0, 0, 0 ] ), 1 ),
  153. writer.createPositionAt( modelRoot.getNodeByPath( [ 1, 0, 1, 0 ] ), 2 )
  154. ) );
  155. } );
  156. expect( getModelData( model ) ).to.equal(
  157. '<paragraph>foo</paragraph>' +
  158. '[<table>' +
  159. '<tableRow>' +
  160. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  161. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  162. '</tableRow>' +
  163. '</table>]' +
  164. '<paragraph>bar</paragraph>'
  165. );
  166. } );
  167. // There's a chance that this and the following test will not be up to date with
  168. // how the table feature is really implemented once we'll introduce row/cells/columns selection
  169. // in which case all these elements will need to be marked as objects.
  170. it( 'should fix #6 (element selection of not an object)', () => {
  171. setModelData( model,
  172. '<paragraph>foo</paragraph>' +
  173. '<table>' +
  174. '[<tableRow>' +
  175. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  176. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  177. '</tableRow>]' +
  178. '</table>' +
  179. '<paragraph>baz</paragraph>'
  180. );
  181. expect( getModelData( model ) ).to.equal(
  182. '<paragraph>foo</paragraph>' +
  183. '[<table>' +
  184. '<tableRow>' +
  185. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  186. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  187. '</tableRow>' +
  188. '</table>]' +
  189. '<paragraph>baz</paragraph>'
  190. );
  191. } );
  192. it( 'should fix #7 (element selection of non-objects)', () => {
  193. setModelData( model,
  194. '<paragraph>foo</paragraph>' +
  195. '<table>' +
  196. '[<tableRow>' +
  197. '<tableCell><paragraph>1</paragraph></tableCell>' +
  198. '<tableCell><paragraph>2</paragraph></tableCell>' +
  199. '</tableRow>' +
  200. '<tableRow>' +
  201. '<tableCell><paragraph>3</paragraph></tableCell>' +
  202. '<tableCell><paragraph>4</paragraph></tableCell>]' +
  203. '</tableRow>' +
  204. '<tableRow>' +
  205. '<tableCell><paragraph>5</paragraph></tableCell>' +
  206. '<tableCell><paragraph>6</paragraph></tableCell>' +
  207. '</tableRow>' +
  208. '</table>' +
  209. '<paragraph>baz</paragraph>'
  210. );
  211. expect( getModelData( model ) ).to.equal(
  212. '<paragraph>foo</paragraph>' +
  213. '[<table>' +
  214. '<tableRow>' +
  215. '<tableCell><paragraph>1</paragraph></tableCell>' +
  216. '<tableCell><paragraph>2</paragraph></tableCell>' +
  217. '</tableRow>' +
  218. '<tableRow>' +
  219. '<tableCell><paragraph>3</paragraph></tableCell>' +
  220. '<tableCell><paragraph>4</paragraph></tableCell>' +
  221. '</tableRow>' +
  222. '<tableRow>' +
  223. '<tableCell><paragraph>5</paragraph></tableCell>' +
  224. '<tableCell><paragraph>6</paragraph></tableCell>' +
  225. '</tableRow>' +
  226. '</table>]' +
  227. '<paragraph>baz</paragraph>'
  228. );
  229. } );
  230. it( 'should fix #8 (cross-limit selection which starts in a non-limit elements)', () => {
  231. model.schema.extend( 'paragraph', { allowIn: 'tableCell' } );
  232. setModelData( model,
  233. '<paragraph>foo</paragraph>' +
  234. '<table>' +
  235. '<tableRow>' +
  236. '<tableCell><paragraph>f[oo</paragraph></tableCell>' +
  237. '<tableCell><paragraph>b]ar</paragraph></tableCell>' +
  238. '</tableRow>' +
  239. '</table>' +
  240. '<paragraph>baz</paragraph>'
  241. );
  242. expect( getModelData( model ) ).to.equal(
  243. '<paragraph>foo</paragraph>' +
  244. '[<table>' +
  245. '<tableRow>' +
  246. '<tableCell><paragraph>foo</paragraph></tableCell>' +
  247. '<tableCell><paragraph>bar</paragraph></tableCell>' +
  248. '</tableRow>' +
  249. '</table>]' +
  250. '<paragraph>baz</paragraph>'
  251. );
  252. } );
  253. it( 'should not fix #1 - selection over paragraphs outside table', () => {
  254. setModelData( model,
  255. '<paragraph>foo</paragraph>' +
  256. '<table>' +
  257. '<tableRow>' +
  258. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  259. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  260. '</tableRow>' +
  261. '</table>' +
  262. '<paragraph>b[ar</paragraph>' +
  263. '<paragraph>ba]z</paragraph>'
  264. );
  265. expect( getModelData( model ) ).to.equal(
  266. '<paragraph>foo</paragraph>' +
  267. '<table>' +
  268. '<tableRow>' +
  269. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  270. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  271. '</tableRow>' +
  272. '</table>' +
  273. '<paragraph>b[ar</paragraph>' +
  274. '<paragraph>ba]z</paragraph>'
  275. );
  276. } );
  277. it( 'should not fix #2 - selection over image in table', () => {
  278. setModelData( model,
  279. '<paragraph>foo</paragraph>' +
  280. '<table>' +
  281. '<tableRow>' +
  282. '<tableCell><paragraph>foo</paragraph><image></image></tableCell>' +
  283. '<tableCell><paragraph>[]bbb</paragraph></tableCell>' +
  284. '</tableRow>' +
  285. '</table>'
  286. );
  287. model.change( writer => {
  288. const image = model.document.getRoot().getNodeByPath( [ 1, 0, 0, 1 ] );
  289. writer.setSelection( writer.createRangeOn( image ) );
  290. } );
  291. expect( getModelData( model ) ).to.equal(
  292. '<paragraph>foo</paragraph>' +
  293. '<table>' +
  294. '<tableRow>' +
  295. '<tableCell><paragraph>foo</paragraph>[<image></image>]</tableCell>' +
  296. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  297. '</tableRow>' +
  298. '</table>'
  299. );
  300. } );
  301. it( 'should not fix #3 - selection over paragraph & image in table', () => {
  302. setModelData( model,
  303. '<paragraph>foo</paragraph>' +
  304. '<table>' +
  305. '<tableRow>' +
  306. '<tableCell><paragraph>foo</paragraph><image></image></tableCell>' +
  307. '<tableCell><paragraph>[]bbb</paragraph></tableCell>' +
  308. '</tableRow>' +
  309. '</table>'
  310. );
  311. model.change( writer => {
  312. const tableCell = model.document.getRoot().getNodeByPath( [ 1, 0, 0 ] );
  313. writer.setSelection( writer.createRangeIn( tableCell ) );
  314. } );
  315. expect( getModelData( model ) ).to.equal(
  316. '<paragraph>foo</paragraph>' +
  317. '<table>' +
  318. '<tableRow>' +
  319. '<tableCell><paragraph>[foo</paragraph><image></image>]</tableCell>' +
  320. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  321. '</tableRow>' +
  322. '</table>'
  323. );
  324. } );
  325. it( 'should not fix #4 - selection over image & paragraph in table', () => {
  326. setModelData( model,
  327. '<paragraph>foo</paragraph>' +
  328. '<table>' +
  329. '<tableRow>' +
  330. '<tableCell><image></image><paragraph>foo</paragraph></tableCell>' +
  331. '<tableCell><paragraph>[]bbb</paragraph></tableCell>' +
  332. '</tableRow>' +
  333. '</table>'
  334. );
  335. model.change( writer => {
  336. const tableCell = model.document.getRoot().getNodeByPath( [ 1, 0, 0 ] );
  337. writer.setSelection( writer.createRangeIn( tableCell ) );
  338. } );
  339. expect( getModelData( model ) ).to.equal(
  340. '<paragraph>foo</paragraph>' +
  341. '<table>' +
  342. '<tableRow>' +
  343. '<tableCell>[<image></image><paragraph>foo]</paragraph></tableCell>' +
  344. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  345. '</tableRow>' +
  346. '</table>'
  347. );
  348. } );
  349. it( 'should not fix #5 - selection over blockQuote in table', () => {
  350. model.schema.register( 'blockQuote', {
  351. allowWhere: '$block',
  352. allowContentOf: '$root'
  353. } );
  354. setModelData( model,
  355. '<paragraph>foo</paragraph>' +
  356. '<table>' +
  357. '<tableRow>' +
  358. '<tableCell><blockQuote><paragraph>foo</paragraph></blockQuote></tableCell>' +
  359. '<tableCell><paragraph>[]bbb</paragraph></tableCell>' +
  360. '</tableRow>' +
  361. '</table>'
  362. );
  363. model.change( writer => {
  364. const tableCell = model.document.getRoot().getNodeByPath( [ 1, 0, 0 ] );
  365. writer.setSelection( writer.createRangeIn( tableCell ) );
  366. } );
  367. expect( getModelData( model ) ).to.equal(
  368. '<paragraph>foo</paragraph>' +
  369. '<table>' +
  370. '<tableRow>' +
  371. '<tableCell><blockQuote><paragraph>[foo]</paragraph></blockQuote></tableCell>' +
  372. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  373. '</tableRow>' +
  374. '</table>'
  375. );
  376. } );
  377. it( 'should fix multiple ranges #1', () => {
  378. model.change( writer => {
  379. const ranges = [
  380. writer.createRange(
  381. writer.createPositionFromPath( modelRoot, [ 0, 1 ] ),
  382. writer.createPositionFromPath( modelRoot, [ 1, 0 ] )
  383. ),
  384. writer.createRange(
  385. writer.createPositionFromPath( modelRoot, [ 1, 0, 0, 0 ] ),
  386. writer.createPositionFromPath( modelRoot, [ 1, 1 ] )
  387. )
  388. ];
  389. writer.setSelection( ranges );
  390. } );
  391. expect( getModelData( model ) ).to.equal(
  392. '<paragraph>f[oo</paragraph>' +
  393. '<table>' +
  394. '<tableRow>' +
  395. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  396. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  397. '</tableRow>' +
  398. '</table>]' +
  399. '<paragraph>bar</paragraph>'
  400. );
  401. } );
  402. it( 'should fix multiple ranges #2', () => {
  403. model.change( writer => {
  404. const ranges = [
  405. writer.createRange(
  406. writer.createPositionFromPath( modelRoot, [ 0, 1 ] ),
  407. writer.createPositionFromPath( modelRoot, [ 1, 0 ] )
  408. ),
  409. writer.createRange(
  410. writer.createPositionFromPath( modelRoot, [ 1, 0, 0, 0 ] ),
  411. writer.createPositionFromPath( modelRoot, [ 2, 2 ] )
  412. )
  413. ];
  414. writer.setSelection( ranges );
  415. } );
  416. expect( getModelData( model ) ).to.equal(
  417. '<paragraph>f[oo</paragraph>' +
  418. '<table>' +
  419. '<tableRow>' +
  420. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  421. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  422. '</tableRow>' +
  423. '</table>' +
  424. '<paragraph>ba]r</paragraph>'
  425. );
  426. } );
  427. it( 'should fix multiple ranges #3', () => {
  428. setModelData( model,
  429. '<paragraph>foo</paragraph>' +
  430. '<table>' +
  431. '<tableRow>' +
  432. '<tableCell><paragraph>[aaa</paragraph></tableCell>' +
  433. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  434. '</tableRow>' +
  435. '<tableRow>]' +
  436. '<tableCell><paragraph>[aaa</paragraph></tableCell>' +
  437. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  438. '</tableRow>' +
  439. '<tableRow>]' +
  440. '<tableCell><paragraph>[aaa</paragraph></tableCell>' +
  441. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  442. '</tableRow>' +
  443. '<tableRow>]' +
  444. '<tableCell><paragraph>[aaa</paragraph></tableCell>' +
  445. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  446. '</tableRow>' +
  447. '</table>' +
  448. '<paragraph>b]az</paragraph>'
  449. );
  450. expect( getModelData( model ) ).to.equal(
  451. '<paragraph>foo</paragraph>' +
  452. '[<table>' +
  453. '<tableRow>' +
  454. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  455. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  456. '</tableRow>' +
  457. '<tableRow>' +
  458. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  459. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  460. '</tableRow>' +
  461. '<tableRow>' +
  462. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  463. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  464. '</tableRow>' +
  465. '<tableRow>' +
  466. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  467. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  468. '</tableRow>' +
  469. '</table>' +
  470. '<paragraph>b]az</paragraph>'
  471. );
  472. } );
  473. it( 'should not fix multiple ranges #1 - not overlapping ranges', () => {
  474. model.change( writer => {
  475. const ranges = [
  476. writer.createRange(
  477. writer.createPositionFromPath( modelRoot, [ 0, 1 ] ),
  478. writer.createPositionFromPath( modelRoot, [ 1, 0 ] )
  479. ),
  480. writer.createRange(
  481. writer.createPositionFromPath( modelRoot, [ 1, 0, 0, 0 ] ),
  482. writer.createPositionFromPath( modelRoot, [ 2, 1 ] )
  483. ),
  484. writer.createRange(
  485. writer.createPositionFromPath( modelRoot, [ 2, 2 ] ),
  486. writer.createPositionFromPath( modelRoot, [ 2, 3 ] )
  487. )
  488. ];
  489. writer.setSelection( ranges );
  490. } );
  491. expect( getModelData( model ) ).to.equal(
  492. '<paragraph>f[oo</paragraph>' +
  493. '<table>' +
  494. '<tableRow>' +
  495. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  496. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  497. '</tableRow>' +
  498. '</table>' +
  499. '<paragraph>b]a[r]</paragraph>'
  500. );
  501. } );
  502. it( 'should not fix multiple ranges on objects - table selection', () => {
  503. setModelData( model,
  504. '<table>' +
  505. '<tableRow>' +
  506. '[<tableCell><paragraph>a</paragraph></tableCell>]' +
  507. '[<tableCell><paragraph>b</paragraph></tableCell>]' +
  508. '</tableRow>' +
  509. '<tableRow>' +
  510. '[<tableCell><paragraph>c</paragraph></tableCell>]' +
  511. '<tableCell><paragraph>d</paragraph></tableCell>' +
  512. '</tableRow>' +
  513. '</table>'
  514. );
  515. assertEqualMarkup( getModelData( model ),
  516. '<table>' +
  517. '<tableRow>' +
  518. '[<tableCell><paragraph>a</paragraph></tableCell>]' +
  519. '[<tableCell><paragraph>b</paragraph></tableCell>]' +
  520. '</tableRow>' +
  521. '<tableRow>' +
  522. '[<tableCell><paragraph>c</paragraph></tableCell>]' +
  523. '<tableCell><paragraph>d</paragraph></tableCell>' +
  524. '</tableRow>' +
  525. '</table>'
  526. );
  527. } );
  528. it( 'should allow selection on block - limit element', () => {
  529. model.schema.extend( '$block', { allowIn: 'tableCell' } );
  530. setModelData( model,
  531. '<table>' +
  532. '<tableRow><tableCell>[<paragraph>aaa</paragraph>]</tableCell></tableRow>' +
  533. '</table>'
  534. );
  535. expect( getModelData( model ) ).to.equal(
  536. '<table>' +
  537. '<tableRow><tableCell><paragraph>[aaa]</paragraph></tableCell></tableRow>' +
  538. '</table>'
  539. );
  540. } );
  541. } );
  542. describe( 'non-collapsed selection - image scenarios', () => {
  543. beforeEach( () => {
  544. setModelData( model,
  545. '<paragraph>[]foo</paragraph>' +
  546. '<image>' +
  547. '<caption>xxx</caption>' +
  548. '</image>' +
  549. '<paragraph>bar</paragraph>'
  550. );
  551. } );
  552. it( 'should fix #1 (crossing object and limit boundaries)', () => {
  553. model.change( writer => {
  554. // <paragraph>f[oo</paragraph><image><caption>x]xx</caption>...
  555. writer.setSelection( writer.createRange(
  556. writer.createPositionAt( modelRoot.getChild( 0 ), 1 ),
  557. writer.createPositionAt( modelRoot.getChild( 1 ).getChild( 0 ), 1 )
  558. ) );
  559. } );
  560. expect( getModelData( model ) ).to.equal(
  561. '<paragraph>f[oo</paragraph>' +
  562. '<image>' +
  563. '<caption>xxx</caption>' +
  564. '</image>]' +
  565. '<paragraph>bar</paragraph>'
  566. );
  567. } );
  568. it( 'should fix #2 (crossing object boundary)', () => {
  569. model.change( writer => {
  570. // <paragraph>f[oo</paragraph><image>]<caption>xxx</caption>...
  571. writer.setSelection( writer.createRange(
  572. writer.createPositionAt( modelRoot.getChild( 0 ), 1 ),
  573. writer.createPositionAt( modelRoot.getChild( 1 ), 0 )
  574. ) );
  575. } );
  576. expect( getModelData( model ) ).to.equal(
  577. '<paragraph>f[oo</paragraph>' +
  578. '<image>' +
  579. '<caption>xxx</caption>' +
  580. '</image>]' +
  581. '<paragraph>bar</paragraph>'
  582. );
  583. } );
  584. it( 'should fix #3 (crossing object boundary)', () => {
  585. model.change( writer => {
  586. // <paragraph>f[oo</paragraph><image><caption>xxx</caption>]</image>...
  587. writer.setSelection( writer.createRange(
  588. writer.createPositionAt( modelRoot.getChild( 0 ), 1 ),
  589. writer.createPositionAt( modelRoot.getChild( 1 ), 1 )
  590. ) );
  591. } );
  592. expect( getModelData( model ) ).to.equal(
  593. '<paragraph>f[oo</paragraph>' +
  594. '<image>' +
  595. '<caption>xxx</caption>' +
  596. '</image>]' +
  597. '<paragraph>bar</paragraph>'
  598. );
  599. } );
  600. it( 'should fix #4 (element selection of not an object)', () => {
  601. model.change( writer => {
  602. // <paragraph>foo</paragraph><image>[<caption>xxx</caption>]</image>...
  603. writer.setSelection( writer.createRange(
  604. writer.createPositionAt( modelRoot.getChild( 1 ), 0 ),
  605. writer.createPositionAt( modelRoot.getChild( 1 ), 1 )
  606. ) );
  607. } );
  608. expect( getModelData( model ) ).to.equal(
  609. '<paragraph>foo</paragraph>' +
  610. '[<image>' +
  611. '<caption>xxx</caption>' +
  612. '</image>]' +
  613. '<paragraph>bar</paragraph>'
  614. );
  615. } );
  616. it( 'should not fix #1 (element selection of an object)', () => {
  617. model.change( writer => {
  618. // <paragraph>foo</paragraph>[<image><caption>xxx</caption></image>]...
  619. writer.setSelection( writer.createRange(
  620. writer.createPositionAt( modelRoot, 1 ),
  621. writer.createPositionAt( modelRoot, 2 )
  622. ) );
  623. } );
  624. expect( getModelData( model ) ).to.equal(
  625. '<paragraph>foo</paragraph>' +
  626. '[<image>' +
  627. '<caption>xxx</caption>' +
  628. '</image>]' +
  629. '<paragraph>bar</paragraph>'
  630. );
  631. } );
  632. it( 'should not fix #2 (inside a limit)', () => {
  633. model.change( writer => {
  634. const caption = modelRoot.getChild( 1 ).getChild( 0 );
  635. // <paragraph>foo</paragraph><image><caption>[xxx]</caption></image>...
  636. writer.setSelection( writer.createRange(
  637. writer.createPositionAt( caption, 0 ),
  638. writer.createPositionAt( caption, 3 )
  639. ) );
  640. } );
  641. expect( getModelData( model ) ).to.equal(
  642. '<paragraph>foo</paragraph>' +
  643. '<image>' +
  644. '<caption>[xxx]</caption>' +
  645. '</image>' +
  646. '<paragraph>bar</paragraph>'
  647. );
  648. } );
  649. it( 'should not fix #3 (inside a limit - partial text selection)', () => {
  650. model.change( writer => {
  651. const caption = modelRoot.getChild( 1 ).getChild( 0 );
  652. // <paragraph>foo</paragraph><image><caption>[xx]x</caption></image>...
  653. writer.setSelection( writer.createRange(
  654. writer.createPositionAt( caption, 0 ),
  655. writer.createPositionAt( caption, 2 )
  656. ) );
  657. } );
  658. expect( getModelData( model ) ).to.equal(
  659. '<paragraph>foo</paragraph>' +
  660. '<image>' +
  661. '<caption>[xx]x</caption>' +
  662. '</image>' +
  663. '<paragraph>bar</paragraph>'
  664. );
  665. } );
  666. it( 'should not fix #4 (inside a limit - partial text selection)', () => {
  667. model.change( writer => {
  668. const caption = modelRoot.getChild( 1 ).getChild( 0 );
  669. // <paragraph>foo</paragraph><image><caption>x[xx]</caption></image>...
  670. writer.setSelection( writer.createRange(
  671. writer.createPositionAt( caption, 1 ),
  672. writer.createPositionAt( caption, 3 )
  673. ) );
  674. } );
  675. expect( getModelData( model ) ).to.equal(
  676. '<paragraph>foo</paragraph>' +
  677. '<image>' +
  678. '<caption>x[xx]</caption>' +
  679. '</image>' +
  680. '<paragraph>bar</paragraph>'
  681. );
  682. } );
  683. it( 'should not fix #5 (selection in root on non limit element that doesn\'t allow text)', () => {
  684. setModelData( model,
  685. '[<figure></figure>]'
  686. );
  687. expect( getModelData( model ) ).to.equal(
  688. '[<figure></figure>]'
  689. );
  690. } );
  691. } );
  692. describe( 'non-collapsed selection - other scenarios', () => {
  693. it( 'should fix #1 (element selection of not an object)', () => {
  694. setModelData( model,
  695. '<paragraph>aaa</paragraph>' +
  696. '[<paragraph>bbb</paragraph>]' +
  697. '<paragraph>ccc</paragraph>'
  698. );
  699. expect( getModelData( model ) ).to.equal(
  700. '<paragraph>aaa</paragraph>' +
  701. '<paragraph>[bbb]</paragraph>' +
  702. '<paragraph>ccc</paragraph>'
  703. );
  704. } );
  705. it( 'should fix #2 (elements selection of not an object)', () => {
  706. setModelData( model,
  707. '<paragraph>aaa</paragraph>' +
  708. '[<paragraph>bbb</paragraph>' +
  709. '<paragraph>ccc</paragraph>]'
  710. );
  711. expect( getModelData( model ) ).to.equal(
  712. '<paragraph>aaa</paragraph>' +
  713. '<paragraph>[bbb</paragraph>' +
  714. '<paragraph>ccc]</paragraph>'
  715. );
  716. } );
  717. it( 'should fix #3 (partial selection of not an object)', () => {
  718. setModelData( model,
  719. '<paragraph>aaa</paragraph>' +
  720. '[<paragraph>bbb</paragraph>' +
  721. '<paragraph>ccc]</paragraph>'
  722. );
  723. expect( getModelData( model ) ).to.equal(
  724. '<paragraph>aaa</paragraph>' +
  725. '<paragraph>[bbb</paragraph>' +
  726. '<paragraph>ccc]</paragraph>'
  727. );
  728. } );
  729. it( 'should fix #4 (partial selection of not an object)', () => {
  730. setModelData( model,
  731. '<paragraph>aaa</paragraph>' +
  732. '<paragraph>b[bb</paragraph>]' +
  733. '<paragraph>ccc</paragraph>'
  734. );
  735. expect( getModelData( model ) ).to.equal(
  736. '<paragraph>aaa</paragraph>' +
  737. '<paragraph>b[bb]</paragraph>' +
  738. '<paragraph>ccc</paragraph>'
  739. );
  740. } );
  741. it( 'should fix #5 (partial selection of not an object)', () => {
  742. setModelData( model,
  743. '<paragraph>aaa</paragraph>' +
  744. '[<paragraph>bb]b</paragraph>' +
  745. '<paragraph>ccc</paragraph>'
  746. );
  747. expect( getModelData( model ) ).to.equal(
  748. '<paragraph>aaa</paragraph>' +
  749. '<paragraph>[bb]b</paragraph>' +
  750. '<paragraph>ccc</paragraph>'
  751. );
  752. } );
  753. it( 'should fix #6 (selection must not cross a limit element; starts in a root)', () => {
  754. model.schema.register( 'a', { isLimit: true, allowIn: '$root' } );
  755. model.schema.register( 'b', { isLimit: true, allowIn: 'a' } );
  756. model.schema.register( 'c', { allowIn: 'b' } );
  757. model.schema.extend( '$text', { allowIn: 'c' } );
  758. setModelData( model,
  759. '<a><b><c>[</c></b></a>]'
  760. );
  761. expect( getModelData( model ) ).to.equal( '[<a><b><c></c></b></a>]' );
  762. } );
  763. it( 'should fix #7 (selection must not cross a limit element; ends in a root)', () => {
  764. model.schema.register( 'a', { isLimit: true, allowIn: '$root' } );
  765. model.schema.register( 'b', { isLimit: true, allowIn: 'a' } );
  766. model.schema.register( 'c', { allowIn: 'b' } );
  767. model.schema.extend( '$text', { allowIn: 'c' } );
  768. setModelData( model,
  769. '[<a><b><c>]</c></b></a>'
  770. );
  771. expect( getModelData( model ) ).to.equal( '[<a><b><c></c></b></a>]' );
  772. } );
  773. it( 'should fix #8 (selection must not cross a limit element; starts in a non-limit)', () => {
  774. model.schema.register( 'div', { allowIn: '$root' } );
  775. model.schema.register( 'a', { isLimit: true, allowIn: 'div' } );
  776. model.schema.register( 'b', { isLimit: true, allowIn: 'a' } );
  777. model.schema.register( 'c', { allowIn: 'b' } );
  778. model.schema.extend( '$text', { allowIn: 'c' } );
  779. setModelData( model,
  780. '<div>[<a><b><c>]</c></b></a></div>'
  781. );
  782. expect( getModelData( model ) ).to.equal( '<div>[<a><b><c></c></b></a>]</div>' );
  783. } );
  784. it( 'should fix #9 (selection must not cross a limit element; ends in a non-limit)', () => {
  785. model.schema.register( 'div', { allowIn: '$root' } );
  786. model.schema.register( 'a', { isLimit: true, allowIn: 'div' } );
  787. model.schema.register( 'b', { isLimit: true, allowIn: 'a' } );
  788. model.schema.register( 'c', { allowIn: 'b' } );
  789. model.schema.extend( '$text', { allowIn: 'c' } );
  790. setModelData( model,
  791. '<div><a><b><c>[</c></b></a>]</div>'
  792. );
  793. expect( getModelData( model ) ).to.equal( '<div>[<a><b><c></c></b></a>]</div>' );
  794. } );
  795. it( 'should not fix #1 (selection on text node)', () => {
  796. setModelData( model, '<paragraph>foob[a]r</paragraph>', { lastRangeBackward: true } );
  797. expect( getModelData( model ) ).to.equal( '<paragraph>foob[a]r</paragraph>' );
  798. } );
  799. it( 'should not fix #2 (inline widget selected)', () => {
  800. setModelData( model,
  801. '<paragraph>[<inlineWidget></inlineWidget>]</paragraph>'
  802. );
  803. expect( getModelData( model ) ).to.equal(
  804. '<paragraph>[<inlineWidget></inlineWidget>]</paragraph>'
  805. );
  806. } );
  807. it( 'should not fix #3 (text around inline widget)', () => {
  808. setModelData( model,
  809. '<paragraph>fo[o<inlineWidget></inlineWidget>b]ar</paragraph>'
  810. );
  811. expect( getModelData( model ) ).to.equal(
  812. '<paragraph>fo[o<inlineWidget></inlineWidget>b]ar</paragraph>'
  813. );
  814. } );
  815. it( 'should not fix #4 - object in object', () => {
  816. model.schema.register( 'div', {
  817. allowIn: [ '$root', 'div' ],
  818. isObject: true
  819. } );
  820. setModelData( model, '<div>[<div></div>]</div>' );
  821. model.change( writer => {
  822. const innerDiv = model.document.getRoot().getNodeByPath( [ 0, 0 ] );
  823. writer.setSelection( writer.createRangeOn( innerDiv ) );
  824. } );
  825. expect( getModelData( model ) ).to.equal( '<div>[<div></div>]</div>' );
  826. } );
  827. } );
  828. describe( 'non-collapsed selection - inline widget scenarios', () => {
  829. beforeEach( () => {
  830. model.schema.register( 'placeholder', {
  831. allowWhere: '$text',
  832. isInline: true
  833. } );
  834. } );
  835. it( 'should fix selection that ends in inline element', () => {
  836. setModelData( model, '<paragraph>aaa[<placeholder>]</placeholder>bbb</paragraph>' );
  837. expect( getModelData( model ) ).to.equal( '<paragraph>aaa[]<placeholder></placeholder>bbb</paragraph>' );
  838. } );
  839. it( 'should fix selection that starts in inline element', () => {
  840. setModelData( model, '<paragraph>aaa<placeholder>[</placeholder>]bbb</paragraph>' );
  841. expect( getModelData( model ) ).to.equal( '<paragraph>aaa<placeholder></placeholder>[]bbb</paragraph>' );
  842. } );
  843. it( 'should fix selection that ends in inline element that is also an object', () => {
  844. model.schema.extend( 'placeholder', {
  845. isObject: true
  846. } );
  847. setModelData( model, '<paragraph>aaa[<placeholder>]</placeholder>bbb</paragraph>' );
  848. expect( getModelData( model ) ).to.equal( '<paragraph>aaa[<placeholder></placeholder>]bbb</paragraph>' );
  849. } );
  850. it( 'should fix selection that starts in inline element that is also an object', () => {
  851. model.schema.extend( 'placeholder', {
  852. isObject: true
  853. } );
  854. setModelData( model, '<paragraph>aaa<placeholder>[</placeholder>]bbb</paragraph>' );
  855. expect( getModelData( model ) ).to.equal( '<paragraph>aaa[<placeholder></placeholder>]bbb</paragraph>' );
  856. } );
  857. } );
  858. describe( 'collapsed selection', () => {
  859. it( 'should fix #1 - selection in limit element & before limit element', () => {
  860. setModelData( model,
  861. '<paragraph>foo</paragraph>' +
  862. '<table>' +
  863. '<tableRow>' +
  864. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  865. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  866. '</tableRow>' +
  867. '</table>' +
  868. '<paragraph>bar</paragraph>'
  869. );
  870. // <table>[]<tableRow>...
  871. model.change( writer => {
  872. writer.setSelection(
  873. writer.createRange( writer.createPositionAt( modelRoot.getChild( 1 ), 0 ) )
  874. );
  875. } );
  876. expect( getModelData( model ) ).to.equal(
  877. '<paragraph>foo[]</paragraph>' +
  878. '<table>' +
  879. '<tableRow>' +
  880. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  881. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  882. '</tableRow>' +
  883. '</table>' +
  884. '<paragraph>bar</paragraph>'
  885. );
  886. } );
  887. it( 'should fix #2 - selection in limit element & before limit+object element', () => {
  888. setModelData( model,
  889. '<paragraph>foo</paragraph>' +
  890. '<table>' +
  891. '<tableRow>' +
  892. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  893. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  894. '</tableRow>' +
  895. '</table>' +
  896. '<paragraph>bar</paragraph>'
  897. );
  898. // <table><tableRow>[]<tableCell>...
  899. model.change( writer => {
  900. const row = modelRoot.getChild( 1 ).getChild( 0 );
  901. writer.setSelection(
  902. writer.createRange( writer.createPositionAt( row, 0 ) )
  903. );
  904. } );
  905. expect( getModelData( model ) ).to.equal(
  906. '<paragraph>foo</paragraph>' +
  907. '<table>' +
  908. '<tableRow>' +
  909. '<tableCell><paragraph>[]aaa</paragraph></tableCell>' +
  910. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  911. '</tableRow>' +
  912. '</table>' +
  913. '<paragraph>bar</paragraph>'
  914. );
  915. } );
  916. it( 'should fix #3 - selection in limit&object element & before object element', () => {
  917. setModelData( model,
  918. '<paragraph>foo</paragraph>' +
  919. '<table>' +
  920. '<tableRow>' +
  921. '<tableCell>[]<paragraph>aaa</paragraph></tableCell>' +
  922. '</tableRow>' +
  923. '</table>' +
  924. '<paragraph>bar</paragraph>'
  925. );
  926. assertEqualMarkup( getModelData( model ),
  927. '<paragraph>foo</paragraph>' +
  928. '<table>' +
  929. '<tableRow>' +
  930. '<tableCell><paragraph>[]aaa</paragraph></tableCell>' +
  931. '</tableRow>' +
  932. '</table>' +
  933. '<paragraph>bar</paragraph>'
  934. );
  935. } );
  936. it( 'should fix multiple ranges outside block element (but not merge them)', () => {
  937. setModelData( model,
  938. '[]<paragraph>foo</paragraph>[]' +
  939. '<table>' +
  940. '<tableRow>' +
  941. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  942. '</tableRow>' +
  943. '</table>' +
  944. '<paragraph>bar</paragraph>'
  945. );
  946. assertEqualMarkup( getModelData( model ),
  947. '<paragraph>[]foo[]</paragraph>' +
  948. '<table>' +
  949. '<tableRow>' +
  950. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  951. '</tableRow>' +
  952. '</table>' +
  953. '<paragraph>bar</paragraph>'
  954. );
  955. } );
  956. it( 'should fix selection between tables', () => {
  957. setModelData( model,
  958. '<paragraph>foo</paragraph>' +
  959. '<table>' +
  960. '<tableRow>' +
  961. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  962. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  963. '</tableRow>' +
  964. '</table>' +
  965. '[]' +
  966. '<table>' +
  967. '<tableRow>' +
  968. '<tableCell><paragraph>xxx</paragraph></tableCell>' +
  969. '<tableCell><paragraph>yyy</paragraph></tableCell>' +
  970. '</tableRow>' +
  971. '</table>' +
  972. '<paragraph>baz</paragraph>'
  973. );
  974. assertEqualMarkup( getModelData( model ),
  975. '<paragraph>foo</paragraph>' +
  976. '[<table>' +
  977. '<tableRow>' +
  978. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  979. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  980. '</tableRow>' +
  981. '</table>]' +
  982. '<table>' +
  983. '<tableRow>' +
  984. '<tableCell><paragraph>xxx</paragraph></tableCell>' +
  985. '<tableCell><paragraph>yyy</paragraph></tableCell>' +
  986. '</tableRow>' +
  987. '</table>' +
  988. '<paragraph>baz</paragraph>'
  989. );
  990. } );
  991. } );
  992. } );
  993. } );