8
0

undointegration.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 Autoformat from '../src/autoformat';
  6. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  7. import ListEditing from '@ckeditor/ckeditor5-list/src/listediting';
  8. import HeadingEditing from '@ckeditor/ckeditor5-heading/src/headingediting';
  9. import BoldEditing from '@ckeditor/ckeditor5-basic-styles/src/bold/boldediting';
  10. import CodeEditing from '@ckeditor/ckeditor5-basic-styles/src/code/codeediting';
  11. import StrikethroughEditing from '@ckeditor/ckeditor5-basic-styles/src/strikethrough/strikethroughediting';
  12. import ItalicEditing from '@ckeditor/ckeditor5-basic-styles/src/italic/italicediting';
  13. import BlockQuoteEditing from '@ckeditor/ckeditor5-block-quote/src/blockquoteediting';
  14. import Enter from '@ckeditor/ckeditor5-enter/src/enter';
  15. import Undo from '@ckeditor/ckeditor5-undo/src/undoediting';
  16. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  17. import { setData, getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  18. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  19. describe( 'Autoformat undo integration', () => {
  20. let editor, model, doc;
  21. testUtils.createSinonSandbox();
  22. beforeEach( () => {
  23. return VirtualTestEditor
  24. .create( {
  25. plugins: [
  26. Enter,
  27. Undo,
  28. Paragraph,
  29. Autoformat,
  30. ListEditing,
  31. HeadingEditing,
  32. BoldEditing,
  33. ItalicEditing,
  34. CodeEditing,
  35. StrikethroughEditing,
  36. BlockQuoteEditing
  37. ]
  38. } )
  39. .then( newEditor => {
  40. editor = newEditor;
  41. model = editor.model;
  42. doc = model.document;
  43. } );
  44. } );
  45. afterEach( () => {
  46. return editor.destroy();
  47. } );
  48. describe( 'inline', () => {
  49. it( 'should undo replacing "**" with bold', () => {
  50. setData( model, '<paragraph>**foobar*[]</paragraph>' );
  51. model.change( writer => {
  52. writer.insertText( '*', doc.selection.getFirstPosition() );
  53. } );
  54. expect( getData( model ) ).to.equal( '<paragraph><$text bold="true">foobar</$text>[]</paragraph>' );
  55. editor.execute( 'undo' );
  56. expect( getData( model ) ).to.equal( '<paragraph>**foobar**[]</paragraph>' );
  57. } );
  58. it( 'should undo replacing "__" with bold', () => {
  59. setData( model, '<paragraph>__foobar_[]</paragraph>' );
  60. model.change( writer => {
  61. writer.insertText( '_', doc.selection.getFirstPosition() );
  62. } );
  63. expect( getData( model ) ).to.equal( '<paragraph><$text bold="true">foobar</$text>[]</paragraph>' );
  64. editor.execute( 'undo' );
  65. expect( getData( model ) ).to.equal( '<paragraph>__foobar__[]</paragraph>' );
  66. } );
  67. it( 'should undo replacing "*" with italic', () => {
  68. setData( model, '<paragraph>*foobar[]</paragraph>' );
  69. model.change( writer => {
  70. writer.insertText( '*', doc.selection.getFirstPosition() );
  71. } );
  72. expect( getData( model ) ).to.equal( '<paragraph><$text italic="true">foobar</$text>[]</paragraph>' );
  73. editor.execute( 'undo' );
  74. expect( getData( model ) ).to.equal( '<paragraph>*foobar*[]</paragraph>' );
  75. } );
  76. it( 'should undo replacing "_" with italic', () => {
  77. setData( model, '<paragraph>_foobar[]</paragraph>' );
  78. model.change( writer => {
  79. writer.insertText( '_', doc.selection.getFirstPosition() );
  80. } );
  81. expect( getData( model ) ).to.equal( '<paragraph><$text italic="true">foobar</$text>[]</paragraph>' );
  82. editor.execute( 'undo' );
  83. expect( getData( model ) ).to.equal( '<paragraph>_foobar_[]</paragraph>' );
  84. } );
  85. it( 'should undo replacing "`" with code', () => {
  86. setData( model, '<paragraph>`foobar[]</paragraph>' );
  87. model.change( writer => {
  88. writer.insertText( '`', doc.selection.getFirstPosition() );
  89. } );
  90. expect( getData( model ) ).to.equal( '<paragraph><$text code="true">foobar</$text>[]</paragraph>' );
  91. editor.execute( 'undo' );
  92. expect( getData( model ) ).to.equal( '<paragraph>`foobar`[]</paragraph>' );
  93. } );
  94. it( 'should undo replacing "~~" with strikethrough', () => {
  95. setData( model, '<paragraph>~~foobar~[]</paragraph>' );
  96. model.change( writer => {
  97. writer.insertText( '~', doc.selection.getFirstPosition() );
  98. } );
  99. expect( getData( model ) ).to.equal( '<paragraph><$text strikethrough="true">foobar</$text>[]</paragraph>' );
  100. editor.execute( 'undo' );
  101. expect( getData( model ) ).to.equal( '<paragraph>~~foobar~~[]</paragraph>' );
  102. } );
  103. } );
  104. describe( 'block', () => {
  105. it( 'should work when replacing asterisk', () => {
  106. setData( model, '<paragraph>*[]</paragraph>' );
  107. model.change( writer => {
  108. writer.insertText( ' ', doc.selection.getFirstPosition() );
  109. } );
  110. expect( getData( model ) ).to.equal( '<listItem listIndent="0" listType="bulleted">[]</listItem>' );
  111. editor.execute( 'undo' );
  112. expect( getData( model ) ).to.equal( '<paragraph>* []</paragraph>' );
  113. } );
  114. it( 'should work when replacing minus character', () => {
  115. setData( model, '<paragraph>-[]</paragraph>' );
  116. model.change( writer => {
  117. writer.insertText( ' ', doc.selection.getFirstPosition() );
  118. } );
  119. expect( getData( model ) ).to.equal( '<listItem listIndent="0" listType="bulleted">[]</listItem>' );
  120. editor.execute( 'undo' );
  121. expect( getData( model ) ).to.equal( '<paragraph>- []</paragraph>' );
  122. } );
  123. it( 'should work when replacing digit with numbered list item using the dot format', () => {
  124. setData( model, '<paragraph>1.[]</paragraph>' );
  125. model.change( writer => {
  126. writer.insertText( ' ', doc.selection.getFirstPosition() );
  127. } );
  128. expect( getData( model ) ).to.equal( '<listItem listIndent="0" listType="numbered">[]</listItem>' );
  129. editor.execute( 'undo' );
  130. expect( getData( model ) ).to.equal( '<paragraph>1. []</paragraph>' );
  131. } );
  132. it( 'should work when replacing digit with numbered list item using the parenthesis format', () => {
  133. setData( model, '<paragraph>1)[]</paragraph>' );
  134. model.change( writer => {
  135. writer.insertText( ' ', doc.selection.getFirstPosition() );
  136. } );
  137. expect( getData( model ) ).to.equal( '<listItem listIndent="0" listType="numbered">[]</listItem>' );
  138. editor.execute( 'undo' );
  139. expect( getData( model ) ).to.equal( '<paragraph>1) []</paragraph>' );
  140. } );
  141. it( 'should work when replacing hash character with heading', () => {
  142. setData( model, '<paragraph>#[]</paragraph>' );
  143. model.change( writer => {
  144. writer.insertText( ' ', doc.selection.getFirstPosition() );
  145. } );
  146. expect( getData( model ) ).to.equal( '<heading1>[]</heading1>' );
  147. editor.execute( 'undo' );
  148. expect( getData( model ) ).to.equal( '<paragraph># []</paragraph>' );
  149. } );
  150. it( 'should work when replacing two hash characters with heading level 2', () => {
  151. setData( model, '<paragraph>##[]</paragraph>' );
  152. model.change( writer => {
  153. writer.insertText( ' ', doc.selection.getFirstPosition() );
  154. } );
  155. expect( getData( model ) ).to.equal( '<heading2>[]</heading2>' );
  156. editor.execute( 'undo' );
  157. expect( getData( model ) ).to.equal( '<paragraph>## []</paragraph>' );
  158. } );
  159. it( 'should work when replacing greater-than character with block quote', () => {
  160. setData( model, '<paragraph>>[]</paragraph>' );
  161. model.change( writer => {
  162. writer.insertText( ' ', doc.selection.getFirstPosition() );
  163. } );
  164. expect( getData( model ) ).to.equal( '<blockQuote><paragraph>[]</paragraph></blockQuote>' );
  165. editor.execute( 'undo' );
  166. expect( getData( model ) ).to.equal( '<paragraph>> []</paragraph>' );
  167. } );
  168. } );
  169. } );