restrictedediting.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. it( 'should not convert other <span> elements', () => {
  63. editor.setData( '<p>foo <span class="foo bar">bar</span> baz</p>' );
  64. expect( model.markers.has( 'restricted-editing-exception:1' ) ).to.be.false;
  65. } );
  66. } );
  67. describe( 'downcast', () => {
  68. it( 'should convert model marker to <span>', () => {
  69. setModelData( model, '<paragraph>foo bar baz</paragraph>' );
  70. const paragraph = model.document.getRoot().getChild( 0 );
  71. model.change( writer => {
  72. writer.addMarker( 'restricted-editing-exception:1', {
  73. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  74. usingOperation: true,
  75. affectsData: true
  76. } );
  77. } );
  78. const expectedView = '<p>foo <span class="ck-restricted-editing-exception">bar</span> baz</p>';
  79. expect( editor.getData() ).to.equal( expectedView );
  80. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( expectedView );
  81. } );
  82. it( 'converted <span> should be the outermost attribute element', () => {
  83. editor.conversion.for( 'downcast' ).attributeToElement( { model: 'bold', view: 'b' } );
  84. setModelData( model, '<paragraph><$text bold="true">foo bar baz</$text></paragraph>' );
  85. const paragraph = model.document.getRoot().getChild( 0 );
  86. model.change( writer => {
  87. writer.addMarker( 'restricted-editing-exception:1', {
  88. range: writer.createRange( writer.createPositionAt( paragraph, 0 ), writer.createPositionAt( paragraph, 'end' ) ),
  89. usingOperation: true,
  90. affectsData: true
  91. } );
  92. } );
  93. const expectedView = '<p><span class="ck-restricted-editing-exception"><b>foo bar baz</b></span></p>';
  94. expect( editor.getData() ).to.equal( expectedView );
  95. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( expectedView );
  96. } );
  97. } );
  98. } );
  99. } );