removeformatcommand.js 5.7 KB

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