boldediting.js 3.7 KB

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