removeformatcommand.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. // Text attribtue "irrelevant" will be used to make sure that non-formatting
  26. // is note being removed.
  27. if ( ctx.endsWith( 'p $text' ) && attributeName == 'irrelevant' ) {
  28. return true;
  29. }
  30. } );
  31. } );
  32. } );
  33. afterEach( () => {
  34. editor.destroy();
  35. } );
  36. it( 'is a command', () => {
  37. expect( RemoveFormatCommand.prototype ).to.be.instanceOf( Command );
  38. expect( command ).to.be.instanceOf( Command );
  39. } );
  40. describe( 'isEnabled', () => {
  41. const expectEnabledPropertyToBe = expectedValue => expect( command ).to.have.property( 'isEnabled', expectedValue );
  42. const cases = {
  43. 'state when in non-formatting markup': {
  44. input: '<p>fo[]o</p>',
  45. assert: () => expectEnabledPropertyToBe( false )
  46. },
  47. 'state with collapsed selection in formatting markup': {
  48. input: '<p>f<$text bold="true">o[]o</$text></p>',
  49. assert: () => expectEnabledPropertyToBe( true )
  50. },
  51. 'state with selection containing formatting in the middle': {
  52. input: '<p>f[oo <$text bold="true">bar</$text> ba]z</p>',
  53. assert: () => expectEnabledPropertyToBe( true )
  54. },
  55. 'state with partially selected formatting at the start': {
  56. input: '<p><$text bold="true">b[ar</$text> ba]z</p>',
  57. assert: () => expectEnabledPropertyToBe( true )
  58. },
  59. 'state with partially selected formatting at the end': {
  60. input: '<p>f[oo <$text bold="true">ba]z</$text></p>',
  61. assert: () => expectEnabledPropertyToBe( true )
  62. },
  63. 'state with formatted selection alone': {
  64. input: '<p>fo[]o</p>',
  65. setDataOptions: {
  66. selectionAttributes: {
  67. bold: true,
  68. irrelevant: true
  69. }
  70. },
  71. assert: () => expectEnabledPropertyToBe( true )
  72. }
  73. };
  74. generateTypicalUseCases( cases );
  75. } );
  76. describe( 'execute()', () => {
  77. const expectModelToBeEqual = expectedValue => expect( getData( model ) ).to.equal( expectedValue );
  78. const cases = {
  79. 'state when in non-formatting markup': {
  80. input: '<p>fo[]o</p>',
  81. assert: () => expectModelToBeEqual( '<p>fo[]o</p>' )
  82. },
  83. 'state with collapsed selection in formatting markup': {
  84. input: '<p>f<$text bold="true">o[]o</$text></p>',
  85. assert: () => expectModelToBeEqual( '<p>f<$text bold="true">o</$text>[]<$text bold="true">o</$text></p>' )
  86. },
  87. 'state with selection containing formatting in the middle': {
  88. input: '<p>f[oo <$text bold="true">bar</$text> ba]z</p>',
  89. assert: () => expectModelToBeEqual( '<p>f[oo bar ba]z</p>' )
  90. },
  91. 'state with partially selected formatting at the start': {
  92. input: '<p><$text bold="true">b[ar</$text> ba]z</p>',
  93. assert: () => expectModelToBeEqual( '<p><$text bold="true">b</$text>[ar ba]z</p>' )
  94. },
  95. 'state with partially selected formatting at the end': {
  96. input: '<p>f[oo <$text bold="true">ba]z</$text></p>',
  97. assert: () => expectModelToBeEqual( '<p>f[oo ba]<$text bold="true">z</$text></p>' )
  98. },
  99. 'state with formatted selection alone': {
  100. input: '<p>fo[]o</p>',
  101. setDataOptions: {
  102. selectionAttributes: {
  103. bold: true,
  104. irrelevant: true
  105. }
  106. },
  107. assert: () => {
  108. expect( model.document.selection.hasAttribute( 'bold' ) ).to.equal( false );
  109. expect( model.document.selection.hasAttribute( 'irrelevant' ) ).to.equal( true );
  110. }
  111. }
  112. };
  113. generateTypicalUseCases( cases, {
  114. beforeAssert: () => command.execute()
  115. } );
  116. } );
  117. function generateTypicalUseCases( useCases, options ) {
  118. for ( const [ key, testConfig ] of Object.entries( useCases ) ) {
  119. it( key, () => {
  120. setData( model, testConfig.input, testConfig.setDataOptions );
  121. if ( options && options.beforeAssert ) {
  122. options.beforeAssert();
  123. }
  124. testConfig.assert();
  125. } );
  126. }
  127. }
  128. } );