codeediting.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 CodeEditing from '../../src/code/codeediting';
  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. /* global document */
  13. describe( 'CodeEditing', () => {
  14. let editor, model;
  15. beforeEach( () => {
  16. return VirtualTestEditor
  17. .create( {
  18. plugins: [ Paragraph, CodeEditing ]
  19. } )
  20. .then( newEditor => {
  21. editor = newEditor;
  22. model = editor.model;
  23. } );
  24. } );
  25. afterEach( () => {
  26. return editor.destroy();
  27. } );
  28. it( 'should have pluginName', () => {
  29. expect( CodeEditing.pluginName ).to.equal( 'CodeEditing' );
  30. } );
  31. it( 'should be loaded', () => {
  32. expect( editor.plugins.get( CodeEditing ) ).to.be.instanceOf( CodeEditing );
  33. } );
  34. it( 'should set proper schema rules', () => {
  35. expect( model.schema.checkAttribute( [ '$root', '$block', '$text' ], 'code' ) ).to.be.true;
  36. expect( model.schema.checkAttribute( [ '$clipboardHolder', '$text' ], 'code' ) ).to.be.true;
  37. } );
  38. it( 'should be marked with a formatting property', () => {
  39. expect( model.schema.getAttributeProperties( 'code' ) ).to.include( {
  40. isFormatting: true
  41. } );
  42. } );
  43. it( 'its attribute is marked with a copOnEnter property', () => {
  44. expect( model.schema.getAttributeProperties( 'code' ) ).to.include( {
  45. copyOnEnter: false
  46. } );
  47. } );
  48. describe( 'command', () => {
  49. it( 'should register code command', () => {
  50. const command = editor.commands.get( 'code' );
  51. expect( command ).to.be.instanceOf( AttributeCommand );
  52. expect( command ).to.have.property( 'attributeKey', 'code' );
  53. } );
  54. } );
  55. describe( 'data pipeline conversions', () => {
  56. it( 'should convert <code> to code attribute', () => {
  57. editor.setData( '<p><code>foo</code>bar</p>' );
  58. expect( getModelData( model, { withoutSelection: true } ) )
  59. .to.equal( '<paragraph><$text code="true">foo</$text>bar</paragraph>' );
  60. expect( editor.getData() ).to.equal( '<p><code>foo</code>bar</p>' );
  61. } );
  62. it( 'should convert word-wrap:break-word to code attribute', () => {
  63. editor.setData( '<p><span style="word-wrap: break-word">foo</span>bar</p>' );
  64. expect( getModelData( model, { withoutSelection: true } ) )
  65. .to.equal( '<paragraph><$text code="true">foo</$text>bar</paragraph>' );
  66. expect( editor.getData() ).to.equal( '<p><code>foo</code>bar</p>' );
  67. } );
  68. it( 'should be integrated with autoparagraphing', () => {
  69. editor.setData( '<code>foo</code>bar' );
  70. expect( getModelData( model, { withoutSelection: true } ) )
  71. .to.equal( '<paragraph><$text code="true">foo</$text>bar</paragraph>' );
  72. expect( editor.getData() ).to.equal( '<p><code>foo</code>bar</p>' );
  73. } );
  74. } );
  75. describe( 'editing pipeline conversion', () => {
  76. it( 'should convert attribute', () => {
  77. setModelData( model, '<paragraph><$text code="true">foo</$text>bar</paragraph>' );
  78. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( '<p><code>foo</code>bar</p>' );
  79. } );
  80. } );
  81. it( 'should add `ck-code_selected` class when caret enters the element', () => {
  82. // Put selection before the link element.
  83. setModelData( editor.model, '<paragraph>foo[]<$text code="true">ba</$text>r</paragraph>' );
  84. // So let's simulate the `keydown` event.
  85. editor.editing.view.document.fire( 'keydown', {
  86. keyCode: keyCodes.arrowright,
  87. preventDefault: () => {},
  88. domTarget: document.body
  89. } );
  90. expect( getViewData( editor.editing.view ) ).to.equal(
  91. '<p>foo<code class="ck-code_selected">{}ba</code>r</p>'
  92. );
  93. } );
  94. it( 'should remove `ck-code_selected` class when caret leaves the element', () => {
  95. // Put selection before the link element.
  96. setModelData( editor.model, '<paragraph>foo<$text code="true">ba[]</$text>r</paragraph>' );
  97. // So let's simulate the `keydown` event.
  98. editor.editing.view.document.fire( 'keydown', {
  99. keyCode: keyCodes.arrowright,
  100. preventDefault: () => {},
  101. domTarget: document.body
  102. } );
  103. expect( getViewData( editor.editing.view ) ).to.equal(
  104. '<p>foo<code>ba</code>{}r</p>'
  105. );
  106. } );
  107. } );