restrictedediting.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. describe( 'RestrictedEditing', () => {
  14. let editor, element;
  15. testUtils.createSinonSandbox();
  16. describe( 'plugin', () => {
  17. beforeEach( async () => {
  18. element = document.createElement( 'div' );
  19. document.body.appendChild( element );
  20. editor = await ClassicTestEditor.create( element, { plugins: [ RestrictedEditing ] } );
  21. } );
  22. afterEach( () => {
  23. element.remove();
  24. return editor.destroy();
  25. } );
  26. it( 'should be named', () => {
  27. expect( RestrictedEditing.pluginName ).to.equal( 'RestrictedEditing' );
  28. } );
  29. it( 'should be loaded', () => {
  30. expect( editor.plugins.get( RestrictedEditing ) ).to.be.instanceOf( RestrictedEditing );
  31. } );
  32. } );
  33. describe( 'conversion', () => {
  34. let model;
  35. beforeEach( async () => {
  36. editor = await VirtualTestEditor.create( { plugins: [ Paragraph, RestrictedEditing ] } );
  37. model = editor.model;
  38. } );
  39. afterEach( () => {
  40. return editor.destroy();
  41. } );
  42. describe( 'upcast', () => {
  43. it( 'should convert <span class="ck-restricted-editing-exception"> to marker', () => {
  44. editor.setData( '<p>foo <span class="ck-restricted-editing-exception">bar</span> baz</p>' );
  45. expect( model.markers.has( 'restricted-editing-exception:1' ) ).to.be.true;
  46. const marker = model.markers.get( 'restricted-editing-exception:1' );
  47. expect( marker.getStart().path ).to.deep.equal( [ 0, 4 ] );
  48. expect( marker.getEnd().path ).to.deep.equal( [ 0, 7 ] );
  49. } );
  50. it( 'should convert multiple <span class="ck-restricted-editing-exception">', () => {
  51. editor.setData(
  52. '<p>foo <span class="ck-restricted-editing-exception">bar</span> baz</p>' +
  53. '<p>ABCDEF<span class="ck-restricted-editing-exception">GHIJK</span>LMNOPQRST</p>'
  54. );
  55. expect( model.markers.has( 'restricted-editing-exception:1' ) ).to.be.true;
  56. expect( model.markers.has( 'restricted-editing-exception:2' ) ).to.be.true;
  57. // Data for the first marker is the same as in previous tests so no need to test it again.
  58. const secondMarker = model.markers.get( 'restricted-editing-exception:2' );
  59. expect( secondMarker.getStart().path ).to.deep.equal( [ 1, 6 ] );
  60. expect( secondMarker.getEnd().path ).to.deep.equal( [ 1, 11 ] );
  61. } );
  62. } );
  63. describe( 'downcast', () => {
  64. it( 'should convert model marker to <span>', () => {
  65. setModelData( model, '<paragraph>foo bar baz</paragraph>' );
  66. const paragraph = model.document.getRoot().getChild( 0 );
  67. model.change( writer => {
  68. writer.addMarker( 'restricted-editing-exception:1', {
  69. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  70. usingOperation: true,
  71. affectsData: true
  72. } );
  73. } );
  74. const expectedView = '<p>foo <span class="ck-restricted-editing-exception">bar</span> baz</p>';
  75. expect( editor.getData() ).to.equal( expectedView );
  76. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( expectedView );
  77. } );
  78. } );
  79. } );
  80. } );