autoformat.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 BlockQuoteEngine from '@ckeditor/ckeditor5-block-quote/src/blockquoteengine';
  12. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  13. import Enter from '@ckeditor/ckeditor5-enter/src/enter';
  14. import { setData, getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  15. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  16. testUtils.createSinonSandbox();
  17. describe( 'Autoformat', () => {
  18. let editor, doc, batch;
  19. beforeEach( () => {
  20. return VirtualTestEditor.create( {
  21. plugins: [ Enter, Paragraph, Autoformat, ListEngine, HeadingEngine, BoldEngine, ItalicEngine, BlockQuoteEngine ]
  22. } )
  23. .then( newEditor => {
  24. editor = newEditor;
  25. doc = editor.document;
  26. batch = doc.batch();
  27. } );
  28. } );
  29. describe( 'Bulleted list', () => {
  30. it( 'should replace asterisk with bulleted list item', () => {
  31. setData( doc, '<paragraph>*[]</paragraph>' );
  32. doc.enqueueChanges( () => {
  33. batch.insert( doc.selection.getFirstPosition(), ' ' );
  34. } );
  35. expect( getData( doc ) ).to.equal( '<listItem indent="0" type="bulleted">[]</listItem>' );
  36. } );
  37. it( 'should replace minus character with bulleted list item', () => {
  38. setData( doc, '<paragraph>-[]</paragraph>' );
  39. doc.enqueueChanges( () => {
  40. batch.insert( doc.selection.getFirstPosition(), ' ' );
  41. } );
  42. expect( getData( doc ) ).to.equal( '<listItem indent="0" type="bulleted">[]</listItem>' );
  43. } );
  44. it( 'should not replace minus character when inside bulleted list item', () => {
  45. setData( doc, '<listItem indent="0" type="bulleted">-[]</listItem>' );
  46. doc.enqueueChanges( () => {
  47. batch.insert( doc.selection.getFirstPosition(), ' ' );
  48. } );
  49. expect( getData( doc ) ).to.equal( '<listItem indent="0" type="bulleted">- []</listItem>' );
  50. } );
  51. } );
  52. describe( 'Numbered list', () => {
  53. it( 'should replace digit with numbered list item', () => {
  54. setData( doc, '<paragraph>1.[]</paragraph>' );
  55. doc.enqueueChanges( () => {
  56. batch.insert( doc.selection.getFirstPosition(), ' ' );
  57. } );
  58. expect( getData( doc ) ).to.equal( '<listItem indent="0" type="numbered">[]</listItem>' );
  59. } );
  60. it( 'should not replace digit character when inside numbered list item', () => {
  61. setData( doc, '<listItem indent="0" type="numbered">1.[]</listItem>' );
  62. doc.enqueueChanges( () => {
  63. batch.insert( doc.selection.getFirstPosition(), ' ' );
  64. } );
  65. expect( getData( doc ) ).to.equal( '<listItem indent="0" type="numbered">1. []</listItem>' );
  66. } );
  67. } );
  68. describe( 'Heading', () => {
  69. it( 'should replace hash character with heading', () => {
  70. setData( doc, '<paragraph>#[]</paragraph>' );
  71. doc.enqueueChanges( () => {
  72. batch.insert( doc.selection.getFirstPosition(), ' ' );
  73. } );
  74. expect( getData( doc ) ).to.equal( '<heading1>[]</heading1>' );
  75. } );
  76. it( 'should replace two hash characters with heading level 2', () => {
  77. setData( doc, '<paragraph>##[]</paragraph>' );
  78. doc.enqueueChanges( () => {
  79. batch.insert( doc.selection.getFirstPosition(), ' ' );
  80. } );
  81. expect( getData( doc ) ).to.equal( '<heading2>[]</heading2>' );
  82. } );
  83. it( 'should not replace minus character when inside heading', () => {
  84. setData( doc, '<heading1>#[]</heading1>' );
  85. doc.enqueueChanges( () => {
  86. batch.insert( doc.selection.getFirstPosition(), ' ' );
  87. } );
  88. expect( getData( doc ) ).to.equal( '<heading1># []</heading1>' );
  89. } );
  90. } );
  91. describe( 'Block quote', () => {
  92. it( 'should replace greater-than character with heading', () => {
  93. setData( doc, '<paragraph>>[]</paragraph>' );
  94. doc.enqueueChanges( () => {
  95. batch.insert( doc.selection.getFirstPosition(), ' ' );
  96. } );
  97. expect( getData( doc ) ).to.equal( '<blockQuote><paragraph>[]</paragraph></blockQuote>' );
  98. } );
  99. it( 'should not replace greater-than character when inside heading', () => {
  100. setData( doc, '<heading1>>[]</heading1>' );
  101. doc.enqueueChanges( () => {
  102. batch.insert( doc.selection.getFirstPosition(), ' ' );
  103. } );
  104. expect( getData( doc ) ).to.equal( '<heading1>> []</heading1>' );
  105. } );
  106. } );
  107. describe( 'Inline autoformat', () => {
  108. it( 'should replace both `**` with bold', () => {
  109. setData( doc, '<paragraph>**foobar*[]</paragraph>' );
  110. doc.enqueueChanges( () => {
  111. batch.insert( doc.selection.getFirstPosition(), '*' );
  112. } );
  113. expect( getData( doc ) ).to.equal( '<paragraph><$text bold="true">foobar</$text>[]</paragraph>' );
  114. } );
  115. it( 'should replace both `*` with italic', () => {
  116. setData( doc, '<paragraph>*foobar[]</paragraph>' );
  117. doc.enqueueChanges( () => {
  118. batch.insert( doc.selection.getFirstPosition(), '*' );
  119. } );
  120. expect( getData( doc ) ).to.equal( '<paragraph><$text italic="true">foobar</$text>[]</paragraph>' );
  121. } );
  122. it( 'nothing should be replaces when typing `*`', () => {
  123. setData( doc, '<paragraph>foobar[]</paragraph>' );
  124. doc.enqueueChanges( () => {
  125. batch.insert( doc.selection.getFirstPosition(), '*' );
  126. } );
  127. expect( getData( doc ) ).to.equal( '<paragraph>foobar*[]</paragraph>' );
  128. } );
  129. it( 'should format inside the text', () => {
  130. setData( doc, '<paragraph>foo **bar*[] baz</paragraph>' );
  131. doc.enqueueChanges( () => {
  132. batch.insert( doc.selection.getFirstPosition(), '*' );
  133. } );
  134. expect( getData( doc ) ).to.equal( '<paragraph>foo <$text bold="true">bar</$text>[] baz</paragraph>' );
  135. } );
  136. } );
  137. describe( 'without commands', () => {
  138. beforeEach( () => {
  139. return VirtualTestEditor.create( {
  140. plugins: [ Enter, Paragraph, Autoformat ]
  141. } )
  142. .then( newEditor => {
  143. editor = newEditor;
  144. doc = editor.document;
  145. batch = doc.batch();
  146. } );
  147. } );
  148. it( 'should not replace asterisk with bulleted list item', () => {
  149. setData( doc, '<paragraph>*[]</paragraph>' );
  150. doc.enqueueChanges( () => {
  151. batch.insert( doc.selection.getFirstPosition(), ' ' );
  152. } );
  153. expect( getData( doc ) ).to.equal( '<paragraph>* []</paragraph>' );
  154. } );
  155. it( 'should not replace minus character with bulleted list item', () => {
  156. setData( doc, '<paragraph>-[]</paragraph>' );
  157. doc.enqueueChanges( () => {
  158. batch.insert( doc.selection.getFirstPosition(), ' ' );
  159. } );
  160. expect( getData( doc ) ).to.equal( '<paragraph>- []</paragraph>' );
  161. } );
  162. it( 'should not replace digit with numbered list item', () => {
  163. setData( doc, '<paragraph>1.[]</paragraph>' );
  164. doc.enqueueChanges( () => {
  165. batch.insert( doc.selection.getFirstPosition(), ' ' );
  166. } );
  167. expect( getData( doc ) ).to.equal( '<paragraph>1. []</paragraph>' );
  168. } );
  169. it( 'should not replace hash character with heading', () => {
  170. setData( doc, '<paragraph>#[]</paragraph>' );
  171. doc.enqueueChanges( () => {
  172. batch.insert( doc.selection.getFirstPosition(), ' ' );
  173. } );
  174. expect( getData( doc ) ).to.equal( '<paragraph># []</paragraph>' );
  175. } );
  176. it( 'should not replace two hash characters with heading level 2', () => {
  177. setData( doc, '<paragraph>##[]</paragraph>' );
  178. doc.enqueueChanges( () => {
  179. batch.insert( doc.selection.getFirstPosition(), ' ' );
  180. } );
  181. expect( getData( doc ) ).to.equal( '<paragraph>## []</paragraph>' );
  182. } );
  183. it( 'should not replace both `**` with bold', () => {
  184. setData( doc, '<paragraph>**foobar*[]</paragraph>' );
  185. doc.enqueueChanges( () => {
  186. batch.insert( doc.selection.getFirstPosition(), '*' );
  187. } );
  188. expect( getData( doc ) ).to.equal( '<paragraph>**foobar**[]</paragraph>' );
  189. } );
  190. it( 'should not replace both `*` with italic', () => {
  191. setData( doc, '<paragraph>*foobar[]</paragraph>' );
  192. doc.enqueueChanges( () => {
  193. batch.insert( doc.selection.getFirstPosition(), '*' );
  194. } );
  195. expect( getData( doc ) ).to.equal( '<paragraph>*foobar*[]</paragraph>' );
  196. } );
  197. it( 'should not replace `>` with block quote', () => {
  198. setData( doc, '<paragraph>>[]</paragraph>' );
  199. doc.enqueueChanges( () => {
  200. batch.insert( doc.selection.getFirstPosition(), ' ' );
  201. } );
  202. expect( getData( doc ) ).to.equal( '<paragraph>> []</paragraph>' );
  203. } );
  204. it( 'should use only configured headings', () => {
  205. return VirtualTestEditor.create( {
  206. plugins: [ Enter, Paragraph, Autoformat, ListEngine, HeadingEngine ],
  207. heading: {
  208. options: [
  209. { modelElement: 'paragraph' },
  210. { modelElement: 'heading1', viewElement: 'h2' }
  211. ]
  212. }
  213. } )
  214. .then( editor => {
  215. doc = editor.document;
  216. batch = doc.batch();
  217. setData( doc, '<paragraph>##[]</paragraph>' );
  218. doc.enqueueChanges( () => {
  219. batch.insert( doc.selection.getFirstPosition(), ' ' );
  220. } );
  221. expect( getData( doc ) ).to.equal( '<paragraph>## []</paragraph>' );
  222. } );
  223. } );
  224. } );
  225. } );