removeformatcommand.js 3.9 KB

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