8
0

restrictededitingediting.js 24 KB

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