selection-post-fixer.js 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323
  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. } );
  751. describe( 'non-collapsed selection - image scenarios', () => {
  752. beforeEach( () => {
  753. setModelData( model,
  754. '<paragraph>[]foo</paragraph>' +
  755. '<image>' +
  756. '<caption>xxx</caption>' +
  757. '</image>' +
  758. '<paragraph>bar</paragraph>'
  759. );
  760. } );
  761. it( 'should fix #1 (crossing object and limit boundaries)', () => {
  762. model.change( writer => {
  763. // <paragraph>f[oo</paragraph><image><caption>x]xx</caption>...
  764. writer.setSelection( writer.createRange(
  765. writer.createPositionAt( modelRoot.getChild( 0 ), 1 ),
  766. writer.createPositionAt( modelRoot.getChild( 1 ).getChild( 0 ), 1 )
  767. ) );
  768. } );
  769. expect( getModelData( model ) ).to.equal(
  770. '<paragraph>f[oo</paragraph>' +
  771. '<image>' +
  772. '<caption>xxx</caption>' +
  773. '</image>]' +
  774. '<paragraph>bar</paragraph>'
  775. );
  776. } );
  777. it( 'should fix #2 (crossing object boundary)', () => {
  778. model.change( writer => {
  779. // <paragraph>f[oo</paragraph><image>]<caption>xxx</caption>...
  780. writer.setSelection( writer.createRange(
  781. writer.createPositionAt( modelRoot.getChild( 0 ), 1 ),
  782. writer.createPositionAt( modelRoot.getChild( 1 ), 0 )
  783. ) );
  784. } );
  785. expect( getModelData( model ) ).to.equal(
  786. '<paragraph>f[oo</paragraph>' +
  787. '<image>' +
  788. '<caption>xxx</caption>' +
  789. '</image>]' +
  790. '<paragraph>bar</paragraph>'
  791. );
  792. } );
  793. it( 'should fix #3 (crossing object boundary)', () => {
  794. model.change( writer => {
  795. // <paragraph>f[oo</paragraph><image><caption>xxx</caption>]</image>...
  796. writer.setSelection( writer.createRange(
  797. writer.createPositionAt( modelRoot.getChild( 0 ), 1 ),
  798. writer.createPositionAt( modelRoot.getChild( 1 ), 1 )
  799. ) );
  800. } );
  801. expect( getModelData( model ) ).to.equal(
  802. '<paragraph>f[oo</paragraph>' +
  803. '<image>' +
  804. '<caption>xxx</caption>' +
  805. '</image>]' +
  806. '<paragraph>bar</paragraph>'
  807. );
  808. } );
  809. it( 'should fix #4 (element selection of not an object)', () => {
  810. model.change( writer => {
  811. // <paragraph>foo</paragraph><image>[<caption>xxx</caption>]</image>...
  812. writer.setSelection( writer.createRange(
  813. writer.createPositionAt( modelRoot.getChild( 1 ), 0 ),
  814. writer.createPositionAt( modelRoot.getChild( 1 ), 1 )
  815. ) );
  816. } );
  817. expect( getModelData( model ) ).to.equal(
  818. '<paragraph>foo</paragraph>' +
  819. '[<image>' +
  820. '<caption>xxx</caption>' +
  821. '</image>]' +
  822. '<paragraph>bar</paragraph>'
  823. );
  824. } );
  825. it( 'should not fix #1 (element selection of an object)', () => {
  826. model.change( writer => {
  827. // <paragraph>foo</paragraph>[<image><caption>xxx</caption></image>]...
  828. writer.setSelection( writer.createRange(
  829. writer.createPositionAt( modelRoot, 1 ),
  830. writer.createPositionAt( modelRoot, 2 )
  831. ) );
  832. } );
  833. expect( getModelData( model ) ).to.equal(
  834. '<paragraph>foo</paragraph>' +
  835. '[<image>' +
  836. '<caption>xxx</caption>' +
  837. '</image>]' +
  838. '<paragraph>bar</paragraph>'
  839. );
  840. } );
  841. it( 'should not fix #2 (inside a limit)', () => {
  842. model.change( writer => {
  843. const caption = modelRoot.getChild( 1 ).getChild( 0 );
  844. // <paragraph>foo</paragraph><image><caption>[xxx]</caption></image>...
  845. writer.setSelection( writer.createRange(
  846. writer.createPositionAt( caption, 0 ),
  847. writer.createPositionAt( caption, 3 )
  848. ) );
  849. } );
  850. expect( getModelData( model ) ).to.equal(
  851. '<paragraph>foo</paragraph>' +
  852. '<image>' +
  853. '<caption>[xxx]</caption>' +
  854. '</image>' +
  855. '<paragraph>bar</paragraph>'
  856. );
  857. } );
  858. it( 'should not fix #3 (inside a limit - partial text selection)', () => {
  859. model.change( writer => {
  860. const caption = modelRoot.getChild( 1 ).getChild( 0 );
  861. // <paragraph>foo</paragraph><image><caption>[xx]x</caption></image>...
  862. writer.setSelection( writer.createRange(
  863. writer.createPositionAt( caption, 0 ),
  864. writer.createPositionAt( caption, 2 )
  865. ) );
  866. } );
  867. expect( getModelData( model ) ).to.equal(
  868. '<paragraph>foo</paragraph>' +
  869. '<image>' +
  870. '<caption>[xx]x</caption>' +
  871. '</image>' +
  872. '<paragraph>bar</paragraph>'
  873. );
  874. } );
  875. it( 'should not fix #4 (inside a limit - partial text selection)', () => {
  876. model.change( writer => {
  877. const caption = modelRoot.getChild( 1 ).getChild( 0 );
  878. // <paragraph>foo</paragraph><image><caption>x[xx]</caption></image>...
  879. writer.setSelection( writer.createRange(
  880. writer.createPositionAt( caption, 1 ),
  881. writer.createPositionAt( caption, 3 )
  882. ) );
  883. } );
  884. expect( getModelData( model ) ).to.equal(
  885. '<paragraph>foo</paragraph>' +
  886. '<image>' +
  887. '<caption>x[xx]</caption>' +
  888. '</image>' +
  889. '<paragraph>bar</paragraph>'
  890. );
  891. } );
  892. it( 'should not fix #5 (selection in root on non limit element that doesn\'t allow text)', () => {
  893. setModelData( model,
  894. '[<figure></figure>]'
  895. );
  896. expect( getModelData( model ) ).to.equal(
  897. '[<figure></figure>]'
  898. );
  899. } );
  900. } );
  901. describe( 'non-collapsed selection - other scenarios', () => {
  902. it( 'should fix #1 (element selection of not an object)', () => {
  903. setModelData( model,
  904. '<paragraph>aaa</paragraph>' +
  905. '[<paragraph>bbb</paragraph>]' +
  906. '<paragraph>ccc</paragraph>'
  907. );
  908. expect( getModelData( model ) ).to.equal(
  909. '<paragraph>aaa</paragraph>' +
  910. '<paragraph>[bbb]</paragraph>' +
  911. '<paragraph>ccc</paragraph>'
  912. );
  913. } );
  914. it( 'should fix #2 (elements selection of not an object)', () => {
  915. setModelData( model,
  916. '<paragraph>aaa</paragraph>' +
  917. '[<paragraph>bbb</paragraph>' +
  918. '<paragraph>ccc</paragraph>]'
  919. );
  920. expect( getModelData( model ) ).to.equal(
  921. '<paragraph>aaa</paragraph>' +
  922. '<paragraph>[bbb</paragraph>' +
  923. '<paragraph>ccc]</paragraph>'
  924. );
  925. } );
  926. it( 'should fix #3 (partial selection of not an object)', () => {
  927. setModelData( model,
  928. '<paragraph>aaa</paragraph>' +
  929. '[<paragraph>bbb</paragraph>' +
  930. '<paragraph>ccc]</paragraph>'
  931. );
  932. expect( getModelData( model ) ).to.equal(
  933. '<paragraph>aaa</paragraph>' +
  934. '<paragraph>[bbb</paragraph>' +
  935. '<paragraph>ccc]</paragraph>'
  936. );
  937. } );
  938. it( 'should fix #4 (partial selection of not an object)', () => {
  939. setModelData( model,
  940. '<paragraph>aaa</paragraph>' +
  941. '<paragraph>b[bb</paragraph>]' +
  942. '<paragraph>ccc</paragraph>'
  943. );
  944. expect( getModelData( model ) ).to.equal(
  945. '<paragraph>aaa</paragraph>' +
  946. '<paragraph>b[bb]</paragraph>' +
  947. '<paragraph>ccc</paragraph>'
  948. );
  949. } );
  950. it( 'should fix #5 (partial selection of not an object)', () => {
  951. setModelData( model,
  952. '<paragraph>aaa</paragraph>' +
  953. '[<paragraph>bb]b</paragraph>' +
  954. '<paragraph>ccc</paragraph>'
  955. );
  956. expect( getModelData( model ) ).to.equal(
  957. '<paragraph>aaa</paragraph>' +
  958. '<paragraph>[bb]b</paragraph>' +
  959. '<paragraph>ccc</paragraph>'
  960. );
  961. } );
  962. it( 'should fix #6 (selection must not cross a limit element; starts in a root)', () => {
  963. model.schema.register( 'a', { isLimit: true, allowIn: '$root' } );
  964. model.schema.register( 'b', { isLimit: true, allowIn: 'a' } );
  965. model.schema.register( 'c', { allowIn: 'b' } );
  966. model.schema.extend( '$text', { allowIn: 'c' } );
  967. setModelData( model,
  968. '<a><b><c>[</c></b></a>]'
  969. );
  970. expect( getModelData( model ) ).to.equal( '[<a><b><c></c></b></a>]' );
  971. } );
  972. it( 'should fix #7 (selection must not cross a limit element; ends in a root)', () => {
  973. model.schema.register( 'a', { isLimit: true, allowIn: '$root' } );
  974. model.schema.register( 'b', { isLimit: true, allowIn: 'a' } );
  975. model.schema.register( 'c', { allowIn: 'b' } );
  976. model.schema.extend( '$text', { allowIn: 'c' } );
  977. setModelData( model,
  978. '[<a><b><c>]</c></b></a>'
  979. );
  980. expect( getModelData( model ) ).to.equal( '[<a><b><c></c></b></a>]' );
  981. } );
  982. it( 'should fix #8 (selection must not cross a limit element; starts in a non-limit)', () => {
  983. model.schema.register( 'div', { allowIn: '$root' } );
  984. model.schema.register( 'a', { isLimit: true, allowIn: 'div' } );
  985. model.schema.register( 'b', { isLimit: true, allowIn: 'a' } );
  986. model.schema.register( 'c', { allowIn: 'b' } );
  987. model.schema.extend( '$text', { allowIn: 'c' } );
  988. setModelData( model,
  989. '<div>[<a><b><c>]</c></b></a></div>'
  990. );
  991. expect( getModelData( model ) ).to.equal( '<div>[<a><b><c></c></b></a>]</div>' );
  992. } );
  993. it( 'should fix #9 (selection must not cross a limit element; ends in a non-limit)', () => {
  994. model.schema.register( 'div', { allowIn: '$root' } );
  995. model.schema.register( 'a', { isLimit: true, allowIn: 'div' } );
  996. model.schema.register( 'b', { isLimit: true, allowIn: 'a' } );
  997. model.schema.register( 'c', { allowIn: 'b' } );
  998. model.schema.extend( '$text', { allowIn: 'c' } );
  999. setModelData( model,
  1000. '<div><a><b><c>[</c></b></a>]</div>'
  1001. );
  1002. expect( getModelData( model ) ).to.equal( '<div>[<a><b><c></c></b></a>]</div>' );
  1003. } );
  1004. it( 'should not fix #1 (selection on text node)', () => {
  1005. setModelData( model, '<paragraph>foob[a]r</paragraph>', { lastRangeBackward: true } );
  1006. expect( getModelData( model ) ).to.equal( '<paragraph>foob[a]r</paragraph>' );
  1007. } );
  1008. it( 'should not fix #2 (inline widget selected)', () => {
  1009. setModelData( model,
  1010. '<paragraph>[<inlineWidget></inlineWidget>]</paragraph>'
  1011. );
  1012. expect( getModelData( model ) ).to.equal(
  1013. '<paragraph>[<inlineWidget></inlineWidget>]</paragraph>'
  1014. );
  1015. } );
  1016. it( 'should not fix #3 (text around inline widget)', () => {
  1017. setModelData( model,
  1018. '<paragraph>fo[o<inlineWidget></inlineWidget>b]ar</paragraph>'
  1019. );
  1020. expect( getModelData( model ) ).to.equal(
  1021. '<paragraph>fo[o<inlineWidget></inlineWidget>b]ar</paragraph>'
  1022. );
  1023. } );
  1024. it( 'should not fix #4 (object in object)', () => {
  1025. model.schema.register( 'div', {
  1026. allowIn: [ '$root', 'div' ],
  1027. isObject: true
  1028. } );
  1029. setModelData( model, '<div>[<div></div>]</div>' );
  1030. model.change( writer => {
  1031. const innerDiv = model.document.getRoot().getNodeByPath( [ 0, 0 ] );
  1032. writer.setSelection( writer.createRangeOn( innerDiv ) );
  1033. } );
  1034. expect( getModelData( model ) ).to.equal( '<div>[<div></div>]</div>' );
  1035. } );
  1036. } );
  1037. describe( 'non-collapsed selection - inline widget scenarios', () => {
  1038. beforeEach( () => {
  1039. model.schema.register( 'placeholder', {
  1040. allowWhere: '$text',
  1041. isInline: true
  1042. } );
  1043. } );
  1044. it( 'should fix selection that ends in inline element', () => {
  1045. setModelData( model, '<paragraph>aaa[<placeholder>]</placeholder>bbb</paragraph>' );
  1046. expect( getModelData( model ) ).to.equal( '<paragraph>aaa[]<placeholder></placeholder>bbb</paragraph>' );
  1047. } );
  1048. it( 'should fix selection that starts in inline element', () => {
  1049. setModelData( model, '<paragraph>aaa<placeholder>[</placeholder>]bbb</paragraph>' );
  1050. expect( getModelData( model ) ).to.equal( '<paragraph>aaa<placeholder></placeholder>[]bbb</paragraph>' );
  1051. } );
  1052. it( 'should fix selection that ends in inline element that is also an object', () => {
  1053. model.schema.extend( 'placeholder', {
  1054. isObject: true
  1055. } );
  1056. setModelData( model, '<paragraph>aaa[<placeholder>]</placeholder>bbb</paragraph>' );
  1057. expect( getModelData( model ) ).to.equal( '<paragraph>aaa[<placeholder></placeholder>]bbb</paragraph>' );
  1058. } );
  1059. it( 'should fix selection that starts in inline element that is also an object', () => {
  1060. model.schema.extend( 'placeholder', {
  1061. isObject: true
  1062. } );
  1063. setModelData( model, '<paragraph>aaa<placeholder>[</placeholder>]bbb</paragraph>' );
  1064. expect( getModelData( model ) ).to.equal( '<paragraph>aaa[<placeholder></placeholder>]bbb</paragraph>' );
  1065. } );
  1066. } );
  1067. describe( 'collapsed selection', () => {
  1068. beforeEach( () => {
  1069. setModelData( model,
  1070. '<paragraph>foo</paragraph>' +
  1071. '<table>' +
  1072. '<tableRow>' +
  1073. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  1074. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  1075. '</tableRow>' +
  1076. '</table>' +
  1077. '<paragraph>bar</paragraph>'
  1078. );
  1079. } );
  1080. it( 'should fix #1 (selection in limit element & before limit element)', () => {
  1081. // <table>[]<tableRow>...
  1082. model.change( writer => {
  1083. writer.setSelection(
  1084. writer.createRange( writer.createPositionAt( modelRoot.getChild( 1 ), 0 ) )
  1085. );
  1086. } );
  1087. expect( getModelData( model ) ).to.equal(
  1088. '<paragraph>foo</paragraph>' +
  1089. '<table>' +
  1090. '<tableRow>' +
  1091. '[<tableCell><paragraph>aaa</paragraph></tableCell>]' +
  1092. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  1093. '</tableRow>' +
  1094. '</table>' +
  1095. '<paragraph>bar</paragraph>'
  1096. );
  1097. } );
  1098. it( 'should fix #2 (selection in limit element & before limit+object element)', () => {
  1099. // <table><tableRow>[]<tableCell>...
  1100. model.change( writer => {
  1101. const row = modelRoot.getChild( 1 ).getChild( 0 );
  1102. writer.setSelection(
  1103. writer.createRange( writer.createPositionAt( row, 0 ) )
  1104. );
  1105. } );
  1106. expect( getModelData( model ) ).to.equal(
  1107. '<paragraph>foo</paragraph>' +
  1108. '<table>' +
  1109. '<tableRow>' +
  1110. '[<tableCell><paragraph>aaa</paragraph></tableCell>]' +
  1111. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  1112. '</tableRow>' +
  1113. '</table>' +
  1114. '<paragraph>bar</paragraph>'
  1115. );
  1116. } );
  1117. it( 'should fix #3 (selection inside object element and before block element)', () => {
  1118. setModelData( model,
  1119. '<paragraph>foo</paragraph>' +
  1120. '<table>' +
  1121. '<tableRow>' +
  1122. '<tableCell>[]<paragraph>aaa</paragraph></tableCell>' +
  1123. '</tableRow>' +
  1124. '</table>' +
  1125. '<paragraph>bar</paragraph>'
  1126. );
  1127. assertEqualMarkup( getModelData( model ),
  1128. '<paragraph>foo</paragraph>' +
  1129. '<table>' +
  1130. '<tableRow>' +
  1131. '<tableCell><paragraph>[]aaa</paragraph></tableCell>' +
  1132. '</tableRow>' +
  1133. '</table>' +
  1134. '<paragraph>bar</paragraph>'
  1135. );
  1136. } );
  1137. it( 'should fix multiple ranges outside block element (but not merge them)', () => {
  1138. setModelData( model,
  1139. '[]<paragraph>foo</paragraph>[]' +
  1140. '<table>' +
  1141. '<tableRow>' +
  1142. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  1143. '</tableRow>' +
  1144. '</table>' +
  1145. '<paragraph>bar</paragraph>'
  1146. );
  1147. assertEqualMarkup( getModelData( model ),
  1148. '<paragraph>[]foo[]</paragraph>' +
  1149. '<table>' +
  1150. '<tableRow>' +
  1151. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  1152. '</tableRow>' +
  1153. '</table>' +
  1154. '<paragraph>bar</paragraph>'
  1155. );
  1156. } );
  1157. } );
  1158. } );
  1159. } );