modifyselection.js 16 KB

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