restrictededitingediting.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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. describe( 'RestrictedEditingEditing', () => {
  15. let editor;
  16. testUtils.createSinonSandbox();
  17. describe( 'plugin', () => {
  18. beforeEach( async () => {
  19. editor = await VirtualTestEditor.create( { plugins: [ RestrictedEditingEditing ] } );
  20. } );
  21. afterEach( async () => {
  22. await editor.destroy();
  23. } );
  24. it( 'should be named', () => {
  25. expect( RestrictedEditingEditing.pluginName ).to.equal( 'RestrictedEditingEditing' );
  26. } );
  27. it( 'should be loaded', () => {
  28. expect( editor.plugins.get( RestrictedEditingEditing ) ).to.be.instanceOf( RestrictedEditingEditing );
  29. } );
  30. } );
  31. describe( 'conversion', () => {
  32. let model;
  33. beforeEach( async () => {
  34. editor = await VirtualTestEditor.create( { plugins: [ Paragraph, RestrictedEditingEditing ] } );
  35. model = editor.model;
  36. } );
  37. afterEach( () => {
  38. return editor.destroy();
  39. } );
  40. describe( 'upcast', () => {
  41. it( 'should convert <span class="ck-restricted-editing-exception"> to marker', () => {
  42. editor.setData( '<p>foo <span class="ck-restricted-editing-exception">bar</span> baz</p>' );
  43. expect( model.markers.has( 'restricted-editing-exception:1' ) ).to.be.true;
  44. const marker = model.markers.get( 'restricted-editing-exception:1' );
  45. expect( marker.getStart().path ).to.deep.equal( [ 0, 4 ] );
  46. expect( marker.getEnd().path ).to.deep.equal( [ 0, 7 ] );
  47. } );
  48. it( 'should convert multiple <span class="ck-restricted-editing-exception">', () => {
  49. editor.setData(
  50. '<p>foo <span class="ck-restricted-editing-exception">bar</span> baz</p>' +
  51. '<p>ABCDEF<span class="ck-restricted-editing-exception">GHIJK</span>LMNOPQRST</p>'
  52. );
  53. expect( model.markers.has( 'restricted-editing-exception:1' ) ).to.be.true;
  54. expect( model.markers.has( 'restricted-editing-exception:2' ) ).to.be.true;
  55. // Data for the first marker is the same as in previous tests so no need to test it again.
  56. const secondMarker = model.markers.get( 'restricted-editing-exception:2' );
  57. expect( secondMarker.getStart().path ).to.deep.equal( [ 1, 6 ] );
  58. expect( secondMarker.getEnd().path ).to.deep.equal( [ 1, 11 ] );
  59. } );
  60. it( 'should not convert other <span> elements', () => {
  61. editor.setData( '<p>foo <span class="foo bar">bar</span> baz</p>' );
  62. expect( model.markers.has( 'restricted-editing-exception:1' ) ).to.be.false;
  63. } );
  64. } );
  65. describe( 'downcast', () => {
  66. it( 'should convert model marker to <span>', () => {
  67. setModelData( model, '<paragraph>foo bar baz</paragraph>' );
  68. const paragraph = model.document.getRoot().getChild( 0 );
  69. model.change( writer => {
  70. writer.addMarker( 'restricted-editing-exception:1', {
  71. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  72. usingOperation: true,
  73. affectsData: true
  74. } );
  75. } );
  76. const expectedView = '<p>foo <span class="ck-restricted-editing-exception">bar</span> baz</p>';
  77. expect( editor.getData() ).to.equal( expectedView );
  78. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( expectedView );
  79. } );
  80. it( 'converted <span> should be the outermost attribute element', () => {
  81. editor.conversion.for( 'downcast' ).attributeToElement( { model: 'bold', view: 'b' } );
  82. setModelData( model, '<paragraph><$text bold="true">foo bar baz</$text></paragraph>' );
  83. const paragraph = model.document.getRoot().getChild( 0 );
  84. model.change( writer => {
  85. writer.addMarker( 'restricted-editing-exception:1', {
  86. range: writer.createRange( writer.createPositionAt( paragraph, 0 ), writer.createPositionAt( paragraph, 'end' ) ),
  87. usingOperation: true,
  88. affectsData: true
  89. } );
  90. } );
  91. const expectedView = '<p><span class="ck-restricted-editing-exception"><b>foo bar baz</b></span></p>';
  92. expect( editor.getData() ).to.equal( expectedView );
  93. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( expectedView );
  94. } );
  95. } );
  96. } );
  97. describe( 'editing behavior', () => {
  98. let model;
  99. beforeEach( async () => {
  100. editor = await VirtualTestEditor.create( { plugins: [ Paragraph, Typing, RestrictedEditingEditing ] } );
  101. model = editor.model;
  102. } );
  103. afterEach( async () => {
  104. await editor.destroy();
  105. } );
  106. it( 'should keep markers in the view when editable region is edited', () => {
  107. setModelData( model,
  108. '<paragraph>foo bar baz</paragraph>' +
  109. '<paragraph>xxx y[]yy zzz</paragraph>'
  110. );
  111. const firstParagraph = model.document.getRoot().getChild( 0 );
  112. const secondParagraph = model.document.getRoot().getChild( 1 );
  113. model.change( writer => {
  114. writer.addMarker( 'restricted-editing-exception:1', {
  115. range: writer.createRange( writer.createPositionAt( firstParagraph, 4 ), writer.createPositionAt( firstParagraph, 7 ) ),
  116. usingOperation: true,
  117. affectsData: true
  118. } );
  119. writer.addMarker( 'restricted-editing-exception:2', {
  120. range: writer.createRange(
  121. writer.createPositionAt( secondParagraph, 4 ),
  122. writer.createPositionAt( secondParagraph, 7 )
  123. ),
  124. usingOperation: true,
  125. affectsData: true
  126. } );
  127. } );
  128. model.change( writer => {
  129. model.insertContent( writer.createText( 'R', model.document.selection.getAttributes() ) );
  130. } );
  131. const expectedView = '<p>foo <span class="ck-restricted-editing-exception">bar</span> baz</p>' +
  132. '<p>xxx <span class="ck-restricted-editing-exception">yRyy</span> zzz</p>';
  133. expect( editor.getData() ).to.equal( expectedView );
  134. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( expectedView );
  135. } );
  136. it( 'should block user typing outside exception markers', () => {
  137. setModelData( model, '<paragraph>foo []bar baz</paragraph>' );
  138. editor.execute( 'input', { text: 'X' } );
  139. assertEqualMarkup( getModelData( model ), '<paragraph>foo []bar baz</paragraph>' );
  140. } );
  141. it( 'should not block user typing inside exception marker', () => {
  142. setModelData( model, '<paragraph>[]foo bar baz</paragraph>' );
  143. const firstParagraph = model.document.getRoot().getChild( 0 );
  144. model.change( writer => {
  145. writer.addMarker( 'restricted-editing-exception:1', {
  146. range: writer.createRange( writer.createPositionAt( firstParagraph, 4 ), writer.createPositionAt( firstParagraph, 7 ) ),
  147. usingOperation: true,
  148. affectsData: true
  149. } );
  150. } );
  151. model.change( writer => {
  152. writer.setSelection( firstParagraph, 5 );
  153. } );
  154. editor.execute( 'input', { text: 'X' } );
  155. assertEqualMarkup( getModelData( model ), '<paragraph>foo bX[]ar baz</paragraph>' );
  156. } );
  157. } );
  158. describe( 'commands integration', () => {
  159. let model, firstParagraph;
  160. beforeEach( async () => {
  161. editor = await VirtualTestEditor.create( { plugins: [ Paragraph, Typing, RestrictedEditingEditing ] } );
  162. model = editor.model;
  163. setModelData( model, '<paragraph>[]foo bar baz</paragraph>' );
  164. firstParagraph = model.document.getRoot().getChild( 0 );
  165. model.change( writer => {
  166. writer.addMarker( 'restricted-editing-exception:1', {
  167. range: writer.createRange(
  168. writer.createPositionAt( firstParagraph, 4 ),
  169. writer.createPositionAt( firstParagraph, 7 ) ),
  170. usingOperation: true,
  171. affectsData: true
  172. } );
  173. } );
  174. } );
  175. afterEach( async () => {
  176. await editor.destroy();
  177. } );
  178. describe( 'undo', () => {
  179. beforeEach( () => {
  180. editor.commands.add( 'undo', buildFakeCommand( editor ) );
  181. } );
  182. it( 'should be enabled outside exception marker', () => {
  183. model.change( writer => {
  184. writer.setSelection( firstParagraph, 1 );
  185. } );
  186. expect( editor.commands.get( 'undo' ).isEnabled ).to.be.true;
  187. } );
  188. it( 'should be enabled inside exception marker', () => {
  189. model.change( writer => {
  190. writer.setSelection( firstParagraph, 5 );
  191. } );
  192. expect( editor.commands.get( 'undo' ).isEnabled ).to.be.true;
  193. } );
  194. } );
  195. describe( 'redo', () => {
  196. beforeEach( () => {
  197. editor.commands.add( 'redo', buildFakeCommand( editor ) );
  198. } );
  199. it( 'should be enabled outside exception marker', () => {
  200. model.change( writer => {
  201. writer.setSelection( firstParagraph, 1 );
  202. } );
  203. expect( editor.commands.get( 'redo' ).isEnabled ).to.be.true;
  204. } );
  205. it( 'should be enabled inside exception marker', () => {
  206. model.change( writer => {
  207. writer.setSelection( firstParagraph, 5 );
  208. } );
  209. expect( editor.commands.get( 'redo' ).isEnabled ).to.be.true;
  210. } );
  211. } );
  212. describe( 'input', () => {
  213. beforeEach( () => {
  214. editor.commands.add( 'input', buildFakeCommand( editor ) );
  215. } );
  216. it( 'should be disabled when caret is outside exception marker', () => {
  217. model.change( writer => {
  218. writer.setSelection( firstParagraph, 1 );
  219. } );
  220. expect( editor.commands.get( 'input' ).isEnabled ).to.be.false;
  221. } );
  222. it( 'should be enabled when caret is inside exception marker (not touching boundaries)', () => {
  223. model.change( writer => {
  224. writer.setSelection( firstParagraph, 5 );
  225. } );
  226. expect( editor.commands.get( 'input' ).isEnabled ).to.be.true;
  227. } );
  228. it( 'should be enabled when caret is inside exception marker (start boundary)', () => {
  229. model.change( writer => {
  230. writer.setSelection( firstParagraph, 4 );
  231. } );
  232. expect( editor.commands.get( 'input' ).isEnabled ).to.be.true;
  233. } );
  234. it( 'should be enabled when caret is inside exception marker (end boundary)', () => {
  235. model.change( writer => {
  236. writer.setSelection( firstParagraph, 7 );
  237. } );
  238. expect( editor.commands.get( 'input' ).isEnabled ).to.be.true;
  239. } );
  240. it( 'should be disabled for non-collapsed selection that expands over exception marker', () => {
  241. model.change( writer => {
  242. writer.setSelection( writer.createRange(
  243. writer.createPositionAt( firstParagraph, 0 ),
  244. writer.createPositionAt( firstParagraph, 5 )
  245. ) );
  246. } );
  247. expect( editor.commands.get( 'input' ).isEnabled ).to.be.false;
  248. } );
  249. it( 'should be enabled for non-collapsed selection that is fully contained inside exception marker', () => {
  250. model.change( writer => {
  251. writer.setSelection( writer.createRange(
  252. writer.createPositionAt( firstParagraph, 5 ),
  253. writer.createPositionAt( firstParagraph, 6 )
  254. ) );
  255. } );
  256. expect( editor.commands.get( 'input' ).isEnabled ).to.be.true;
  257. } );
  258. it( 'should be enabled for non-collapsed selection inside exception marker (start position on marker boundary)', () => {
  259. model.change( writer => {
  260. writer.setSelection( writer.createRange(
  261. writer.createPositionAt( firstParagraph, 4 ),
  262. writer.createPositionAt( firstParagraph, 6 )
  263. ) );
  264. } );
  265. expect( editor.commands.get( 'input' ).isEnabled ).to.be.true;
  266. } );
  267. it( 'should be enabled for non-collapsed selection inside exception marker (end position on marker boundary)', () => {
  268. model.change( writer => {
  269. writer.setSelection( writer.createRange(
  270. writer.createPositionAt( firstParagraph, 5 ),
  271. writer.createPositionAt( firstParagraph, 7 )
  272. ) );
  273. } );
  274. expect( editor.commands.get( 'input' ).isEnabled ).to.be.true;
  275. } );
  276. it( 'should be enabled for non-collapsed selection is equal to exception marker', () => {
  277. model.change( writer => {
  278. writer.setSelection( writer.createRange(
  279. writer.createPositionAt( firstParagraph, 4 ),
  280. writer.createPositionAt( firstParagraph, 7 )
  281. ) );
  282. } );
  283. expect( editor.commands.get( 'input' ).isEnabled ).to.be.true;
  284. } );
  285. } );
  286. describe( 'bold', () => {
  287. beforeEach( () => {
  288. editor.commands.add( 'bold', buildFakeCommand( editor ) );
  289. } );
  290. it( 'should be disabled outside exception marker', () => {
  291. model.change( writer => {
  292. writer.setSelection( firstParagraph, 1 );
  293. } );
  294. expect( editor.commands.get( 'bold' ).isEnabled ).to.be.false;
  295. } );
  296. it( 'should be enabled inside exception marker', () => {
  297. model.change( writer => {
  298. writer.setSelection( firstParagraph, 5 );
  299. } );
  300. expect( editor.commands.get( 'bold' ).isEnabled ).to.be.true;
  301. } );
  302. } );
  303. describe( 'other', () => {
  304. beforeEach( () => {
  305. editor.commands.add( 'other', buildFakeCommand( editor ) );
  306. } );
  307. it( 'should be disabled outside exception marker', () => {
  308. model.change( writer => {
  309. writer.setSelection( firstParagraph, 1 );
  310. } );
  311. expect( editor.commands.get( 'other' ).isEnabled ).to.be.false;
  312. } );
  313. it( 'should be disabled inside exception marker', () => {
  314. model.change( writer => {
  315. writer.setSelection( firstParagraph, 1 );
  316. } );
  317. model.change( writer => {
  318. writer.setSelection( firstParagraph, 5 );
  319. } );
  320. expect( editor.commands.get( 'other' ).isEnabled ).to.be.false;
  321. } );
  322. } );
  323. } );
  324. class FakeCommand extends Command {
  325. refresh() {
  326. this.isEnabled = true;
  327. }
  328. }
  329. function buildFakeCommand( editor ) {
  330. return new FakeCommand( editor );
  331. }
  332. } );