selection-post-fixer.js 42 KB

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