undointegration.js 6.8 KB

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