restrictededitingediting.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  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 enabled when caret is inside exception marker (start boundary)', () => {
  237. model.change( writer => {
  238. writer.setSelection( firstParagraph, 4 );
  239. } );
  240. expect( editor.commands.get( commandName ).isEnabled ).to.be.true;
  241. } );
  242. it( 'should be enabled when caret is inside exception marker (end boundary)', () => {
  243. model.change( writer => {
  244. writer.setSelection( firstParagraph, 7 );
  245. } );
  246. expect( editor.commands.get( commandName ).isEnabled ).to.be.true;
  247. } );
  248. it( 'should be disabled for multi-range selection (collapsed ranges)', () => {
  249. model.change( writer => {
  250. writer.setSelection( [
  251. writer.createRange(
  252. writer.createPositionAt( firstParagraph, 5 )
  253. ),
  254. writer.createRange(
  255. writer.createPositionAt( firstParagraph, 9 )
  256. )
  257. ] );
  258. } );
  259. expect( editor.commands.get( commandName ).isEnabled ).to.be.false;
  260. } );
  261. it( 'should be disabled for non-collapsed selection that expands over exception marker', () => {
  262. model.change( writer => {
  263. writer.setSelection( writer.createRange(
  264. writer.createPositionAt( firstParagraph, 0 ),
  265. writer.createPositionAt( firstParagraph, 5 )
  266. ) );
  267. } );
  268. expect( editor.commands.get( commandName ).isEnabled ).to.be.false;
  269. } );
  270. it( 'should be enabled for non-collapsed selection that is fully contained inside exception marker', () => {
  271. model.change( writer => {
  272. writer.setSelection( writer.createRange(
  273. writer.createPositionAt( firstParagraph, 5 ),
  274. writer.createPositionAt( firstParagraph, 6 )
  275. ) );
  276. } );
  277. expect( editor.commands.get( commandName ).isEnabled ).to.be.true;
  278. } );
  279. it( 'should be enabled for non-collapsed selection inside exception marker (start position on marker boundary)', () => {
  280. model.change( writer => {
  281. writer.setSelection( writer.createRange(
  282. writer.createPositionAt( firstParagraph, 4 ),
  283. writer.createPositionAt( firstParagraph, 6 )
  284. ) );
  285. } );
  286. expect( editor.commands.get( commandName ).isEnabled ).to.be.true;
  287. } );
  288. it( 'should be enabled for non-collapsed selection inside exception marker (end position on marker boundary)', () => {
  289. model.change( writer => {
  290. writer.setSelection( writer.createRange(
  291. writer.createPositionAt( firstParagraph, 5 ),
  292. writer.createPositionAt( firstParagraph, 7 )
  293. ) );
  294. } );
  295. expect( editor.commands.get( commandName ).isEnabled ).to.be.true;
  296. } );
  297. it( 'should be enabled for non-collapsed selection is equal to exception marker', () => {
  298. model.change( writer => {
  299. writer.setSelection( writer.createRange(
  300. writer.createPositionAt( firstParagraph, 4 ),
  301. writer.createPositionAt( firstParagraph, 7 )
  302. ) );
  303. } );
  304. expect( editor.commands.get( commandName ).isEnabled ).to.be.true;
  305. } );
  306. it( 'should be disabled for non-collapsed selection with more then one range', () => {
  307. model.change( writer => {
  308. writer.setSelection( [
  309. writer.createRange(
  310. writer.createPositionAt( firstParagraph, 5 ),
  311. writer.createPositionAt( firstParagraph, 6 )
  312. ),
  313. writer.createRange(
  314. writer.createPositionAt( firstParagraph, 8 ),
  315. writer.createPositionAt( firstParagraph, 9 )
  316. )
  317. ] );
  318. } );
  319. expect( editor.commands.get( commandName ).isEnabled ).to.be.false;
  320. } );
  321. } );
  322. }
  323. describe( 'delete', () => {
  324. beforeEach( () => {
  325. editor.commands.add( 'delete', buildFakeCommand( editor ) );
  326. model.change( writer => {
  327. writer.setSelection( firstParagraph, 'end' );
  328. } );
  329. } );
  330. it( 'should be disabled when caret is outside exception marker', () => {
  331. model.change( writer => {
  332. writer.setSelection( firstParagraph, 1 );
  333. } );
  334. expect( editor.commands.get( 'delete' ).isEnabled ).to.be.false;
  335. } );
  336. it( 'should be enabled when caret is inside exception marker (not touching boundaries)', () => {
  337. model.change( writer => {
  338. writer.setSelection( firstParagraph, 5 );
  339. } );
  340. expect( editor.commands.get( 'delete' ).isEnabled ).to.be.true;
  341. } );
  342. it( 'should be disabled when caret is inside exception marker (start boundary)', () => {
  343. model.change( writer => {
  344. writer.setSelection( firstParagraph, 4 );
  345. } );
  346. expect( editor.commands.get( 'delete' ).isEnabled ).to.be.false;
  347. } );
  348. it( 'should be enabled when caret is inside exception marker (end boundary)', () => {
  349. model.change( writer => {
  350. writer.setSelection( firstParagraph, 7 );
  351. } );
  352. expect( editor.commands.get( 'delete' ).isEnabled ).to.be.true;
  353. } );
  354. it( 'should be disabled for non-collapsed selection that expands over exception marker', () => {
  355. model.change( writer => {
  356. writer.setSelection( writer.createRange(
  357. writer.createPositionAt( firstParagraph, 0 ),
  358. writer.createPositionAt( firstParagraph, 5 )
  359. ) );
  360. } );
  361. expect( editor.commands.get( 'delete' ).isEnabled ).to.be.false;
  362. } );
  363. it( 'should be enabled for non-collapsed selection that is fully contained inside exception marker', () => {
  364. model.change( writer => {
  365. writer.setSelection( writer.createRange(
  366. writer.createPositionAt( firstParagraph, 5 ),
  367. writer.createPositionAt( firstParagraph, 6 )
  368. ) );
  369. } );
  370. expect( editor.commands.get( 'delete' ).isEnabled ).to.be.true;
  371. } );
  372. it( 'should be enabled for non-collapsed selection inside exception marker (start position on marker boundary)', () => {
  373. model.change( writer => {
  374. writer.setSelection( writer.createRange(
  375. writer.createPositionAt( firstParagraph, 4 ),
  376. writer.createPositionAt( firstParagraph, 6 )
  377. ) );
  378. } );
  379. expect( editor.commands.get( 'delete' ).isEnabled ).to.be.true;
  380. } );
  381. it( 'should be enabled for non-collapsed selection inside exception marker (end position on marker boundary)', () => {
  382. model.change( writer => {
  383. writer.setSelection( writer.createRange(
  384. writer.createPositionAt( firstParagraph, 5 ),
  385. writer.createPositionAt( firstParagraph, 7 )
  386. ) );
  387. } );
  388. expect( editor.commands.get( 'delete' ).isEnabled ).to.be.true;
  389. } );
  390. it( 'should be enabled for non-collapsed selection is equal to exception marker', () => {
  391. model.change( writer => {
  392. writer.setSelection( writer.createRange(
  393. writer.createPositionAt( firstParagraph, 4 ),
  394. writer.createPositionAt( firstParagraph, 7 )
  395. ) );
  396. } );
  397. expect( editor.commands.get( 'delete' ).isEnabled ).to.be.true;
  398. } );
  399. } );
  400. describe( 'forwardDelete', () => {
  401. beforeEach( () => {
  402. editor.commands.add( 'forwardDelete', buildFakeCommand( editor ) );
  403. model.change( writer => {
  404. writer.setSelection( firstParagraph, 'end' );
  405. } );
  406. } );
  407. it( 'should be disabled when caret is outside exception marker', () => {
  408. model.change( writer => {
  409. writer.setSelection( firstParagraph, 1 );
  410. } );
  411. expect( editor.commands.get( 'forwardDelete' ).isEnabled ).to.be.false;
  412. } );
  413. it( 'should be enabled when caret is inside exception marker (not touching boundaries)', () => {
  414. model.change( writer => {
  415. writer.setSelection( firstParagraph, 5 );
  416. } );
  417. expect( editor.commands.get( 'forwardDelete' ).isEnabled ).to.be.true;
  418. } );
  419. it( 'should be enabled when caret is inside exception marker (start boundary)', () => {
  420. model.change( writer => {
  421. writer.setSelection( firstParagraph, 4 );
  422. } );
  423. expect( editor.commands.get( 'forwardDelete' ).isEnabled ).to.be.true;
  424. } );
  425. it( 'should be disabled when caret is inside exception marker (end boundary)', () => {
  426. model.change( writer => {
  427. writer.setSelection( firstParagraph, 7 );
  428. } );
  429. expect( editor.commands.get( 'forwardDelete' ).isEnabled ).to.be.false;
  430. } );
  431. it( 'should be disabled for non-collapsed selection that expands over exception marker', () => {
  432. model.change( writer => {
  433. writer.setSelection( writer.createRange(
  434. writer.createPositionAt( firstParagraph, 0 ),
  435. writer.createPositionAt( firstParagraph, 5 )
  436. ) );
  437. } );
  438. expect( editor.commands.get( 'forwardDelete' ).isEnabled ).to.be.false;
  439. } );
  440. it( 'should be enabled for non-collapsed selection that is fully contained inside exception marker', () => {
  441. model.change( writer => {
  442. writer.setSelection( writer.createRange(
  443. writer.createPositionAt( firstParagraph, 5 ),
  444. writer.createPositionAt( firstParagraph, 6 )
  445. ) );
  446. } );
  447. expect( editor.commands.get( 'forwardDelete' ).isEnabled ).to.be.true;
  448. } );
  449. it( 'should be enabled for non-collapsed selection inside exception marker (start position on marker boundary)', () => {
  450. model.change( writer => {
  451. writer.setSelection( writer.createRange(
  452. writer.createPositionAt( firstParagraph, 4 ),
  453. writer.createPositionAt( firstParagraph, 6 )
  454. ) );
  455. } );
  456. expect( editor.commands.get( 'forwardDelete' ).isEnabled ).to.be.true;
  457. } );
  458. it( 'should be enabled for non-collapsed selection inside exception marker (end position on marker boundary)', () => {
  459. model.change( writer => {
  460. writer.setSelection( writer.createRange(
  461. writer.createPositionAt( firstParagraph, 5 ),
  462. writer.createPositionAt( firstParagraph, 7 )
  463. ) );
  464. } );
  465. expect( editor.commands.get( 'forwardDelete' ).isEnabled ).to.be.true;
  466. } );
  467. it( 'should be enabled for non-collapsed selection is equal to exception marker', () => {
  468. model.change( writer => {
  469. writer.setSelection( writer.createRange(
  470. writer.createPositionAt( firstParagraph, 4 ),
  471. writer.createPositionAt( firstParagraph, 7 )
  472. ) );
  473. } );
  474. expect( editor.commands.get( 'forwardDelete' ).isEnabled ).to.be.true;
  475. } );
  476. } );
  477. describe( 'other', () => {
  478. beforeEach( () => {
  479. editor.commands.add( 'other', buildFakeCommand( editor ) );
  480. model.change( writer => {
  481. writer.setSelection( firstParagraph, 'end' );
  482. } );
  483. } );
  484. it( 'should be disabled outside exception marker', () => {
  485. model.change( writer => {
  486. writer.setSelection( firstParagraph, 1 );
  487. } );
  488. expect( editor.commands.get( 'other' ).isEnabled ).to.be.false;
  489. } );
  490. it( 'should be disabled inside exception marker', () => {
  491. model.change( writer => {
  492. writer.setSelection( firstParagraph, 1 );
  493. } );
  494. model.change( writer => {
  495. writer.setSelection( firstParagraph, 5 );
  496. } );
  497. expect( editor.commands.get( 'other' ).isEnabled ).to.be.false;
  498. } );
  499. it( 'should be disabled when caret is inside exception marker (not touching boundaries)', () => {
  500. model.change( writer => {
  501. writer.setSelection( firstParagraph, 5 );
  502. } );
  503. expect( editor.commands.get( 'other' ).isEnabled ).to.be.false;
  504. } );
  505. it( 'should be disabled when caret is inside exception marker (start boundary)', () => {
  506. model.change( writer => {
  507. writer.setSelection( firstParagraph, 4 );
  508. } );
  509. expect( editor.commands.get( 'other' ).isEnabled ).to.be.false;
  510. } );
  511. it( 'should be disabled when caret is inside exception marker (end boundary)', () => {
  512. model.change( writer => {
  513. writer.setSelection( firstParagraph, 7 );
  514. } );
  515. expect( editor.commands.get( 'other' ).isEnabled ).to.be.false;
  516. } );
  517. it( 'should be disabled for non-collapsed selection that expands over exception marker', () => {
  518. model.change( writer => {
  519. writer.setSelection( writer.createRange(
  520. writer.createPositionAt( firstParagraph, 0 ),
  521. writer.createPositionAt( firstParagraph, 5 )
  522. ) );
  523. } );
  524. expect( editor.commands.get( 'other' ).isEnabled ).to.be.false;
  525. } );
  526. it( 'should be disabled for non-collapsed selection that is fully contained inside exception marker', () => {
  527. model.change( writer => {
  528. writer.setSelection( writer.createRange(
  529. writer.createPositionAt( firstParagraph, 5 ),
  530. writer.createPositionAt( firstParagraph, 6 )
  531. ) );
  532. } );
  533. expect( editor.commands.get( 'other' ).isEnabled ).to.be.false;
  534. } );
  535. it( 'should be disabled for non-collapsed selection inside exception marker (start position on marker boundary)', () => {
  536. model.change( writer => {
  537. writer.setSelection( writer.createRange(
  538. writer.createPositionAt( firstParagraph, 4 ),
  539. writer.createPositionAt( firstParagraph, 6 )
  540. ) );
  541. } );
  542. expect( editor.commands.get( 'other' ).isEnabled ).to.be.false;
  543. } );
  544. it( 'should be disabled for non-collapsed selection inside exception marker (end position on marker boundary)', () => {
  545. model.change( writer => {
  546. writer.setSelection( writer.createRange(
  547. writer.createPositionAt( firstParagraph, 5 ),
  548. writer.createPositionAt( firstParagraph, 7 )
  549. ) );
  550. } );
  551. expect( editor.commands.get( 'other' ).isEnabled ).to.be.false;
  552. } );
  553. it( 'should be disabled for non-collapsed selection is equal to exception marker', () => {
  554. model.change( writer => {
  555. writer.setSelection( writer.createRange(
  556. writer.createPositionAt( firstParagraph, 4 ),
  557. writer.createPositionAt( firstParagraph, 7 )
  558. ) );
  559. } );
  560. expect( editor.commands.get( 'other' ).isEnabled ).to.be.false;
  561. } );
  562. } );
  563. } );
  564. describe( 'commands integration', () => {
  565. let model, firstParagraph;
  566. beforeEach( async () => {
  567. editor = await VirtualTestEditor.create( { plugins: [ Paragraph, Typing, UndoEditing, RestrictedEditingEditing ] } );
  568. model = editor.model;
  569. setModelData( model, '<paragraph>[]foo bar baz</paragraph>' );
  570. firstParagraph = model.document.getRoot().getChild( 0 );
  571. model.change( writer => {
  572. writer.addMarker( 'restricted-editing-exception:1', {
  573. range: writer.createRange(
  574. writer.createPositionAt( firstParagraph, 4 ),
  575. writer.createPositionAt( firstParagraph, 7 ) ),
  576. usingOperation: true,
  577. affectsData: true
  578. } );
  579. } );
  580. } );
  581. afterEach( async () => {
  582. await editor.destroy();
  583. } );
  584. describe( 'delete + undo', () => {
  585. it( 'should be enabled after data change (no selection change event on undo)', () => {
  586. model.change( writer => {
  587. writer.setSelection( firstParagraph, 7 );
  588. } );
  589. editor.execute( 'delete' );
  590. editor.execute( 'undo' );
  591. expect( editor.commands.get( 'delete' ).isEnabled ).to.be.true;
  592. } );
  593. } );
  594. } );
  595. class FakeCommand extends Command {
  596. refresh() {
  597. this.isEnabled = true;
  598. }
  599. }
  600. function buildFakeCommand( editor ) {
  601. return new FakeCommand( editor );
  602. }
  603. } );