8
0

restrictedediting.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 RestrictedEditing from './../src/restrictedediting';
  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. describe( 'RestrictedEditing', () => {
  16. let editor, element;
  17. testUtils.createSinonSandbox();
  18. describe( 'plugin', () => {
  19. beforeEach( async () => {
  20. element = document.createElement( 'div' );
  21. document.body.appendChild( element );
  22. editor = await ClassicTestEditor.create( element, { plugins: [ RestrictedEditing ] } );
  23. } );
  24. afterEach( () => {
  25. element.remove();
  26. return editor.destroy();
  27. } );
  28. it( 'should be named', () => {
  29. expect( RestrictedEditing.pluginName ).to.equal( 'RestrictedEditing' );
  30. } );
  31. it( 'should be loaded', () => {
  32. expect( editor.plugins.get( RestrictedEditing ) ).to.be.instanceOf( RestrictedEditing );
  33. } );
  34. } );
  35. describe( 'conversion', () => {
  36. let model;
  37. beforeEach( async () => {
  38. editor = await VirtualTestEditor.create( { plugins: [ Paragraph, RestrictedEditing ] } );
  39. model = editor.model;
  40. } );
  41. afterEach( () => {
  42. return editor.destroy();
  43. } );
  44. describe( 'upcast', () => {
  45. it( 'should convert <span class="ck-restricted-editing-exception"> to marker', () => {
  46. editor.setData( '<p>foo <span class="ck-restricted-editing-exception">bar</span> baz</p>' );
  47. expect( model.markers.has( 'restricted-editing-exception:1' ) ).to.be.true;
  48. const marker = model.markers.get( 'restricted-editing-exception:1' );
  49. expect( marker.getStart().path ).to.deep.equal( [ 0, 4 ] );
  50. expect( marker.getEnd().path ).to.deep.equal( [ 0, 7 ] );
  51. } );
  52. it( 'should convert multiple <span class="ck-restricted-editing-exception">', () => {
  53. editor.setData(
  54. '<p>foo <span class="ck-restricted-editing-exception">bar</span> baz</p>' +
  55. '<p>ABCDEF<span class="ck-restricted-editing-exception">GHIJK</span>LMNOPQRST</p>'
  56. );
  57. expect( model.markers.has( 'restricted-editing-exception:1' ) ).to.be.true;
  58. expect( model.markers.has( 'restricted-editing-exception:2' ) ).to.be.true;
  59. // Data for the first marker is the same as in previous tests so no need to test it again.
  60. const secondMarker = model.markers.get( 'restricted-editing-exception:2' );
  61. expect( secondMarker.getStart().path ).to.deep.equal( [ 1, 6 ] );
  62. expect( secondMarker.getEnd().path ).to.deep.equal( [ 1, 11 ] );
  63. } );
  64. it( 'should not convert other <span> elements', () => {
  65. editor.setData( '<p>foo <span class="foo bar">bar</span> baz</p>' );
  66. expect( model.markers.has( 'restricted-editing-exception:1' ) ).to.be.false;
  67. } );
  68. } );
  69. describe( 'downcast', () => {
  70. it( 'should convert model marker to <span>', () => {
  71. setModelData( model, '<paragraph>foo bar baz</paragraph>' );
  72. const paragraph = model.document.getRoot().getChild( 0 );
  73. model.change( writer => {
  74. writer.addMarker( 'restricted-editing-exception:1', {
  75. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  76. usingOperation: true,
  77. affectsData: true
  78. } );
  79. } );
  80. const expectedView = '<p>foo <span class="ck-restricted-editing-exception">bar</span> baz</p>';
  81. expect( editor.getData() ).to.equal( expectedView );
  82. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( expectedView );
  83. } );
  84. it( 'converted <span> should be the outermost attribute element', () => {
  85. editor.conversion.for( 'downcast' ).attributeToElement( { model: 'bold', view: 'b' } );
  86. setModelData( model, '<paragraph><$text bold="true">foo bar baz</$text></paragraph>' );
  87. const paragraph = model.document.getRoot().getChild( 0 );
  88. model.change( writer => {
  89. writer.addMarker( 'restricted-editing-exception:1', {
  90. range: writer.createRange( writer.createPositionAt( paragraph, 0 ), writer.createPositionAt( paragraph, 'end' ) ),
  91. usingOperation: true,
  92. affectsData: true
  93. } );
  94. } );
  95. const expectedView = '<p><span class="ck-restricted-editing-exception"><b>foo bar baz</b></span></p>';
  96. expect( editor.getData() ).to.equal( expectedView );
  97. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( expectedView );
  98. } );
  99. } );
  100. } );
  101. describe.only( 'editing behavior', () => {
  102. let model;
  103. beforeEach( async () => {
  104. editor = await VirtualTestEditor.create( { plugins: [ Paragraph, Typing, RestrictedEditing ] } );
  105. model = editor.model;
  106. } );
  107. afterEach( () => {
  108. return editor.destroy();
  109. } );
  110. it( 'should keep markers in the view when editable region is edited', () => {
  111. setModelData( model,
  112. '<paragraph>foo bar baz</paragraph>' +
  113. '<paragraph>xxx y[]yy zzz</paragraph>'
  114. );
  115. const firstParagraph = model.document.getRoot().getChild( 0 );
  116. const secondParagraph = model.document.getRoot().getChild( 1 );
  117. model.change( writer => {
  118. writer.addMarker( 'restricted-editing-exception:1', {
  119. range: writer.createRange( writer.createPositionAt( firstParagraph, 4 ), writer.createPositionAt( firstParagraph, 7 ) ),
  120. usingOperation: true,
  121. affectsData: true
  122. } );
  123. writer.addMarker( 'restricted-editing-exception:2', {
  124. range: writer.createRange(
  125. writer.createPositionAt( secondParagraph, 4 ),
  126. writer.createPositionAt( secondParagraph, 7 )
  127. ),
  128. usingOperation: true,
  129. affectsData: true
  130. } );
  131. } );
  132. model.change( writer => {
  133. model.insertContent( writer.createText( 'R', model.document.selection.getAttributes() ) );
  134. } );
  135. const expectedView = '<p>foo <span class="ck-restricted-editing-exception">bar</span> baz</p>' +
  136. '<p>xxx <span class="ck-restricted-editing-exception">yRyy</span> zzz</p>';
  137. expect( editor.getData() ).to.equal( expectedView );
  138. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( expectedView );
  139. } );
  140. it( 'should block user typing outside exception markers', () => {
  141. setModelData( model, '<paragraph>foo []bar baz</paragraph>' );
  142. editor.execute( 'input', { text: 'X' } );
  143. assertEqualMarkup( getModelData( model ), '<paragraph>foo []bar baz</paragraph>' );
  144. } );
  145. it( 'should not block user typing inside exception marker', () => {
  146. setModelData( model, '<paragraph>[]foo bar baz</paragraph>' );
  147. const firstParagraph = model.document.getRoot().getChild( 0 );
  148. model.change( writer => {
  149. writer.addMarker( 'restricted-editing-exception:1', {
  150. range: writer.createRange( writer.createPositionAt( firstParagraph, 4 ), writer.createPositionAt( firstParagraph, 7 ) ),
  151. usingOperation: true,
  152. affectsData: true
  153. } );
  154. } );
  155. model.change( writer => {
  156. writer.setSelection( firstParagraph, 5 );
  157. } );
  158. editor.execute( 'input', { text: 'X' } );
  159. assertEqualMarkup( getModelData( model ), '<paragraph>foo bX[]ar baz</paragraph>' );
  160. } );
  161. } );
  162. } );