boldediting.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  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.deep.equal( {
  36. isFormatting: true
  37. } );
  38. } );
  39. it( 'should set editor keystroke', () => {
  40. const spy = sinon.spy( editor, 'execute' );
  41. const keyEventData = {
  42. keyCode: keyCodes.b,
  43. ctrlKey: true,
  44. preventDefault: sinon.spy(),
  45. stopPropagation: sinon.spy()
  46. };
  47. const wasHandled = editor.keystrokes.press( keyEventData );
  48. expect( wasHandled ).to.be.true;
  49. expect( spy.calledOnce ).to.be.true;
  50. expect( keyEventData.preventDefault.calledOnce ).to.be.true;
  51. } );
  52. describe( 'command', () => {
  53. it( 'should register bold command', () => {
  54. const command = editor.commands.get( 'bold' );
  55. expect( command ).to.be.instanceOf( AttributeCommand );
  56. expect( command ).to.have.property( 'attributeKey', 'bold' );
  57. } );
  58. } );
  59. describe( 'data pipeline conversions', () => {
  60. it( 'should convert <strong> to bold attribute', () => {
  61. editor.setData( '<p><strong>foo</strong>bar</p>' );
  62. expect( getModelData( model, { withoutSelection: true } ) )
  63. .to.equal( '<paragraph><$text bold="true">foo</$text>bar</paragraph>' );
  64. expect( editor.getData() ).to.equal( '<p><strong>foo</strong>bar</p>' );
  65. } );
  66. it( 'should convert <b> to bold attribute', () => {
  67. editor.setData( '<p><b>foo</b>bar</p>' );
  68. expect( getModelData( model, { withoutSelection: true } ) )
  69. .to.equal( '<paragraph><$text bold="true">foo</$text>bar</paragraph>' );
  70. expect( editor.getData() ).to.equal( '<p><strong>foo</strong>bar</p>' );
  71. } );
  72. it( 'should convert font-weight:bold to bold attribute', () => {
  73. editor.setData( '<p><span style="font-weight: bold;">foo</span>bar</p>' );
  74. expect( getModelData( model, { withoutSelection: true } ) )
  75. .to.equal( '<paragraph><$text bold="true">foo</$text>bar</paragraph>' );
  76. expect( editor.getData() ).to.equal( '<p><strong>foo</strong>bar</p>' );
  77. } );
  78. it( 'should be integrated with autoparagraphing', () => {
  79. editor.setData( '<strong>foo</strong>bar' );
  80. expect( getModelData( model, { withoutSelection: true } ) )
  81. .to.equal( '<paragraph><$text bold="true">foo</$text>bar</paragraph>' );
  82. expect( editor.getData() ).to.equal( '<p><strong>foo</strong>bar</p>' );
  83. } );
  84. } );
  85. describe( 'editing pipeline conversion', () => {
  86. it( 'should convert attribute', () => {
  87. setModelData( model, '<paragraph><$text bold="true">foo</$text>bar</paragraph>' );
  88. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( '<p><strong>foo</strong>bar</p>' );
  89. } );
  90. } );
  91. } );