highlightediting.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  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 set proper schema rules', () => {
  26. expect( editor.model.schema.checkAttribute( [ '$block', '$text' ], 'highlight' ) ).to.be.true;
  27. expect( editor.model.schema.checkAttribute( [ '$clipboardHolder', '$text' ], 'highlight' ) ).to.be.true;
  28. expect( editor.model.schema.checkAttribute( [ '$block' ], 'highlight' ) ).to.be.false;
  29. } );
  30. it( 'adds highlight command', () => {
  31. expect( editor.commands.get( 'highlight' ) ).to.be.instanceOf( HighlightCommand );
  32. } );
  33. describe( 'data pipeline conversions', () => {
  34. it( 'should convert defined marker classes', () => {
  35. const data = '<p>f<mark class="marker-yellow">o</mark>o</p>';
  36. editor.setData( data );
  37. expect( getModelData( model ) ).to.equal( '<paragraph>[]f<$text highlight="yellowMarker">o</$text>o</paragraph>' );
  38. expect( editor.getData() ).to.equal( data );
  39. } );
  40. it( 'should convert only one defined marker classes', () => {
  41. editor.setData( '<p>f<mark class="marker-green marker-yellow">o</mark>o</p>' );
  42. expect( getModelData( model ) ).to.equal( '<paragraph>[]f<$text highlight="yellowMarker">o</$text>o</paragraph>' );
  43. expect( editor.getData() ).to.equal( '<p>f<mark class="marker-yellow">o</mark>o</p>' );
  44. } );
  45. it( 'should not convert undefined marker classes', () => {
  46. editor.setData( '<p>f<mark class="some-unknown-marker">o</mark>o</p>' );
  47. expect( getModelData( model ) ).to.equal( '<paragraph>[]foo</paragraph>' );
  48. expect( editor.getData() ).to.equal( '<p>foo</p>' );
  49. } );
  50. it( 'should not convert marker without class', () => {
  51. editor.setData( '<p>f<mark>o</mark>o</p>' );
  52. expect( getModelData( model ) ).to.equal( '<paragraph>[]foo</paragraph>' );
  53. expect( editor.getData() ).to.equal( '<p>foo</p>' );
  54. } );
  55. } );
  56. describe( 'editing pipeline conversion', () => {
  57. it( 'should convert mark element with defined class', () => {
  58. setModelData( model, '<paragraph>f<$text highlight="yellowMarker">o</$text>o</paragraph>' );
  59. expect( editor.getData() ).to.equal( '<p>f<mark class="marker-yellow">o</mark>o</p>' );
  60. } );
  61. } );
  62. describe( 'config', () => {
  63. describe( 'default value', () => {
  64. it( 'should be set', () => {
  65. expect( editor.config.get( 'highlight' ) ).to.deep.equal( {
  66. options: [
  67. {
  68. model: 'yellowMarker',
  69. class: 'marker-yellow',
  70. title: 'Yellow marker',
  71. color: 'var(--ck-highlight-marker-yellow)',
  72. type: 'marker'
  73. },
  74. {
  75. model: 'greenMarker',
  76. class: 'marker-green',
  77. title: 'Green marker',
  78. color: 'var(--ck-highlight-marker-green)',
  79. type: 'marker'
  80. },
  81. {
  82. model: 'pinkMarker',
  83. class: 'marker-pink',
  84. title: 'Pink marker',
  85. color: 'var(--ck-highlight-marker-pink)',
  86. type: 'marker'
  87. },
  88. {
  89. model: 'blueMarker',
  90. class: 'marker-blue',
  91. title: 'Blue marker',
  92. color: 'var(--ck-highlight-marker-blue)',
  93. type: 'marker'
  94. },
  95. {
  96. model: 'redPen',
  97. class: 'pen-red',
  98. title: 'Red pen',
  99. color: 'var(--ck-highlight-pen-red)',
  100. type: 'pen'
  101. },
  102. {
  103. model: 'greenPen',
  104. class: 'pen-green',
  105. title: 'Green pen',
  106. color: 'var(--ck-highlight-pen-green)',
  107. type: 'pen'
  108. }
  109. ]
  110. } );
  111. } );
  112. } );
  113. } );
  114. } );