autoformat.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Autoformat from '../src/autoformat';
  6. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  7. import ListEngine from '@ckeditor/ckeditor5-list/src/listengine';
  8. import HeadingEngine from '@ckeditor/ckeditor5-heading/src/headingengine';
  9. import BoldEngine from '@ckeditor/ckeditor5-basic-styles/src/boldengine';
  10. import ItalicEngine from '@ckeditor/ckeditor5-basic-styles/src/italicengine';
  11. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  12. import Enter from '@ckeditor/ckeditor5-enter/src/enter';
  13. import { setData, getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  14. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  15. testUtils.createSinonSandbox();
  16. describe( 'Autoformat', () => {
  17. let editor, doc, batch;
  18. beforeEach( () => {
  19. return VirtualTestEditor.create( {
  20. plugins: [ Enter, Paragraph, Autoformat, ListEngine, HeadingEngine, BoldEngine, ItalicEngine ]
  21. } )
  22. .then( newEditor => {
  23. editor = newEditor;
  24. doc = editor.document;
  25. batch = doc.batch();
  26. } );
  27. } );
  28. describe( 'Bulleted list', () => {
  29. it( 'should replace asterisk with bulleted list item', () => {
  30. setData( doc, '<paragraph>*[]</paragraph>' );
  31. doc.enqueueChanges( () => {
  32. batch.insert( doc.selection.getFirstPosition(), ' ' );
  33. } );
  34. expect( getData( doc ) ).to.equal( '<listItem indent="0" type="bulleted">[]</listItem>' );
  35. } );
  36. it( 'should replace minus character with bulleted list item', () => {
  37. setData( doc, '<paragraph>-[]</paragraph>' );
  38. doc.enqueueChanges( () => {
  39. batch.insert( doc.selection.getFirstPosition(), ' ' );
  40. } );
  41. expect( getData( doc ) ).to.equal( '<listItem indent="0" type="bulleted">[]</listItem>' );
  42. } );
  43. it( 'should not replace minus character when inside bulleted list item', () => {
  44. setData( doc, '<listItem indent="0" type="bulleted">-[]</listItem>' );
  45. doc.enqueueChanges( () => {
  46. batch.insert( doc.selection.getFirstPosition(), ' ' );
  47. } );
  48. expect( getData( doc ) ).to.equal( '<listItem indent="0" type="bulleted">- []</listItem>' );
  49. } );
  50. } );
  51. describe( 'Numbered list', () => {
  52. it( 'should replace digit with numbered list item', () => {
  53. setData( doc, '<paragraph>1.[]</paragraph>' );
  54. doc.enqueueChanges( () => {
  55. batch.insert( doc.selection.getFirstPosition(), ' ' );
  56. } );
  57. expect( getData( doc ) ).to.equal( '<listItem indent="0" type="numbered">[]</listItem>' );
  58. } );
  59. it( 'should not replace digit character when inside numbered list item', () => {
  60. setData( doc, '<listItem indent="0" type="numbered">1.[]</listItem>' );
  61. doc.enqueueChanges( () => {
  62. batch.insert( doc.selection.getFirstPosition(), ' ' );
  63. } );
  64. expect( getData( doc ) ).to.equal( '<listItem indent="0" type="numbered">1. []</listItem>' );
  65. } );
  66. } );
  67. describe( 'Heading', () => {
  68. it( 'should replace hash character with heading', () => {
  69. setData( doc, '<paragraph>#[]</paragraph>' );
  70. doc.enqueueChanges( () => {
  71. batch.insert( doc.selection.getFirstPosition(), ' ' );
  72. } );
  73. expect( getData( doc ) ).to.equal( '<heading1>[]</heading1>' );
  74. } );
  75. it( 'should replace two hash characters with heading level 2', () => {
  76. setData( doc, '<paragraph>##[]</paragraph>' );
  77. doc.enqueueChanges( () => {
  78. batch.insert( doc.selection.getFirstPosition(), ' ' );
  79. } );
  80. expect( getData( doc ) ).to.equal( '<heading2>[]</heading2>' );
  81. } );
  82. it( 'should not replace minus character when inside heading', () => {
  83. setData( doc, '<heading1>#[]</heading1>' );
  84. doc.enqueueChanges( () => {
  85. batch.insert( doc.selection.getFirstPosition(), ' ' );
  86. } );
  87. expect( getData( doc ) ).to.equal( '<heading1># []</heading1>' );
  88. } );
  89. } );
  90. describe( 'Inline autoformat', () => {
  91. it( 'should replace both `**` with bold', () => {
  92. setData( doc, '<paragraph>**foobar*[]</paragraph>' );
  93. doc.enqueueChanges( () => {
  94. batch.insert( doc.selection.getFirstPosition(), '*' );
  95. } );
  96. expect( getData( doc ) ).to.equal( '<paragraph><$text bold="true">foobar</$text>[]</paragraph>' );
  97. } );
  98. it( 'should replace both `*` with italic', () => {
  99. setData( doc, '<paragraph>*foobar[]</paragraph>' );
  100. doc.enqueueChanges( () => {
  101. batch.insert( doc.selection.getFirstPosition(), '*' );
  102. } );
  103. expect( getData( doc ) ).to.equal( '<paragraph><$text italic="true">foobar</$text>[]</paragraph>' );
  104. } );
  105. it( 'nothing should be replaces when typing `*`', () => {
  106. setData( doc, '<paragraph>foobar[]</paragraph>' );
  107. doc.enqueueChanges( () => {
  108. batch.insert( doc.selection.getFirstPosition(), '*' );
  109. } );
  110. expect( getData( doc ) ).to.equal( '<paragraph>foobar*[]</paragraph>' );
  111. } );
  112. it( 'should format inside the text', () => {
  113. setData( doc, '<paragraph>foo **bar*[] baz</paragraph>' );
  114. doc.enqueueChanges( () => {
  115. batch.insert( doc.selection.getFirstPosition(), '*' );
  116. } );
  117. expect( getData( doc ) ).to.equal( '<paragraph>foo <$text bold="true">bar</$text>[] baz</paragraph>' );
  118. } );
  119. } );
  120. describe( 'without commands', () => {
  121. beforeEach( () => {
  122. return VirtualTestEditor.create( {
  123. plugins: [ Enter, Paragraph, Autoformat ]
  124. } )
  125. .then( newEditor => {
  126. editor = newEditor;
  127. doc = editor.document;
  128. batch = doc.batch();
  129. } );
  130. } );
  131. it( 'should not replace asterisk with bulleted list item', () => {
  132. setData( doc, '<paragraph>*[]</paragraph>' );
  133. doc.enqueueChanges( () => {
  134. batch.insert( doc.selection.getFirstPosition(), ' ' );
  135. } );
  136. expect( getData( doc ) ).to.equal( '<paragraph>* []</paragraph>' );
  137. } );
  138. it( 'should not replace minus character with bulleted list item', () => {
  139. setData( doc, '<paragraph>-[]</paragraph>' );
  140. doc.enqueueChanges( () => {
  141. batch.insert( doc.selection.getFirstPosition(), ' ' );
  142. } );
  143. expect( getData( doc ) ).to.equal( '<paragraph>- []</paragraph>' );
  144. } );
  145. it( 'should not replace digit with numbered list item', () => {
  146. setData( doc, '<paragraph>1.[]</paragraph>' );
  147. doc.enqueueChanges( () => {
  148. batch.insert( doc.selection.getFirstPosition(), ' ' );
  149. } );
  150. expect( getData( doc ) ).to.equal( '<paragraph>1. []</paragraph>' );
  151. } );
  152. it( 'should not replace hash character with heading', () => {
  153. setData( doc, '<paragraph>#[]</paragraph>' );
  154. doc.enqueueChanges( () => {
  155. batch.insert( doc.selection.getFirstPosition(), ' ' );
  156. } );
  157. expect( getData( doc ) ).to.equal( '<paragraph># []</paragraph>' );
  158. } );
  159. it( 'should not replace two hash characters with heading level 2', () => {
  160. setData( doc, '<paragraph>##[]</paragraph>' );
  161. doc.enqueueChanges( () => {
  162. batch.insert( doc.selection.getFirstPosition(), ' ' );
  163. } );
  164. expect( getData( doc ) ).to.equal( '<paragraph>## []</paragraph>' );
  165. } );
  166. it( 'should not replace both `**` with bold', () => {
  167. setData( doc, '<paragraph>**foobar*[]</paragraph>' );
  168. doc.enqueueChanges( () => {
  169. batch.insert( doc.selection.getFirstPosition(), '*' );
  170. } );
  171. expect( getData( doc ) ).to.equal( '<paragraph>**foobar**[]</paragraph>' );
  172. } );
  173. it( 'should not replace both `*` with italic', () => {
  174. setData( doc, '<paragraph>*foobar[]</paragraph>' );
  175. doc.enqueueChanges( () => {
  176. batch.insert( doc.selection.getFirstPosition(), '*' );
  177. } );
  178. expect( getData( doc ) ).to.equal( '<paragraph>*foobar*[]</paragraph>' );
  179. } );
  180. } );
  181. } );