standardeditingmodeediting.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /**
  2. * @license Copyright (c) 2003-2020, 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 { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  8. import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  9. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  10. import StandardEditingModeEditing from '../src/standardeditingmodeediting';
  11. import RestrictedEditingExceptionCommand from '../src/restrictededitingexceptioncommand';
  12. import { assertEqualMarkup } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  13. describe( 'StandardEditingModeEditing', () => {
  14. let editor, model;
  15. testUtils.createSinonSandbox();
  16. beforeEach( async () => {
  17. editor = await VirtualTestEditor.create( { plugins: [ Paragraph, StandardEditingModeEditing ] } );
  18. model = editor.model;
  19. } );
  20. afterEach( () => {
  21. return editor.destroy();
  22. } );
  23. it( 'should be named', () => {
  24. expect( StandardEditingModeEditing.pluginName ).to.equal( 'StandardEditingModeEditing' );
  25. } );
  26. it( 'should be loaded', () => {
  27. expect( editor.plugins.get( 'StandardEditingModeEditing' ) ).to.be.instanceOf( StandardEditingModeEditing );
  28. } );
  29. it( 'root should have "ck-restricted-editing_mode_standard" class', () => {
  30. for ( const root of editor.editing.view.document.roots ) {
  31. expect( root.hasClass( 'ck-restricted-editing_mode_standard' ) ).to.be.true;
  32. }
  33. } );
  34. it( 'should set proper schema rules', () => {
  35. expect( model.schema.checkAttribute( [ '$root', '$text' ], 'restrictedEditingException' ) ).to.be.true;
  36. expect( model.schema.checkAttribute( [ '$block', '$text' ], 'restrictedEditingException' ) ).to.be.true;
  37. expect( model.schema.checkAttribute( [ '$clipboardHolder', '$text' ], 'restrictedEditingException' ) ).to.be.true;
  38. expect( model.schema.checkAttribute( [ '$block' ], 'restrictedEditingException' ) ).to.be.false;
  39. } );
  40. it( 'should register the command', () => {
  41. const command = editor.commands.get( 'restrictedEditingException' );
  42. expect( command ).to.be.instanceof( RestrictedEditingExceptionCommand );
  43. } );
  44. describe( 'conversion', () => {
  45. describe( 'upcast', () => {
  46. it( 'should convert <span class="restricted-editing-exception"> to the model attribute', () => {
  47. editor.setData( '<p>foo <span class="restricted-editing-exception">bar</span> baz</p>' );
  48. expect( getModelData( model, { withoutSelection: true } ) )
  49. .to.equal( '<paragraph>foo <$text restrictedEditingException="true">bar</$text> baz</paragraph>' );
  50. } );
  51. } );
  52. describe( 'downcast', () => {
  53. it( 'should convert the model attribute to a <span>', () => {
  54. const expectedView = '<p>foo <span class="restricted-editing-exception">bar</span> baz</p>';
  55. setModelData( editor.model,
  56. '<paragraph>foo <$text restrictedEditingException="true">bar</$text> baz</paragraph>'
  57. );
  58. expect( editor.getData() ).to.equal( expectedView );
  59. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( expectedView );
  60. } );
  61. it( 'converted <span> should be outer most element', () => {
  62. editor.conversion.for( 'downcast' ).attributeToElement( {
  63. model: 'bold',
  64. view: 'b'
  65. } );
  66. editor.conversion.for( 'downcast' ).attributeToElement( {
  67. model: 'italic',
  68. view: 'i'
  69. } );
  70. const expectedView = '<p><span class="restricted-editing-exception"><b>foo</b> <i>bar</i> baz</span></p>';
  71. setModelData( editor.model,
  72. '<paragraph>' +
  73. '<$text restrictedEditingException="true" bold="true">foo</$text>' +
  74. '<$text restrictedEditingException="true"> </$text>' +
  75. '<$text restrictedEditingException="true" italic="true">bar</$text>' +
  76. '<$text restrictedEditingException="true"> baz</$text>' +
  77. '</paragraph>'
  78. );
  79. assertEqualMarkup( editor.getData(), expectedView );
  80. assertEqualMarkup( getViewData( editor.editing.view, { withoutSelection: true } ), expectedView );
  81. } );
  82. } );
  83. } );
  84. } );