modifyselection.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953
  1. /**
  2. * @license Copyright (c) 2003-2018, 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 = new Selection( 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( 'unit=word', () => {
  321. describe( 'within element', () => {
  322. test(
  323. 'does nothing on empty content',
  324. '[]',
  325. '[]',
  326. { unit: 'word' }
  327. );
  328. test(
  329. 'does nothing on empty content (with empty element)',
  330. '<p>[]</p>',
  331. '<p>[]</p>'
  332. );
  333. test(
  334. 'does nothing on empty content (backward)',
  335. '[]',
  336. '[]',
  337. { unit: 'word', direction: 'backward' }
  338. );
  339. test(
  340. 'does nothing on root boundary',
  341. '<p>foo[]</p>',
  342. '<p>foo[]</p>',
  343. { unit: 'word' }
  344. );
  345. test(
  346. 'does nothing on root boundary (backward)',
  347. '<p>[]foo</p>',
  348. '<p>[]foo</p>',
  349. { unit: 'word', direction: 'backward' }
  350. );
  351. for ( const char of ' ,.?!:;"-()'.split( '' ) ) {
  352. testStopCharacter( char );
  353. }
  354. test(
  355. 'extends whole word forward (non-collapsed)',
  356. '<p>f[o]obar</p>',
  357. '<p>f[oobar]</p>',
  358. { unit: 'word' }
  359. );
  360. it( 'extends whole word backward (non-collapsed)', () => {
  361. setData( model, '<p>foo ba[a]r</p>', { lastRangeBackward: true } );
  362. modifySelection( model, doc.selection, { unit: 'word', direction: 'backward' } );
  363. expect( stringify( doc.getRoot(), doc.selection ) ).to.equal( '<p>foo [baa]r</p>' );
  364. expect( doc.selection.isBackward ).to.true;
  365. } );
  366. test(
  367. 'extends to element boundary',
  368. '<p>fo[]oo</p>',
  369. '<p>fo[oo]</p>',
  370. { unit: 'word' }
  371. );
  372. it( 'extends to element boundary (backward)', () => {
  373. setData( model, '<p>ff[]oo</p>' );
  374. modifySelection( model, doc.selection, { unit: 'word', direction: 'backward' } );
  375. expect( stringify( doc.getRoot(), doc.selection ) ).to.equal( '<p>[ff]oo</p>' );
  376. expect( doc.selection.isBackward ).to.true;
  377. } );
  378. test(
  379. 'expands forward selection to the word start',
  380. '<p>foo bar[b]az</p>',
  381. '<p>foo [bar]baz</p>',
  382. { unit: 'word', direction: 'backward' }
  383. );
  384. it( 'expands backward selection to the word end', () => {
  385. setData( model, '<p>foo[b]ar baz</p>', { lastRangeBackward: true } );
  386. modifySelection( model, doc.selection, { unit: 'word' } );
  387. expect( stringify( doc.getRoot(), doc.selection ) ).to.equal( '<p>foob[ar] baz</p>' );
  388. expect( doc.selection.isBackward ).to.false;
  389. } );
  390. test(
  391. 'unicode support - combining mark forward',
  392. '<p>foo[]b̂ar</p>',
  393. '<p>foo[b̂ar]</p>',
  394. { unit: 'word' }
  395. );
  396. it( 'unicode support - combining mark backward', () => {
  397. setData( model, '<p>foob̂[]ar</p>' );
  398. modifySelection( model, doc.selection, { direction: 'backward', unit: 'word' } );
  399. expect( stringify( doc.getRoot(), doc.selection ) ).to.equal( '<p>[foob̂]ar</p>' );
  400. expect( doc.selection.isBackward ).to.true;
  401. } );
  402. test(
  403. 'unicode support - combining mark multiple',
  404. '<p>fo[]o̻̐ͩbar</p>',
  405. '<p>fo[o̻̐ͩbar]</p>',
  406. { unit: 'word' }
  407. );
  408. it( 'unicode support - combining mark multiple backward', () => {
  409. setData( model, '<p>foo̻̐ͩ[]bar</p>' );
  410. modifySelection( model, doc.selection, { direction: 'backward' } );
  411. expect( stringify( doc.getRoot(), doc.selection ) ).to.equal( '<p>fo[o̻̐ͩ]bar</p>' );
  412. expect( doc.selection.isBackward ).to.true;
  413. } );
  414. test(
  415. 'unicode support - combining mark to the end',
  416. '<p>f[o]o̻̐ͩ</p>',
  417. '<p>f[oo̻̐ͩ]</p>',
  418. { unit: 'word' }
  419. );
  420. test(
  421. 'unicode support - surrogate pairs forward',
  422. '<p>[]foo\uD83D\uDCA9</p>',
  423. '<p>[foo\uD83D\uDCA9]</p>',
  424. { unit: 'word' }
  425. );
  426. it( 'unicode support - surrogate pairs backward', () => {
  427. setData( model, '<p>foo\uD83D\uDCA9[]</p>' );
  428. modifySelection( model, doc.selection, { unit: 'word', direction: 'backward' } );
  429. expect( stringify( doc.getRoot(), doc.selection ) ).to.equal( '<p>[foo\uD83D\uDCA9]</p>' );
  430. expect( doc.selection.isBackward ).to.true;
  431. } );
  432. function testStopCharacter( stopCharacter ) {
  433. describe( `stop character: "${ stopCharacter }"`, () => {
  434. test(
  435. 'extends whole word forward',
  436. `<p>f[]oo${ stopCharacter }bar</p>`,
  437. `<p>f[oo]${ stopCharacter }bar</p>`,
  438. { unit: 'word' }
  439. );
  440. it( 'extends whole word backward to the previous word', () => {
  441. setData( model, `<p>foo${ stopCharacter }ba[]r</p>`, { lastRangeBackward: true } );
  442. modifySelection( model, doc.selection, { unit: 'word', direction: 'backward' } );
  443. expect( stringify( doc.getRoot(), doc.selection ) ).to.equal( `<p>foo${ stopCharacter }[ba]r</p>` );
  444. expect( doc.selection.isBackward ).to.true;
  445. } );
  446. it( 'extends whole word backward', () => {
  447. setData( model, `<p>fo[]o${ stopCharacter }bar</p>`, { lastRangeBackward: true } );
  448. modifySelection( model, doc.selection, { unit: 'word', direction: 'backward' } );
  449. expect( stringify( doc.getRoot(), doc.selection ) ).to.equal( `<p>[fo]o${ stopCharacter }bar</p>` );
  450. expect( doc.selection.isBackward ).to.true;
  451. } );
  452. test(
  453. 'ignores attributes when in one word - case 1',
  454. `<p>foo[]<$text bold="true">bar</$text>baz${ stopCharacter }foobarbaz</p>`,
  455. `<p>foo[<$text bold="true">bar</$text>baz]${ stopCharacter }foobarbaz</p>`,
  456. { unit: 'word' }
  457. );
  458. test(
  459. 'ignores attributes when in one word - case 2',
  460. `<p>foo[]<$text bold="true">bar</$text>${ stopCharacter }foobarbaz</p>`,
  461. `<p>foo[<$text bold="true">bar</$text>]${ stopCharacter }foobarbaz</p>`,
  462. { unit: 'word' }
  463. );
  464. test(
  465. 'ignores attributes when in one word - case 3',
  466. `<p>foo[]<$text bold="true">bar</$text><$text italic="true">baz</$text>baz${ stopCharacter }foobarbaz</p>`,
  467. `<p>foo[<$text bold="true">bar</$text><$text italic="true">baz</$text>baz]${ stopCharacter }foobarbaz</p>`,
  468. { unit: 'word' }
  469. );
  470. it( 'extends whole word backward to the previous word ignoring attributes - case 1', () => {
  471. setData(
  472. model,
  473. `<p>foobarbaz${ stopCharacter }foo<$text bold="true">bar</$text>baz[]</p>`
  474. );
  475. modifySelection( model, doc.selection, { unit: 'word', direction: 'backward' } );
  476. expect( stringify( doc.getRoot(), doc.selection ) ).to.equal(
  477. `<p>foobarbaz${ stopCharacter }[foo<$text bold="true">bar</$text>baz]</p>`
  478. );
  479. expect( doc.selection.isBackward ).to.true;
  480. } );
  481. it( 'extends whole word backward to the previous word ignoring attributes - case 2', () => {
  482. setData(
  483. model,
  484. `<p>foobarbaz${ stopCharacter }<$text bold="true">bar</$text>baz[]</p>`
  485. );
  486. modifySelection( model, doc.selection, { unit: 'word', direction: 'backward' } );
  487. expect( stringify( doc.getRoot(), doc.selection ) ).to.equal(
  488. `<p>foobarbaz${ stopCharacter }[<$text bold="true">bar</$text>baz]</p>`
  489. );
  490. expect( doc.selection.isBackward ).to.true;
  491. } );
  492. } );
  493. }
  494. } );
  495. describe( 'beyond element', () => {
  496. test(
  497. 'extends over boundary of empty elements',
  498. '<p>[]</p><p></p><p></p>',
  499. '<p>[</p><p>]</p><p></p>',
  500. { unit: 'word' }
  501. );
  502. it( 'extends over boundary of empty elements (backward)', () => {
  503. setData( model, '<p></p><p></p><p>[]</p>' );
  504. modifySelection( model, doc.selection, { direction: 'backward' } );
  505. expect( stringify( doc.getRoot(), doc.selection ) ).to.equal( '<p></p><p>[</p><p>]</p>' );
  506. expect( doc.selection.isBackward ).to.true;
  507. } );
  508. test(
  509. 'extends over boundary of non-empty elements',
  510. '<p>a[]</p><p>bcd</p>',
  511. '<p>a[</p><p>]bcd</p>',
  512. { unit: 'word' }
  513. );
  514. it( 'extends over boundary of non-empty elements (backward)', () => {
  515. setData( model, '<p>a</p><p>[]bcd</p>' );
  516. modifySelection( model, doc.selection, { direction: 'backward' } );
  517. expect( stringify( doc.getRoot(), doc.selection ) ).to.equal( '<p>a[</p><p>]bcd</p>' );
  518. expect( doc.selection.isBackward ).to.true;
  519. } );
  520. test(
  521. 'extends over character after boundary',
  522. '<p>a[</p><p>]bcd</p>',
  523. '<p>a[</p><p>bcd]</p>',
  524. { unit: 'word' }
  525. );
  526. it( 'extends over character after boundary (backward)', () => {
  527. setData( model, '<p>abc[</p><p>]d</p>', { lastRangeBackward: true } );
  528. modifySelection( model, doc.selection, { direction: 'backward' } );
  529. expect( stringify( doc.getRoot(), doc.selection ) ).to.equal( '<p>ab[c</p><p>]d</p>' );
  530. expect( doc.selection.isBackward ).to.true;
  531. } );
  532. test(
  533. 'stops on the first position where text is allowed - inside block',
  534. '<p>a[]</p><p><x>bcd</x></p>',
  535. '<p>a[</p><p>]<x>bcd</x></p>',
  536. { unit: 'word' }
  537. );
  538. test(
  539. 'stops on the first position where text is allowed - inside inline element',
  540. '<p>a[</p><p>]<x>bcd</x>ef</p>',
  541. '<p>a[</p><p><x>]bcd</x>ef</p>',
  542. { unit: 'word' }
  543. );
  544. test(
  545. 'extends over element when next node is a text',
  546. '<p><x>a[]</x>bc</p>',
  547. '<p><x>a[</x>]bc</p>',
  548. { unit: 'word' }
  549. );
  550. test(
  551. 'extends over element when next node is a text - backward',
  552. '<p>ab<x>[]c</x></p>',
  553. '<p>ab[<x>]c</x></p>',
  554. { unit: 'word', direction: 'backward' }
  555. );
  556. it( 'shrinks over boundary of empty elements', () => {
  557. setData( model, '<p>[</p><p>]</p>', { lastRangeBackward: true } );
  558. modifySelection( model, doc.selection, { unit: 'word' } );
  559. expect( stringify( doc.getRoot(), doc.selection ) ).to.equal( '<p></p><p>[]</p>' );
  560. expect( doc.selection.isBackward ).to.false;
  561. } );
  562. it( 'shrinks over boundary of empty elements (backward)', () => {
  563. setData( model, '<p>[</p><p>]</p>' );
  564. modifySelection( model, doc.selection, { unit: 'word', direction: 'backward' } );
  565. expect( stringify( doc.getRoot(), doc.selection ) ).to.equal( '<p>[]</p><p></p>' );
  566. expect( doc.selection.isBackward ).to.false;
  567. } );
  568. it( 'shrinks over boundary of non-empty elements', () => {
  569. setData( model, '<p>a[</p><p>]b</p>', { lastRangeBackward: true } );
  570. modifySelection( model, doc.selection, { unit: 'word' } );
  571. expect( stringify( doc.getRoot(), doc.selection ) ).to.equal( '<p>a</p><p>[]b</p>' );
  572. expect( doc.selection.isBackward ).to.false;
  573. } );
  574. test(
  575. 'shrinks over boundary of non-empty elements (backward)',
  576. '<p>a[</p><p>]b</p>',
  577. '<p>a[]</p><p>b</p>',
  578. { unit: 'word', direction: 'backward' }
  579. );
  580. it( 'updates selection attributes', () => {
  581. setData( model, '<p><$text bold="true">foo</$text>[b]</p>' );
  582. modifySelection( model, doc.selection, { unit: 'word', direction: 'backward' } );
  583. expect( stringify( doc.getRoot(), doc.selection ) ).to.equal( '<p><$text bold="true">foo[]</$text>b</p>' );
  584. expect( doc.selection.getAttribute( 'bold' ) ).to.equal( true );
  585. } );
  586. } );
  587. describe( 'beyond element – skipping incorrect positions', () => {
  588. beforeEach( () => {
  589. model.schema.register( 'quote' );
  590. model.schema.extend( 'quote', { allowIn: '$root' } );
  591. model.schema.extend( '$block', { allowIn: 'quote' } );
  592. } );
  593. test(
  594. 'skips position at the beginning of an element which does not allow text',
  595. '<p>x[]</p><quote><p>y</p></quote><p>z</p>',
  596. '<p>x[</p><quote><p>]y</p></quote><p>z</p>',
  597. { unit: 'word' }
  598. );
  599. test(
  600. 'skips position at the end of an element which does not allow text - backward',
  601. '<p>x</p><quote><p>y</p></quote><p>[]z</p>',
  602. '<p>x</p><quote><p>y[</p></quote><p>]z</p>',
  603. { unit: 'word', direction: 'backward' }
  604. );
  605. test(
  606. 'skips position at the end of an element which does not allow text',
  607. '<p>x[</p><quote><p>y]</p></quote><p>z</p>',
  608. '<p>x[</p><quote><p>y</p></quote><p>]z</p>',
  609. { unit: 'word' }
  610. );
  611. test(
  612. 'skips position at the beginning of an element which does not allow text - backward',
  613. '<p>x</p><quote><p>[]y</p></quote><p>z</p>',
  614. '<p>x[</p><quote><p>]y</p></quote><p>z</p>',
  615. { unit: 'word', direction: 'backward' }
  616. );
  617. test(
  618. 'extends to an empty block after skipping incorrect position',
  619. '<p>x[]</p><quote><p></p></quote><p>z</p>',
  620. '<p>x[</p><quote><p>]</p></quote><p>z</p>',
  621. { unit: 'word' }
  622. );
  623. test(
  624. 'extends to an empty block after skipping incorrect position - backward',
  625. '<p>x</p><quote><p></p></quote><p>[]z</p>',
  626. '<p>x</p><quote><p>[</p></quote><p>]z</p>',
  627. { unit: 'word', direction: 'backward' }
  628. );
  629. } );
  630. } );
  631. describe( 'objects handling', () => {
  632. beforeEach( () => {
  633. model.schema.register( 'obj', {
  634. isObject: true
  635. } );
  636. model.schema.extend( 'obj', { allowIn: '$root' } );
  637. model.schema.extend( '$text', { allowIn: 'obj' } );
  638. model.schema.register( 'inlineObj', {
  639. allowIn: 'p',
  640. isObject: true
  641. } );
  642. model.schema.extend( '$text', { allowIn: 'inlineObj' } );
  643. } );
  644. test(
  645. 'extends over next object element when at the end of an element',
  646. '<p>foo[]</p><obj>bar</obj>',
  647. '<p>foo[</p><obj>bar</obj>]'
  648. );
  649. test(
  650. 'extends over previous object element when at the beginning of an element ',
  651. '<obj>bar</obj><p>[]foo</p>',
  652. '[<obj>bar</obj><p>]foo</p>',
  653. { direction: 'backward' }
  654. );
  655. test(
  656. 'extends over object elements - forward',
  657. '[<obj></obj>]<obj></obj>',
  658. '[<obj></obj><obj></obj>]'
  659. );
  660. it( 'extends over object elements - backward', () => {
  661. setData( model, '<obj></obj>[<obj></obj>]', { lastRangeBackward: true } );
  662. modifySelection( model, doc.selection, { direction: 'backward' } );
  663. expect( stringify( doc.getRoot(), doc.selection ) ).to.equal( '[<obj></obj><obj></obj>]' );
  664. expect( doc.selection.isBackward ).to.true;
  665. } );
  666. test(
  667. 'extends over inline objects - forward',
  668. '<p>foo[]<inlineObj>bar</inlineObj></p>',
  669. '<p>foo[<inlineObj>bar</inlineObj>]</p>'
  670. );
  671. test(
  672. 'extends over inline objects - backward',
  673. '<p><inlineObj>bar</inlineObj>[]foo</p>',
  674. '<p>[<inlineObj>bar</inlineObj>]foo</p>',
  675. { direction: 'backward' }
  676. );
  677. test(
  678. 'extends over empty inline objects - forward',
  679. '<p>foo[]<inlineObj></inlineObj></p>',
  680. '<p>foo[<inlineObj></inlineObj>]</p>'
  681. );
  682. test(
  683. 'extends over empty inline objects - backward',
  684. '<p><inlineObj></inlineObj>[]foo</p>',
  685. '<p>[<inlineObj></inlineObj>]foo</p>',
  686. { direction: 'backward' }
  687. );
  688. } );
  689. describe( 'limits handling', () => {
  690. beforeEach( () => {
  691. model.schema.register( 'inlineLimit', {
  692. isLimit: true
  693. } );
  694. model.schema.extend( 'inlineLimit', { allowIn: '$block' } );
  695. model.schema.extend( '$text', { allowIn: 'inlineLimit' } );
  696. model.schema.register( 'blockLimit', {
  697. isLimit: true
  698. } );
  699. model.schema.extend( 'blockLimit', { allowIn: '$root' } );
  700. model.schema.extend( 'p', { allowIn: 'blockLimit' } );
  701. } );
  702. test(
  703. 'should not extend to outside of inline limit element',
  704. '<p>x<inlineLimit>foo[]</inlineLimit>x</p>',
  705. '<p>x<inlineLimit>foo[]</inlineLimit>x</p>'
  706. );
  707. test(
  708. 'should not extend to outside of inline limit element - backward',
  709. '<p>x<inlineLimit>[]foo</inlineLimit>x</p>',
  710. '<p>x<inlineLimit>[]foo</inlineLimit>x</p>',
  711. { direction: 'backward' }
  712. );
  713. test(
  714. 'should not extend to outside of block limit element',
  715. '<p>x</p><blockLimit><p>foo[]</p></blockLimit><p>x</p>',
  716. '<p>x</p><blockLimit><p>foo[]</p></blockLimit><p>x</p>'
  717. );
  718. test(
  719. 'should not extend to outside of block limit element - backward',
  720. '<p>x</p><blockLimit><p>[]foo</p></blockLimit><p>x</p>',
  721. '<p>x</p><blockLimit><p>[]foo</p></blockLimit><p>x</p>',
  722. { direction: 'backward' }
  723. );
  724. // This may seem counterintuitive but it makes sense. The limit element means
  725. // that it can't be left or modified from inside. If you want the same behavior from outside
  726. // register it as an object.
  727. test(
  728. 'should enter a limit element',
  729. '<p>foo[]</p><blockLimit><p>x</p></blockLimit>',
  730. '<p>foo[</p><blockLimit><p>]x</p></blockLimit>'
  731. );
  732. test(
  733. 'should enter a limit element - backward',
  734. '<blockLimit><p>x</p></blockLimit><p>[]foo</p>',
  735. '<blockLimit><p>x[</p></blockLimit><p>]foo</p>',
  736. { direction: 'backward' }
  737. );
  738. } );
  739. } );
  740. function test( title, input, output, options ) {
  741. it( title, () => {
  742. input = input.normalize();
  743. output = output.normalize();
  744. setData( model, input );
  745. // Creating new instance of selection instead of operation on module:engine/model/document~Document#selection.
  746. // Document's selection will throw errors in some test cases (which are correct cases, but only for
  747. // non-document selections).
  748. const testSelection = new Selection( doc.selection );
  749. modifySelection( model, testSelection, options );
  750. expect( stringify( doc.getRoot(), testSelection ) ).to.equal( output );
  751. } );
  752. }
  753. } );