8
0

restrictededitingediting.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. /* global document */
  6. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  7. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  8. import RestrictedEditingEditing from './../src/restrictededitingediting';
  9. import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  10. import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  11. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  12. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  13. import Typing from '@ckeditor/ckeditor5-typing/src/typing';
  14. import { assertEqualMarkup } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  15. import UndoEditing from '@ckeditor/ckeditor5-undo/src/undoediting';
  16. import Command from '@ckeditor/ckeditor5-core/src/command';
  17. describe( 'RestrictedEditingEditing', () => {
  18. let editor, element;
  19. testUtils.createSinonSandbox();
  20. describe( 'plugin', () => {
  21. beforeEach( async () => {
  22. element = document.createElement( 'div' );
  23. document.body.appendChild( element );
  24. editor = await ClassicTestEditor.create( element, { plugins: [ RestrictedEditingEditing ] } );
  25. } );
  26. afterEach( () => {
  27. element.remove();
  28. return editor.destroy();
  29. } );
  30. it( 'should be named', () => {
  31. expect( RestrictedEditingEditing.pluginName ).to.equal( 'RestrictedEditingEditing' );
  32. } );
  33. it( 'should be loaded', () => {
  34. expect( editor.plugins.get( RestrictedEditingEditing ) ).to.be.instanceOf( RestrictedEditingEditing );
  35. } );
  36. } );
  37. describe( 'conversion', () => {
  38. let model;
  39. beforeEach( async () => {
  40. editor = await VirtualTestEditor.create( { plugins: [ Paragraph, RestrictedEditingEditing ] } );
  41. model = editor.model;
  42. } );
  43. afterEach( () => {
  44. return editor.destroy();
  45. } );
  46. describe( 'upcast', () => {
  47. it( 'should convert <span class="ck-restricted-editing-exception"> to marker', () => {
  48. editor.setData( '<p>foo <span class="ck-restricted-editing-exception">bar</span> baz</p>' );
  49. expect( model.markers.has( 'restricted-editing-exception:1' ) ).to.be.true;
  50. const marker = model.markers.get( 'restricted-editing-exception:1' );
  51. expect( marker.getStart().path ).to.deep.equal( [ 0, 4 ] );
  52. expect( marker.getEnd().path ).to.deep.equal( [ 0, 7 ] );
  53. } );
  54. it( 'should convert multiple <span class="ck-restricted-editing-exception">', () => {
  55. editor.setData(
  56. '<p>foo <span class="ck-restricted-editing-exception">bar</span> baz</p>' +
  57. '<p>ABCDEF<span class="ck-restricted-editing-exception">GHIJK</span>LMNOPQRST</p>'
  58. );
  59. expect( model.markers.has( 'restricted-editing-exception:1' ) ).to.be.true;
  60. expect( model.markers.has( 'restricted-editing-exception:2' ) ).to.be.true;
  61. // Data for the first marker is the same as in previous tests so no need to test it again.
  62. const secondMarker = model.markers.get( 'restricted-editing-exception:2' );
  63. expect( secondMarker.getStart().path ).to.deep.equal( [ 1, 6 ] );
  64. expect( secondMarker.getEnd().path ).to.deep.equal( [ 1, 11 ] );
  65. } );
  66. it( 'should not convert other <span> elements', () => {
  67. editor.setData( '<p>foo <span class="foo bar">bar</span> baz</p>' );
  68. expect( model.markers.has( 'restricted-editing-exception:1' ) ).to.be.false;
  69. } );
  70. } );
  71. describe( 'downcast', () => {
  72. it( 'should convert model marker to <span>', () => {
  73. setModelData( model, '<paragraph>foo bar baz</paragraph>' );
  74. const paragraph = model.document.getRoot().getChild( 0 );
  75. model.change( writer => {
  76. writer.addMarker( 'restricted-editing-exception:1', {
  77. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  78. usingOperation: true,
  79. affectsData: true
  80. } );
  81. } );
  82. const expectedView = '<p>foo <span class="ck-restricted-editing-exception">bar</span> baz</p>';
  83. expect( editor.getData() ).to.equal( expectedView );
  84. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( expectedView );
  85. } );
  86. it( 'converted <span> should be the outermost attribute element', () => {
  87. editor.conversion.for( 'downcast' ).attributeToElement( { model: 'bold', view: 'b' } );
  88. setModelData( model, '<paragraph><$text bold="true">foo bar baz</$text></paragraph>' );
  89. const paragraph = model.document.getRoot().getChild( 0 );
  90. model.change( writer => {
  91. writer.addMarker( 'restricted-editing-exception:1', {
  92. range: writer.createRange( writer.createPositionAt( paragraph, 0 ), writer.createPositionAt( paragraph, 'end' ) ),
  93. usingOperation: true,
  94. affectsData: true
  95. } );
  96. } );
  97. const expectedView = '<p><span class="ck-restricted-editing-exception"><b>foo bar baz</b></span></p>';
  98. expect( editor.getData() ).to.equal( expectedView );
  99. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( expectedView );
  100. } );
  101. } );
  102. } );
  103. describe( 'editing behavior', () => {
  104. let model;
  105. beforeEach( async () => {
  106. editor = await VirtualTestEditor.create( { plugins: [ Paragraph, Typing, UndoEditing, RestrictedEditingEditing ] } );
  107. model = editor.model;
  108. } );
  109. afterEach( async () => {
  110. await editor.destroy();
  111. } );
  112. it( 'should keep markers in the view when editable region is edited', () => {
  113. setModelData( model,
  114. '<paragraph>foo bar baz</paragraph>' +
  115. '<paragraph>xxx y[]yy zzz</paragraph>'
  116. );
  117. const firstParagraph = model.document.getRoot().getChild( 0 );
  118. const secondParagraph = model.document.getRoot().getChild( 1 );
  119. model.change( writer => {
  120. writer.addMarker( 'restricted-editing-exception:1', {
  121. range: writer.createRange( writer.createPositionAt( firstParagraph, 4 ), writer.createPositionAt( firstParagraph, 7 ) ),
  122. usingOperation: true,
  123. affectsData: true
  124. } );
  125. writer.addMarker( 'restricted-editing-exception:2', {
  126. range: writer.createRange(
  127. writer.createPositionAt( secondParagraph, 4 ),
  128. writer.createPositionAt( secondParagraph, 7 )
  129. ),
  130. usingOperation: true,
  131. affectsData: true
  132. } );
  133. } );
  134. model.change( writer => {
  135. model.insertContent( writer.createText( 'R', model.document.selection.getAttributes() ) );
  136. } );
  137. const expectedView = '<p>foo <span class="ck-restricted-editing-exception">bar</span> baz</p>' +
  138. '<p>xxx <span class="ck-restricted-editing-exception">yRyy</span> zzz</p>';
  139. expect( editor.getData() ).to.equal( expectedView );
  140. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( expectedView );
  141. } );
  142. it( 'should block user typing outside exception markers', () => {
  143. setModelData( model, '<paragraph>foo []bar baz</paragraph>' );
  144. editor.execute( 'input', { text: 'X' } );
  145. assertEqualMarkup( getModelData( model ), '<paragraph>foo []bar baz</paragraph>' );
  146. } );
  147. it( 'should not block user typing inside exception marker', () => {
  148. setModelData( model, '<paragraph>[]foo bar baz</paragraph>' );
  149. const firstParagraph = model.document.getRoot().getChild( 0 );
  150. model.change( writer => {
  151. writer.addMarker( 'restricted-editing-exception:1', {
  152. range: writer.createRange( writer.createPositionAt( firstParagraph, 4 ), writer.createPositionAt( firstParagraph, 7 ) ),
  153. usingOperation: true,
  154. affectsData: true
  155. } );
  156. } );
  157. model.change( writer => {
  158. writer.setSelection( firstParagraph, 5 );
  159. } );
  160. editor.execute( 'input', { text: 'X' } );
  161. assertEqualMarkup( getModelData( model ), '<paragraph>foo bX[]ar baz</paragraph>' );
  162. } );
  163. } );
  164. describe( 'commands integration', () => {
  165. let model, firstParagraph;
  166. beforeEach( async () => {
  167. editor = await VirtualTestEditor.create( { plugins: [ Paragraph, Typing, UndoEditing, RestrictedEditingEditing ] } );
  168. model = editor.model;
  169. setModelData( model, '<paragraph>[]foo bar baz</paragraph>' );
  170. firstParagraph = model.document.getRoot().getChild( 0 );
  171. model.change( writer => {
  172. writer.addMarker( 'restricted-editing-exception:1', {
  173. range: writer.createRange(
  174. writer.createPositionAt( firstParagraph, 4 ),
  175. writer.createPositionAt( firstParagraph, 7 ) ),
  176. usingOperation: true,
  177. affectsData: true
  178. } );
  179. } );
  180. } );
  181. afterEach( async () => {
  182. await editor.destroy();
  183. } );
  184. describe( 'undo', () => {
  185. it( 'should be enabled outside exception marker', () => {
  186. model.change( writer => {
  187. writer.setSelection( firstParagraph, 1 );
  188. } );
  189. expect( editor.commands.get( 'undo' ).isEnabled ).to.be.true;
  190. } );
  191. it( 'should be enabled inside exception marker', () => {
  192. model.change( writer => {
  193. writer.setSelection( firstParagraph, 5 );
  194. } );
  195. expect( editor.commands.get( 'undo' ).isEnabled ).to.be.true;
  196. } );
  197. } );
  198. describe( 'redo', () => {
  199. beforeEach( () => {
  200. editor.commands.add( 'redo', 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( 'redo' ).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( 'redo' ).isEnabled ).to.be.true;
  213. } );
  214. } );
  215. describe( 'bold', () => {
  216. beforeEach( () => {
  217. editor.commands.add( 'bold', buildFakeCommand( editor ) );
  218. } );
  219. it( 'should be disabled outside exception marker', () => {
  220. model.change( writer => {
  221. writer.setSelection( firstParagraph, 1 );
  222. } );
  223. expect( editor.commands.get( 'bold' ).isEnabled ).to.be.false;
  224. } );
  225. it( 'should be enabled inside exception marker', () => {
  226. model.change( writer => {
  227. writer.setSelection( firstParagraph, 5 );
  228. } );
  229. expect( editor.commands.get( 'bold' ).isEnabled ).to.be.true;
  230. } );
  231. } );
  232. describe( 'other', () => {
  233. beforeEach( () => {
  234. editor.commands.add( 'other', buildFakeCommand( editor ) );
  235. } );
  236. it( 'should be disabled outside exception marker', () => {
  237. model.change( writer => {
  238. writer.setSelection( firstParagraph, 1 );
  239. } );
  240. expect( editor.commands.get( 'other' ).isEnabled ).to.be.false;
  241. } );
  242. it( 'should be disabled inside exception marker', () => {
  243. model.change( writer => {
  244. writer.setSelection( firstParagraph, 1 );
  245. } );
  246. model.change( writer => {
  247. writer.setSelection( firstParagraph, 5 );
  248. } );
  249. expect( editor.commands.get( 'other' ).isEnabled ).to.be.false;
  250. } );
  251. } );
  252. } );
  253. class FakeCommand extends Command {
  254. refresh() {
  255. this.isEnabled = true;
  256. }
  257. }
  258. function buildFakeCommand( editor ) {
  259. return new FakeCommand( editor );
  260. }
  261. } );