8
0

restrictededitingmodeediting-commands.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  6. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  7. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  8. import Typing from '@ckeditor/ckeditor5-typing/src/typing';
  9. import Command from '@ckeditor/ckeditor5-core/src/command';
  10. import { setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  11. import RestrictedEditingModeEditing from './../src/restrictededitingmodeediting';
  12. import UndoEditing from '@ckeditor/ckeditor5-undo/src/undoediting';
  13. describe( 'RestrictedEditingEditing - commands', () => {
  14. let editor;
  15. testUtils.createSinonSandbox();
  16. describe( 'commands toggling', () => {
  17. let model, firstParagraph;
  18. beforeEach( async () => {
  19. editor = await VirtualTestEditor.create( { plugins: [ Paragraph, Typing, RestrictedEditingModeEditing ] } );
  20. model = editor.model;
  21. setModelData( model, '<paragraph>[]foo bar baz</paragraph>' );
  22. firstParagraph = model.document.getRoot().getChild( 0 );
  23. model.change( writer => {
  24. writer.addMarker( 'restricted-editing-exception:1', {
  25. range: writer.createRange(
  26. writer.createPositionAt( firstParagraph, 4 ),
  27. writer.createPositionAt( firstParagraph, 7 ) ),
  28. usingOperation: true,
  29. affectsData: true
  30. } );
  31. } );
  32. } );
  33. afterEach( async () => {
  34. await editor.destroy();
  35. } );
  36. describe( 'always enabled commands', () => {
  37. describe( 'undo', () => {
  38. beforeEach( () => {
  39. editor.commands.add( 'undo', buildFakeCommand( editor ) );
  40. } );
  41. it( 'should be enabled outside exception marker', () => {
  42. model.change( writer => {
  43. writer.setSelection( firstParagraph, 1 );
  44. } );
  45. expect( editor.commands.get( 'undo' ).isEnabled ).to.be.true;
  46. } );
  47. it( 'should be enabled inside exception marker', () => {
  48. model.change( writer => {
  49. writer.setSelection( firstParagraph, 5 );
  50. } );
  51. expect( editor.commands.get( 'undo' ).isEnabled ).to.be.true;
  52. } );
  53. } );
  54. describe( 'redo', () => {
  55. beforeEach( () => {
  56. editor.commands.add( 'redo', buildFakeCommand( editor ) );
  57. } );
  58. it( 'should be enabled outside exception marker', () => {
  59. model.change( writer => {
  60. writer.setSelection( firstParagraph, 1 );
  61. } );
  62. expect( editor.commands.get( 'redo' ).isEnabled ).to.be.true;
  63. } );
  64. it( 'should be enabled inside exception marker', () => {
  65. model.change( writer => {
  66. writer.setSelection( firstParagraph, 5 );
  67. } );
  68. expect( editor.commands.get( 'redo' ).isEnabled ).to.be.true;
  69. } );
  70. } );
  71. } );
  72. describe( 'commands enabled in exception marker', () => {
  73. describe( 'input', () => {
  74. beforeEach( () => {
  75. editor.commands.add( 'input', buildFakeCommand( editor ) );
  76. model.change( writer => {
  77. writer.setSelection( firstParagraph, 'end' );
  78. } );
  79. } );
  80. it( 'should be disabled when caret is outside exception marker', () => {
  81. model.change( writer => {
  82. writer.setSelection( firstParagraph, 1 );
  83. } );
  84. expect( editor.commands.get( 'input' ).isEnabled ).to.be.false;
  85. } );
  86. it( 'should be enabled when caret is inside exception marker (not touching boundaries)', () => {
  87. model.change( writer => {
  88. writer.setSelection( firstParagraph, 5 );
  89. } );
  90. expect( editor.commands.get( 'input' ).isEnabled ).to.be.true;
  91. } );
  92. it( 'should be disabled when caret is inside other marker', () => {
  93. model.change( writer => {
  94. writer.addMarker( 'foo-bar:1', {
  95. range: writer.createRange(
  96. writer.createPositionAt( firstParagraph, 0 ),
  97. writer.createPositionAt( firstParagraph, 3 ) ),
  98. usingOperation: true,
  99. affectsData: true
  100. } );
  101. writer.setSelection( firstParagraph, 1 );
  102. } );
  103. expect( editor.commands.get( 'input' ).isEnabled ).to.be.false;
  104. } );
  105. it( 'should be enabled when caret is inside exception marker (start boundary)', () => {
  106. model.change( writer => {
  107. writer.setSelection( firstParagraph, 4 );
  108. } );
  109. expect( editor.commands.get( 'input' ).isEnabled ).to.be.true;
  110. } );
  111. it( 'should be enabled when caret is inside exception marker (end boundary)', () => {
  112. model.change( writer => {
  113. writer.setSelection( firstParagraph, 7 );
  114. } );
  115. expect( editor.commands.get( 'input' ).isEnabled ).to.be.true;
  116. } );
  117. it( 'should be disabled for multi-range selection (collapsed ranges)', () => {
  118. model.change( writer => {
  119. writer.setSelection( [
  120. writer.createRange(
  121. writer.createPositionAt( firstParagraph, 5 )
  122. ),
  123. writer.createRange(
  124. writer.createPositionAt( firstParagraph, 9 )
  125. )
  126. ] );
  127. } );
  128. expect( editor.commands.get( 'input' ).isEnabled ).to.be.false;
  129. } );
  130. it( 'should be disabled for non-collapsed selection that expands over exception marker', () => {
  131. model.change( writer => {
  132. writer.setSelection( writer.createRange(
  133. writer.createPositionAt( firstParagraph, 0 ),
  134. writer.createPositionAt( firstParagraph, 5 )
  135. ) );
  136. } );
  137. expect( editor.commands.get( 'input' ).isEnabled ).to.be.false;
  138. } );
  139. it( 'should be enabled for non-collapsed selection that is fully contained inside exception marker', () => {
  140. model.change( writer => {
  141. writer.setSelection( writer.createRange(
  142. writer.createPositionAt( firstParagraph, 5 ),
  143. writer.createPositionAt( firstParagraph, 6 )
  144. ) );
  145. } );
  146. expect( editor.commands.get( 'input' ).isEnabled ).to.be.true;
  147. } );
  148. it( 'should be enabled for non-collapsed selection inside exception marker (start position on marker boundary)', () => {
  149. model.change( writer => {
  150. writer.setSelection( writer.createRange(
  151. writer.createPositionAt( firstParagraph, 4 ),
  152. writer.createPositionAt( firstParagraph, 6 )
  153. ) );
  154. } );
  155. expect( editor.commands.get( 'input' ).isEnabled ).to.be.true;
  156. } );
  157. it( 'should be enabled for non-collapsed selection inside exception marker (end position on marker boundary)', () => {
  158. model.change( writer => {
  159. writer.setSelection( writer.createRange(
  160. writer.createPositionAt( firstParagraph, 5 ),
  161. writer.createPositionAt( firstParagraph, 7 )
  162. ) );
  163. } );
  164. expect( editor.commands.get( 'input' ).isEnabled ).to.be.true;
  165. } );
  166. it( 'should be enabled for non-collapsed selection is equal to exception marker', () => {
  167. model.change( writer => {
  168. writer.setSelection( writer.createRange(
  169. writer.createPositionAt( firstParagraph, 4 ),
  170. writer.createPositionAt( firstParagraph, 7 )
  171. ) );
  172. } );
  173. expect( editor.commands.get( 'input' ).isEnabled ).to.be.true;
  174. } );
  175. it( 'should be disabled for non-collapsed selection with more then one range', () => {
  176. model.change( writer => {
  177. writer.setSelection( [
  178. writer.createRange(
  179. writer.createPositionAt( firstParagraph, 5 ),
  180. writer.createPositionAt( firstParagraph, 6 )
  181. ),
  182. writer.createRange(
  183. writer.createPositionAt( firstParagraph, 8 ),
  184. writer.createPositionAt( firstParagraph, 9 )
  185. )
  186. ] );
  187. } );
  188. expect( editor.commands.get( 'input' ).isEnabled ).to.be.false;
  189. } );
  190. } );
  191. describe( 'delete', () => {
  192. beforeEach( () => {
  193. editor.commands.add( 'delete', buildFakeCommand( editor ) );
  194. model.change( writer => {
  195. writer.setSelection( firstParagraph, 'end' );
  196. } );
  197. } );
  198. it( 'should be disabled when caret is outside exception marker', () => {
  199. model.change( writer => {
  200. writer.setSelection( firstParagraph, 1 );
  201. } );
  202. expect( editor.commands.get( 'delete' ).isEnabled ).to.be.false;
  203. } );
  204. it( 'should be enabled when caret is inside exception marker (not touching boundaries)', () => {
  205. model.change( writer => {
  206. writer.setSelection( firstParagraph, 5 );
  207. } );
  208. expect( editor.commands.get( 'delete' ).isEnabled ).to.be.true;
  209. } );
  210. it( 'should be disabled when caret is inside exception marker (start boundary)', () => {
  211. model.change( writer => {
  212. writer.setSelection( firstParagraph, 4 );
  213. } );
  214. expect( editor.commands.get( 'delete' ).isEnabled ).to.be.false;
  215. } );
  216. it( 'should be enabled when caret is inside exception marker (end boundary)', () => {
  217. model.change( writer => {
  218. writer.setSelection( firstParagraph, 7 );
  219. } );
  220. expect( editor.commands.get( 'delete' ).isEnabled ).to.be.true;
  221. } );
  222. it( 'should be disabled for non-collapsed selection that expands over exception marker', () => {
  223. model.change( writer => {
  224. writer.setSelection( writer.createRange(
  225. writer.createPositionAt( firstParagraph, 0 ),
  226. writer.createPositionAt( firstParagraph, 5 )
  227. ) );
  228. } );
  229. expect( editor.commands.get( 'delete' ).isEnabled ).to.be.false;
  230. } );
  231. it( 'should be enabled for non-collapsed selection that is fully contained inside exception marker', () => {
  232. model.change( writer => {
  233. writer.setSelection( writer.createRange(
  234. writer.createPositionAt( firstParagraph, 5 ),
  235. writer.createPositionAt( firstParagraph, 6 )
  236. ) );
  237. } );
  238. expect( editor.commands.get( 'delete' ).isEnabled ).to.be.true;
  239. } );
  240. it( 'should be enabled for non-collapsed selection inside exception marker (start position on marker boundary)', () => {
  241. model.change( writer => {
  242. writer.setSelection( writer.createRange(
  243. writer.createPositionAt( firstParagraph, 4 ),
  244. writer.createPositionAt( firstParagraph, 6 )
  245. ) );
  246. } );
  247. expect( editor.commands.get( 'delete' ).isEnabled ).to.be.true;
  248. } );
  249. it( 'should be enabled for non-collapsed selection inside exception marker (end position on marker boundary)', () => {
  250. model.change( writer => {
  251. writer.setSelection( writer.createRange(
  252. writer.createPositionAt( firstParagraph, 5 ),
  253. writer.createPositionAt( firstParagraph, 7 )
  254. ) );
  255. } );
  256. expect( editor.commands.get( 'delete' ).isEnabled ).to.be.true;
  257. } );
  258. it( 'should be enabled for non-collapsed selection is equal to exception marker', () => {
  259. model.change( writer => {
  260. writer.setSelection( writer.createRange(
  261. writer.createPositionAt( firstParagraph, 4 ),
  262. writer.createPositionAt( firstParagraph, 7 )
  263. ) );
  264. } );
  265. expect( editor.commands.get( 'delete' ).isEnabled ).to.be.true;
  266. } );
  267. } );
  268. describe( 'forwardDelete', () => {
  269. beforeEach( () => {
  270. editor.commands.add( 'forwardDelete', buildFakeCommand( editor ) );
  271. model.change( writer => {
  272. writer.setSelection( firstParagraph, 'end' );
  273. } );
  274. } );
  275. it( 'should be disabled when caret is outside exception marker', () => {
  276. model.change( writer => {
  277. writer.setSelection( firstParagraph, 1 );
  278. } );
  279. expect( editor.commands.get( 'forwardDelete' ).isEnabled ).to.be.false;
  280. } );
  281. it( 'should be enabled when caret is inside exception marker (not touching boundaries)', () => {
  282. model.change( writer => {
  283. writer.setSelection( firstParagraph, 5 );
  284. } );
  285. expect( editor.commands.get( 'forwardDelete' ).isEnabled ).to.be.true;
  286. } );
  287. it( 'should be enabled when caret is inside exception marker (start boundary)', () => {
  288. model.change( writer => {
  289. writer.setSelection( firstParagraph, 4 );
  290. } );
  291. expect( editor.commands.get( 'forwardDelete' ).isEnabled ).to.be.true;
  292. } );
  293. it( 'should be disabled when caret is inside exception marker (end boundary)', () => {
  294. model.change( writer => {
  295. writer.setSelection( firstParagraph, 7 );
  296. } );
  297. expect( editor.commands.get( 'forwardDelete' ).isEnabled ).to.be.false;
  298. } );
  299. it( 'should be disabled for non-collapsed selection that expands over exception marker', () => {
  300. model.change( writer => {
  301. writer.setSelection( writer.createRange(
  302. writer.createPositionAt( firstParagraph, 0 ),
  303. writer.createPositionAt( firstParagraph, 5 )
  304. ) );
  305. } );
  306. expect( editor.commands.get( 'forwardDelete' ).isEnabled ).to.be.false;
  307. } );
  308. it( 'should be enabled for non-collapsed selection that is fully contained inside exception marker', () => {
  309. model.change( writer => {
  310. writer.setSelection( writer.createRange(
  311. writer.createPositionAt( firstParagraph, 5 ),
  312. writer.createPositionAt( firstParagraph, 6 )
  313. ) );
  314. } );
  315. expect( editor.commands.get( 'forwardDelete' ).isEnabled ).to.be.true;
  316. } );
  317. it( 'should be enabled for non-collapsed selection inside exception marker (start position on marker boundary)', () => {
  318. model.change( writer => {
  319. writer.setSelection( writer.createRange(
  320. writer.createPositionAt( firstParagraph, 4 ),
  321. writer.createPositionAt( firstParagraph, 6 )
  322. ) );
  323. } );
  324. expect( editor.commands.get( 'forwardDelete' ).isEnabled ).to.be.true;
  325. } );
  326. it( 'should be enabled for non-collapsed selection inside exception marker (end position on marker boundary)', () => {
  327. model.change( writer => {
  328. writer.setSelection( writer.createRange(
  329. writer.createPositionAt( firstParagraph, 5 ),
  330. writer.createPositionAt( firstParagraph, 7 )
  331. ) );
  332. } );
  333. expect( editor.commands.get( 'forwardDelete' ).isEnabled ).to.be.true;
  334. } );
  335. it( 'should be enabled for non-collapsed selection is equal to exception marker', () => {
  336. model.change( writer => {
  337. writer.setSelection( writer.createRange(
  338. writer.createPositionAt( firstParagraph, 4 ),
  339. writer.createPositionAt( firstParagraph, 7 )
  340. ) );
  341. } );
  342. expect( editor.commands.get( 'forwardDelete' ).isEnabled ).to.be.true;
  343. } );
  344. } );
  345. } );
  346. describe( 'non-enabled commands', () => {
  347. beforeEach( () => {
  348. editor.commands.add( 'other', buildFakeCommand( editor ) );
  349. model.change( writer => {
  350. writer.setSelection( firstParagraph, 'end' );
  351. } );
  352. } );
  353. it( 'should be disabled outside exception marker', () => {
  354. model.change( writer => {
  355. writer.setSelection( firstParagraph, 1 );
  356. } );
  357. expect( editor.commands.get( 'other' ).isEnabled ).to.be.false;
  358. } );
  359. it( 'should be disabled inside exception marker', () => {
  360. model.change( writer => {
  361. writer.setSelection( firstParagraph, 1 );
  362. } );
  363. model.change( writer => {
  364. writer.setSelection( firstParagraph, 5 );
  365. } );
  366. expect( editor.commands.get( 'other' ).isEnabled ).to.be.false;
  367. } );
  368. it( 'should be disabled when caret is inside exception marker (not touching boundaries)', () => {
  369. model.change( writer => {
  370. writer.setSelection( firstParagraph, 5 );
  371. } );
  372. expect( editor.commands.get( 'other' ).isEnabled ).to.be.false;
  373. } );
  374. it( 'should be disabled when caret is inside exception marker (start boundary)', () => {
  375. model.change( writer => {
  376. writer.setSelection( firstParagraph, 4 );
  377. } );
  378. expect( editor.commands.get( 'other' ).isEnabled ).to.be.false;
  379. } );
  380. it( 'should be disabled when caret is inside exception marker (end boundary)', () => {
  381. model.change( writer => {
  382. writer.setSelection( firstParagraph, 7 );
  383. } );
  384. expect( editor.commands.get( 'other' ).isEnabled ).to.be.false;
  385. } );
  386. it( 'should be disabled for non-collapsed selection that expands over exception marker', () => {
  387. model.change( writer => {
  388. writer.setSelection( writer.createRange(
  389. writer.createPositionAt( firstParagraph, 0 ),
  390. writer.createPositionAt( firstParagraph, 5 )
  391. ) );
  392. } );
  393. expect( editor.commands.get( 'other' ).isEnabled ).to.be.false;
  394. } );
  395. it( 'should be disabled for non-collapsed selection that is fully contained inside exception marker', () => {
  396. model.change( writer => {
  397. writer.setSelection( writer.createRange(
  398. writer.createPositionAt( firstParagraph, 5 ),
  399. writer.createPositionAt( firstParagraph, 6 )
  400. ) );
  401. } );
  402. expect( editor.commands.get( 'other' ).isEnabled ).to.be.false;
  403. } );
  404. it( 'should be disabled for non-collapsed selection inside exception marker (start position on marker boundary)', () => {
  405. model.change( writer => {
  406. writer.setSelection( writer.createRange(
  407. writer.createPositionAt( firstParagraph, 4 ),
  408. writer.createPositionAt( firstParagraph, 6 )
  409. ) );
  410. } );
  411. expect( editor.commands.get( 'other' ).isEnabled ).to.be.false;
  412. } );
  413. it( 'should be disabled for non-collapsed selection inside exception marker (end position on marker boundary)', () => {
  414. model.change( writer => {
  415. writer.setSelection( writer.createRange(
  416. writer.createPositionAt( firstParagraph, 5 ),
  417. writer.createPositionAt( firstParagraph, 7 )
  418. ) );
  419. } );
  420. expect( editor.commands.get( 'other' ).isEnabled ).to.be.false;
  421. } );
  422. it( 'should be disabled for non-collapsed selection is equal to exception marker', () => {
  423. model.change( writer => {
  424. writer.setSelection( writer.createRange(
  425. writer.createPositionAt( firstParagraph, 4 ),
  426. writer.createPositionAt( firstParagraph, 7 )
  427. ) );
  428. } );
  429. expect( editor.commands.get( 'other' ).isEnabled ).to.be.false;
  430. } );
  431. } );
  432. } );
  433. describe( 'commands enabled in exception marker by configuration', () => {
  434. let model, firstParagraph;
  435. beforeEach( async () => {
  436. editor = await VirtualTestEditor.create( {
  437. plugins: [ Paragraph, Typing, RestrictedEditingModeEditing ],
  438. restrictedEditing: {
  439. allowedCommands: [ 'allowed' ]
  440. }
  441. } );
  442. model = editor.model;
  443. editor.commands.add( 'allowed', buildFakeCommand( editor ) );
  444. setModelData( model, '<paragraph>[]foo bar baz</paragraph>' );
  445. firstParagraph = model.document.getRoot().getChild( 0 );
  446. model.change( writer => {
  447. writer.addMarker( 'restricted-editing-exception:1', {
  448. range: writer.createRange(
  449. writer.createPositionAt( firstParagraph, 4 ),
  450. writer.createPositionAt( firstParagraph, 7 ) ),
  451. usingOperation: true,
  452. affectsData: true
  453. } );
  454. writer.setSelection( firstParagraph, 'end' );
  455. } );
  456. } );
  457. afterEach( async () => {
  458. await editor.destroy();
  459. } );
  460. it( 'should be disabled when caret is outside exception marker', () => {
  461. model.change( writer => {
  462. writer.setSelection( firstParagraph, 1 );
  463. } );
  464. expect( editor.commands.get( 'allowed' ).isEnabled ).to.be.false;
  465. } );
  466. it( 'should be enabled when caret is inside exception marker (not touching boundaries)', () => {
  467. model.change( writer => {
  468. writer.setSelection( firstParagraph, 5 );
  469. } );
  470. expect( editor.commands.get( 'allowed' ).isEnabled ).to.be.true;
  471. } );
  472. it( 'should be disabled when caret is inside other marker', () => {
  473. model.change( writer => {
  474. writer.addMarker( 'foo-bar:1', {
  475. range: writer.createRange(
  476. writer.createPositionAt( firstParagraph, 0 ),
  477. writer.createPositionAt( firstParagraph, 3 ) ),
  478. usingOperation: true,
  479. affectsData: true
  480. } );
  481. writer.setSelection( firstParagraph, 1 );
  482. } );
  483. expect( editor.commands.get( 'allowed' ).isEnabled ).to.be.false;
  484. } );
  485. it( 'should be enabled when caret is inside exception marker (start boundary)', () => {
  486. model.change( writer => {
  487. writer.setSelection( firstParagraph, 4 );
  488. } );
  489. expect( editor.commands.get( 'allowed' ).isEnabled ).to.be.true;
  490. } );
  491. it( 'should be enabled when caret is inside exception marker (end boundary)', () => {
  492. model.change( writer => {
  493. writer.setSelection( firstParagraph, 7 );
  494. } );
  495. expect( editor.commands.get( 'allowed' ).isEnabled ).to.be.true;
  496. } );
  497. it( 'should be disabled for multi-range selection (collapsed ranges)', () => {
  498. model.change( writer => {
  499. writer.setSelection( [
  500. writer.createRange(
  501. writer.createPositionAt( firstParagraph, 5 )
  502. ),
  503. writer.createRange(
  504. writer.createPositionAt( firstParagraph, 9 )
  505. )
  506. ] );
  507. } );
  508. expect( editor.commands.get( 'allowed' ).isEnabled ).to.be.false;
  509. } );
  510. it( 'should be disabled for non-collapsed selection that expands over exception marker', () => {
  511. model.change( writer => {
  512. writer.setSelection( writer.createRange(
  513. writer.createPositionAt( firstParagraph, 0 ),
  514. writer.createPositionAt( firstParagraph, 5 )
  515. ) );
  516. } );
  517. expect( editor.commands.get( 'allowed' ).isEnabled ).to.be.false;
  518. } );
  519. it( 'should be enabled for non-collapsed selection that is fully contained inside exception marker', () => {
  520. model.change( writer => {
  521. writer.setSelection( writer.createRange(
  522. writer.createPositionAt( firstParagraph, 5 ),
  523. writer.createPositionAt( firstParagraph, 6 )
  524. ) );
  525. } );
  526. expect( editor.commands.get( 'allowed' ).isEnabled ).to.be.true;
  527. } );
  528. it( 'should be enabled for non-collapsed selection inside exception marker (start position on marker boundary)', () => {
  529. model.change( writer => {
  530. writer.setSelection( writer.createRange(
  531. writer.createPositionAt( firstParagraph, 4 ),
  532. writer.createPositionAt( firstParagraph, 6 )
  533. ) );
  534. } );
  535. expect( editor.commands.get( 'allowed' ).isEnabled ).to.be.true;
  536. } );
  537. it( 'should be enabled for non-collapsed selection inside exception marker (end position on marker boundary)', () => {
  538. model.change( writer => {
  539. writer.setSelection( writer.createRange(
  540. writer.createPositionAt( firstParagraph, 5 ),
  541. writer.createPositionAt( firstParagraph, 7 )
  542. ) );
  543. } );
  544. expect( editor.commands.get( 'allowed' ).isEnabled ).to.be.true;
  545. } );
  546. it( 'should be enabled for non-collapsed selection is equal to exception marker', () => {
  547. model.change( writer => {
  548. writer.setSelection( writer.createRange(
  549. writer.createPositionAt( firstParagraph, 4 ),
  550. writer.createPositionAt( firstParagraph, 7 )
  551. ) );
  552. } );
  553. expect( editor.commands.get( 'allowed' ).isEnabled ).to.be.true;
  554. } );
  555. it( 'should be disabled for non-collapsed selection with more then one range', () => {
  556. model.change( writer => {
  557. writer.setSelection( [
  558. writer.createRange(
  559. writer.createPositionAt( firstParagraph, 5 ),
  560. writer.createPositionAt( firstParagraph, 6 )
  561. ),
  562. writer.createRange(
  563. writer.createPositionAt( firstParagraph, 8 ),
  564. writer.createPositionAt( firstParagraph, 9 )
  565. )
  566. ] );
  567. } );
  568. expect( editor.commands.get( 'allowed' ).isEnabled ).to.be.false;
  569. } );
  570. } );
  571. describe( 'integration', () => {
  572. let model, firstParagraph;
  573. beforeEach( async () => {
  574. editor = await VirtualTestEditor.create( { plugins: [ Paragraph, Typing, UndoEditing, RestrictedEditingModeEditing ] } );
  575. model = editor.model;
  576. setModelData( model, '<paragraph>[]foo bar baz</paragraph>' );
  577. firstParagraph = model.document.getRoot().getChild( 0 );
  578. model.change( writer => {
  579. writer.addMarker( 'restricted-editing-exception:1', {
  580. range: writer.createRange(
  581. writer.createPositionAt( firstParagraph, 4 ),
  582. writer.createPositionAt( firstParagraph, 7 ) ),
  583. usingOperation: true,
  584. affectsData: true
  585. } );
  586. } );
  587. } );
  588. afterEach( async () => {
  589. await editor.destroy();
  590. } );
  591. describe( 'delete + undo', () => {
  592. it( 'should be enabled after data change (no selection change event on undo)', () => {
  593. model.change( writer => {
  594. writer.setSelection( firstParagraph, 7 );
  595. } );
  596. editor.execute( 'delete' );
  597. editor.execute( 'undo' );
  598. expect( editor.commands.get( 'delete' ).isEnabled ).to.be.true;
  599. } );
  600. } );
  601. } );
  602. class FakeCommand extends Command {
  603. refresh() {
  604. this.isEnabled = true;
  605. }
  606. }
  607. function buildFakeCommand( editor ) {
  608. return new FakeCommand( editor );
  609. }
  610. } );