removeformatcommand.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 RemoveFormatCommand from '../src/removeformatcommand';
  6. import Command from '@ckeditor/ckeditor5-core/src/command';
  7. import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
  8. import {
  9. getData,
  10. setData
  11. } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  12. describe( 'RemoveFormatCommand', () => {
  13. let editor, model, command;
  14. beforeEach( () => {
  15. return ModelTestEditor.create()
  16. .then( newEditor => {
  17. editor = newEditor;
  18. model = editor.model;
  19. command = new RemoveFormatCommand( newEditor );
  20. editor.commands.add( 'removeFormat', command );
  21. model.schema.register( 'p', {
  22. inheritAllFrom: '$block'
  23. } );
  24. model.schema.addAttributeCheck( ( ctx, attributeName ) => {
  25. // Bold will be used as an example formatting attribute.
  26. if ( ctx.endsWith( 'p $text' ) && attributeName == 'bold' ) {
  27. return true;
  28. }
  29. } );
  30. model.schema.addAttributeCheck( ( ctx, attributeName ) => {
  31. // Text attribtue "irrelevant" will be used to make sure that non-formatting
  32. // is note being removed.
  33. if ( ctx.endsWith( 'p $text' ) && attributeName == 'irrelevant' ) {
  34. return true;
  35. }
  36. } );
  37. model.schema.setAttributeProperties( 'bold', {
  38. isFormatting: true
  39. } );
  40. } );
  41. } );
  42. afterEach( () => {
  43. editor.destroy();
  44. } );
  45. it( 'is a command', () => {
  46. expect( RemoveFormatCommand.prototype ).to.be.instanceOf( Command );
  47. expect( command ).to.be.instanceOf( Command );
  48. } );
  49. describe( 'isEnabled', () => {
  50. const expectEnabledPropertyToBe = expectedValue => expect( command ).to.have.property( 'isEnabled', expectedValue );
  51. const cases = {
  52. 'state when in non-formatting markup': {
  53. input: '<p>fo[]o</p>',
  54. assert: () => expectEnabledPropertyToBe( false )
  55. },
  56. 'state with collapsed selection in formatting markup': {
  57. input: '<p>f<$text bold="true">o[]o</$text></p>',
  58. assert: () => expectEnabledPropertyToBe( true )
  59. },
  60. 'state with selection containing formatting in the middle': {
  61. input: '<p>f[oo <$text bold="true">bar</$text> ba]z</p>',
  62. assert: () => expectEnabledPropertyToBe( true )
  63. },
  64. 'state with partially selected formatting at the start': {
  65. input: '<p><$text bold="true">b[ar</$text> ba]z</p>',
  66. assert: () => expectEnabledPropertyToBe( true )
  67. },
  68. 'state with partially selected formatting at the end': {
  69. input: '<p>f[oo <$text bold="true">ba]z</$text></p>',
  70. assert: () => expectEnabledPropertyToBe( true )
  71. },
  72. 'state with formatted selection alone': {
  73. input: '<p>fo[]o</p>',
  74. setDataOptions: {
  75. selectionAttributes: {
  76. bold: true,
  77. irrelevant: true
  78. }
  79. },
  80. assert: () => expectEnabledPropertyToBe( true )
  81. }
  82. };
  83. generateTypicalUseCases( cases );
  84. } );
  85. describe( 'execute()', () => {
  86. const expectModelToBeEqual = expectedValue => expect( getData( model ) ).to.equal( expectedValue );
  87. const cases = {
  88. 'state when in non-formatting markup': {
  89. input: '<p>fo[]o</p>',
  90. assert: () => expectModelToBeEqual( '<p>fo[]o</p>' )
  91. },
  92. 'state with collapsed selection in formatting markup': {
  93. input: '<p>f<$text bold="true">o[]o</$text></p>',
  94. assert: () => expectModelToBeEqual( '<p>f<$text bold="true">o</$text>[]<$text bold="true">o</$text></p>' )
  95. },
  96. 'state with selection containing formatting in the middle': {
  97. input: '<p>f[oo <$text bold="true">bar</$text> ba]z</p>',
  98. assert: () => expectModelToBeEqual( '<p>f[oo bar ba]z</p>' )
  99. },
  100. 'state with partially selected formatting at the start': {
  101. input: '<p><$text bold="true">b[ar</$text> ba]z</p>',
  102. assert: () => expectModelToBeEqual( '<p><$text bold="true">b</$text>[ar ba]z</p>' )
  103. },
  104. 'state with partially selected formatting at the end': {
  105. input: '<p>f[oo <$text bold="true">ba]z</$text></p>',
  106. assert: () => expectModelToBeEqual( '<p>f[oo ba]<$text bold="true">z</$text></p>' )
  107. },
  108. 'state with formatted selection alone': {
  109. input: '<p>fo[]o</p>',
  110. setDataOptions: {
  111. selectionAttributes: {
  112. bold: true,
  113. irrelevant: true
  114. }
  115. },
  116. assert: () => {
  117. expect( model.document.selection.hasAttribute( 'bold' ) ).to.equal( false );
  118. expect( model.document.selection.hasAttribute( 'irrelevant' ) ).to.equal( true );
  119. }
  120. }
  121. };
  122. generateTypicalUseCases( cases, {
  123. beforeAssert: () => command.execute()
  124. } );
  125. } );
  126. function generateTypicalUseCases( useCases, options ) {
  127. for ( const [ key, testConfig ] of Object.entries( useCases ) ) {
  128. it( key, () => {
  129. setData( model, testConfig.input, testConfig.setDataOptions );
  130. if ( options && options.beforeAssert ) {
  131. options.beforeAssert();
  132. }
  133. testConfig.assert();
  134. } );
  135. }
  136. }
  137. } );