8
0

boldediting.js 5.2 KB

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