removeformatcommand.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  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. } );
  38. } );
  39. afterEach( () => {
  40. editor.destroy();
  41. } );
  42. it( 'is a command', () => {
  43. expect( RemoveFormatCommand.prototype ).to.be.instanceOf( Command );
  44. expect( command ).to.be.instanceOf( Command );
  45. } );
  46. describe( 'isEnabled', () => {
  47. const expectEnabledPropertyToBe = expectedValue => expect( command ).to.have.property( 'isEnabled', expectedValue );
  48. const cases = {
  49. 'state when in non-formatting markup': {
  50. input: '<p>fo[]o</p>',
  51. assert: () => expectEnabledPropertyToBe( false )
  52. },
  53. 'state with collapsed selection in formatting markup': {
  54. input: '<p>f<$text bold="true">o[]o</$text></p>',
  55. assert: () => expectEnabledPropertyToBe( true )
  56. },
  57. 'state with selection containing formatting in the middle': {
  58. input: '<p>f[oo <$text bold="true">bar</$text> ba]z</p>',
  59. assert: () => expectEnabledPropertyToBe( true )
  60. },
  61. 'state with partially selected formatting at the start': {
  62. input: '<p><$text bold="true">b[ar</$text> ba]z</p>',
  63. assert: () => expectEnabledPropertyToBe( true )
  64. },
  65. 'state with partially selected formatting at the end': {
  66. input: '<p>f[oo <$text bold="true">ba]z</$text></p>',
  67. assert: () => expectEnabledPropertyToBe( true )
  68. },
  69. 'state with formatted selection alone': {
  70. input: '<p>fo[]o</p>',
  71. setDataOptions: {
  72. selectionAttributes: {
  73. bold: true,
  74. irrelevant: true
  75. }
  76. },
  77. assert: () => expectEnabledPropertyToBe( true )
  78. }
  79. };
  80. generateTypicalUseCases( cases );
  81. } );
  82. describe( 'execute()', () => {
  83. const expectModelToBeEqual = expectedValue => expect( getData( model ) ).to.equal( expectedValue );
  84. const cases = {
  85. 'state when in non-formatting markup': {
  86. input: '<p>fo[]o</p>',
  87. assert: () => expectModelToBeEqual( '<p>fo[]o</p>' )
  88. },
  89. 'state with collapsed selection in formatting markup': {
  90. input: '<p>f<$text bold="true">o[]o</$text></p>',
  91. assert: () => expectModelToBeEqual( '<p>f<$text bold="true">o</$text>[]<$text bold="true">o</$text></p>' )
  92. },
  93. 'state with selection containing formatting in the middle': {
  94. input: '<p>f[oo <$text bold="true">bar</$text> ba]z</p>',
  95. assert: () => expectModelToBeEqual( '<p>f[oo bar ba]z</p>' )
  96. },
  97. 'state with partially selected formatting at the start': {
  98. input: '<p><$text bold="true">b[ar</$text> ba]z</p>',
  99. assert: () => expectModelToBeEqual( '<p><$text bold="true">b</$text>[ar ba]z</p>' )
  100. },
  101. 'state with partially selected formatting at the end': {
  102. input: '<p>f[oo <$text bold="true">ba]z</$text></p>',
  103. assert: () => expectModelToBeEqual( '<p>f[oo ba]<$text bold="true">z</$text></p>' )
  104. },
  105. 'state with formatted selection alone': {
  106. input: '<p>fo[]o</p>',
  107. setDataOptions: {
  108. selectionAttributes: {
  109. bold: true,
  110. irrelevant: true
  111. }
  112. },
  113. assert: () => {
  114. expect( model.document.selection.hasAttribute( 'bold' ) ).to.equal( false );
  115. expect( model.document.selection.hasAttribute( 'irrelevant' ) ).to.equal( true );
  116. }
  117. }
  118. };
  119. generateTypicalUseCases( cases, {
  120. beforeAssert: () => command.execute()
  121. } );
  122. } );
  123. function generateTypicalUseCases( useCases, options ) {
  124. for ( const [ key, testConfig ] of Object.entries( useCases ) ) {
  125. it( key, () => {
  126. setData( model, testConfig.input, testConfig.setDataOptions );
  127. if ( options && options.beforeAssert ) {
  128. options.beforeAssert();
  129. }
  130. testConfig.assert();
  131. } );
  132. }
  133. }
  134. } );