selection-post-fixer.js 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137
  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. expect( getModelData( model ) ).to.equal(
  571. '<table>' +
  572. '<tableRow><tableCell><paragraph>[aaa]</paragraph></tableCell></tableRow>' +
  573. '</table>'
  574. );
  575. } );
  576. } );
  577. describe( 'non-collapsed selection - image scenarios', () => {
  578. beforeEach( () => {
  579. setModelData( model,
  580. '<paragraph>[]foo</paragraph>' +
  581. '<image>' +
  582. '<caption>xxx</caption>' +
  583. '</image>' +
  584. '<paragraph>bar</paragraph>'
  585. );
  586. } );
  587. it( 'should fix #1 (crossing object and limit boundaries)', () => {
  588. model.change( writer => {
  589. // <paragraph>f[oo</paragraph><image><caption>x]xx</caption>...
  590. writer.setSelection( writer.createRange(
  591. writer.createPositionAt( modelRoot.getChild( 0 ), 1 ),
  592. writer.createPositionAt( modelRoot.getChild( 1 ).getChild( 0 ), 1 )
  593. ) );
  594. } );
  595. expect( getModelData( model ) ).to.equal(
  596. '<paragraph>f[oo</paragraph>' +
  597. '<image>' +
  598. '<caption>xxx</caption>' +
  599. '</image>]' +
  600. '<paragraph>bar</paragraph>'
  601. );
  602. } );
  603. it( 'should fix #2 (crossing object boundary)', () => {
  604. model.change( writer => {
  605. // <paragraph>f[oo</paragraph><image>]<caption>xxx</caption>...
  606. writer.setSelection( writer.createRange(
  607. writer.createPositionAt( modelRoot.getChild( 0 ), 1 ),
  608. writer.createPositionAt( modelRoot.getChild( 1 ), 0 )
  609. ) );
  610. } );
  611. expect( getModelData( model ) ).to.equal(
  612. '<paragraph>f[oo</paragraph>' +
  613. '<image>' +
  614. '<caption>xxx</caption>' +
  615. '</image>]' +
  616. '<paragraph>bar</paragraph>'
  617. );
  618. } );
  619. it( 'should fix #3 (crossing object boundary)', () => {
  620. model.change( writer => {
  621. // <paragraph>f[oo</paragraph><image><caption>xxx</caption>]</image>...
  622. writer.setSelection( writer.createRange(
  623. writer.createPositionAt( modelRoot.getChild( 0 ), 1 ),
  624. writer.createPositionAt( modelRoot.getChild( 1 ), 1 )
  625. ) );
  626. } );
  627. expect( getModelData( model ) ).to.equal(
  628. '<paragraph>f[oo</paragraph>' +
  629. '<image>' +
  630. '<caption>xxx</caption>' +
  631. '</image>]' +
  632. '<paragraph>bar</paragraph>'
  633. );
  634. } );
  635. it( 'should fix #4 (element selection of not an object)', () => {
  636. model.change( writer => {
  637. // <paragraph>foo</paragraph><image>[<caption>xxx</caption>]</image>...
  638. writer.setSelection( writer.createRange(
  639. writer.createPositionAt( modelRoot.getChild( 1 ), 0 ),
  640. writer.createPositionAt( modelRoot.getChild( 1 ), 1 )
  641. ) );
  642. } );
  643. expect( getModelData( model ) ).to.equal(
  644. '<paragraph>foo</paragraph>' +
  645. '[<image>' +
  646. '<caption>xxx</caption>' +
  647. '</image>]' +
  648. '<paragraph>bar</paragraph>'
  649. );
  650. } );
  651. it( 'should not fix #1 (element selection of an object)', () => {
  652. model.change( writer => {
  653. // <paragraph>foo</paragraph>[<image><caption>xxx</caption></image>]...
  654. writer.setSelection( writer.createRange(
  655. writer.createPositionAt( modelRoot, 1 ),
  656. writer.createPositionAt( modelRoot, 2 )
  657. ) );
  658. } );
  659. expect( getModelData( model ) ).to.equal(
  660. '<paragraph>foo</paragraph>' +
  661. '[<image>' +
  662. '<caption>xxx</caption>' +
  663. '</image>]' +
  664. '<paragraph>bar</paragraph>'
  665. );
  666. } );
  667. it( 'should not fix #2 (inside a limit)', () => {
  668. model.change( writer => {
  669. const caption = modelRoot.getChild( 1 ).getChild( 0 );
  670. // <paragraph>foo</paragraph><image><caption>[xxx]</caption></image>...
  671. writer.setSelection( writer.createRange(
  672. writer.createPositionAt( caption, 0 ),
  673. writer.createPositionAt( caption, 3 )
  674. ) );
  675. } );
  676. expect( getModelData( model ) ).to.equal(
  677. '<paragraph>foo</paragraph>' +
  678. '<image>' +
  679. '<caption>[xxx]</caption>' +
  680. '</image>' +
  681. '<paragraph>bar</paragraph>'
  682. );
  683. } );
  684. it( 'should not fix #3 (inside a limit - partial text selection)', () => {
  685. model.change( writer => {
  686. const caption = modelRoot.getChild( 1 ).getChild( 0 );
  687. // <paragraph>foo</paragraph><image><caption>[xx]x</caption></image>...
  688. writer.setSelection( writer.createRange(
  689. writer.createPositionAt( caption, 0 ),
  690. writer.createPositionAt( caption, 2 )
  691. ) );
  692. } );
  693. expect( getModelData( model ) ).to.equal(
  694. '<paragraph>foo</paragraph>' +
  695. '<image>' +
  696. '<caption>[xx]x</caption>' +
  697. '</image>' +
  698. '<paragraph>bar</paragraph>'
  699. );
  700. } );
  701. it( 'should not fix #4 (inside a limit - partial text selection)', () => {
  702. model.change( writer => {
  703. const caption = modelRoot.getChild( 1 ).getChild( 0 );
  704. // <paragraph>foo</paragraph><image><caption>x[xx]</caption></image>...
  705. writer.setSelection( writer.createRange(
  706. writer.createPositionAt( caption, 1 ),
  707. writer.createPositionAt( caption, 3 )
  708. ) );
  709. } );
  710. expect( getModelData( model ) ).to.equal(
  711. '<paragraph>foo</paragraph>' +
  712. '<image>' +
  713. '<caption>x[xx]</caption>' +
  714. '</image>' +
  715. '<paragraph>bar</paragraph>'
  716. );
  717. } );
  718. it( 'should not fix #5 (selection in root on non limit element that doesn\'t allow text)', () => {
  719. setModelData( model,
  720. '[<figure></figure>]'
  721. );
  722. expect( getModelData( model ) ).to.equal(
  723. '[<figure></figure>]'
  724. );
  725. } );
  726. } );
  727. describe( 'non-collapsed selection - other scenarios', () => {
  728. it( 'should fix #1 (element selection of not an object)', () => {
  729. setModelData( model,
  730. '<paragraph>aaa</paragraph>' +
  731. '[<paragraph>bbb</paragraph>]' +
  732. '<paragraph>ccc</paragraph>'
  733. );
  734. expect( getModelData( model ) ).to.equal(
  735. '<paragraph>aaa</paragraph>' +
  736. '<paragraph>[bbb]</paragraph>' +
  737. '<paragraph>ccc</paragraph>'
  738. );
  739. } );
  740. it( 'should fix #2 (elements selection of not an object)', () => {
  741. setModelData( model,
  742. '<paragraph>aaa</paragraph>' +
  743. '[<paragraph>bbb</paragraph>' +
  744. '<paragraph>ccc</paragraph>]'
  745. );
  746. expect( getModelData( model ) ).to.equal(
  747. '<paragraph>aaa</paragraph>' +
  748. '<paragraph>[bbb</paragraph>' +
  749. '<paragraph>ccc]</paragraph>'
  750. );
  751. } );
  752. it( 'should fix #3 (partial selection of not an object)', () => {
  753. setModelData( model,
  754. '<paragraph>aaa</paragraph>' +
  755. '[<paragraph>bbb</paragraph>' +
  756. '<paragraph>ccc]</paragraph>'
  757. );
  758. expect( getModelData( model ) ).to.equal(
  759. '<paragraph>aaa</paragraph>' +
  760. '<paragraph>[bbb</paragraph>' +
  761. '<paragraph>ccc]</paragraph>'
  762. );
  763. } );
  764. it( 'should fix #4 (partial selection of not an object)', () => {
  765. setModelData( model,
  766. '<paragraph>aaa</paragraph>' +
  767. '<paragraph>b[bb</paragraph>]' +
  768. '<paragraph>ccc</paragraph>'
  769. );
  770. expect( getModelData( model ) ).to.equal(
  771. '<paragraph>aaa</paragraph>' +
  772. '<paragraph>b[bb]</paragraph>' +
  773. '<paragraph>ccc</paragraph>'
  774. );
  775. } );
  776. it( 'should fix #5 (partial selection of not an object)', () => {
  777. setModelData( model,
  778. '<paragraph>aaa</paragraph>' +
  779. '[<paragraph>bb]b</paragraph>' +
  780. '<paragraph>ccc</paragraph>'
  781. );
  782. expect( getModelData( model ) ).to.equal(
  783. '<paragraph>aaa</paragraph>' +
  784. '<paragraph>[bb]b</paragraph>' +
  785. '<paragraph>ccc</paragraph>'
  786. );
  787. } );
  788. it( 'should fix #6 (selection must not cross a limit element; starts in a root)', () => {
  789. model.schema.register( 'a', { isLimit: true, allowIn: '$root' } );
  790. model.schema.register( 'b', { isLimit: true, allowIn: 'a' } );
  791. model.schema.register( 'c', { allowIn: 'b' } );
  792. model.schema.extend( '$text', { allowIn: 'c' } );
  793. setModelData( model,
  794. '<a><b><c>[</c></b></a>]'
  795. );
  796. expect( getModelData( model ) ).to.equal( '[<a><b><c></c></b></a>]' );
  797. } );
  798. it( 'should fix #7 (selection must not cross a limit element; ends in a root)', () => {
  799. model.schema.register( 'a', { isLimit: true, allowIn: '$root' } );
  800. model.schema.register( 'b', { isLimit: true, allowIn: 'a' } );
  801. model.schema.register( 'c', { allowIn: 'b' } );
  802. model.schema.extend( '$text', { allowIn: 'c' } );
  803. setModelData( model,
  804. '[<a><b><c>]</c></b></a>'
  805. );
  806. expect( getModelData( model ) ).to.equal( '[<a><b><c></c></b></a>]' );
  807. } );
  808. it( 'should fix #8 (selection must not cross a limit element; starts in a non-limit)', () => {
  809. model.schema.register( 'div', { allowIn: '$root' } );
  810. model.schema.register( 'a', { isLimit: true, allowIn: 'div' } );
  811. model.schema.register( 'b', { isLimit: true, allowIn: 'a' } );
  812. model.schema.register( 'c', { allowIn: 'b' } );
  813. model.schema.extend( '$text', { allowIn: 'c' } );
  814. setModelData( model,
  815. '<div>[<a><b><c>]</c></b></a></div>'
  816. );
  817. expect( getModelData( model ) ).to.equal( '<div>[<a><b><c></c></b></a>]</div>' );
  818. } );
  819. it( 'should fix #9 (selection must not cross a limit element; ends in a non-limit)', () => {
  820. model.schema.register( 'div', { allowIn: '$root' } );
  821. model.schema.register( 'a', { isLimit: true, allowIn: 'div' } );
  822. model.schema.register( 'b', { isLimit: true, allowIn: 'a' } );
  823. model.schema.register( 'c', { allowIn: 'b' } );
  824. model.schema.extend( '$text', { allowIn: 'c' } );
  825. setModelData( model,
  826. '<div><a><b><c>[</c></b></a>]</div>'
  827. );
  828. expect( getModelData( model ) ).to.equal( '<div>[<a><b><c></c></b></a>]</div>' );
  829. } );
  830. it( 'should not fix #1 (selection on text node)', () => {
  831. setModelData( model, '<paragraph>foob[a]r</paragraph>', { lastRangeBackward: true } );
  832. expect( getModelData( model ) ).to.equal( '<paragraph>foob[a]r</paragraph>' );
  833. } );
  834. it( 'should not fix #2 (inline widget selected)', () => {
  835. setModelData( model,
  836. '<paragraph>[<inlineWidget></inlineWidget>]</paragraph>'
  837. );
  838. expect( getModelData( model ) ).to.equal(
  839. '<paragraph>[<inlineWidget></inlineWidget>]</paragraph>'
  840. );
  841. } );
  842. it( 'should not fix #3 (text around inline widget)', () => {
  843. setModelData( model,
  844. '<paragraph>fo[o<inlineWidget></inlineWidget>b]ar</paragraph>'
  845. );
  846. expect( getModelData( model ) ).to.equal(
  847. '<paragraph>fo[o<inlineWidget></inlineWidget>b]ar</paragraph>'
  848. );
  849. } );
  850. it( 'should not fix #4 (object in object)', () => {
  851. model.schema.register( 'div', {
  852. allowIn: [ '$root', 'div' ],
  853. isObject: true
  854. } );
  855. setModelData( model, '<div>[<div></div>]</div>' );
  856. model.change( writer => {
  857. const innerDiv = model.document.getRoot().getNodeByPath( [ 0, 0 ] );
  858. writer.setSelection( writer.createRangeOn( innerDiv ) );
  859. } );
  860. expect( getModelData( model ) ).to.equal( '<div>[<div></div>]</div>' );
  861. } );
  862. } );
  863. describe( 'non-collapsed selection - inline widget scenarios', () => {
  864. beforeEach( () => {
  865. model.schema.register( 'placeholder', {
  866. allowWhere: '$text',
  867. isInline: true
  868. } );
  869. } );
  870. it( 'should fix selection that ends in inline element', () => {
  871. setModelData( model, '<paragraph>aaa[<placeholder>]</placeholder>bbb</paragraph>' );
  872. expect( getModelData( model ) ).to.equal( '<paragraph>aaa[]<placeholder></placeholder>bbb</paragraph>' );
  873. } );
  874. it( 'should fix selection that starts in inline element', () => {
  875. setModelData( model, '<paragraph>aaa<placeholder>[</placeholder>]bbb</paragraph>' );
  876. expect( getModelData( model ) ).to.equal( '<paragraph>aaa<placeholder></placeholder>[]bbb</paragraph>' );
  877. } );
  878. it( 'should fix selection that ends in inline element that is also an object', () => {
  879. model.schema.extend( 'placeholder', {
  880. isObject: true
  881. } );
  882. setModelData( model, '<paragraph>aaa[<placeholder>]</placeholder>bbb</paragraph>' );
  883. expect( getModelData( model ) ).to.equal( '<paragraph>aaa[<placeholder></placeholder>]bbb</paragraph>' );
  884. } );
  885. it( 'should fix selection that starts in inline element that is also an object', () => {
  886. model.schema.extend( 'placeholder', {
  887. isObject: true
  888. } );
  889. setModelData( model, '<paragraph>aaa<placeholder>[</placeholder>]bbb</paragraph>' );
  890. expect( getModelData( model ) ).to.equal( '<paragraph>aaa[<placeholder></placeholder>]bbb</paragraph>' );
  891. } );
  892. } );
  893. describe( 'collapsed selection', () => {
  894. beforeEach( () => {
  895. setModelData( model,
  896. '<paragraph>foo</paragraph>' +
  897. '<table>' +
  898. '<tableRow>' +
  899. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  900. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  901. '</tableRow>' +
  902. '</table>' +
  903. '<paragraph>bar</paragraph>'
  904. );
  905. } );
  906. it( 'should fix #1 (selection in limit element & before limit element)', () => {
  907. // <table>[]<tableRow>...
  908. model.change( writer => {
  909. writer.setSelection(
  910. writer.createRange( writer.createPositionAt( modelRoot.getChild( 1 ), 0 ) )
  911. );
  912. } );
  913. expect( getModelData( model ) ).to.equal(
  914. '<paragraph>foo[]</paragraph>' +
  915. '<table>' +
  916. '<tableRow>' +
  917. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  918. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  919. '</tableRow>' +
  920. '</table>' +
  921. '<paragraph>bar</paragraph>'
  922. );
  923. } );
  924. it( 'should fix #2 (selection in limit element & before limit+object element)', () => {
  925. // <table><tableRow>[]<tableCell>...
  926. model.change( writer => {
  927. const row = modelRoot.getChild( 1 ).getChild( 0 );
  928. writer.setSelection(
  929. writer.createRange( writer.createPositionAt( row, 0 ) )
  930. );
  931. } );
  932. expect( getModelData( model ) ).to.equal(
  933. '<paragraph>foo</paragraph>' +
  934. '<table>' +
  935. '<tableRow>' +
  936. '[<tableCell><paragraph>aaa</paragraph></tableCell>]' +
  937. '<tableCell><paragraph>bbb</paragraph></tableCell>' +
  938. '</tableRow>' +
  939. '</table>' +
  940. '<paragraph>bar</paragraph>'
  941. );
  942. } );
  943. it( 'should fix #3 (selection inside object element and before block element)', () => {
  944. setModelData( model,
  945. '<paragraph>foo</paragraph>' +
  946. '<table>' +
  947. '<tableRow>' +
  948. '<tableCell>[]<paragraph>aaa</paragraph></tableCell>' +
  949. '</tableRow>' +
  950. '</table>' +
  951. '<paragraph>bar</paragraph>'
  952. );
  953. assertEqualMarkup( getModelData( model ),
  954. '<paragraph>foo</paragraph>' +
  955. '<table>' +
  956. '<tableRow>' +
  957. '<tableCell><paragraph>[]aaa</paragraph></tableCell>' +
  958. '</tableRow>' +
  959. '</table>' +
  960. '<paragraph>bar</paragraph>'
  961. );
  962. } );
  963. it( 'should fix multiple ranges outside block element (but not merge them)', () => {
  964. setModelData( model,
  965. '[]<paragraph>foo</paragraph>[]' +
  966. '<table>' +
  967. '<tableRow>' +
  968. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  969. '</tableRow>' +
  970. '</table>' +
  971. '<paragraph>bar</paragraph>'
  972. );
  973. assertEqualMarkup( getModelData( model ),
  974. '<paragraph>[]foo[]</paragraph>' +
  975. '<table>' +
  976. '<tableRow>' +
  977. '<tableCell><paragraph>aaa</paragraph></tableCell>' +
  978. '</tableRow>' +
  979. '</table>' +
  980. '<paragraph>bar</paragraph>'
  981. );
  982. } );
  983. } );
  984. } );
  985. } );