restrictededitingediting.js 29 KB

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