modifyselection.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Document from '../../src/model/document';
  6. import DataController from '../../src/controller/datacontroller';
  7. import Selection from '../../src/model/selection';
  8. import modifySelection from '../../src/controller/modifyselection';
  9. import { setData, stringify } from '../../src/dev-utils/model';
  10. describe( 'DataController', () => {
  11. let document, dataController;
  12. beforeEach( () => {
  13. document = new Document();
  14. dataController = new DataController( document );
  15. document.schema.registerItem( 'p', '$block' );
  16. document.schema.registerItem( 'x', '$block' );
  17. document.schema.allow( { name: 'x', inside: 'p' } );
  18. document.createRoot();
  19. } );
  20. describe( 'modifySelection', () => {
  21. describe( 'unit=character', () => {
  22. describe( 'within element', () => {
  23. test(
  24. 'does nothing on empty content',
  25. '[]',
  26. '[]'
  27. );
  28. test(
  29. 'does nothing on empty content (with empty element)',
  30. '<p>[]</p>',
  31. '<p>[]</p>'
  32. );
  33. test(
  34. 'does nothing on empty content (backward)',
  35. '[]',
  36. '[]',
  37. { direction: 'backward' }
  38. );
  39. test(
  40. 'does nothing on root boundary',
  41. '<p>foo[]</p>',
  42. '<p>foo[]</p>'
  43. );
  44. test(
  45. 'does nothing on root boundary (backward)',
  46. '<p>[]foo</p>',
  47. '<p>[]foo</p>',
  48. { direction: 'backward' }
  49. );
  50. test(
  51. 'extends one character forward',
  52. '<p>f[]oo</p>',
  53. '<p>f[o]o</p>'
  54. );
  55. it( 'extends one character backward', () => {
  56. setData( document, '<p>fo[]o</p>', { lastRangeBackward: true } );
  57. modifySelection( dataController, document.selection, { direction: 'backward' } );
  58. expect( stringify( document.getRoot(), document.selection ) ).to.equal( '<p>f[o]o</p>' );
  59. expect( document.selection.isBackward ).to.true;
  60. } );
  61. test(
  62. 'extends one character forward (non-collapsed)',
  63. '<p>f[o]obar</p>',
  64. '<p>f[oo]bar</p>'
  65. );
  66. it( 'extends one character backward (non-collapsed)', () => {
  67. setData( document, '<p>foob[a]r</p>', { lastRangeBackward: true } );
  68. modifySelection( dataController, document.selection, { direction: 'backward' } );
  69. expect( stringify( document.getRoot(), document.selection ) ).to.equal( '<p>foo[ba]r</p>' );
  70. expect( document.selection.isBackward ).to.true;
  71. } );
  72. test(
  73. 'extends to element boundary',
  74. '<p>fo[]o</p>',
  75. '<p>fo[o]</p>'
  76. );
  77. it( 'extends to element boundary (backward)', () => {
  78. setData( document, '<p>f[]oo</p>' );
  79. modifySelection( dataController, document.selection, { direction: 'backward' } );
  80. expect( stringify( document.getRoot(), document.selection ) ).to.equal( '<p>[f]oo</p>' );
  81. expect( document.selection.isBackward ).to.true;
  82. } );
  83. test(
  84. 'shrinks forward selection (to collapsed)',
  85. '<p>foo[b]ar</p>',
  86. '<p>foo[]bar</p>',
  87. { direction: 'backward' }
  88. );
  89. it( 'shrinks backward selection (to collapsed)', () => {
  90. setData( document, '<p>foo[b]ar</p>', { lastRangeBackward: true } );
  91. modifySelection( dataController, document.selection );
  92. expect( stringify( document.getRoot(), document.selection ) ).to.equal( '<p>foob[]ar</p>' );
  93. expect( document.selection.isBackward ).to.false;
  94. } );
  95. test(
  96. 'unicode support - combining mark forward',
  97. '<p>foo[]b̂ar</p>',
  98. '<p>foo[b̂]ar</p>'
  99. );
  100. it( 'unicode support - combining mark backward', () => {
  101. setData( document, '<p>foob̂[]ar</p>' );
  102. modifySelection( dataController, document.selection, { direction: 'backward' } );
  103. expect( stringify( document.getRoot(), document.selection ) ).to.equal( '<p>foo[b̂]ar</p>' );
  104. expect( document.selection.isBackward ).to.true;
  105. } );
  106. test(
  107. 'unicode support - combining mark multiple',
  108. '<p>fo[]o̻̐ͩbar</p>',
  109. '<p>fo[o̻̐ͩ]bar</p>'
  110. );
  111. it( 'unicode support - combining mark multiple backward', () => {
  112. setData( document, '<p>foo̻̐ͩ[]bar</p>' );
  113. modifySelection( dataController, document.selection, { direction: 'backward' } );
  114. expect( stringify( document.getRoot(), document.selection ) ).to.equal( '<p>fo[o̻̐ͩ]bar</p>' );
  115. expect( document.selection.isBackward ).to.true;
  116. } );
  117. test(
  118. 'unicode support - combining mark to the end',
  119. '<p>fo[]o̻̐ͩ</p>',
  120. '<p>fo[o̻̐ͩ]</p>'
  121. );
  122. test(
  123. 'unicode support - surrogate pairs forward',
  124. '<p>[]\uD83D\uDCA9</p>',
  125. '<p>[\uD83D\uDCA9]</p>'
  126. );
  127. it( 'unicode support - surrogate pairs backward', () => {
  128. setData( document, '<p>\uD83D\uDCA9[]</p>' );
  129. modifySelection( dataController, document.selection, { direction: 'backward' } );
  130. expect( stringify( document.getRoot(), document.selection ) ).to.equal( '<p>[\uD83D\uDCA9]</p>' );
  131. expect( document.selection.isBackward ).to.true;
  132. } );
  133. } );
  134. describe( 'beyond element', () => {
  135. test(
  136. 'extends over boundary of empty elements',
  137. '<p>[]</p><p></p><p></p>',
  138. '<p>[</p><p>]</p><p></p>'
  139. );
  140. it( 'extends over boundary of empty elements (backward)', () => {
  141. setData( document, '<p></p><p></p><p>[]</p>' );
  142. modifySelection( dataController, document.selection, { direction: 'backward' } );
  143. expect( stringify( document.getRoot(), document.selection ) ).to.equal( '<p></p><p>[</p><p>]</p>' );
  144. expect( document.selection.isBackward ).to.true;
  145. } );
  146. test(
  147. 'extends over boundary of non-empty elements',
  148. '<p>a[]</p><p>bcd</p>',
  149. '<p>a[</p><p>]bcd</p>'
  150. );
  151. it( 'extends over boundary of non-empty elements (backward)', () => {
  152. setData( document, '<p>a</p><p>[]bcd</p>' );
  153. modifySelection( dataController, document.selection, { direction: 'backward' } );
  154. expect( stringify( document.getRoot(), document.selection ) ).to.equal( '<p>a[</p><p>]bcd</p>' );
  155. expect( document.selection.isBackward ).to.true;
  156. } );
  157. test(
  158. 'extends over character after boundary',
  159. '<p>a[</p><p>]bcd</p>',
  160. '<p>a[</p><p>b]cd</p>'
  161. );
  162. it( 'extends over character after boundary (backward)', () => {
  163. setData( document, '<p>abc[</p><p>]d</p>', { lastRangeBackward: true } );
  164. modifySelection( dataController, document.selection, { direction: 'backward' } );
  165. expect( stringify( document.getRoot(), document.selection ) ).to.equal( '<p>ab[c</p><p>]d</p>' );
  166. expect( document.selection.isBackward ).to.true;
  167. } );
  168. test(
  169. 'stops on the first position where text is allowed - inside block',
  170. '<p>a[]</p><p><x>bcd</x></p>',
  171. '<p>a[</p><p>]<x>bcd</x></p>'
  172. );
  173. test(
  174. 'stops on the first position where text is allowed - inside inline element',
  175. '<p>a[</p><p>]<x>bcd</x>ef</p>',
  176. '<p>a[</p><p><x>]bcd</x>ef</p>'
  177. );
  178. test(
  179. 'extends over element when next node is a text',
  180. '<p><x>a[]</x>bc</p>',
  181. '<p><x>a[</x>]bc</p>'
  182. );
  183. test(
  184. 'extends over element when next node is a text - backward',
  185. '<p>ab<x>[]c</x></p>',
  186. '<p>ab[<x>]c</x></p>',
  187. { direction: 'backward' }
  188. );
  189. it( 'shrinks over boundary of empty elements', () => {
  190. setData( document, '<p>[</p><p>]</p>', { lastRangeBackward: true } );
  191. modifySelection( dataController, document.selection );
  192. expect( stringify( document.getRoot(), document.selection ) ).to.equal( '<p></p><p>[]</p>' );
  193. expect( document.selection.isBackward ).to.false;
  194. } );
  195. it( 'shrinks over boundary of empty elements (backward)', () => {
  196. setData( document, '<p>[</p><p>]</p>' );
  197. modifySelection( dataController, document.selection, { direction: 'backward' } );
  198. expect( stringify( document.getRoot(), document.selection ) ).to.equal( '<p>[]</p><p></p>' );
  199. expect( document.selection.isBackward ).to.false;
  200. } );
  201. it( 'shrinks over boundary of non-empty elements', () => {
  202. setData( document, '<p>a[</p><p>]b</p>', { lastRangeBackward: true } );
  203. modifySelection( dataController, document.selection );
  204. expect( stringify( document.getRoot(), document.selection ) ).to.equal( '<p>a</p><p>[]b</p>' );
  205. expect( document.selection.isBackward ).to.false;
  206. } );
  207. test(
  208. 'shrinks over boundary of non-empty elements (backward)',
  209. '<p>a[</p><p>]b</p>',
  210. '<p>a[]</p><p>b</p>',
  211. { direction: 'backward' }
  212. );
  213. it( 'updates selection attributes', () => {
  214. setData( document, '<p><$text bold="true">foo</$text>[b]</p>' );
  215. modifySelection( dataController, document.selection, { direction: 'backward' } );
  216. expect( stringify( document.getRoot(), document.selection ) ).to.equal( '<p><$text bold="true">foo[]</$text>b</p>' );
  217. expect( document.selection.getAttribute( 'bold' ) ).to.equal( true );
  218. } );
  219. } );
  220. describe( 'beyond element – skipping incorrect positions', () => {
  221. beforeEach( () => {
  222. document.schema.registerItem( 'quote' );
  223. document.schema.allow( { name: 'quote', inside: '$root' } );
  224. document.schema.allow( { name: '$block', inside: 'quote' } );
  225. } );
  226. test(
  227. 'skips position at the beginning of an element which does not allow text',
  228. '<p>x[]</p><quote><p>y</p></quote><p>z</p>',
  229. '<p>x[</p><quote><p>]y</p></quote><p>z</p>'
  230. );
  231. test(
  232. 'skips position at the end of an element which does not allow text - backward',
  233. '<p>x</p><quote><p>y</p></quote><p>[]z</p>',
  234. '<p>x</p><quote><p>y[</p></quote><p>]z</p>',
  235. { direction: 'backward' }
  236. );
  237. test(
  238. 'skips position at the end of an element which does not allow text',
  239. '<p>x[</p><quote><p>y]</p></quote><p>z</p>',
  240. '<p>x[</p><quote><p>y</p></quote><p>]z</p>'
  241. );
  242. test(
  243. 'skips position at the beginning of an element which does not allow text - backward',
  244. '<p>x</p><quote><p>[]y</p></quote><p>z</p>',
  245. '<p>x[</p><quote><p>]y</p></quote><p>z</p>',
  246. { direction: 'backward' }
  247. );
  248. test(
  249. 'extends to an empty block after skipping incorrect position',
  250. '<p>x[]</p><quote><p></p></quote><p>z</p>',
  251. '<p>x[</p><quote><p>]</p></quote><p>z</p>'
  252. );
  253. test(
  254. 'extends to an empty block after skipping incorrect position - backward',
  255. '<p>x</p><quote><p></p></quote><p>[]z</p>',
  256. '<p>x</p><quote><p>[</p></quote><p>]z</p>',
  257. { direction: 'backward' }
  258. );
  259. } );
  260. } );
  261. describe( 'unit=codePoint', () => {
  262. it( 'does nothing on empty content', () => {
  263. document.schema.allow( { name: '$text', inside: '$root' } );
  264. setData( document, '' );
  265. modifySelection( dataController, document.selection, { unit: 'codePoint' } );
  266. expect( stringify( document.getRoot(), document.selection ) ).to.equal( '[]' );
  267. } );
  268. test(
  269. 'does nothing on empty content (with empty element)',
  270. '<p>[]</p>',
  271. '<p>[]</p>',
  272. { unit: 'codePoint' }
  273. );
  274. test(
  275. 'extends one user-perceived character forward - latin letters',
  276. '<p>f[]oo</p>',
  277. '<p>f[o]o</p>',
  278. { unit: 'codePoint' }
  279. );
  280. it( 'extends one user-perceived character backward - latin letters', () => {
  281. setData( document, '<p>fo[]o</p>' );
  282. modifySelection( dataController, document.selection, { unit: 'codePoint', direction: 'backward' } );
  283. expect( stringify( document.getRoot(), document.selection ) ).to.equal( '<p>f[o]o</p>' );
  284. expect( document.selection.isBackward ).to.true;
  285. } );
  286. test(
  287. 'unicode support - combining mark forward',
  288. '<p>foo[]b̂ar</p>',
  289. '<p>foo[b]̂ar</p>',
  290. { unit: 'codePoint' }
  291. );
  292. it( 'unicode support - combining mark backward', () => {
  293. setData( document, '<p>foob̂[]ar</p>' );
  294. // Creating new instance of selection instead of operation on module:engine/model/document~Document#selection.
  295. // Document's selection will throw errors in some test cases (which are correct cases, but only for
  296. // non-document selections).
  297. const testSelection = Selection.createFromSelection( document.selection );
  298. modifySelection( dataController, testSelection, { unit: 'codePoint', direction: 'backward' } );
  299. expect( stringify( document.getRoot(), testSelection ) ).to.equal( '<p>foob[̂]ar</p>' );
  300. expect( testSelection.isBackward ).to.true;
  301. } );
  302. test(
  303. 'unicode support - combining mark multiple',
  304. '<p>fo[]o̻̐ͩbar</p>',
  305. '<p>fo[o]̻̐ͩbar</p>',
  306. { unit: 'codePoint' }
  307. );
  308. test(
  309. 'unicode support - surrogate pairs forward',
  310. '<p>[]\uD83D\uDCA9</p>',
  311. '<p>[\uD83D\uDCA9]</p>',
  312. { unit: 'codePoint' }
  313. );
  314. it( 'unicode support surrogate pairs backward', () => {
  315. setData( document, '<p>\uD83D\uDCA9[]</p>' );
  316. modifySelection( dataController, document.selection, { unit: 'codePoint', direction: 'backward' } );
  317. expect( stringify( document.getRoot(), document.selection ) ).to.equal( '<p>[\uD83D\uDCA9]</p>' );
  318. expect( document.selection.isBackward ).to.true;
  319. } );
  320. } );
  321. describe( 'objects handling', () => {
  322. beforeEach( () => {
  323. document.schema.registerItem( 'obj' );
  324. document.schema.allow( { name: 'obj', inside: '$root' } );
  325. document.schema.allow( { name: '$text', inside: 'obj' } );
  326. document.schema.objects.add( 'obj' );
  327. document.schema.registerItem( 'inlineObj', '$inline' );
  328. document.schema.allow( { name: '$text', inside: 'inlineObj' } );
  329. document.schema.objects.add( 'inlineObj' );
  330. } );
  331. test(
  332. 'extends over next object element when at the end of an element',
  333. '<p>foo[]</p><obj>bar</obj>',
  334. '<p>foo[</p><obj>bar</obj>]'
  335. );
  336. test(
  337. 'extends over previous object element when at the beginning of an element ',
  338. '<obj>bar</obj><p>[]foo</p>',
  339. '[<obj>bar</obj><p>]foo</p>',
  340. { direction: 'backward' }
  341. );
  342. test(
  343. 'extends over object elements - forward',
  344. '[<obj></obj>]<obj></obj>',
  345. '[<obj></obj><obj></obj>]'
  346. );
  347. it( 'extends over object elements - backward', () => {
  348. setData( document, '<obj></obj>[<obj></obj>]', { lastRangeBackward: true } );
  349. modifySelection( dataController, document.selection, { direction: 'backward' } );
  350. expect( stringify( document.getRoot(), document.selection ) ).to.equal( '[<obj></obj><obj></obj>]' );
  351. expect( document.selection.isBackward ).to.true;
  352. } );
  353. test(
  354. 'extends over inline objects - forward',
  355. '<p>foo[]<inlineObj>bar</inlineObj></p>',
  356. '<p>foo[<inlineObj>bar</inlineObj>]</p>'
  357. );
  358. test(
  359. 'extends over inline objects - backward',
  360. '<p><inlineObj>bar</inlineObj>[]foo</p>',
  361. '<p>[<inlineObj>bar</inlineObj>]foo</p>',
  362. { direction: 'backward' }
  363. );
  364. test(
  365. 'extends over empty inline objects - forward',
  366. '<p>foo[]<inlineObj></inlineObj></p>',
  367. '<p>foo[<inlineObj></inlineObj>]</p>'
  368. );
  369. test(
  370. 'extends over empty inline objects - backward',
  371. '<p><inlineObj></inlineObj>[]foo</p>',
  372. '<p>[<inlineObj></inlineObj>]foo</p>',
  373. { direction: 'backward' }
  374. );
  375. } );
  376. describe( 'limits handling', () => {
  377. beforeEach( () => {
  378. document.schema.registerItem( 'inlineLimit' );
  379. document.schema.allow( { name: 'inlineLimit', inside: '$block' } );
  380. document.schema.allow( { name: '$text', inside: 'inlineLimit' } );
  381. document.schema.registerItem( 'blockLimit' );
  382. document.schema.allow( { name: 'blockLimit', inside: '$root' } );
  383. document.schema.allow( { name: 'p', inside: 'blockLimit' } );
  384. document.schema.limits.add( 'inlineLimit' );
  385. document.schema.limits.add( 'blockLimit' );
  386. } );
  387. test(
  388. 'should not extend to outside of inline limit element',
  389. '<p>x<inlineLimit>foo[]</inlineLimit>x</p>',
  390. '<p>x<inlineLimit>foo[]</inlineLimit>x</p>'
  391. );
  392. test(
  393. 'should not extend to outside of inline limit element - backward',
  394. '<p>x<inlineLimit>[]foo</inlineLimit>x</p>',
  395. '<p>x<inlineLimit>[]foo</inlineLimit>x</p>',
  396. { direction: 'backward' }
  397. );
  398. test(
  399. 'should not extend to outside of block limit element',
  400. '<p>x</p><blockLimit><p>foo[]</p></blockLimit><p>x</p>',
  401. '<p>x</p><blockLimit><p>foo[]</p></blockLimit><p>x</p>'
  402. );
  403. test(
  404. 'should not extend to outside of block limit element - backward',
  405. '<p>x</p><blockLimit><p>[]foo</p></blockLimit><p>x</p>',
  406. '<p>x</p><blockLimit><p>[]foo</p></blockLimit><p>x</p>',
  407. { direction: 'backward' }
  408. );
  409. // This may seem counterintuitive but it makes sense. The limit element means
  410. // that it can't be left or modified from inside. If you want the same behavior from outside
  411. // register it as an object.
  412. test(
  413. 'should enter a limit element',
  414. '<p>foo[]</p><blockLimit><p>x</p></blockLimit>',
  415. '<p>foo[</p><blockLimit><p>]x</p></blockLimit>'
  416. );
  417. test(
  418. 'should enter a limit element - backward',
  419. '<blockLimit><p>x</p></blockLimit><p>[]foo</p>',
  420. '<blockLimit><p>x[</p></blockLimit><p>]foo</p>',
  421. { direction: 'backward' }
  422. );
  423. } );
  424. } );
  425. function test( title, input, output, options ) {
  426. it( title, () => {
  427. input = input.normalize();
  428. output = output.normalize();
  429. setData( document, input );
  430. // Creating new instance of selection instead of operation on module:engine/model/document~Document#selection.
  431. // Document's selection will throw errors in some test cases (which are correct cases, but only for
  432. // non-document selections).
  433. const testSelection = Selection.createFromSelection( document.selection );
  434. modifySelection( dataController, testSelection, options );
  435. expect( stringify( document.getRoot(), testSelection ) ).to.equal( output );
  436. } );
  437. }
  438. } );