boldengine.js 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import BoldEngine from '../src/boldengine';
  6. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  7. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  8. import ToggleAttributeCommand from '@ckeditor/ckeditor5-core/src/command/toggleattributecommand';
  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. describe( 'BoldEngine', () => {
  12. let editor, doc;
  13. beforeEach( () => {
  14. return VirtualTestEditor.create( {
  15. plugins: [ Paragraph, BoldEngine ]
  16. } )
  17. .then( newEditor => {
  18. editor = newEditor;
  19. doc = editor.document;
  20. } );
  21. } );
  22. it( 'should be loaded', () => {
  23. expect( editor.plugins.get( BoldEngine ) ).to.be.instanceOf( BoldEngine );
  24. } );
  25. it( 'should set proper schema rules', () => {
  26. expect( doc.schema.check( { name: '$inline', attributes: [ 'bold' ], inside: '$root' } ) ).to.be.false;
  27. expect( doc.schema.check( { name: '$inline', attributes: [ 'bold' ], inside: '$block' } ) ).to.be.true;
  28. } );
  29. describe( 'command', () => {
  30. it( 'should register bold command', () => {
  31. const command = editor.commands.get( 'bold' );
  32. expect( command ).to.be.instanceOf( ToggleAttributeCommand );
  33. expect( command ).to.have.property( 'attributeKey', 'bold' );
  34. } );
  35. } );
  36. describe( 'data pipeline conversions', () => {
  37. it( 'should convert <strong> to bold attribute', () => {
  38. editor.setData( '<p><strong>foo</strong>bar</p>' );
  39. expect( getModelData( doc, { withoutSelection: true } ) )
  40. .to.equal( '<paragraph><$text bold="true">foo</$text>bar</paragraph>' );
  41. expect( editor.getData() ).to.equal( '<p><strong>foo</strong>bar</p>' );
  42. } );
  43. it( 'should convert <b> to bold attribute', () => {
  44. editor.setData( '<p><b>foo</b>bar</p>' );
  45. expect( getModelData( doc, { withoutSelection: true } ) )
  46. .to.equal( '<paragraph><$text bold="true">foo</$text>bar</paragraph>' );
  47. expect( editor.getData() ).to.equal( '<p><strong>foo</strong>bar</p>' );
  48. } );
  49. it( 'should convert font-weight:bold to bold attribute', () => {
  50. editor.setData( '<p><span style="font-weight: bold;">foo</span>bar</p>' );
  51. expect( getModelData( doc, { withoutSelection: true } ) )
  52. .to.equal( '<paragraph><$text bold="true">foo</$text>bar</paragraph>' );
  53. expect( editor.getData() ).to.equal( '<p><strong>foo</strong>bar</p>' );
  54. } );
  55. it( 'should be integrated with autoparagraphing', () => {
  56. // Incorrect results because autoparagraphing works incorrectly (issue in paragraph).
  57. // https://github.com/ckeditor/ckeditor5-paragraph/issues/10
  58. editor.setData( '<strong>foo</strong>bar' );
  59. expect( getModelData( doc, { withoutSelection: true } ) ).to.equal( '<paragraph>foobar</paragraph>' );
  60. expect( editor.getData() ).to.equal( '<p>foobar</p>' );
  61. } );
  62. } );
  63. describe( 'editing pipeline conversion', () => {
  64. it( 'should convert attribute', () => {
  65. setModelData( doc, '<paragraph><$text bold="true">foo</$text>bar</paragraph>' );
  66. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( '<p><strong>foo</strong>bar</p>' );
  67. } );
  68. } );
  69. } );