8
0

restrictededitingediting.js 25 KB

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