boldediting.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 BoldEditing from '../../src/bold/boldediting';
  6. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  7. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  8. import AttributeCommand from '../../src/attributecommand';
  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 { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
  12. describe( 'BoldEditing', () => {
  13. let editor, model;
  14. beforeEach( () => {
  15. return VirtualTestEditor
  16. .create( {
  17. plugins: [ Paragraph, BoldEditing ]
  18. } )
  19. .then( newEditor => {
  20. editor = newEditor;
  21. model = editor.model;
  22. } );
  23. } );
  24. afterEach( () => {
  25. return editor.destroy();
  26. } );
  27. it( 'should have pluginName', () => {
  28. expect( BoldEditing.pluginName ).to.equal( 'BoldEditing' );
  29. } );
  30. it( 'should be loaded', () => {
  31. expect( editor.plugins.get( BoldEditing ) ).to.be.instanceOf( BoldEditing );
  32. } );
  33. it( 'should set proper schema rules', () => {
  34. expect( model.schema.checkAttribute( [ '$root', '$block', '$text' ], 'bold' ) ).to.be.true;
  35. expect( model.schema.checkAttribute( [ '$clipboardHolder', '$text' ], 'bold' ) ).to.be.true;
  36. } );
  37. it( 'should be marked with a formatting property', () => {
  38. expect( model.schema.getAttributeProperties( 'bold' ) ).to.include( {
  39. isFormatting: true
  40. } );
  41. } );
  42. it( 'its attribute is marked with a copOnEnter property', () => {
  43. expect( model.schema.getAttributeProperties( 'bold' ) ).to.include( {
  44. copyOnEnter: true
  45. } );
  46. } );
  47. it( 'should set editor keystroke', () => {
  48. const spy = sinon.spy( editor, 'execute' );
  49. const keyEventData = {
  50. keyCode: keyCodes.b,
  51. ctrlKey: true,
  52. preventDefault: sinon.spy(),
  53. stopPropagation: sinon.spy()
  54. };
  55. const wasHandled = editor.keystrokes.press( keyEventData );
  56. expect( wasHandled ).to.be.true;
  57. expect( spy.calledOnce ).to.be.true;
  58. expect( keyEventData.preventDefault.calledOnce ).to.be.true;
  59. } );
  60. describe( 'command', () => {
  61. it( 'should register bold command', () => {
  62. const command = editor.commands.get( 'bold' );
  63. expect( command ).to.be.instanceOf( AttributeCommand );
  64. expect( command ).to.have.property( 'attributeKey', 'bold' );
  65. } );
  66. } );
  67. describe( 'data pipeline conversions', () => {
  68. it( 'should convert <strong> to bold attribute', () => {
  69. editor.setData( '<p><strong>foo</strong>bar</p>' );
  70. expect( getModelData( model, { withoutSelection: true } ) )
  71. .to.equal( '<paragraph><$text bold="true">foo</$text>bar</paragraph>' );
  72. expect( editor.getData() ).to.equal( '<p><strong>foo</strong>bar</p>' );
  73. } );
  74. it( 'should convert <b> to bold attribute', () => {
  75. editor.setData( '<p><b>foo</b>bar</p>' );
  76. expect( getModelData( model, { withoutSelection: true } ) )
  77. .to.equal( '<paragraph><$text bold="true">foo</$text>bar</paragraph>' );
  78. expect( editor.getData() ).to.equal( '<p><strong>foo</strong>bar</p>' );
  79. } );
  80. it( 'should convert font-weight:bold to bold attribute', () => {
  81. editor.setData( '<p><span style="font-weight: bold;">foo</span>bar</p>' );
  82. expect( getModelData( model, { withoutSelection: true } ) )
  83. .to.equal( '<paragraph><$text bold="true">foo</$text>bar</paragraph>' );
  84. expect( editor.getData() ).to.equal( '<p><strong>foo</strong>bar</p>' );
  85. } );
  86. it( 'should convert font-weight defined as number to bold attribute (if the value is higher or equal to 600)', () => {
  87. editor.setData( '<p><span style="font-weight: 600;">foo</span>bar</p>' );
  88. expect( getModelData( model, { withoutSelection: true } ) )
  89. .to.equal( '<paragraph><$text bold="true">foo</$text>bar</paragraph>' );
  90. expect( editor.getData() ).to.equal( '<p><strong>foo</strong>bar</p>' );
  91. } );
  92. it( 'should not convert font-weight defined as number to bold attribute (if the value is lower than 600)', () => {
  93. editor.setData( '<p><span style="font-weight: 500;">foo</span>bar</p>' );
  94. expect( getModelData( model, { withoutSelection: true } ) )
  95. .to.equal( '<paragraph>foobar</paragraph>' );
  96. expect( editor.getData() ).to.equal( '<p>foobar</p>' );
  97. } );
  98. it( 'should not convert font-weight if the value is invalid', () => {
  99. editor.setData( '<p><span style="font-weight: foo;">foo</span>bar</p>' );
  100. expect( getModelData( model, { withoutSelection: true } ) )
  101. .to.equal( '<paragraph>foobar</paragraph>' );
  102. expect( editor.getData() ).to.equal( '<p>foobar</p>' );
  103. } );
  104. it( 'should be integrated with autoparagraphing', () => {
  105. editor.setData( '<strong>foo</strong>bar' );
  106. expect( getModelData( model, { withoutSelection: true } ) )
  107. .to.equal( '<paragraph><$text bold="true">foo</$text>bar</paragraph>' );
  108. expect( editor.getData() ).to.equal( '<p><strong>foo</strong>bar</p>' );
  109. } );
  110. } );
  111. describe( 'editing pipeline conversion', () => {
  112. it( 'should convert attribute', () => {
  113. setModelData( model, '<paragraph><$text bold="true">foo</$text>bar</paragraph>' );
  114. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( '<p><strong>foo</strong>bar</p>' );
  115. } );
  116. } );
  117. } );