selection-post-fixer.js 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378
  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( '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. isLimit: true,
  35. isSelectable: true
  36. } );
  37. model.schema.extend( '$block', { allowIn: 'tableCell' } );
  38. model.schema.register( 'image', {
  39. isObject: true,
  40. isBlock: true,
  41. allowWhere: '$block'
  42. } );
  43. model.schema.extend( '$block', { allowIn: 'tableCell' } );
  44. model.schema.register( 'caption', {
  45. allowIn: 'image',
  46. allowContentOf: '$block',
  47. isLimit: true
  48. } );
  49. model.schema.register( 'inlineWidget', {
  50. isObject: true,
  51. allowIn: [ '$block', '$clipboardHolder' ]
  52. } );
  53. model.schema.register( 'figure', {
  54. allowIn: '$root',
  55. allowAttributes: [ 'name', 'title' ]
  56. } );
  57. } );
  58. it( 'should not crash if there is no correct position for model selection', () => {
  59. setModelData( model, '' );
  60. // Note that auto-paragraphing post-fixer injected a paragraph into the empty root.
  61. expect( getModelData( model ) ).to.equal( '<paragraph>[]</paragraph>' );
  62. } );
  63. it( 'should react to structure changes', () => {
  64. setModelData( model, '<paragraph>[]foo</paragraph><image></image>' );
  65. model.change( writer => {
  66. writer.remove( modelRoot.getChild( 0 ) );
  67. } );
  68. expect( getModelData( model ) ).to.equal( '[<image></image>]' );
  69. } );
  70. it( 'should react to selection changes', () => {
  71. setModelData( model, '<paragraph>[]foo</paragraph><image></image>' );
  72. // <paragraph>foo</paragraph>[]<image></image>
  73. model.change( writer => {
  74. writer.setSelection(
  75. writer.createRange( writer.createPositionAt( modelRoot, 1 ), writer.createPositionAt( modelRoot, 1 ) )
  76. );
  77. } );
  78. expect( getModelData( model ) ).to.equal( '<paragraph>foo[]</paragraph><image></image>' );
  79. } );
  80. describe( 'selection - table scenarios', () => {
  81. beforeEach( () => {
  82. setModelData( model,
  83. '<paragraph>[]foo</paragraph>' +
  84. '<table>' +
  85. '<tableRow>' +
  86. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  87. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  88. '</tableRow>' +
  89. '</table>' +
  90. '<paragraph>bar</paragraph>'
  91. );
  92. } );
  93. it( 'should fix #1 (range start outside table, end on table cell)', () => {
  94. // <paragraph>f[oo</paragraph><table><tableRow><tableCell></tableCell>]<tableCell>...
  95. model.change( writer => {
  96. writer.setSelection( writer.createRange(
  97. writer.createPositionAt( modelRoot.getChild( 0 ), 1 ),
  98. writer.createPositionAt( modelRoot.getChild( 1 ).getChild( 0 ), 1 )
  99. ) );
  100. } );
  101. expect( getModelData( model ) ).to.equal(
  102. '<paragraph>f[oo</paragraph>' +
  103. '<table>' +
  104. '<tableRow>' +
  105. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  106. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  107. '</tableRow>' +
  108. '</table>]' +
  109. '<paragraph>bar</paragraph>'
  110. );
  111. } );
  112. it( 'should fix #2 (range start on table cell, end outside table)', () => {
  113. // ...<table><tableRow><tableCell></tableCell>[<tableCell></tableCell></tableRow></table><paragraph>b]ar</paragraph>
  114. model.change( writer => {
  115. writer.setSelection( writer.createRange(
  116. writer.createPositionAt( modelRoot.getChild( 1 ).getChild( 0 ), 1 ),
  117. writer.createPositionAt( modelRoot.getChild( 2 ), 1 )
  118. ) );
  119. } );
  120. expect( getModelData( model ) ).to.equal(
  121. '<paragraph>foo</paragraph>' +
  122. '[<table>' +
  123. '<tableRow>' +
  124. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  125. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  126. '</tableRow>' +
  127. '</table>' +
  128. '<paragraph>b]ar</paragraph>'
  129. );
  130. } );
  131. it( 'should fix #3', () => {
  132. // <paragraph>f[oo</paragraph><table>]<tableRow>...
  133. model.change( writer => {
  134. writer.setSelection( writer.createRange(
  135. writer.createPositionAt( modelRoot.getChild( 0 ), 1 ),
  136. writer.createPositionAt( modelRoot.getChild( 1 ), 0 )
  137. ) );
  138. } );
  139. expect( getModelData( model ) ).to.equal(
  140. '<paragraph>f[oo</paragraph>' +
  141. '<table>' +
  142. '<tableRow>' +
  143. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  144. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  145. '</tableRow>' +
  146. '</table>]' +
  147. '<paragraph>bar</paragraph>'
  148. );
  149. } );
  150. it( 'should fix #4', () => {
  151. // <paragraph>foo</paragraph><table><tableRow><tableCell>a[aa</tableCell><tableCell>b]bb</tableCell>
  152. model.change( writer => {
  153. writer.setSelection( writer.createRange(
  154. writer.createPositionAt( modelRoot.getNodeByPath( [ 1, 0, 0, 0 ] ), 1 ),
  155. writer.createPositionAt( modelRoot.getNodeByPath( [ 1, 0, 1, 0 ] ), 2 )
  156. ) );
  157. } );
  158. expect( getModelData( model ) ).to.equal(
  159. '<paragraph>foo</paragraph>' +
  160. '[<table>' +
  161. '<tableRow>' +
  162. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  163. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  164. '</tableRow>' +
  165. '</table>]' +
  166. '<paragraph>bar</paragraph>'
  167. );
  168. } );
  169. it( 'should fix #5 (collapsed selection between tables)', () => {
  170. setModelData( model,
  171. '<paragraph>foo</paragraph>' +
  172. '<table>' +
  173. '<tableRow>' +
  174. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  175. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  176. '</tableRow>' +
  177. '</table>' +
  178. '[]' +
  179. '<table>' +
  180. '<tableRow>' +
  181. '<tableCell><paragraph>xxx</paragraph></tableCell>' +
  182. '<tableCell><paragraph>yyy</paragraph></tableCell>' +
  183. '</tableRow>' +
  184. '</table>' +
  185. '<paragraph>baz</paragraph>'
  186. );
  187. assertEqualMarkup( getModelData( model ),
  188. '<paragraph>foo</paragraph>' +
  189. '[<table>' +
  190. '<tableRow>' +
  191. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  192. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  193. '</tableRow>' +
  194. '</table>]' +
  195. '<table>' +
  196. '<tableRow>' +
  197. '<tableCell><paragraph>xxx</paragraph></tableCell>' +
  198. '<tableCell><paragraph>yyy</paragraph></tableCell>' +
  199. '</tableRow>' +
  200. '</table>' +
  201. '<paragraph>baz</paragraph>'
  202. );
  203. } );
  204. // There's a chance that this and the following test will not be up to date with
  205. // how the table feature is really implemented once we'll introduce row/cells/columns selection
  206. // in which case all these elements will need to be marked as objects.
  207. it( 'should fix #6 (element selection of not an object)', () => {
  208. setModelData( model,
  209. '<paragraph>foo</paragraph>' +
  210. '<table>' +
  211. '[<tableRow>' +
  212. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  213. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  214. '</tableRow>]' +
  215. '</table>' +
  216. '<paragraph>baz</paragraph>'
  217. );
  218. expect( getModelData( model ) ).to.equal(
  219. '<paragraph>foo</paragraph>' +
  220. '[<table>' +
  221. '<tableRow>' +
  222. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  223. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  224. '</tableRow>' +
  225. '</table>]' +
  226. '<paragraph>baz</paragraph>'
  227. );
  228. } );
  229. it( 'should fix #7 (element selection of non-objects)', () => {
  230. setModelData( model,
  231. '<paragraph>foo</paragraph>' +
  232. '<table>' +
  233. '[<tableRow>' +
  234. '<tableCell><paragraph>1</paragraph></tableCell>' +
  235. '<tableCell><paragraph>2</paragraph></tableCell>' +
  236. '</tableRow>' +
  237. '<tableRow>' +
  238. '<tableCell><paragraph>3</paragraph></tableCell>' +
  239. '<tableCell><paragraph>4</paragraph></tableCell>]' +
  240. '</tableRow>' +
  241. '<tableRow>' +
  242. '<tableCell><paragraph>5</paragraph></tableCell>' +
  243. '<tableCell><paragraph>6</paragraph></tableCell>' +
  244. '</tableRow>' +
  245. '</table>' +
  246. '<paragraph>baz</paragraph>'
  247. );
  248. expect( getModelData( model ) ).to.equal(
  249. '<paragraph>foo</paragraph>' +
  250. '[<table>' +
  251. '<tableRow>' +
  252. '<tableCell><paragraph>1</paragraph></tableCell>' +
  253. '<tableCell><paragraph>2</paragraph></tableCell>' +
  254. '</tableRow>' +
  255. '<tableRow>' +
  256. '<tableCell><paragraph>3</paragraph></tableCell>' +
  257. '<tableCell><paragraph>4</paragraph></tableCell>' +
  258. '</tableRow>' +
  259. '<tableRow>' +
  260. '<tableCell><paragraph>5</paragraph></tableCell>' +
  261. '<tableCell><paragraph>6</paragraph></tableCell>' +
  262. '</tableRow>' +
  263. '</table>]' +
  264. '<paragraph>baz</paragraph>'
  265. );
  266. } );
  267. it( 'should fix #8 (cross-limit selection which starts in a non-limit elements)', () => {
  268. model.schema.extend( 'paragraph', { allowIn: 'tableCell' } );
  269. setModelData( model,
  270. '<paragraph>foo</paragraph>' +
  271. '<table>' +
  272. '<tableRow>' +
  273. '<tableCell><paragraph>f[oo</paragraph></tableCell>' +
  274. '<tableCell><paragraph>b]ar</paragraph></tableCell>' +
  275. '</tableRow>' +
  276. '</table>' +
  277. '<paragraph>baz</paragraph>'
  278. );
  279. expect( getModelData( model ) ).to.equal(
  280. '<paragraph>foo</paragraph>' +
  281. '[<table>' +
  282. '<tableRow>' +
  283. '<tableCell><paragraph>foo</paragraph></tableCell>' +
  284. '<tableCell><paragraph>bar</paragraph></tableCell>' +
  285. '</tableRow>' +
  286. '</table>]' +
  287. '<paragraph>baz</paragraph>'
  288. );
  289. } );
  290. it( 'should not fix #1 (selection over paragraphs outside table)', () => {
  291. setModelData( model,
  292. '<paragraph>foo</paragraph>' +
  293. '<table>' +
  294. '<tableRow>' +
  295. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  296. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  297. '</tableRow>' +
  298. '</table>' +
  299. '<paragraph>b[ar</paragraph>' +
  300. '<paragraph>ba]z</paragraph>'
  301. );
  302. expect( getModelData( model ) ).to.equal(
  303. '<paragraph>foo</paragraph>' +
  304. '<table>' +
  305. '<tableRow>' +
  306. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  307. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  308. '</tableRow>' +
  309. '</table>' +
  310. '<paragraph>b[ar</paragraph>' +
  311. '<paragraph>ba]z</paragraph>'
  312. );
  313. } );
  314. it( 'should not fix #2 (selection over image in table)', () => {
  315. setModelData( model,
  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. model.change( writer => {
  325. const image = model.document.getRoot().getNodeByPath( [ 1, 0, 0, 1 ] );
  326. writer.setSelection( writer.createRangeOn( image ) );
  327. } );
  328. expect( getModelData( model ) ).to.equal(
  329. '<paragraph>foo</paragraph>' +
  330. '<table>' +
  331. '<tableRow>' +
  332. '<tableCell><paragraph>foo</paragraph>[<image></image>]</tableCell>' +
  333. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  334. '</tableRow>' +
  335. '</table>'
  336. );
  337. } );
  338. it( 'should not fix #3 (selection over paragraph & image in table)', () => {
  339. setModelData( model,
  340. '<paragraph>foo</paragraph>' +
  341. '<table>' +
  342. '<tableRow>' +
  343. '<tableCell><paragraph>foo</paragraph><image></image></tableCell>' +
  344. '<tableCell><paragraph>[]bbb</paragraph></tableCell>' +
  345. '</tableRow>' +
  346. '</table>'
  347. );
  348. model.change( writer => {
  349. const tableCell = model.document.getRoot().getNodeByPath( [ 1, 0, 0 ] );
  350. writer.setSelection( writer.createRangeIn( tableCell ) );
  351. } );
  352. expect( getModelData( model ) ).to.equal(
  353. '<paragraph>foo</paragraph>' +
  354. '<table>' +
  355. '<tableRow>' +
  356. '<tableCell><paragraph>[foo</paragraph><image></image>]</tableCell>' +
  357. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  358. '</tableRow>' +
  359. '</table>'
  360. );
  361. } );
  362. it( 'should not fix #4 (selection over image & paragraph in table)', () => {
  363. setModelData( model,
  364. '<paragraph>foo</paragraph>' +
  365. '<table>' +
  366. '<tableRow>' +
  367. '<tableCell><image></image><paragraph>foo</paragraph></tableCell>' +
  368. '<tableCell><paragraph>[]bbb</paragraph></tableCell>' +
  369. '</tableRow>' +
  370. '</table>'
  371. );
  372. model.change( writer => {
  373. const tableCell = model.document.getRoot().getNodeByPath( [ 1, 0, 0 ] );
  374. writer.setSelection( writer.createRangeIn( tableCell ) );
  375. } );
  376. expect( getModelData( model ) ).to.equal(
  377. '<paragraph>foo</paragraph>' +
  378. '<table>' +
  379. '<tableRow>' +
  380. '<tableCell>[<image></image><paragraph>foo]</paragraph></tableCell>' +
  381. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  382. '</tableRow>' +
  383. '</table>'
  384. );
  385. } );
  386. it( 'should not fix #5 (selection over blockQuote in table)', () => {
  387. model.schema.register( 'blockQuote', {
  388. allowWhere: '$block',
  389. allowContentOf: '$root'
  390. } );
  391. setModelData( model,
  392. '<paragraph>foo</paragraph>' +
  393. '<table>' +
  394. '<tableRow>' +
  395. '<tableCell><blockQuote><paragraph>foo</paragraph></blockQuote></tableCell>' +
  396. '<tableCell><paragraph>[]bbb</paragraph></tableCell>' +
  397. '</tableRow>' +
  398. '</table>'
  399. );
  400. model.change( writer => {
  401. const tableCell = model.document.getRoot().getNodeByPath( [ 1, 0, 0 ] );
  402. writer.setSelection( writer.createRangeIn( tableCell ) );
  403. } );
  404. expect( getModelData( model ) ).to.equal(
  405. '<paragraph>foo</paragraph>' +
  406. '<table>' +
  407. '<tableRow>' +
  408. '<tableCell><blockQuote><paragraph>[foo]</paragraph></blockQuote></tableCell>' +
  409. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  410. '</tableRow>' +
  411. '</table>'
  412. );
  413. } );
  414. it( 'should fix multiple ranges #1', () => {
  415. model.change( writer => {
  416. const ranges = [
  417. writer.createRange(
  418. writer.createPositionFromPath( modelRoot, [ 0, 1 ] ),
  419. writer.createPositionFromPath( modelRoot, [ 1, 0 ] )
  420. ),
  421. writer.createRange(
  422. writer.createPositionFromPath( modelRoot, [ 1, 0, 0, 0 ] ),
  423. writer.createPositionFromPath( modelRoot, [ 1, 1 ] )
  424. )
  425. ];
  426. writer.setSelection( ranges );
  427. } );
  428. expect( getModelData( model ) ).to.equal(
  429. '<paragraph>f[oo</paragraph>' +
  430. '<table>' +
  431. '<tableRow>' +
  432. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  433. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  434. '</tableRow>' +
  435. '</table>]' +
  436. '<paragraph>bar</paragraph>'
  437. );
  438. } );
  439. it( 'should fix multiple ranges #2', () => {
  440. model.change( writer => {
  441. const ranges = [
  442. writer.createRange(
  443. writer.createPositionFromPath( modelRoot, [ 0, 1 ] ),
  444. writer.createPositionFromPath( modelRoot, [ 1, 0 ] )
  445. ),
  446. writer.createRange(
  447. writer.createPositionFromPath( modelRoot, [ 1, 0, 0, 0 ] ),
  448. writer.createPositionFromPath( modelRoot, [ 2, 2 ] )
  449. )
  450. ];
  451. writer.setSelection( ranges );
  452. } );
  453. expect( getModelData( model ) ).to.equal(
  454. '<paragraph>f[oo</paragraph>' +
  455. '<table>' +
  456. '<tableRow>' +
  457. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  458. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  459. '</tableRow>' +
  460. '</table>' +
  461. '<paragraph>ba]r</paragraph>'
  462. );
  463. } );
  464. it( 'should fix multiple ranges #3', () => {
  465. setModelData( model,
  466. '<paragraph>foo</paragraph>' +
  467. '<table>' +
  468. '<tableRow>' +
  469. '<tableCell><paragraph>[aaa</paragraph></tableCell>' +
  470. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  471. '</tableRow>' +
  472. '<tableRow>]' +
  473. '<tableCell><paragraph>[aaa</paragraph></tableCell>' +
  474. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  475. '</tableRow>' +
  476. '<tableRow>]' +
  477. '<tableCell><paragraph>[aaa</paragraph></tableCell>' +
  478. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  479. '</tableRow>' +
  480. '<tableRow>]' +
  481. '<tableCell><paragraph>[aaa</paragraph></tableCell>' +
  482. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  483. '</tableRow>' +
  484. '</table>' +
  485. '<paragraph>b]az</paragraph>'
  486. );
  487. expect( getModelData( model ) ).to.equal(
  488. '<paragraph>foo</paragraph>' +
  489. '[<table>' +
  490. '<tableRow>' +
  491. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  492. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  493. '</tableRow>' +
  494. '<tableRow>' +
  495. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  496. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  497. '</tableRow>' +
  498. '<tableRow>' +
  499. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  500. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  501. '</tableRow>' +
  502. '<tableRow>' +
  503. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  504. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  505. '</tableRow>' +
  506. '</table>' +
  507. '<paragraph>b]az</paragraph>'
  508. );
  509. } );
  510. it( 'should not fix multiple ranges #1 (not overlapping ranges)', () => {
  511. model.change( writer => {
  512. const ranges = [
  513. writer.createRange(
  514. writer.createPositionFromPath( modelRoot, [ 0, 1 ] ),
  515. writer.createPositionFromPath( modelRoot, [ 1, 0 ] )
  516. ),
  517. writer.createRange(
  518. writer.createPositionFromPath( modelRoot, [ 1, 0, 0, 0 ] ),
  519. writer.createPositionFromPath( modelRoot, [ 2, 1 ] )
  520. ),
  521. writer.createRange(
  522. writer.createPositionFromPath( modelRoot, [ 2, 2 ] ),
  523. writer.createPositionFromPath( modelRoot, [ 2, 3 ] )
  524. )
  525. ];
  526. writer.setSelection( ranges );
  527. } );
  528. expect( getModelData( model ) ).to.equal(
  529. '<paragraph>f[oo</paragraph>' +
  530. '<table>' +
  531. '<tableRow>' +
  532. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  533. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  534. '</tableRow>' +
  535. '</table>' +
  536. '<paragraph>b]a[r]</paragraph>'
  537. );
  538. } );
  539. it( 'should not fix multiple ranges on objects (table selection)', () => {
  540. setModelData( model,
  541. '<table>' +
  542. '<tableRow>' +
  543. '[<tableCell><paragraph>a</paragraph></tableCell>]' +
  544. '[<tableCell><paragraph>b</paragraph></tableCell>]' +
  545. '</tableRow>' +
  546. '<tableRow>' +
  547. '[<tableCell><paragraph>c</paragraph></tableCell>]' +
  548. '<tableCell><paragraph>d</paragraph></tableCell>' +
  549. '</tableRow>' +
  550. '</table>'
  551. );
  552. assertEqualMarkup( getModelData( model ),
  553. '<table>' +
  554. '<tableRow>' +
  555. '[<tableCell><paragraph>a</paragraph></tableCell>]' +
  556. '[<tableCell><paragraph>b</paragraph></tableCell>]' +
  557. '</tableRow>' +
  558. '<tableRow>' +
  559. '[<tableCell><paragraph>c</paragraph></tableCell>]' +
  560. '<tableCell><paragraph>d</paragraph></tableCell>' +
  561. '</tableRow>' +
  562. '</table>'
  563. );
  564. } );
  565. it( 'should fix selection on block', () => {
  566. model.schema.extend( '$block', { allowIn: 'tableCell' } );
  567. setModelData( model,
  568. '<table>' +
  569. '<tableRow><tableCell>[<paragraph>aaa</paragraph>]</tableCell></tableRow>' +
  570. '</table>'
  571. );
  572. assertEqualMarkup( getModelData( model ),
  573. '<table>' +
  574. '<tableRow><tableCell><paragraph>[aaa]</paragraph></tableCell></tableRow>' +
  575. '</table>'
  576. );
  577. } );
  578. it( 'should allow multi-range selection on non-continues blocks (column selected)', () => {
  579. setModelData( model,
  580. '<table>' +
  581. '<tableRow>' +
  582. '[<tableCell><paragraph>A1</paragraph></tableCell>]' +
  583. '<tableCell><paragraph>B1</paragraph></tableCell>' +
  584. '</tableRow>' +
  585. '<tableRow>' +
  586. '[<tableCell><paragraph>A2</paragraph></tableCell>]' +
  587. '<tableCell><paragraph>B2</paragraph></tableCell>' +
  588. '</tableRow>' +
  589. '<tableRow>' +
  590. '[<tableCell><paragraph>A3</paragraph></tableCell>]' +
  591. '<tableCell><paragraph>B3</paragraph></tableCell>' +
  592. '</tableRow>' +
  593. '</table>'
  594. );
  595. assertEqualMarkup( getModelData( model ),
  596. '<table>' +
  597. '<tableRow>' +
  598. '[<tableCell><paragraph>A1</paragraph></tableCell>]' +
  599. '<tableCell><paragraph>B1</paragraph></tableCell>' +
  600. '</tableRow>' +
  601. '<tableRow>' +
  602. '[<tableCell><paragraph>A2</paragraph></tableCell>]' +
  603. '<tableCell><paragraph>B2</paragraph></tableCell>' +
  604. '</tableRow>' +
  605. '<tableRow>' +
  606. '[<tableCell><paragraph>A3</paragraph></tableCell>]' +
  607. '<tableCell><paragraph>B3</paragraph></tableCell>' +
  608. '</tableRow>' +
  609. '</table>'
  610. );
  611. } );
  612. it( 'should allow multi-range selection on continues blocks (row selected)', () => {
  613. setModelData( model,
  614. '<table>' +
  615. '<tableRow>' +
  616. '<tableCell><paragraph>A1</paragraph></tableCell>' +
  617. '<tableCell><paragraph>B1</paragraph></tableCell>' +
  618. '<tableCell><paragraph>C1</paragraph></tableCell>' +
  619. '</tableRow>' +
  620. '<tableRow>' +
  621. '[<tableCell><paragraph>A2</paragraph></tableCell>]' +
  622. '[<tableCell><paragraph>B2</paragraph></tableCell>]' +
  623. '[<tableCell><paragraph>C2</paragraph></tableCell>]' +
  624. '</tableRow>' +
  625. '<tableRow>' +
  626. '<tableCell><paragraph>A3</paragraph></tableCell>' +
  627. '<tableCell><paragraph>B3</paragraph></tableCell>' +
  628. '<tableCell><paragraph>C3</paragraph></tableCell>' +
  629. '</tableRow>' +
  630. '</table>'
  631. );
  632. assertEqualMarkup( getModelData( model ),
  633. '<table>' +
  634. '<tableRow>' +
  635. '<tableCell><paragraph>A1</paragraph></tableCell>' +
  636. '<tableCell><paragraph>B1</paragraph></tableCell>' +
  637. '<tableCell><paragraph>C1</paragraph></tableCell>' +
  638. '</tableRow>' +
  639. '<tableRow>' +
  640. '[<tableCell><paragraph>A2</paragraph></tableCell>]' +
  641. '[<tableCell><paragraph>B2</paragraph></tableCell>]' +
  642. '[<tableCell><paragraph>C2</paragraph></tableCell>]' +
  643. '</tableRow>' +
  644. '<tableRow>' +
  645. '<tableCell><paragraph>A3</paragraph></tableCell>' +
  646. '<tableCell><paragraph>B3</paragraph></tableCell>' +
  647. '<tableCell><paragraph>C3</paragraph></tableCell>' +
  648. '</tableRow>' +
  649. '</table>'
  650. );
  651. } );
  652. it( 'should allow multi-range selection with mixed continues/non-continues blocks (part of table selected)', () => {
  653. setModelData( model,
  654. '<table>' +
  655. '<tableRow>' +
  656. '<tableCell><paragraph>A1</paragraph></tableCell>' +
  657. '<tableCell><paragraph>B1</paragraph></tableCell>' +
  658. '<tableCell><paragraph>C1</paragraph></tableCell>' +
  659. '</tableRow>' +
  660. '<tableRow>' +
  661. '<tableCell><paragraph>A2</paragraph></tableCell>' +
  662. '[<tableCell><paragraph>B2</paragraph></tableCell>]' +
  663. '[<tableCell><paragraph>C2</paragraph></tableCell>]' +
  664. '</tableRow>' +
  665. '<tableRow>' +
  666. '<tableCell><paragraph>A3</paragraph></tableCell>' +
  667. '[<tableCell><paragraph>B3</paragraph></tableCell>]' +
  668. '[<tableCell><paragraph>C3</paragraph></tableCell>]' +
  669. '</tableRow>' +
  670. '</table>'
  671. );
  672. assertEqualMarkup( getModelData( model ),
  673. '<table>' +
  674. '<tableRow>' +
  675. '<tableCell><paragraph>A1</paragraph></tableCell>' +
  676. '<tableCell><paragraph>B1</paragraph></tableCell>' +
  677. '<tableCell><paragraph>C1</paragraph></tableCell>' +
  678. '</tableRow>' +
  679. '<tableRow>' +
  680. '<tableCell><paragraph>A2</paragraph></tableCell>' +
  681. '[<tableCell><paragraph>B2</paragraph></tableCell>]' +
  682. '[<tableCell><paragraph>C2</paragraph></tableCell>]' +
  683. '</tableRow>' +
  684. '<tableRow>' +
  685. '<tableCell><paragraph>A3</paragraph></tableCell>' +
  686. '[<tableCell><paragraph>B3</paragraph></tableCell>]' +
  687. '[<tableCell><paragraph>C3</paragraph></tableCell>]' +
  688. '</tableRow>' +
  689. '</table>'
  690. );
  691. } );
  692. it( 'should not fix ranges in multi-range selection (each range set differently - but valid)', () => {
  693. setModelData( model,
  694. '<paragraph>[foo]</paragraph>' +
  695. '<table>' +
  696. '<tableRow>' +
  697. '[<tableCell><paragraph>aaa</paragraph></tableCell>]' +
  698. '<tableCell><paragraph>[bbb]</paragraph></tableCell>' +
  699. '</tableRow>' +
  700. '</table>'
  701. );
  702. assertEqualMarkup( getModelData( model ),
  703. '<paragraph>[foo]</paragraph>' +
  704. '<table>' +
  705. '<tableRow>' +
  706. '[<tableCell><paragraph>aaa</paragraph></tableCell>]' +
  707. '<tableCell><paragraph>[bbb]</paragraph></tableCell>' +
  708. '</tableRow>' +
  709. '</table>'
  710. );
  711. } );
  712. it( 'should fix partially wrong selection (last range is post-fixed on whole table)', () => {
  713. setModelData( model,
  714. '<table>' +
  715. '<tableRow>' +
  716. '[<tableCell><paragraph>aaa</paragraph></tableCell>]' +
  717. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  718. '[<tableCell><paragraph>ccc</paragraph></tableCell>' +
  719. '</tableRow>]' +
  720. '</table>'
  721. );
  722. assertEqualMarkup( getModelData( model ),
  723. '[<table>' +
  724. '<tableRow>' +
  725. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  726. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  727. '<tableCell><paragraph>ccc</paragraph></tableCell>' +
  728. '</tableRow>' +
  729. '</table>]'
  730. );
  731. } );
  732. it( 'should fix partially wrong selection (first range is post-fixed on whole table)', () => {
  733. setModelData( model,
  734. '<table>' +
  735. '[<tableRow>' +
  736. '<tableCell><paragraph>aaa</paragraph></tableCell>]' +
  737. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  738. '[<tableCell><paragraph>ccc</paragraph></tableCell>]' +
  739. '</tableRow>' +
  740. '</table>'
  741. );
  742. assertEqualMarkup( getModelData( model ),
  743. '[<table>' +
  744. '<tableRow>' +
  745. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  746. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  747. '<tableCell><paragraph>ccc</paragraph></tableCell>' +
  748. '</tableRow>' +
  749. '</table>]'
  750. );
  751. } );
  752. it( 'should not reset the selection if the final range is the same as the initial one', () => {
  753. setModelData( model,
  754. '<table>' +
  755. '<tableRow>' +
  756. '<tableCell>[<image></image>]</tableCell>' +
  757. '</tableRow>' +
  758. '</table>'
  759. );
  760. // Setting a selection attribute will trigger the post-fixer. However, because this
  761. // action does not affect ranges, the post-fixer should not set a new selection and,
  762. // in consequence, should not clear the selection attribute (like it normally would when
  763. // a new selection is set).
  764. // https://github.com/ckeditor/ckeditor5/issues/6693
  765. model.change( writer => {
  766. writer.setSelectionAttribute( 'foo', 'bar' );
  767. } );
  768. assertEqualMarkup( getModelData( model ),
  769. '<table>' +
  770. '<tableRow>' +
  771. '<tableCell>[<image></image>]</tableCell>' +
  772. '</tableRow>' +
  773. '</table>'
  774. );
  775. expect( model.document.selection.hasAttribute( 'foo' ) ).to.be.true;
  776. } );
  777. } );
  778. describe( 'non-collapsed selection - image scenarios', () => {
  779. beforeEach( () => {
  780. setModelData( model,
  781. '<paragraph>[]foo</paragraph>' +
  782. '<image>' +
  783. '<caption>xxx</caption>' +
  784. '</image>' +
  785. '<paragraph>bar</paragraph>'
  786. );
  787. } );
  788. it( 'should fix #1 (crossing object and limit boundaries)', () => {
  789. model.change( writer => {
  790. // <paragraph>f[oo</paragraph><image><caption>x]xx</caption>...
  791. writer.setSelection( writer.createRange(
  792. writer.createPositionAt( modelRoot.getChild( 0 ), 1 ),
  793. writer.createPositionAt( modelRoot.getChild( 1 ).getChild( 0 ), 1 )
  794. ) );
  795. } );
  796. expect( getModelData( model ) ).to.equal(
  797. '<paragraph>f[oo</paragraph>' +
  798. '<image>' +
  799. '<caption>xxx</caption>' +
  800. '</image>]' +
  801. '<paragraph>bar</paragraph>'
  802. );
  803. } );
  804. it( 'should fix #2 (crossing object boundary)', () => {
  805. model.change( writer => {
  806. // <paragraph>f[oo</paragraph><image>]<caption>xxx</caption>...
  807. writer.setSelection( writer.createRange(
  808. writer.createPositionAt( modelRoot.getChild( 0 ), 1 ),
  809. writer.createPositionAt( modelRoot.getChild( 1 ), 0 )
  810. ) );
  811. } );
  812. expect( getModelData( model ) ).to.equal(
  813. '<paragraph>f[oo</paragraph>' +
  814. '<image>' +
  815. '<caption>xxx</caption>' +
  816. '</image>]' +
  817. '<paragraph>bar</paragraph>'
  818. );
  819. } );
  820. it( 'should fix #3 (crossing object boundary)', () => {
  821. model.change( writer => {
  822. // <paragraph>f[oo</paragraph><image><caption>xxx</caption>]</image>...
  823. writer.setSelection( writer.createRange(
  824. writer.createPositionAt( modelRoot.getChild( 0 ), 1 ),
  825. writer.createPositionAt( modelRoot.getChild( 1 ), 1 )
  826. ) );
  827. } );
  828. expect( getModelData( model ) ).to.equal(
  829. '<paragraph>f[oo</paragraph>' +
  830. '<image>' +
  831. '<caption>xxx</caption>' +
  832. '</image>]' +
  833. '<paragraph>bar</paragraph>'
  834. );
  835. } );
  836. it( 'should fix #4 (element selection of not an object)', () => {
  837. model.change( writer => {
  838. // <paragraph>foo</paragraph><image>[<caption>xxx</caption>]</image>...
  839. writer.setSelection( writer.createRange(
  840. writer.createPositionAt( modelRoot.getChild( 1 ), 0 ),
  841. writer.createPositionAt( modelRoot.getChild( 1 ), 1 )
  842. ) );
  843. } );
  844. expect( getModelData( model ) ).to.equal(
  845. '<paragraph>foo</paragraph>' +
  846. '[<image>' +
  847. '<caption>xxx</caption>' +
  848. '</image>]' +
  849. '<paragraph>bar</paragraph>'
  850. );
  851. } );
  852. it( 'should not fix #1 (element selection of an object)', () => {
  853. model.change( writer => {
  854. // <paragraph>foo</paragraph>[<image><caption>xxx</caption></image>]...
  855. writer.setSelection( writer.createRange(
  856. writer.createPositionAt( modelRoot, 1 ),
  857. writer.createPositionAt( modelRoot, 2 )
  858. ) );
  859. } );
  860. expect( getModelData( model ) ).to.equal(
  861. '<paragraph>foo</paragraph>' +
  862. '[<image>' +
  863. '<caption>xxx</caption>' +
  864. '</image>]' +
  865. '<paragraph>bar</paragraph>'
  866. );
  867. } );
  868. it( 'should not fix #2 (inside a limit)', () => {
  869. model.change( writer => {
  870. const caption = modelRoot.getChild( 1 ).getChild( 0 );
  871. // <paragraph>foo</paragraph><image><caption>[xxx]</caption></image>...
  872. writer.setSelection( writer.createRange(
  873. writer.createPositionAt( caption, 0 ),
  874. writer.createPositionAt( caption, 3 )
  875. ) );
  876. } );
  877. expect( getModelData( model ) ).to.equal(
  878. '<paragraph>foo</paragraph>' +
  879. '<image>' +
  880. '<caption>[xxx]</caption>' +
  881. '</image>' +
  882. '<paragraph>bar</paragraph>'
  883. );
  884. } );
  885. it( 'should not fix #3 (inside a limit - partial text selection)', () => {
  886. model.change( writer => {
  887. const caption = modelRoot.getChild( 1 ).getChild( 0 );
  888. // <paragraph>foo</paragraph><image><caption>[xx]x</caption></image>...
  889. writer.setSelection( writer.createRange(
  890. writer.createPositionAt( caption, 0 ),
  891. writer.createPositionAt( caption, 2 )
  892. ) );
  893. } );
  894. expect( getModelData( model ) ).to.equal(
  895. '<paragraph>foo</paragraph>' +
  896. '<image>' +
  897. '<caption>[xx]x</caption>' +
  898. '</image>' +
  899. '<paragraph>bar</paragraph>'
  900. );
  901. } );
  902. it( 'should not fix #4 (inside a limit - partial text selection)', () => {
  903. model.change( writer => {
  904. const caption = modelRoot.getChild( 1 ).getChild( 0 );
  905. // <paragraph>foo</paragraph><image><caption>x[xx]</caption></image>...
  906. writer.setSelection( writer.createRange(
  907. writer.createPositionAt( caption, 1 ),
  908. writer.createPositionAt( caption, 3 )
  909. ) );
  910. } );
  911. expect( getModelData( model ) ).to.equal(
  912. '<paragraph>foo</paragraph>' +
  913. '<image>' +
  914. '<caption>x[xx]</caption>' +
  915. '</image>' +
  916. '<paragraph>bar</paragraph>'
  917. );
  918. } );
  919. it( 'should not fix #5 (selection in root on non limit element that doesn\'t allow text)', () => {
  920. setModelData( model,
  921. '[<figure></figure>]'
  922. );
  923. expect( getModelData( model ) ).to.equal(
  924. '[<figure></figure>]'
  925. );
  926. } );
  927. } );
  928. describe( 'non-collapsed selection - other scenarios', () => {
  929. it( 'should fix #1 (element selection of not an object)', () => {
  930. setModelData( model,
  931. '<paragraph>aaa</paragraph>' +
  932. '[<paragraph>bbb</paragraph>]' +
  933. '<paragraph>ccc</paragraph>'
  934. );
  935. expect( getModelData( model ) ).to.equal(
  936. '<paragraph>aaa</paragraph>' +
  937. '<paragraph>[bbb]</paragraph>' +
  938. '<paragraph>ccc</paragraph>'
  939. );
  940. } );
  941. it( 'should fix #2 (elements selection of not an object)', () => {
  942. setModelData( model,
  943. '<paragraph>aaa</paragraph>' +
  944. '[<paragraph>bbb</paragraph>' +
  945. '<paragraph>ccc</paragraph>]'
  946. );
  947. expect( getModelData( model ) ).to.equal(
  948. '<paragraph>aaa</paragraph>' +
  949. '<paragraph>[bbb</paragraph>' +
  950. '<paragraph>ccc]</paragraph>'
  951. );
  952. } );
  953. it( 'should fix #3 (partial selection of not an object)', () => {
  954. setModelData( model,
  955. '<paragraph>aaa</paragraph>' +
  956. '[<paragraph>bbb</paragraph>' +
  957. '<paragraph>ccc]</paragraph>'
  958. );
  959. expect( getModelData( model ) ).to.equal(
  960. '<paragraph>aaa</paragraph>' +
  961. '<paragraph>[bbb</paragraph>' +
  962. '<paragraph>ccc]</paragraph>'
  963. );
  964. } );
  965. it( 'should fix #4 (partial selection of not an object)', () => {
  966. setModelData( model,
  967. '<paragraph>aaa</paragraph>' +
  968. '<paragraph>b[bb</paragraph>]' +
  969. '<paragraph>ccc</paragraph>'
  970. );
  971. expect( getModelData( model ) ).to.equal(
  972. '<paragraph>aaa</paragraph>' +
  973. '<paragraph>b[bb]</paragraph>' +
  974. '<paragraph>ccc</paragraph>'
  975. );
  976. } );
  977. it( 'should fix #5 (partial selection of not an object)', () => {
  978. setModelData( model,
  979. '<paragraph>aaa</paragraph>' +
  980. '[<paragraph>bb]b</paragraph>' +
  981. '<paragraph>ccc</paragraph>'
  982. );
  983. expect( getModelData( model ) ).to.equal(
  984. '<paragraph>aaa</paragraph>' +
  985. '<paragraph>[bb]b</paragraph>' +
  986. '<paragraph>ccc</paragraph>'
  987. );
  988. } );
  989. it( 'should fix #6 (selection must not cross a limit element; starts in a root)', () => {
  990. model.schema.register( 'a', { isLimit: true, allowIn: '$root' } );
  991. model.schema.register( 'b', { isLimit: true, allowIn: 'a' } );
  992. model.schema.register( 'c', { allowIn: 'b' } );
  993. model.schema.extend( '$text', { allowIn: 'c' } );
  994. setModelData( model,
  995. '<a><b><c>[</c></b></a>]'
  996. );
  997. expect( getModelData( model ) ).to.equal( '[<a><b><c></c></b></a>]' );
  998. } );
  999. it( 'should fix #7 (selection must not cross a limit element; ends in a root)', () => {
  1000. model.schema.register( 'a', { isLimit: true, allowIn: '$root' } );
  1001. model.schema.register( 'b', { isLimit: true, allowIn: 'a' } );
  1002. model.schema.register( 'c', { allowIn: 'b' } );
  1003. model.schema.extend( '$text', { allowIn: 'c' } );
  1004. setModelData( model,
  1005. '[<a><b><c>]</c></b></a>'
  1006. );
  1007. expect( getModelData( model ) ).to.equal( '[<a><b><c></c></b></a>]' );
  1008. } );
  1009. it( 'should fix #8 (selection must not cross a limit element; starts in a non-limit)', () => {
  1010. model.schema.register( 'div', { allowIn: '$root' } );
  1011. model.schema.register( 'a', { isLimit: true, allowIn: 'div' } );
  1012. model.schema.register( 'b', { isLimit: true, allowIn: 'a' } );
  1013. model.schema.register( 'c', { allowIn: 'b' } );
  1014. model.schema.extend( '$text', { allowIn: 'c' } );
  1015. setModelData( model,
  1016. '<div>[<a><b><c>]</c></b></a></div>'
  1017. );
  1018. expect( getModelData( model ) ).to.equal( '<div>[<a><b><c></c></b></a>]</div>' );
  1019. } );
  1020. it( 'should fix #9 (selection must not cross a limit element; ends in a non-limit)', () => {
  1021. model.schema.register( 'div', { allowIn: '$root' } );
  1022. model.schema.register( 'a', { isLimit: true, allowIn: 'div' } );
  1023. model.schema.register( 'b', { isLimit: true, allowIn: 'a' } );
  1024. model.schema.register( 'c', { allowIn: 'b' } );
  1025. model.schema.extend( '$text', { allowIn: 'c' } );
  1026. setModelData( model,
  1027. '<div><a><b><c>[</c></b></a>]</div>'
  1028. );
  1029. expect( getModelData( model ) ).to.equal( '<div>[<a><b><c></c></b></a>]</div>' );
  1030. } );
  1031. it( 'should not fix #1 (selection on text node)', () => {
  1032. setModelData( model, '<paragraph>foob[a]r</paragraph>', { lastRangeBackward: true } );
  1033. expect( getModelData( model ) ).to.equal( '<paragraph>foob[a]r</paragraph>' );
  1034. } );
  1035. it( 'should not fix #2 (inline widget selected)', () => {
  1036. setModelData( model,
  1037. '<paragraph>[<inlineWidget></inlineWidget>]</paragraph>'
  1038. );
  1039. expect( getModelData( model ) ).to.equal(
  1040. '<paragraph>[<inlineWidget></inlineWidget>]</paragraph>'
  1041. );
  1042. } );
  1043. it( 'should not fix #3 (text around inline widget)', () => {
  1044. setModelData( model,
  1045. '<paragraph>fo[o<inlineWidget></inlineWidget>b]ar</paragraph>'
  1046. );
  1047. expect( getModelData( model ) ).to.equal(
  1048. '<paragraph>fo[o<inlineWidget></inlineWidget>b]ar</paragraph>'
  1049. );
  1050. } );
  1051. it( 'should not fix #4 (object in object)', () => {
  1052. model.schema.register( 'div', {
  1053. allowIn: [ '$root', 'div' ],
  1054. isObject: true
  1055. } );
  1056. setModelData( model, '<div>[<div></div>]</div>' );
  1057. model.change( writer => {
  1058. const innerDiv = model.document.getRoot().getNodeByPath( [ 0, 0 ] );
  1059. writer.setSelection( writer.createRangeOn( innerDiv ) );
  1060. } );
  1061. expect( getModelData( model ) ).to.equal( '<div>[<div></div>]</div>' );
  1062. } );
  1063. it( 'should not fix #5 (selection starts before a selectable, which is not an object)', () => {
  1064. model.schema.register( 'div', { allowIn: '$root' } );
  1065. model.schema.register( 'selectable', { isSelectable: true, allowIn: 'div' } );
  1066. model.schema.extend( '$text', { allowIn: 'selectable' } );
  1067. setModelData( model,
  1068. '<div>[<selectable>foo]</selectable></div>'
  1069. );
  1070. expect( getModelData( model ) ).to.equal( '<div>[<selectable>foo]</selectable></div>' );
  1071. } );
  1072. it( 'should not fix #6 (selection ends after before a selectable, which is not an object)', () => {
  1073. model.schema.register( 'div', { allowIn: '$root' } );
  1074. model.schema.register( 'selectable', { isSelectable: true, allowIn: 'div' } );
  1075. model.schema.extend( '$text', { allowIn: 'selectable' } );
  1076. setModelData( model,
  1077. '<div><selectable>[foo</selectable>]</div>'
  1078. );
  1079. expect( getModelData( model ) ).to.equal( '<div><selectable>[foo</selectable>]</div>' );
  1080. } );
  1081. } );
  1082. describe( 'non-collapsed selection - inline widget scenarios', () => {
  1083. beforeEach( () => {
  1084. model.schema.register( 'placeholder', {
  1085. allowWhere: '$text',
  1086. isInline: true
  1087. } );
  1088. } );
  1089. it( 'should fix selection that ends in inline element', () => {
  1090. setModelData( model, '<paragraph>aaa[<placeholder>]</placeholder>bbb</paragraph>' );
  1091. expect( getModelData( model ) ).to.equal( '<paragraph>aaa[]<placeholder></placeholder>bbb</paragraph>' );
  1092. } );
  1093. it( 'should fix selection that starts in inline element', () => {
  1094. setModelData( model, '<paragraph>aaa<placeholder>[</placeholder>]bbb</paragraph>' );
  1095. expect( getModelData( model ) ).to.equal( '<paragraph>aaa<placeholder></placeholder>[]bbb</paragraph>' );
  1096. } );
  1097. it( 'should fix selection that ends in inline element that is also an object', () => {
  1098. model.schema.extend( 'placeholder', {
  1099. isObject: true
  1100. } );
  1101. setModelData( model, '<paragraph>aaa[<placeholder>]</placeholder>bbb</paragraph>' );
  1102. expect( getModelData( model ) ).to.equal( '<paragraph>aaa[<placeholder></placeholder>]bbb</paragraph>' );
  1103. } );
  1104. it( 'should fix selection that starts in inline element that is also an object', () => {
  1105. model.schema.extend( 'placeholder', {
  1106. isObject: true
  1107. } );
  1108. setModelData( model, '<paragraph>aaa<placeholder>[</placeholder>]bbb</paragraph>' );
  1109. expect( getModelData( model ) ).to.equal( '<paragraph>aaa[<placeholder></placeholder>]bbb</paragraph>' );
  1110. } );
  1111. } );
  1112. describe( 'collapsed selection', () => {
  1113. beforeEach( () => {
  1114. setModelData( model,
  1115. '<paragraph>foo</paragraph>' +
  1116. '<table>' +
  1117. '<tableRow>' +
  1118. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  1119. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  1120. '</tableRow>' +
  1121. '</table>' +
  1122. '<paragraph>bar</paragraph>'
  1123. );
  1124. } );
  1125. it( 'should fix #1 (selection in limit element & before limit element)', () => {
  1126. // <table>[]<tableRow>...
  1127. model.change( writer => {
  1128. writer.setSelection(
  1129. writer.createRange( writer.createPositionAt( modelRoot.getChild( 1 ), 0 ) )
  1130. );
  1131. } );
  1132. expect( getModelData( model ) ).to.equal(
  1133. '<paragraph>foo</paragraph>' +
  1134. '<table>' +
  1135. '<tableRow>' +
  1136. '<tableCell><paragraph>[]aaa</paragraph></tableCell>' +
  1137. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  1138. '</tableRow>' +
  1139. '</table>' +
  1140. '<paragraph>bar</paragraph>'
  1141. );
  1142. } );
  1143. it( 'should fix #2 (selection in limit element & before limit+object element)', () => {
  1144. // <table><tableRow>[]<tableCell>...
  1145. model.change( writer => {
  1146. const row = modelRoot.getChild( 1 ).getChild( 0 );
  1147. writer.setSelection(
  1148. writer.createRange( writer.createPositionAt( row, 0 ) )
  1149. );
  1150. } );
  1151. expect( getModelData( model ) ).to.equal(
  1152. '<paragraph>foo</paragraph>' +
  1153. '<table>' +
  1154. '<tableRow>' +
  1155. '<tableCell><paragraph>[]aaa</paragraph></tableCell>' +
  1156. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  1157. '</tableRow>' +
  1158. '</table>' +
  1159. '<paragraph>bar</paragraph>'
  1160. );
  1161. } );
  1162. it( 'should fix #3 (selection inside object element and before block element)', () => {
  1163. setModelData( model,
  1164. '<paragraph>foo</paragraph>' +
  1165. '<table>' +
  1166. '<tableRow>' +
  1167. '<tableCell>[]<paragraph>aaa</paragraph></tableCell>' +
  1168. '</tableRow>' +
  1169. '</table>' +
  1170. '<paragraph>bar</paragraph>'
  1171. );
  1172. assertEqualMarkup( getModelData( model ),
  1173. '<paragraph>foo</paragraph>' +
  1174. '<table>' +
  1175. '<tableRow>' +
  1176. '<tableCell><paragraph>[]aaa</paragraph></tableCell>' +
  1177. '</tableRow>' +
  1178. '</table>' +
  1179. '<paragraph>bar</paragraph>'
  1180. );
  1181. } );
  1182. it( 'should fix multiple ranges outside block element (but not merge them)', () => {
  1183. setModelData( model,
  1184. '[]<paragraph>foo</paragraph>[]' +
  1185. '<table>' +
  1186. '<tableRow>' +
  1187. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  1188. '</tableRow>' +
  1189. '</table>' +
  1190. '<paragraph>bar</paragraph>'
  1191. );
  1192. assertEqualMarkup( getModelData( model ),
  1193. '<paragraph>[]foo[]</paragraph>' +
  1194. '<table>' +
  1195. '<tableRow>' +
  1196. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  1197. '</tableRow>' +
  1198. '</table>' +
  1199. '<paragraph>bar</paragraph>'
  1200. );
  1201. } );
  1202. } );
  1203. } );
  1204. } );