restrictededitingediting.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  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. it( 'should extend maker when typing on the marker boundary (end)', () => {
  159. setModelData( model, '<paragraph>foo bar[] baz</paragraph>' );
  160. const firstParagraph = model.document.getRoot().getChild( 0 );
  161. model.change( writer => {
  162. writer.addMarker( 'restricted-editing-exception:1', {
  163. range: writer.createRange( writer.createPositionAt( firstParagraph, 4 ), writer.createPositionAt( firstParagraph, 7 ) ),
  164. usingOperation: true,
  165. affectsData: true
  166. } );
  167. } );
  168. editor.execute( 'input', { text: 'X' } );
  169. assertEqualMarkup( getModelData( model ), '<paragraph>foo barX[] baz</paragraph>' );
  170. const markerRange = editor.model.markers.get( 'restricted-editing-exception:1' ).getRange();
  171. const expectedRange = model.createRange(
  172. model.createPositionAt( firstParagraph, 4 ),
  173. model.createPositionAt( firstParagraph, 8 )
  174. );
  175. expect( markerRange.isEqual( expectedRange ) ).to.be.true;
  176. } );
  177. it( 'should extend maker when typing on the marker boundary (start)', () => {
  178. setModelData( model, '<paragraph>foo []bar baz</paragraph>' );
  179. const firstParagraph = model.document.getRoot().getChild( 0 );
  180. model.change( writer => {
  181. writer.addMarker( 'restricted-editing-exception:1', {
  182. range: writer.createRange( writer.createPositionAt( firstParagraph, 4 ), writer.createPositionAt( firstParagraph, 7 ) ),
  183. usingOperation: true,
  184. affectsData: true
  185. } );
  186. } );
  187. editor.execute( 'input', { text: 'X' } );
  188. assertEqualMarkup( getModelData( model ), '<paragraph>foo X[]bar baz</paragraph>' );
  189. const markerRange = editor.model.markers.get( 'restricted-editing-exception:1' ).getRange();
  190. const expectedRange = model.createRange(
  191. model.createPositionAt( firstParagraph, 4 ),
  192. model.createPositionAt( firstParagraph, 8 )
  193. );
  194. expect( markerRange.isEqual( expectedRange ) ).to.be.true;
  195. } );
  196. } );
  197. describe( 'commands behavior', () => {
  198. let model, firstParagraph;
  199. beforeEach( async () => {
  200. editor = await VirtualTestEditor.create( { plugins: [ Paragraph, Typing, RestrictedEditingEditing ] } );
  201. model = editor.model;
  202. setModelData( model, '<paragraph>[]foo bar baz</paragraph>' );
  203. firstParagraph = model.document.getRoot().getChild( 0 );
  204. model.change( writer => {
  205. writer.addMarker( 'restricted-editing-exception:1', {
  206. range: writer.createRange(
  207. writer.createPositionAt( firstParagraph, 4 ),
  208. writer.createPositionAt( firstParagraph, 7 ) ),
  209. usingOperation: true,
  210. affectsData: true
  211. } );
  212. } );
  213. } );
  214. afterEach( async () => {
  215. await editor.destroy();
  216. } );
  217. describe( 'undo', () => {
  218. beforeEach( () => {
  219. editor.commands.add( 'undo', buildFakeCommand( editor ) );
  220. } );
  221. it( 'should be enabled outside exception marker', () => {
  222. model.change( writer => {
  223. writer.setSelection( firstParagraph, 1 );
  224. } );
  225. expect( editor.commands.get( 'undo' ).isEnabled ).to.be.true;
  226. } );
  227. it( 'should be enabled inside exception marker', () => {
  228. model.change( writer => {
  229. writer.setSelection( firstParagraph, 5 );
  230. } );
  231. expect( editor.commands.get( 'undo' ).isEnabled ).to.be.true;
  232. } );
  233. } );
  234. describe( 'redo', () => {
  235. beforeEach( () => {
  236. editor.commands.add( 'redo', buildFakeCommand( editor ) );
  237. } );
  238. it( 'should be enabled outside exception marker', () => {
  239. model.change( writer => {
  240. writer.setSelection( firstParagraph, 1 );
  241. } );
  242. expect( editor.commands.get( 'redo' ).isEnabled ).to.be.true;
  243. } );
  244. it( 'should be enabled inside exception marker', () => {
  245. model.change( writer => {
  246. writer.setSelection( firstParagraph, 5 );
  247. } );
  248. expect( editor.commands.get( 'redo' ).isEnabled ).to.be.true;
  249. } );
  250. } );
  251. const commandsEnabledOnWholeRange = [
  252. 'input', 'bold', 'link', 'italic'
  253. ];
  254. for ( const commandName of commandsEnabledOnWholeRange ) {
  255. describe( commandName, () => {
  256. beforeEach( () => {
  257. editor.commands.add( commandName, buildFakeCommand( editor ) );
  258. model.change( writer => {
  259. writer.setSelection( firstParagraph, 'end' );
  260. } );
  261. } );
  262. it( 'should be disabled when caret is outside exception marker', () => {
  263. model.change( writer => {
  264. writer.setSelection( firstParagraph, 1 );
  265. } );
  266. expect( editor.commands.get( commandName ).isEnabled ).to.be.false;
  267. } );
  268. it( 'should be enabled when caret is inside exception marker (not touching boundaries)', () => {
  269. model.change( writer => {
  270. writer.setSelection( firstParagraph, 5 );
  271. } );
  272. expect( editor.commands.get( commandName ).isEnabled ).to.be.true;
  273. } );
  274. it( 'should be disabled when caret is inside other marker', () => {
  275. model.change( writer => {
  276. writer.addMarker( 'foo-bar:1', {
  277. range: writer.createRange(
  278. writer.createPositionAt( firstParagraph, 0 ),
  279. writer.createPositionAt( firstParagraph, 3 ) ),
  280. usingOperation: true,
  281. affectsData: true
  282. } );
  283. writer.setSelection( firstParagraph, 1 );
  284. } );
  285. expect( editor.commands.get( commandName ).isEnabled ).to.be.false;
  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( commandName ).isEnabled ).to.be.true;
  292. } );
  293. it( 'should be enabled when caret is inside exception marker (end boundary)', () => {
  294. model.change( writer => {
  295. writer.setSelection( firstParagraph, 7 );
  296. } );
  297. expect( editor.commands.get( commandName ).isEnabled ).to.be.true;
  298. } );
  299. it( 'should be disabled for multi-range selection (collapsed ranges)', () => {
  300. model.change( writer => {
  301. writer.setSelection( [
  302. writer.createRange(
  303. writer.createPositionAt( firstParagraph, 5 )
  304. ),
  305. writer.createRange(
  306. writer.createPositionAt( firstParagraph, 9 )
  307. )
  308. ] );
  309. } );
  310. expect( editor.commands.get( commandName ).isEnabled ).to.be.false;
  311. } );
  312. it( 'should be disabled for non-collapsed selection that expands over exception marker', () => {
  313. model.change( writer => {
  314. writer.setSelection( writer.createRange(
  315. writer.createPositionAt( firstParagraph, 0 ),
  316. writer.createPositionAt( firstParagraph, 5 )
  317. ) );
  318. } );
  319. expect( editor.commands.get( commandName ).isEnabled ).to.be.false;
  320. } );
  321. it( 'should be enabled for non-collapsed selection that is fully contained inside exception marker', () => {
  322. model.change( writer => {
  323. writer.setSelection( writer.createRange(
  324. writer.createPositionAt( firstParagraph, 5 ),
  325. writer.createPositionAt( firstParagraph, 6 )
  326. ) );
  327. } );
  328. expect( editor.commands.get( commandName ).isEnabled ).to.be.true;
  329. } );
  330. it( 'should be enabled for non-collapsed selection inside exception marker (start position on marker boundary)', () => {
  331. model.change( writer => {
  332. writer.setSelection( writer.createRange(
  333. writer.createPositionAt( firstParagraph, 4 ),
  334. writer.createPositionAt( firstParagraph, 6 )
  335. ) );
  336. } );
  337. expect( editor.commands.get( commandName ).isEnabled ).to.be.true;
  338. } );
  339. it( 'should be enabled for non-collapsed selection inside exception marker (end position on marker boundary)', () => {
  340. model.change( writer => {
  341. writer.setSelection( writer.createRange(
  342. writer.createPositionAt( firstParagraph, 5 ),
  343. writer.createPositionAt( firstParagraph, 7 )
  344. ) );
  345. } );
  346. expect( editor.commands.get( commandName ).isEnabled ).to.be.true;
  347. } );
  348. it( 'should be enabled for non-collapsed selection is equal to exception marker', () => {
  349. model.change( writer => {
  350. writer.setSelection( writer.createRange(
  351. writer.createPositionAt( firstParagraph, 4 ),
  352. writer.createPositionAt( firstParagraph, 7 )
  353. ) );
  354. } );
  355. expect( editor.commands.get( commandName ).isEnabled ).to.be.true;
  356. } );
  357. it( 'should be disabled for non-collapsed selection with more then one range', () => {
  358. model.change( writer => {
  359. writer.setSelection( [
  360. writer.createRange(
  361. writer.createPositionAt( firstParagraph, 5 ),
  362. writer.createPositionAt( firstParagraph, 6 )
  363. ),
  364. writer.createRange(
  365. writer.createPositionAt( firstParagraph, 8 ),
  366. writer.createPositionAt( firstParagraph, 9 )
  367. )
  368. ] );
  369. } );
  370. expect( editor.commands.get( commandName ).isEnabled ).to.be.false;
  371. } );
  372. } );
  373. }
  374. describe( 'delete', () => {
  375. beforeEach( () => {
  376. editor.commands.add( 'delete', buildFakeCommand( editor ) );
  377. model.change( writer => {
  378. writer.setSelection( firstParagraph, 'end' );
  379. } );
  380. } );
  381. it( 'should be disabled when caret is outside exception marker', () => {
  382. model.change( writer => {
  383. writer.setSelection( firstParagraph, 1 );
  384. } );
  385. expect( editor.commands.get( 'delete' ).isEnabled ).to.be.false;
  386. } );
  387. it( 'should be enabled when caret is inside exception marker (not touching boundaries)', () => {
  388. model.change( writer => {
  389. writer.setSelection( firstParagraph, 5 );
  390. } );
  391. expect( editor.commands.get( 'delete' ).isEnabled ).to.be.true;
  392. } );
  393. it( 'should be disabled when caret is inside exception marker (start boundary)', () => {
  394. model.change( writer => {
  395. writer.setSelection( firstParagraph, 4 );
  396. } );
  397. expect( editor.commands.get( 'delete' ).isEnabled ).to.be.false;
  398. } );
  399. it( 'should be enabled when caret is inside exception marker (end boundary)', () => {
  400. model.change( writer => {
  401. writer.setSelection( firstParagraph, 7 );
  402. } );
  403. expect( editor.commands.get( 'delete' ).isEnabled ).to.be.true;
  404. } );
  405. it( 'should be disabled for non-collapsed selection that expands over exception marker', () => {
  406. model.change( writer => {
  407. writer.setSelection( writer.createRange(
  408. writer.createPositionAt( firstParagraph, 0 ),
  409. writer.createPositionAt( firstParagraph, 5 )
  410. ) );
  411. } );
  412. expect( editor.commands.get( 'delete' ).isEnabled ).to.be.false;
  413. } );
  414. it( 'should be enabled for non-collapsed selection that is fully contained inside exception marker', () => {
  415. model.change( writer => {
  416. writer.setSelection( writer.createRange(
  417. writer.createPositionAt( firstParagraph, 5 ),
  418. writer.createPositionAt( firstParagraph, 6 )
  419. ) );
  420. } );
  421. expect( editor.commands.get( 'delete' ).isEnabled ).to.be.true;
  422. } );
  423. it( 'should be enabled for non-collapsed selection inside exception marker (start position on marker boundary)', () => {
  424. model.change( writer => {
  425. writer.setSelection( writer.createRange(
  426. writer.createPositionAt( firstParagraph, 4 ),
  427. writer.createPositionAt( firstParagraph, 6 )
  428. ) );
  429. } );
  430. expect( editor.commands.get( 'delete' ).isEnabled ).to.be.true;
  431. } );
  432. it( 'should be enabled for non-collapsed selection inside exception marker (end position on marker boundary)', () => {
  433. model.change( writer => {
  434. writer.setSelection( writer.createRange(
  435. writer.createPositionAt( firstParagraph, 5 ),
  436. writer.createPositionAt( firstParagraph, 7 )
  437. ) );
  438. } );
  439. expect( editor.commands.get( 'delete' ).isEnabled ).to.be.true;
  440. } );
  441. it( 'should be enabled for non-collapsed selection is equal to exception marker', () => {
  442. model.change( writer => {
  443. writer.setSelection( writer.createRange(
  444. writer.createPositionAt( firstParagraph, 4 ),
  445. writer.createPositionAt( firstParagraph, 7 )
  446. ) );
  447. } );
  448. expect( editor.commands.get( 'delete' ).isEnabled ).to.be.true;
  449. } );
  450. } );
  451. describe( 'forwardDelete', () => {
  452. beforeEach( () => {
  453. editor.commands.add( 'forwardDelete', buildFakeCommand( editor ) );
  454. model.change( writer => {
  455. writer.setSelection( firstParagraph, 'end' );
  456. } );
  457. } );
  458. it( 'should be disabled when caret is outside exception marker', () => {
  459. model.change( writer => {
  460. writer.setSelection( firstParagraph, 1 );
  461. } );
  462. expect( editor.commands.get( 'forwardDelete' ).isEnabled ).to.be.false;
  463. } );
  464. it( 'should be enabled when caret is inside exception marker (not touching boundaries)', () => {
  465. model.change( writer => {
  466. writer.setSelection( firstParagraph, 5 );
  467. } );
  468. expect( editor.commands.get( 'forwardDelete' ).isEnabled ).to.be.true;
  469. } );
  470. it( 'should be enabled when caret is inside exception marker (start boundary)', () => {
  471. model.change( writer => {
  472. writer.setSelection( firstParagraph, 4 );
  473. } );
  474. expect( editor.commands.get( 'forwardDelete' ).isEnabled ).to.be.true;
  475. } );
  476. it( 'should be disabled when caret is inside exception marker (end boundary)', () => {
  477. model.change( writer => {
  478. writer.setSelection( firstParagraph, 7 );
  479. } );
  480. expect( editor.commands.get( 'forwardDelete' ).isEnabled ).to.be.false;
  481. } );
  482. it( 'should be disabled for non-collapsed selection that expands over exception marker', () => {
  483. model.change( writer => {
  484. writer.setSelection( writer.createRange(
  485. writer.createPositionAt( firstParagraph, 0 ),
  486. writer.createPositionAt( firstParagraph, 5 )
  487. ) );
  488. } );
  489. expect( editor.commands.get( 'forwardDelete' ).isEnabled ).to.be.false;
  490. } );
  491. it( 'should be enabled for non-collapsed selection that is fully contained inside exception marker', () => {
  492. model.change( writer => {
  493. writer.setSelection( writer.createRange(
  494. writer.createPositionAt( firstParagraph, 5 ),
  495. writer.createPositionAt( firstParagraph, 6 )
  496. ) );
  497. } );
  498. expect( editor.commands.get( 'forwardDelete' ).isEnabled ).to.be.true;
  499. } );
  500. it( 'should be enabled for non-collapsed selection inside exception marker (start position on marker boundary)', () => {
  501. model.change( writer => {
  502. writer.setSelection( writer.createRange(
  503. writer.createPositionAt( firstParagraph, 4 ),
  504. writer.createPositionAt( firstParagraph, 6 )
  505. ) );
  506. } );
  507. expect( editor.commands.get( 'forwardDelete' ).isEnabled ).to.be.true;
  508. } );
  509. it( 'should be enabled for non-collapsed selection inside exception marker (end position on marker boundary)', () => {
  510. model.change( writer => {
  511. writer.setSelection( writer.createRange(
  512. writer.createPositionAt( firstParagraph, 5 ),
  513. writer.createPositionAt( firstParagraph, 7 )
  514. ) );
  515. } );
  516. expect( editor.commands.get( 'forwardDelete' ).isEnabled ).to.be.true;
  517. } );
  518. it( 'should be enabled for non-collapsed selection is equal to exception marker', () => {
  519. model.change( writer => {
  520. writer.setSelection( writer.createRange(
  521. writer.createPositionAt( firstParagraph, 4 ),
  522. writer.createPositionAt( firstParagraph, 7 )
  523. ) );
  524. } );
  525. expect( editor.commands.get( 'forwardDelete' ).isEnabled ).to.be.true;
  526. } );
  527. } );
  528. describe( 'other', () => {
  529. beforeEach( () => {
  530. editor.commands.add( 'other', buildFakeCommand( editor ) );
  531. model.change( writer => {
  532. writer.setSelection( firstParagraph, 'end' );
  533. } );
  534. } );
  535. it( 'should be disabled outside exception marker', () => {
  536. model.change( writer => {
  537. writer.setSelection( firstParagraph, 1 );
  538. } );
  539. expect( editor.commands.get( 'other' ).isEnabled ).to.be.false;
  540. } );
  541. it( 'should be disabled inside exception marker', () => {
  542. model.change( writer => {
  543. writer.setSelection( firstParagraph, 1 );
  544. } );
  545. model.change( writer => {
  546. writer.setSelection( firstParagraph, 5 );
  547. } );
  548. expect( editor.commands.get( 'other' ).isEnabled ).to.be.false;
  549. } );
  550. it( 'should be disabled when caret is inside exception marker (not touching boundaries)', () => {
  551. model.change( writer => {
  552. writer.setSelection( firstParagraph, 5 );
  553. } );
  554. expect( editor.commands.get( 'other' ).isEnabled ).to.be.false;
  555. } );
  556. it( 'should be disabled when caret is inside exception marker (start boundary)', () => {
  557. model.change( writer => {
  558. writer.setSelection( firstParagraph, 4 );
  559. } );
  560. expect( editor.commands.get( 'other' ).isEnabled ).to.be.false;
  561. } );
  562. it( 'should be disabled when caret is inside exception marker (end boundary)', () => {
  563. model.change( writer => {
  564. writer.setSelection( firstParagraph, 7 );
  565. } );
  566. expect( editor.commands.get( 'other' ).isEnabled ).to.be.false;
  567. } );
  568. it( 'should be disabled for non-collapsed selection that expands over exception marker', () => {
  569. model.change( writer => {
  570. writer.setSelection( writer.createRange(
  571. writer.createPositionAt( firstParagraph, 0 ),
  572. writer.createPositionAt( firstParagraph, 5 )
  573. ) );
  574. } );
  575. expect( editor.commands.get( 'other' ).isEnabled ).to.be.false;
  576. } );
  577. it( 'should be disabled for non-collapsed selection that is fully contained inside exception marker', () => {
  578. model.change( writer => {
  579. writer.setSelection( writer.createRange(
  580. writer.createPositionAt( firstParagraph, 5 ),
  581. writer.createPositionAt( firstParagraph, 6 )
  582. ) );
  583. } );
  584. expect( editor.commands.get( 'other' ).isEnabled ).to.be.false;
  585. } );
  586. it( 'should be disabled for non-collapsed selection inside exception marker (start position on marker boundary)', () => {
  587. model.change( writer => {
  588. writer.setSelection( writer.createRange(
  589. writer.createPositionAt( firstParagraph, 4 ),
  590. writer.createPositionAt( firstParagraph, 6 )
  591. ) );
  592. } );
  593. expect( editor.commands.get( 'other' ).isEnabled ).to.be.false;
  594. } );
  595. it( 'should be disabled for non-collapsed selection inside exception marker (end position on marker boundary)', () => {
  596. model.change( writer => {
  597. writer.setSelection( writer.createRange(
  598. writer.createPositionAt( firstParagraph, 5 ),
  599. writer.createPositionAt( firstParagraph, 7 )
  600. ) );
  601. } );
  602. expect( editor.commands.get( 'other' ).isEnabled ).to.be.false;
  603. } );
  604. it( 'should be disabled for non-collapsed selection is equal to exception marker', () => {
  605. model.change( writer => {
  606. writer.setSelection( writer.createRange(
  607. writer.createPositionAt( firstParagraph, 4 ),
  608. writer.createPositionAt( firstParagraph, 7 )
  609. ) );
  610. } );
  611. expect( editor.commands.get( 'other' ).isEnabled ).to.be.false;
  612. } );
  613. } );
  614. } );
  615. describe( 'commands integration', () => {
  616. let model, firstParagraph;
  617. beforeEach( async () => {
  618. editor = await VirtualTestEditor.create( { plugins: [ Paragraph, Typing, UndoEditing, RestrictedEditingEditing ] } );
  619. model = editor.model;
  620. setModelData( model, '<paragraph>[]foo bar baz</paragraph>' );
  621. firstParagraph = model.document.getRoot().getChild( 0 );
  622. model.change( writer => {
  623. writer.addMarker( 'restricted-editing-exception:1', {
  624. range: writer.createRange(
  625. writer.createPositionAt( firstParagraph, 4 ),
  626. writer.createPositionAt( firstParagraph, 7 ) ),
  627. usingOperation: true,
  628. affectsData: true
  629. } );
  630. } );
  631. } );
  632. afterEach( async () => {
  633. await editor.destroy();
  634. } );
  635. describe( 'delete + undo', () => {
  636. it( 'should be enabled after data change (no selection change event on undo)', () => {
  637. model.change( writer => {
  638. writer.setSelection( firstParagraph, 7 );
  639. } );
  640. editor.execute( 'delete' );
  641. editor.execute( 'undo' );
  642. expect( editor.commands.get( 'delete' ).isEnabled ).to.be.true;
  643. } );
  644. } );
  645. } );
  646. class FakeCommand extends Command {
  647. refresh() {
  648. this.isEnabled = true;
  649. }
  650. }
  651. function buildFakeCommand( editor ) {
  652. return new FakeCommand( editor );
  653. }
  654. } );