8
0

restrictededitingediting.js 24 KB

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