restrictededitingediting.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 { setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  9. import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  10. import RestrictedEditingEditing from './../src/restrictededitingediting';
  11. describe( 'RestrictedEditingEditing', () => {
  12. let editor;
  13. testUtils.createSinonSandbox();
  14. describe( 'plugin', () => {
  15. beforeEach( async () => {
  16. editor = await VirtualTestEditor.create( { plugins: [ RestrictedEditingEditing ] } );
  17. } );
  18. afterEach( async () => {
  19. await editor.destroy();
  20. } );
  21. it( 'should be named', () => {
  22. expect( RestrictedEditingEditing.pluginName ).to.equal( 'RestrictedEditingEditing' );
  23. } );
  24. it( 'should be loaded', () => {
  25. expect( editor.plugins.get( RestrictedEditingEditing ) ).to.be.instanceOf( RestrictedEditingEditing );
  26. } );
  27. } );
  28. describe( 'conversion', () => {
  29. let model;
  30. beforeEach( async () => {
  31. editor = await VirtualTestEditor.create( { plugins: [ Paragraph, RestrictedEditingEditing ] } );
  32. model = editor.model;
  33. } );
  34. afterEach( () => {
  35. return editor.destroy();
  36. } );
  37. describe( 'upcast', () => {
  38. it( 'should convert <span class="ck-restricted-editing-exception"> to marker', () => {
  39. editor.setData( '<p>foo <span class="ck-restricted-editing-exception">bar</span> baz</p>' );
  40. expect( model.markers.has( 'restricted-editing-exception:1' ) ).to.be.true;
  41. const marker = model.markers.get( 'restricted-editing-exception:1' );
  42. expect( marker.getStart().path ).to.deep.equal( [ 0, 4 ] );
  43. expect( marker.getEnd().path ).to.deep.equal( [ 0, 7 ] );
  44. } );
  45. it( 'should convert multiple <span class="ck-restricted-editing-exception">', () => {
  46. editor.setData(
  47. '<p>foo <span class="ck-restricted-editing-exception">bar</span> baz</p>' +
  48. '<p>ABCDEF<span class="ck-restricted-editing-exception">GHIJK</span>LMNOPQRST</p>'
  49. );
  50. expect( model.markers.has( 'restricted-editing-exception:1' ) ).to.be.true;
  51. expect( model.markers.has( 'restricted-editing-exception:2' ) ).to.be.true;
  52. // Data for the first marker is the same as in previous tests so no need to test it again.
  53. const secondMarker = model.markers.get( 'restricted-editing-exception:2' );
  54. expect( secondMarker.getStart().path ).to.deep.equal( [ 1, 6 ] );
  55. expect( secondMarker.getEnd().path ).to.deep.equal( [ 1, 11 ] );
  56. } );
  57. it( 'should not convert other <span> elements', () => {
  58. editor.setData( '<p>foo <span class="foo bar">bar</span> baz</p>' );
  59. expect( model.markers.has( 'restricted-editing-exception:1' ) ).to.be.false;
  60. } );
  61. } );
  62. describe( 'downcast', () => {
  63. it( 'should convert model marker to <span>', () => {
  64. setModelData( model, '<paragraph>foo bar baz</paragraph>' );
  65. const paragraph = model.document.getRoot().getChild( 0 );
  66. model.change( writer => {
  67. writer.addMarker( 'restricted-editing-exception:1', {
  68. range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
  69. usingOperation: true,
  70. affectsData: true
  71. } );
  72. } );
  73. const expectedView = '<p>foo <span class="ck-restricted-editing-exception">bar</span> baz</p>';
  74. expect( editor.getData() ).to.equal( expectedView );
  75. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( expectedView );
  76. } );
  77. it( 'converted <span> should be the outermost attribute element', () => {
  78. editor.conversion.for( 'downcast' ).attributeToElement( { model: 'bold', view: 'b' } );
  79. setModelData( model, '<paragraph><$text bold="true">foo bar baz</$text></paragraph>' );
  80. const paragraph = model.document.getRoot().getChild( 0 );
  81. model.change( writer => {
  82. writer.addMarker( 'restricted-editing-exception:1', {
  83. range: writer.createRange( writer.createPositionAt( paragraph, 0 ), writer.createPositionAt( paragraph, 'end' ) ),
  84. usingOperation: true,
  85. affectsData: true
  86. } );
  87. } );
  88. const expectedView = '<p><span class="ck-restricted-editing-exception"><b>foo bar baz</b></span></p>';
  89. expect( editor.getData() ).to.equal( expectedView );
  90. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( expectedView );
  91. } );
  92. } );
  93. } );
  94. } );