autoformat.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. it( 'should not replace greater-than character when inside numbered list', () => {
  107. setData( doc, '<listItem indent="0" type="numbered">1. >[]</listItem>' );
  108. doc.enqueueChanges( () => {
  109. batch.insert( doc.selection.getFirstPosition(), ' ' );
  110. } );
  111. expect( getData( doc ) ).to.equal( '<listItem indent="0" type="numbered">1. > []</listItem>' );
  112. } );
  113. it( 'should not replace greater-than character when inside buletted list', () => {
  114. setData( doc, '<listItem indent="0" type="bulleted">1. >[]</listItem>' );
  115. doc.enqueueChanges( () => {
  116. batch.insert( doc.selection.getFirstPosition(), ' ' );
  117. } );
  118. expect( getData( doc ) ).to.equal( '<listItem indent="0" type="bulleted">1. > []</listItem>' );
  119. } );
  120. } );
  121. describe( 'Inline autoformat', () => {
  122. it( 'should replace both `**` with bold', () => {
  123. setData( doc, '<paragraph>**foobar*[]</paragraph>' );
  124. doc.enqueueChanges( () => {
  125. batch.insert( doc.selection.getFirstPosition(), '*' );
  126. } );
  127. expect( getData( doc ) ).to.equal( '<paragraph><$text bold="true">foobar</$text>[]</paragraph>' );
  128. } );
  129. it( 'should replace both `*` with italic', () => {
  130. setData( doc, '<paragraph>*foobar[]</paragraph>' );
  131. doc.enqueueChanges( () => {
  132. batch.insert( doc.selection.getFirstPosition(), '*' );
  133. } );
  134. expect( getData( doc ) ).to.equal( '<paragraph><$text italic="true">foobar</$text>[]</paragraph>' );
  135. } );
  136. it( 'nothing should be replaces when typing `*`', () => {
  137. setData( doc, '<paragraph>foobar[]</paragraph>' );
  138. doc.enqueueChanges( () => {
  139. batch.insert( doc.selection.getFirstPosition(), '*' );
  140. } );
  141. expect( getData( doc ) ).to.equal( '<paragraph>foobar*[]</paragraph>' );
  142. } );
  143. it( 'should format inside the text', () => {
  144. setData( doc, '<paragraph>foo **bar*[] baz</paragraph>' );
  145. doc.enqueueChanges( () => {
  146. batch.insert( doc.selection.getFirstPosition(), '*' );
  147. } );
  148. expect( getData( doc ) ).to.equal( '<paragraph>foo <$text bold="true">bar</$text>[] baz</paragraph>' );
  149. } );
  150. } );
  151. describe( 'without commands', () => {
  152. beforeEach( () => {
  153. return VirtualTestEditor.create( {
  154. plugins: [ Enter, Paragraph, Autoformat ]
  155. } )
  156. .then( newEditor => {
  157. editor = newEditor;
  158. doc = editor.document;
  159. batch = doc.batch();
  160. } );
  161. } );
  162. it( 'should not replace asterisk with bulleted list item', () => {
  163. setData( doc, '<paragraph>*[]</paragraph>' );
  164. doc.enqueueChanges( () => {
  165. batch.insert( doc.selection.getFirstPosition(), ' ' );
  166. } );
  167. expect( getData( doc ) ).to.equal( '<paragraph>* []</paragraph>' );
  168. } );
  169. it( 'should not replace minus character with bulleted list item', () => {
  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 digit with numbered list item', () => {
  177. setData( doc, '<paragraph>1.[]</paragraph>' );
  178. doc.enqueueChanges( () => {
  179. batch.insert( doc.selection.getFirstPosition(), ' ' );
  180. } );
  181. expect( getData( doc ) ).to.equal( '<paragraph>1. []</paragraph>' );
  182. } );
  183. it( 'should not replace hash character with heading', () => {
  184. setData( doc, '<paragraph>#[]</paragraph>' );
  185. doc.enqueueChanges( () => {
  186. batch.insert( doc.selection.getFirstPosition(), ' ' );
  187. } );
  188. expect( getData( doc ) ).to.equal( '<paragraph># []</paragraph>' );
  189. } );
  190. it( 'should not replace two hash characters with heading level 2', () => {
  191. setData( doc, '<paragraph>##[]</paragraph>' );
  192. doc.enqueueChanges( () => {
  193. batch.insert( doc.selection.getFirstPosition(), ' ' );
  194. } );
  195. expect( getData( doc ) ).to.equal( '<paragraph>## []</paragraph>' );
  196. } );
  197. it( 'should not replace both `**` with bold', () => {
  198. setData( doc, '<paragraph>**foobar*[]</paragraph>' );
  199. doc.enqueueChanges( () => {
  200. batch.insert( doc.selection.getFirstPosition(), '*' );
  201. } );
  202. expect( getData( doc ) ).to.equal( '<paragraph>**foobar**[]</paragraph>' );
  203. } );
  204. it( 'should not replace both `*` with italic', () => {
  205. setData( doc, '<paragraph>*foobar[]</paragraph>' );
  206. doc.enqueueChanges( () => {
  207. batch.insert( doc.selection.getFirstPosition(), '*' );
  208. } );
  209. expect( getData( doc ) ).to.equal( '<paragraph>*foobar*[]</paragraph>' );
  210. } );
  211. it( 'should not replace `>` with block quote', () => {
  212. setData( doc, '<paragraph>>[]</paragraph>' );
  213. doc.enqueueChanges( () => {
  214. batch.insert( doc.selection.getFirstPosition(), ' ' );
  215. } );
  216. expect( getData( doc ) ).to.equal( '<paragraph>> []</paragraph>' );
  217. } );
  218. it( 'should use only configured headings', () => {
  219. return VirtualTestEditor.create( {
  220. plugins: [ Enter, Paragraph, Autoformat, ListEngine, HeadingEngine ],
  221. heading: {
  222. options: [
  223. { modelElement: 'paragraph' },
  224. { modelElement: 'heading1', viewElement: 'h2' }
  225. ]
  226. }
  227. } )
  228. .then( editor => {
  229. doc = editor.document;
  230. batch = doc.batch();
  231. setData( doc, '<paragraph>##[]</paragraph>' );
  232. doc.enqueueChanges( () => {
  233. batch.insert( doc.selection.getFirstPosition(), ' ' );
  234. } );
  235. expect( getData( doc ) ).to.equal( '<paragraph>## []</paragraph>' );
  236. } );
  237. } );
  238. } );
  239. } );