8
0

restrictededitingediting.js 26 KB

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