highlightediting.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 HighlightEditing from './../src/highlightediting';
  6. import HighlightCommand from './../src/highlightcommand';
  7. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  8. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  9. import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  10. describe( 'HighlightEditing', () => {
  11. let editor, model;
  12. beforeEach( () => {
  13. return VirtualTestEditor
  14. .create( {
  15. plugins: [ HighlightEditing, Paragraph ]
  16. } )
  17. .then( newEditor => {
  18. editor = newEditor;
  19. model = editor.model;
  20. } );
  21. } );
  22. afterEach( () => {
  23. editor.destroy();
  24. } );
  25. it( 'should have pluginName', () => {
  26. expect( HighlightEditing.pluginName ).to.equal( 'HighlightEditing' );
  27. } );
  28. it( 'should set proper schema rules', () => {
  29. expect( editor.model.schema.checkAttribute( [ '$block', '$text' ], 'highlight' ) ).to.be.true;
  30. expect( editor.model.schema.checkAttribute( [ '$clipboardHolder', '$text' ], 'highlight' ) ).to.be.true;
  31. expect( editor.model.schema.checkAttribute( [ '$block' ], 'highlight' ) ).to.be.false;
  32. } );
  33. it( 'adds highlight command', () => {
  34. expect( editor.commands.get( 'highlight' ) ).to.be.instanceOf( HighlightCommand );
  35. } );
  36. describe( 'data pipeline conversions', () => {
  37. it( 'should convert defined marker classes', () => {
  38. const data = '<p>f<mark class="marker-yellow">o</mark>o</p>';
  39. editor.setData( data );
  40. expect( getModelData( model ) ).to.equal( '<paragraph>[]f<$text highlight="yellowMarker">o</$text>o</paragraph>' );
  41. expect( editor.getData() ).to.equal( data );
  42. } );
  43. it( 'should convert only one defined marker classes', () => {
  44. editor.setData( '<p>f<mark class="marker-green marker-yellow">o</mark>o</p>' );
  45. expect( getModelData( model ) ).to.equal( '<paragraph>[]f<$text highlight="greenMarker">o</$text>o</paragraph>' );
  46. expect( editor.getData() ).to.equal( '<p>f<mark class="marker-green">o</mark>o</p>' );
  47. } );
  48. it( 'should not convert undefined marker classes', () => {
  49. editor.setData( '<p>f<mark class="some-unknown-marker">o</mark>o</p>' );
  50. expect( getModelData( model ) ).to.equal( '<paragraph>[]foo</paragraph>' );
  51. expect( editor.getData() ).to.equal( '<p>foo</p>' );
  52. } );
  53. it( 'should not convert marker without class', () => {
  54. editor.setData( '<p>f<mark>o</mark>o</p>' );
  55. expect( getModelData( model ) ).to.equal( '<paragraph>[]foo</paragraph>' );
  56. expect( editor.getData() ).to.equal( '<p>foo</p>' );
  57. } );
  58. } );
  59. describe( 'editing pipeline conversion', () => {
  60. it( 'should convert mark element with defined class', () => {
  61. setModelData( model, '<paragraph>f<$text highlight="yellowMarker">o</$text>o</paragraph>' );
  62. expect( editor.getData() ).to.equal( '<p>f<mark class="marker-yellow">o</mark>o</p>' );
  63. } );
  64. } );
  65. describe( 'config', () => {
  66. describe( 'default value', () => {
  67. it( 'should be set', () => {
  68. expect( editor.config.get( 'highlight' ) ).to.deep.equal( {
  69. options: [
  70. {
  71. model: 'yellowMarker',
  72. class: 'marker-yellow',
  73. title: 'Yellow marker',
  74. color: 'var(--ck-highlight-marker-yellow)',
  75. type: 'marker'
  76. },
  77. {
  78. model: 'greenMarker',
  79. class: 'marker-green',
  80. title: 'Green marker',
  81. color: 'var(--ck-highlight-marker-green)',
  82. type: 'marker'
  83. },
  84. {
  85. model: 'pinkMarker',
  86. class: 'marker-pink',
  87. title: 'Pink marker',
  88. color: 'var(--ck-highlight-marker-pink)',
  89. type: 'marker'
  90. },
  91. {
  92. model: 'blueMarker',
  93. class: 'marker-blue',
  94. title: 'Blue marker',
  95. color: 'var(--ck-highlight-marker-blue)',
  96. type: 'marker'
  97. },
  98. {
  99. model: 'redPen',
  100. class: 'pen-red',
  101. title: 'Red pen',
  102. color: 'var(--ck-highlight-pen-red)',
  103. type: 'pen'
  104. },
  105. {
  106. model: 'greenPen',
  107. class: 'pen-green',
  108. title: 'Green pen',
  109. color: 'var(--ck-highlight-pen-green)',
  110. type: 'pen'
  111. }
  112. ]
  113. } );
  114. } );
  115. } );
  116. } );
  117. } );