standardmodeediting.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 { 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( 'should set proper schema rules', () => {
  30. expect( model.schema.checkAttribute( [ '$root', '$text' ], 'restrictedEditingException' ) ).to.be.true;
  31. expect( model.schema.checkAttribute( [ '$block', '$text' ], 'restrictedEditingException' ) ).to.be.true;
  32. expect( model.schema.checkAttribute( [ '$clipboardHolder', '$text' ], 'restrictedEditingException' ) ).to.be.true;
  33. expect( model.schema.checkAttribute( [ '$block' ], 'restrictedEditingException' ) ).to.be.false;
  34. } );
  35. it( 'should register the command', () => {
  36. const command = editor.commands.get( 'restrictedEditingException' );
  37. expect( command ).to.be.instanceof( RestrictedEditingExceptionCommand );
  38. } );
  39. describe( 'conversion', () => {
  40. describe( 'upcast', () => {
  41. it( 'should convert <span class="ck-restricted-editing-exception"> to the model attribute', () => {
  42. editor.setData( '<p>foo <span class="ck-restricted-editing-exception">bar</span> baz</p>' );
  43. expect( getModelData( model, { withoutSelection: true } ) )
  44. .to.equal( '<paragraph>foo <$text restrictedEditingException="true">bar</$text> baz</paragraph>' );
  45. } );
  46. } );
  47. describe( 'downcast', () => {
  48. it( 'should convert the model attribute to a <span>', () => {
  49. const expectedView = '<p>foo <span class="ck-restricted-editing-exception">bar</span> baz</p>';
  50. setModelData( editor.model,
  51. '<paragraph>foo <$text restrictedEditingException="true">bar</$text> baz</paragraph>'
  52. );
  53. expect( editor.getData() ).to.equal( expectedView );
  54. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( expectedView );
  55. } );
  56. it( 'converted <span> should be outer most element', () => {
  57. editor.conversion.for( 'downcast' ).attributeToElement( {
  58. model: 'bold',
  59. view: 'b'
  60. } );
  61. editor.conversion.for( 'downcast' ).attributeToElement( {
  62. model: 'italic',
  63. view: 'i'
  64. } );
  65. const expectedView = '<p><span class="ck-restricted-editing-exception"><b>foo</b> <i>bar</i> baz</span></p>';
  66. setModelData( editor.model,
  67. '<paragraph>' +
  68. '<$text restrictedEditingException="true" bold="true">foo</$text>' +
  69. '<$text restrictedEditingException="true"> </$text>' +
  70. '<$text restrictedEditingException="true" italic="true">bar</$text>' +
  71. '<$text restrictedEditingException="true"> baz</$text>' +
  72. '</paragraph>'
  73. );
  74. assertEqualMarkup( editor.getData(), expectedView );
  75. assertEqualMarkup( getViewData( editor.editing.view, { withoutSelection: true } ), expectedView );
  76. } );
  77. } );
  78. } );
  79. } );