restrictedediting-commands.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  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 RestrictedEditingEditing from './../src/restrictededitingediting';
  12. import UndoEditing from '@ckeditor/ckeditor5-undo/src/undoediting';
  13. describe( 'RestrictedEditingEditing - commands', () => {
  14. let editor;
  15. testUtils.createSinonSandbox();
  16. describe( 'commands behavior', () => {
  17. let model, firstParagraph;
  18. beforeEach( async () => {
  19. editor = await VirtualTestEditor.create( { plugins: [ Paragraph, Typing, RestrictedEditingEditing ] } );
  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( 'undo', () => {
  37. beforeEach( () => {
  38. editor.commands.add( 'undo', buildFakeCommand( editor ) );
  39. } );
  40. it( 'should be enabled outside exception marker', () => {
  41. model.change( writer => {
  42. writer.setSelection( firstParagraph, 1 );
  43. } );
  44. expect( editor.commands.get( 'undo' ).isEnabled ).to.be.true;
  45. } );
  46. it( 'should be enabled inside exception marker', () => {
  47. model.change( writer => {
  48. writer.setSelection( firstParagraph, 5 );
  49. } );
  50. expect( editor.commands.get( 'undo' ).isEnabled ).to.be.true;
  51. } );
  52. } );
  53. describe( 'redo', () => {
  54. beforeEach( () => {
  55. editor.commands.add( 'redo', buildFakeCommand( editor ) );
  56. } );
  57. it( 'should be enabled outside exception marker', () => {
  58. model.change( writer => {
  59. writer.setSelection( firstParagraph, 1 );
  60. } );
  61. expect( editor.commands.get( 'redo' ).isEnabled ).to.be.true;
  62. } );
  63. it( 'should be enabled inside exception marker', () => {
  64. model.change( writer => {
  65. writer.setSelection( firstParagraph, 5 );
  66. } );
  67. expect( editor.commands.get( 'redo' ).isEnabled ).to.be.true;
  68. } );
  69. } );
  70. const commandsEnabledOnWholeRange = [
  71. 'input', 'bold', 'link', 'italic'
  72. ];
  73. for ( const commandName of commandsEnabledOnWholeRange ) {
  74. describe( commandName, () => {
  75. beforeEach( () => {
  76. editor.commands.add( commandName, buildFakeCommand( editor ) );
  77. model.change( writer => {
  78. writer.setSelection( firstParagraph, 'end' );
  79. } );
  80. } );
  81. it( 'should be disabled when caret is outside exception marker', () => {
  82. model.change( writer => {
  83. writer.setSelection( firstParagraph, 1 );
  84. } );
  85. expect( editor.commands.get( commandName ).isEnabled ).to.be.false;
  86. } );
  87. it( 'should be enabled when caret is inside exception marker (not touching boundaries)', () => {
  88. model.change( writer => {
  89. writer.setSelection( firstParagraph, 5 );
  90. } );
  91. expect( editor.commands.get( commandName ).isEnabled ).to.be.true;
  92. } );
  93. it( 'should be disabled when caret is inside other marker', () => {
  94. model.change( writer => {
  95. writer.addMarker( 'foo-bar:1', {
  96. range: writer.createRange(
  97. writer.createPositionAt( firstParagraph, 0 ),
  98. writer.createPositionAt( firstParagraph, 3 ) ),
  99. usingOperation: true,
  100. affectsData: true
  101. } );
  102. writer.setSelection( firstParagraph, 1 );
  103. } );
  104. expect( editor.commands.get( commandName ).isEnabled ).to.be.false;
  105. } );
  106. it( 'should be enabled when caret is inside exception marker (start boundary)', () => {
  107. model.change( writer => {
  108. writer.setSelection( firstParagraph, 4 );
  109. } );
  110. expect( editor.commands.get( commandName ).isEnabled ).to.be.true;
  111. } );
  112. it( 'should be enabled when caret is inside exception marker (end boundary)', () => {
  113. model.change( writer => {
  114. writer.setSelection( firstParagraph, 7 );
  115. } );
  116. expect( editor.commands.get( commandName ).isEnabled ).to.be.true;
  117. } );
  118. it( 'should be disabled for multi-range selection (collapsed ranges)', () => {
  119. model.change( writer => {
  120. writer.setSelection( [
  121. writer.createRange(
  122. writer.createPositionAt( firstParagraph, 5 )
  123. ),
  124. writer.createRange(
  125. writer.createPositionAt( firstParagraph, 9 )
  126. )
  127. ] );
  128. } );
  129. expect( editor.commands.get( commandName ).isEnabled ).to.be.false;
  130. } );
  131. it( 'should be disabled for non-collapsed selection that expands over exception marker', () => {
  132. model.change( writer => {
  133. writer.setSelection( writer.createRange(
  134. writer.createPositionAt( firstParagraph, 0 ),
  135. writer.createPositionAt( firstParagraph, 5 )
  136. ) );
  137. } );
  138. expect( editor.commands.get( commandName ).isEnabled ).to.be.false;
  139. } );
  140. it( 'should be enabled for non-collapsed selection that is fully contained inside exception marker', () => {
  141. model.change( writer => {
  142. writer.setSelection( writer.createRange(
  143. writer.createPositionAt( firstParagraph, 5 ),
  144. writer.createPositionAt( firstParagraph, 6 )
  145. ) );
  146. } );
  147. expect( editor.commands.get( commandName ).isEnabled ).to.be.true;
  148. } );
  149. it( 'should be enabled for non-collapsed selection inside exception marker (start position on marker boundary)', () => {
  150. model.change( writer => {
  151. writer.setSelection( writer.createRange(
  152. writer.createPositionAt( firstParagraph, 4 ),
  153. writer.createPositionAt( firstParagraph, 6 )
  154. ) );
  155. } );
  156. expect( editor.commands.get( commandName ).isEnabled ).to.be.true;
  157. } );
  158. it( 'should be enabled for non-collapsed selection inside exception marker (end position on marker boundary)', () => {
  159. model.change( writer => {
  160. writer.setSelection( writer.createRange(
  161. writer.createPositionAt( firstParagraph, 5 ),
  162. writer.createPositionAt( firstParagraph, 7 )
  163. ) );
  164. } );
  165. expect( editor.commands.get( commandName ).isEnabled ).to.be.true;
  166. } );
  167. it( 'should be enabled for non-collapsed selection is equal to exception marker', () => {
  168. model.change( writer => {
  169. writer.setSelection( writer.createRange(
  170. writer.createPositionAt( firstParagraph, 4 ),
  171. writer.createPositionAt( firstParagraph, 7 )
  172. ) );
  173. } );
  174. expect( editor.commands.get( commandName ).isEnabled ).to.be.true;
  175. } );
  176. it( 'should be disabled for non-collapsed selection with more then one range', () => {
  177. model.change( writer => {
  178. writer.setSelection( [
  179. writer.createRange(
  180. writer.createPositionAt( firstParagraph, 5 ),
  181. writer.createPositionAt( firstParagraph, 6 )
  182. ),
  183. writer.createRange(
  184. writer.createPositionAt( firstParagraph, 8 ),
  185. writer.createPositionAt( firstParagraph, 9 )
  186. )
  187. ] );
  188. } );
  189. expect( editor.commands.get( commandName ).isEnabled ).to.be.false;
  190. } );
  191. } );
  192. }
  193. describe( 'delete', () => {
  194. beforeEach( () => {
  195. editor.commands.add( 'delete', buildFakeCommand( editor ) );
  196. model.change( writer => {
  197. writer.setSelection( firstParagraph, 'end' );
  198. } );
  199. } );
  200. it( 'should be disabled when caret is outside exception marker', () => {
  201. model.change( writer => {
  202. writer.setSelection( firstParagraph, 1 );
  203. } );
  204. expect( editor.commands.get( 'delete' ).isEnabled ).to.be.false;
  205. } );
  206. it( 'should be enabled when caret is inside exception marker (not touching boundaries)', () => {
  207. model.change( writer => {
  208. writer.setSelection( firstParagraph, 5 );
  209. } );
  210. expect( editor.commands.get( 'delete' ).isEnabled ).to.be.true;
  211. } );
  212. it( 'should be disabled when caret is inside exception marker (start boundary)', () => {
  213. model.change( writer => {
  214. writer.setSelection( firstParagraph, 4 );
  215. } );
  216. expect( editor.commands.get( 'delete' ).isEnabled ).to.be.false;
  217. } );
  218. it( 'should be enabled when caret is inside exception marker (end boundary)', () => {
  219. model.change( writer => {
  220. writer.setSelection( firstParagraph, 7 );
  221. } );
  222. expect( editor.commands.get( 'delete' ).isEnabled ).to.be.true;
  223. } );
  224. it( 'should be disabled for non-collapsed selection that expands over exception marker', () => {
  225. model.change( writer => {
  226. writer.setSelection( writer.createRange(
  227. writer.createPositionAt( firstParagraph, 0 ),
  228. writer.createPositionAt( firstParagraph, 5 )
  229. ) );
  230. } );
  231. expect( editor.commands.get( 'delete' ).isEnabled ).to.be.false;
  232. } );
  233. it( 'should be enabled for non-collapsed selection that is fully contained inside exception marker', () => {
  234. model.change( writer => {
  235. writer.setSelection( writer.createRange(
  236. writer.createPositionAt( firstParagraph, 5 ),
  237. writer.createPositionAt( firstParagraph, 6 )
  238. ) );
  239. } );
  240. expect( editor.commands.get( 'delete' ).isEnabled ).to.be.true;
  241. } );
  242. it( 'should be enabled for non-collapsed selection inside exception marker (start position on marker boundary)', () => {
  243. model.change( writer => {
  244. writer.setSelection( writer.createRange(
  245. writer.createPositionAt( firstParagraph, 4 ),
  246. writer.createPositionAt( firstParagraph, 6 )
  247. ) );
  248. } );
  249. expect( editor.commands.get( 'delete' ).isEnabled ).to.be.true;
  250. } );
  251. it( 'should be enabled for non-collapsed selection inside exception marker (end position on marker boundary)', () => {
  252. model.change( writer => {
  253. writer.setSelection( writer.createRange(
  254. writer.createPositionAt( firstParagraph, 5 ),
  255. writer.createPositionAt( firstParagraph, 7 )
  256. ) );
  257. } );
  258. expect( editor.commands.get( 'delete' ).isEnabled ).to.be.true;
  259. } );
  260. it( 'should be enabled for non-collapsed selection is equal to exception marker', () => {
  261. model.change( writer => {
  262. writer.setSelection( writer.createRange(
  263. writer.createPositionAt( firstParagraph, 4 ),
  264. writer.createPositionAt( firstParagraph, 7 )
  265. ) );
  266. } );
  267. expect( editor.commands.get( 'delete' ).isEnabled ).to.be.true;
  268. } );
  269. } );
  270. describe( 'forwardDelete', () => {
  271. beforeEach( () => {
  272. editor.commands.add( 'forwardDelete', buildFakeCommand( editor ) );
  273. model.change( writer => {
  274. writer.setSelection( firstParagraph, 'end' );
  275. } );
  276. } );
  277. it( 'should be disabled when caret is outside exception marker', () => {
  278. model.change( writer => {
  279. writer.setSelection( firstParagraph, 1 );
  280. } );
  281. expect( editor.commands.get( 'forwardDelete' ).isEnabled ).to.be.false;
  282. } );
  283. it( 'should be enabled when caret is inside exception marker (not touching boundaries)', () => {
  284. model.change( writer => {
  285. writer.setSelection( firstParagraph, 5 );
  286. } );
  287. expect( editor.commands.get( 'forwardDelete' ).isEnabled ).to.be.true;
  288. } );
  289. it( 'should be enabled when caret is inside exception marker (start boundary)', () => {
  290. model.change( writer => {
  291. writer.setSelection( firstParagraph, 4 );
  292. } );
  293. expect( editor.commands.get( 'forwardDelete' ).isEnabled ).to.be.true;
  294. } );
  295. it( 'should be disabled when caret is inside exception marker (end boundary)', () => {
  296. model.change( writer => {
  297. writer.setSelection( firstParagraph, 7 );
  298. } );
  299. expect( editor.commands.get( 'forwardDelete' ).isEnabled ).to.be.false;
  300. } );
  301. it( 'should be disabled for non-collapsed selection that expands over exception marker', () => {
  302. model.change( writer => {
  303. writer.setSelection( writer.createRange(
  304. writer.createPositionAt( firstParagraph, 0 ),
  305. writer.createPositionAt( firstParagraph, 5 )
  306. ) );
  307. } );
  308. expect( editor.commands.get( 'forwardDelete' ).isEnabled ).to.be.false;
  309. } );
  310. it( 'should be enabled for non-collapsed selection that is fully contained inside exception marker', () => {
  311. model.change( writer => {
  312. writer.setSelection( writer.createRange(
  313. writer.createPositionAt( firstParagraph, 5 ),
  314. writer.createPositionAt( firstParagraph, 6 )
  315. ) );
  316. } );
  317. expect( editor.commands.get( 'forwardDelete' ).isEnabled ).to.be.true;
  318. } );
  319. it( 'should be enabled for non-collapsed selection inside exception marker (start position on marker boundary)', () => {
  320. model.change( writer => {
  321. writer.setSelection( writer.createRange(
  322. writer.createPositionAt( firstParagraph, 4 ),
  323. writer.createPositionAt( firstParagraph, 6 )
  324. ) );
  325. } );
  326. expect( editor.commands.get( 'forwardDelete' ).isEnabled ).to.be.true;
  327. } );
  328. it( 'should be enabled for non-collapsed selection inside exception marker (end position on marker boundary)', () => {
  329. model.change( writer => {
  330. writer.setSelection( writer.createRange(
  331. writer.createPositionAt( firstParagraph, 5 ),
  332. writer.createPositionAt( firstParagraph, 7 )
  333. ) );
  334. } );
  335. expect( editor.commands.get( 'forwardDelete' ).isEnabled ).to.be.true;
  336. } );
  337. it( 'should be enabled for non-collapsed selection is equal to exception marker', () => {
  338. model.change( writer => {
  339. writer.setSelection( writer.createRange(
  340. writer.createPositionAt( firstParagraph, 4 ),
  341. writer.createPositionAt( firstParagraph, 7 )
  342. ) );
  343. } );
  344. expect( editor.commands.get( 'forwardDelete' ).isEnabled ).to.be.true;
  345. } );
  346. } );
  347. describe( 'other', () => {
  348. beforeEach( () => {
  349. editor.commands.add( 'other', buildFakeCommand( editor ) );
  350. model.change( writer => {
  351. writer.setSelection( firstParagraph, 'end' );
  352. } );
  353. } );
  354. it( 'should be disabled outside exception marker', () => {
  355. model.change( writer => {
  356. writer.setSelection( firstParagraph, 1 );
  357. } );
  358. expect( editor.commands.get( 'other' ).isEnabled ).to.be.false;
  359. } );
  360. it( 'should be disabled inside exception marker', () => {
  361. model.change( writer => {
  362. writer.setSelection( firstParagraph, 1 );
  363. } );
  364. model.change( writer => {
  365. writer.setSelection( firstParagraph, 5 );
  366. } );
  367. expect( editor.commands.get( 'other' ).isEnabled ).to.be.false;
  368. } );
  369. it( 'should be disabled when caret is inside exception marker (not touching boundaries)', () => {
  370. model.change( writer => {
  371. writer.setSelection( firstParagraph, 5 );
  372. } );
  373. expect( editor.commands.get( 'other' ).isEnabled ).to.be.false;
  374. } );
  375. it( 'should be disabled when caret is inside exception marker (start boundary)', () => {
  376. model.change( writer => {
  377. writer.setSelection( firstParagraph, 4 );
  378. } );
  379. expect( editor.commands.get( 'other' ).isEnabled ).to.be.false;
  380. } );
  381. it( 'should be disabled when caret is inside exception marker (end boundary)', () => {
  382. model.change( writer => {
  383. writer.setSelection( firstParagraph, 7 );
  384. } );
  385. expect( editor.commands.get( 'other' ).isEnabled ).to.be.false;
  386. } );
  387. it( 'should be disabled for non-collapsed selection that expands over exception marker', () => {
  388. model.change( writer => {
  389. writer.setSelection( writer.createRange(
  390. writer.createPositionAt( firstParagraph, 0 ),
  391. writer.createPositionAt( firstParagraph, 5 )
  392. ) );
  393. } );
  394. expect( editor.commands.get( 'other' ).isEnabled ).to.be.false;
  395. } );
  396. it( 'should be disabled for non-collapsed selection that is fully contained inside exception marker', () => {
  397. model.change( writer => {
  398. writer.setSelection( writer.createRange(
  399. writer.createPositionAt( firstParagraph, 5 ),
  400. writer.createPositionAt( firstParagraph, 6 )
  401. ) );
  402. } );
  403. expect( editor.commands.get( 'other' ).isEnabled ).to.be.false;
  404. } );
  405. it( 'should be disabled for non-collapsed selection inside exception marker (start position on marker boundary)', () => {
  406. model.change( writer => {
  407. writer.setSelection( writer.createRange(
  408. writer.createPositionAt( firstParagraph, 4 ),
  409. writer.createPositionAt( firstParagraph, 6 )
  410. ) );
  411. } );
  412. expect( editor.commands.get( 'other' ).isEnabled ).to.be.false;
  413. } );
  414. it( 'should be disabled for non-collapsed selection inside exception marker (end position on marker boundary)', () => {
  415. model.change( writer => {
  416. writer.setSelection( writer.createRange(
  417. writer.createPositionAt( firstParagraph, 5 ),
  418. writer.createPositionAt( firstParagraph, 7 )
  419. ) );
  420. } );
  421. expect( editor.commands.get( 'other' ).isEnabled ).to.be.false;
  422. } );
  423. it( 'should be disabled for non-collapsed selection is equal to exception marker', () => {
  424. model.change( writer => {
  425. writer.setSelection( writer.createRange(
  426. writer.createPositionAt( firstParagraph, 4 ),
  427. writer.createPositionAt( firstParagraph, 7 )
  428. ) );
  429. } );
  430. expect( editor.commands.get( 'other' ).isEnabled ).to.be.false;
  431. } );
  432. } );
  433. } );
  434. describe( 'commands integration', () => {
  435. let model, firstParagraph;
  436. beforeEach( async () => {
  437. editor = await VirtualTestEditor.create( { plugins: [ Paragraph, Typing, UndoEditing, RestrictedEditingEditing ] } );
  438. model = editor.model;
  439. setModelData( model, '<paragraph>[]foo bar baz</paragraph>' );
  440. firstParagraph = model.document.getRoot().getChild( 0 );
  441. model.change( writer => {
  442. writer.addMarker( 'restricted-editing-exception:1', {
  443. range: writer.createRange(
  444. writer.createPositionAt( firstParagraph, 4 ),
  445. writer.createPositionAt( firstParagraph, 7 ) ),
  446. usingOperation: true,
  447. affectsData: true
  448. } );
  449. } );
  450. } );
  451. afterEach( async () => {
  452. await editor.destroy();
  453. } );
  454. describe( 'delete + undo', () => {
  455. it( 'should be enabled after data change (no selection change event on undo)', () => {
  456. model.change( writer => {
  457. writer.setSelection( firstParagraph, 7 );
  458. } );
  459. editor.execute( 'delete' );
  460. editor.execute( 'undo' );
  461. expect( editor.commands.get( 'delete' ).isEnabled ).to.be.true;
  462. } );
  463. } );
  464. } );
  465. class FakeCommand extends Command {
  466. refresh() {
  467. this.isEnabled = true;
  468. }
  469. }
  470. function buildFakeCommand( editor ) {
  471. return new FakeCommand( editor );
  472. }
  473. } );