restrictedediting-commands.js 25 KB

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