autoformat.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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 Enter from '@ckeditor/ckeditor5-enter/src/enter';
  13. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  14. import { setData, getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  15. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  16. import Command from '@ckeditor/ckeditor5-core/src/command/command';
  17. testUtils.createSinonSandbox();
  18. describe( 'Autoformat', () => {
  19. let editor, doc, batch;
  20. beforeEach( () => {
  21. return VirtualTestEditor.create( {
  22. plugins: [ Enter, Paragraph, Autoformat, ListEngine, HeadingEngine, BoldEngine, ItalicEngine, BlockQuoteEngine ]
  23. } )
  24. .then( newEditor => {
  25. editor = newEditor;
  26. doc = editor.document;
  27. batch = doc.batch();
  28. } );
  29. } );
  30. afterEach( () => {
  31. return editor.destroy();
  32. } );
  33. describe( 'Bulleted list', () => {
  34. it( 'should replace asterisk with bulleted list item', () => {
  35. setData( doc, '<paragraph>*[]</paragraph>' );
  36. doc.enqueueChanges( () => {
  37. batch.insert( doc.selection.getFirstPosition(), ' ' );
  38. } );
  39. expect( getData( doc ) ).to.equal( '<listItem indent="0" type="bulleted">[]</listItem>' );
  40. } );
  41. it( 'should replace minus character with bulleted list item', () => {
  42. setData( doc, '<paragraph>-[]</paragraph>' );
  43. doc.enqueueChanges( () => {
  44. batch.insert( doc.selection.getFirstPosition(), ' ' );
  45. } );
  46. expect( getData( doc ) ).to.equal( '<listItem indent="0" type="bulleted">[]</listItem>' );
  47. } );
  48. it( 'should not replace minus character when inside bulleted list item', () => {
  49. setData( doc, '<listItem indent="0" type="bulleted">-[]</listItem>' );
  50. doc.enqueueChanges( () => {
  51. batch.insert( doc.selection.getFirstPosition(), ' ' );
  52. } );
  53. expect( getData( doc ) ).to.equal( '<listItem indent="0" type="bulleted">- []</listItem>' );
  54. } );
  55. } );
  56. describe( 'Numbered list', () => {
  57. it( 'should replace digit with numbered list item', () => {
  58. setData( doc, '<paragraph>1.[]</paragraph>' );
  59. doc.enqueueChanges( () => {
  60. batch.insert( doc.selection.getFirstPosition(), ' ' );
  61. } );
  62. expect( getData( doc ) ).to.equal( '<listItem indent="0" type="numbered">[]</listItem>' );
  63. } );
  64. it( 'should not replace digit character when inside numbered list item', () => {
  65. setData( doc, '<listItem indent="0" type="numbered">1.[]</listItem>' );
  66. doc.enqueueChanges( () => {
  67. batch.insert( doc.selection.getFirstPosition(), ' ' );
  68. } );
  69. expect( getData( doc ) ).to.equal( '<listItem indent="0" type="numbered">1. []</listItem>' );
  70. } );
  71. } );
  72. describe( 'Heading', () => {
  73. it( 'should replace hash character with heading', () => {
  74. setData( doc, '<paragraph>#[]</paragraph>' );
  75. doc.enqueueChanges( () => {
  76. batch.insert( doc.selection.getFirstPosition(), ' ' );
  77. } );
  78. expect( getData( doc ) ).to.equal( '<heading1>[]</heading1>' );
  79. } );
  80. it( 'should replace two hash characters with heading level 2', () => {
  81. setData( doc, '<paragraph>##[]</paragraph>' );
  82. doc.enqueueChanges( () => {
  83. batch.insert( doc.selection.getFirstPosition(), ' ' );
  84. } );
  85. expect( getData( doc ) ).to.equal( '<heading2>[]</heading2>' );
  86. } );
  87. it( 'should not replace hash character when inside heading', () => {
  88. setData( doc, '<heading1>#[]</heading1>' );
  89. doc.enqueueChanges( () => {
  90. batch.insert( doc.selection.getFirstPosition(), ' ' );
  91. } );
  92. expect( getData( doc ) ).to.equal( '<heading1># []</heading1>' );
  93. } );
  94. it( 'should work with heading1-heading6 commands regardless of the config of the heading feature', () => {
  95. const spy1 = sinon.spy();
  96. const spy6 = sinon.spy();
  97. class Heading6 extends Command {
  98. _doExecute() {
  99. spy6();
  100. }
  101. }
  102. class Heading1 extends Command {
  103. _doExecute() {
  104. spy1();
  105. }
  106. }
  107. function HeadingPlugin( editor ) {
  108. editor.commands.set( 'heading1', new Heading1() );
  109. editor.commands.set( 'heading6', new Heading6() );
  110. }
  111. return VirtualTestEditor
  112. .create( {
  113. plugins: [
  114. Paragraph, Autoformat, HeadingPlugin
  115. ]
  116. } )
  117. .then( editor => {
  118. const doc = editor.document;
  119. setData( doc, '<paragraph>#[]</paragraph>' );
  120. doc.enqueueChanges( () => {
  121. doc.batch().insert( doc.selection.getFirstPosition(), ' ' );
  122. } );
  123. expect( spy1.calledOnce ).to.be.true;
  124. setData( doc, '<paragraph>######[]</paragraph>' );
  125. doc.enqueueChanges( () => {
  126. doc.batch().insert( doc.selection.getFirstPosition(), ' ' );
  127. } );
  128. expect( spy6.calledOnce ).to.be.true;
  129. return editor.destroy();
  130. } );
  131. } );
  132. } );
  133. describe( 'Block quote', () => {
  134. it( 'should replace greater-than character with heading', () => {
  135. setData( doc, '<paragraph>>[]</paragraph>' );
  136. doc.enqueueChanges( () => {
  137. batch.insert( doc.selection.getFirstPosition(), ' ' );
  138. } );
  139. expect( getData( doc ) ).to.equal( '<blockQuote><paragraph>[]</paragraph></blockQuote>' );
  140. } );
  141. it( 'should not replace greater-than character when inside heading', () => {
  142. setData( doc, '<heading1>>[]</heading1>' );
  143. doc.enqueueChanges( () => {
  144. batch.insert( doc.selection.getFirstPosition(), ' ' );
  145. } );
  146. expect( getData( doc ) ).to.equal( '<heading1>> []</heading1>' );
  147. } );
  148. it( 'should not replace greater-than character when inside numbered list', () => {
  149. setData( doc, '<listItem indent="0" type="numbered">1. >[]</listItem>' );
  150. doc.enqueueChanges( () => {
  151. batch.insert( doc.selection.getFirstPosition(), ' ' );
  152. } );
  153. expect( getData( doc ) ).to.equal( '<listItem indent="0" type="numbered">1. > []</listItem>' );
  154. } );
  155. it( 'should not replace greater-than character when inside buletted list', () => {
  156. setData( doc, '<listItem indent="0" type="bulleted">1. >[]</listItem>' );
  157. doc.enqueueChanges( () => {
  158. batch.insert( doc.selection.getFirstPosition(), ' ' );
  159. } );
  160. expect( getData( doc ) ).to.equal( '<listItem indent="0" type="bulleted">1. > []</listItem>' );
  161. } );
  162. } );
  163. describe( 'Inline autoformat', () => {
  164. it( 'should replace both `**` with bold', () => {
  165. setData( doc, '<paragraph>**foobar*[]</paragraph>' );
  166. doc.enqueueChanges( () => {
  167. batch.insert( doc.selection.getFirstPosition(), '*' );
  168. } );
  169. expect( getData( doc ) ).to.equal( '<paragraph><$text bold="true">foobar</$text>[]</paragraph>' );
  170. } );
  171. it( 'should replace both `*` with italic', () => {
  172. setData( doc, '<paragraph>*foobar[]</paragraph>' );
  173. doc.enqueueChanges( () => {
  174. batch.insert( doc.selection.getFirstPosition(), '*' );
  175. } );
  176. expect( getData( doc ) ).to.equal( '<paragraph><$text italic="true">foobar</$text>[]</paragraph>' );
  177. } );
  178. it( 'nothing should be replaces when typing `*`', () => {
  179. setData( doc, '<paragraph>foobar[]</paragraph>' );
  180. doc.enqueueChanges( () => {
  181. batch.insert( doc.selection.getFirstPosition(), '*' );
  182. } );
  183. expect( getData( doc ) ).to.equal( '<paragraph>foobar*[]</paragraph>' );
  184. } );
  185. it( 'should format inside the text', () => {
  186. setData( doc, '<paragraph>foo **bar*[] baz</paragraph>' );
  187. doc.enqueueChanges( () => {
  188. batch.insert( doc.selection.getFirstPosition(), '*' );
  189. } );
  190. expect( getData( doc ) ).to.equal( '<paragraph>foo <$text bold="true">bar</$text>[] baz</paragraph>' );
  191. } );
  192. } );
  193. describe( 'without commands', () => {
  194. beforeEach( () => {
  195. return VirtualTestEditor.create( {
  196. plugins: [ Enter, Paragraph, Autoformat ]
  197. } )
  198. .then( newEditor => {
  199. editor = newEditor;
  200. doc = editor.document;
  201. batch = doc.batch();
  202. } );
  203. } );
  204. it( 'should not replace asterisk with bulleted list item', () => {
  205. setData( doc, '<paragraph>*[]</paragraph>' );
  206. doc.enqueueChanges( () => {
  207. batch.insert( doc.selection.getFirstPosition(), ' ' );
  208. } );
  209. expect( getData( doc ) ).to.equal( '<paragraph>* []</paragraph>' );
  210. } );
  211. it( 'should not replace minus character with bulleted list item', () => {
  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 not replace digit with numbered list item', () => {
  219. setData( doc, '<paragraph>1.[]</paragraph>' );
  220. doc.enqueueChanges( () => {
  221. batch.insert( doc.selection.getFirstPosition(), ' ' );
  222. } );
  223. expect( getData( doc ) ).to.equal( '<paragraph>1. []</paragraph>' );
  224. } );
  225. it( 'should not replace hash character with heading', () => {
  226. setData( doc, '<paragraph>#[]</paragraph>' );
  227. doc.enqueueChanges( () => {
  228. batch.insert( doc.selection.getFirstPosition(), ' ' );
  229. } );
  230. expect( getData( doc ) ).to.equal( '<paragraph># []</paragraph>' );
  231. } );
  232. it( 'should not replace two hash characters with heading level 2', () => {
  233. setData( doc, '<paragraph>##[]</paragraph>' );
  234. doc.enqueueChanges( () => {
  235. batch.insert( doc.selection.getFirstPosition(), ' ' );
  236. } );
  237. expect( getData( doc ) ).to.equal( '<paragraph>## []</paragraph>' );
  238. } );
  239. it( 'should not replace both `**` with bold', () => {
  240. setData( doc, '<paragraph>**foobar*[]</paragraph>' );
  241. doc.enqueueChanges( () => {
  242. batch.insert( doc.selection.getFirstPosition(), '*' );
  243. } );
  244. expect( getData( doc ) ).to.equal( '<paragraph>**foobar**[]</paragraph>' );
  245. } );
  246. it( 'should not replace both `*` with italic', () => {
  247. setData( doc, '<paragraph>*foobar[]</paragraph>' );
  248. doc.enqueueChanges( () => {
  249. batch.insert( doc.selection.getFirstPosition(), '*' );
  250. } );
  251. expect( getData( doc ) ).to.equal( '<paragraph>*foobar*[]</paragraph>' );
  252. } );
  253. it( 'should not replace `>` with block quote', () => {
  254. setData( doc, '<paragraph>>[]</paragraph>' );
  255. doc.enqueueChanges( () => {
  256. batch.insert( doc.selection.getFirstPosition(), ' ' );
  257. } );
  258. expect( getData( doc ) ).to.equal( '<paragraph>> []</paragraph>' );
  259. } );
  260. it( 'should use only configured headings', () => {
  261. return VirtualTestEditor.create( {
  262. plugins: [ Enter, Paragraph, Autoformat, ListEngine, HeadingEngine ],
  263. heading: {
  264. options: [
  265. { modelElement: 'paragraph' },
  266. { modelElement: 'heading1', viewElement: 'h2' }
  267. ]
  268. }
  269. } )
  270. .then( editor => {
  271. doc = editor.document;
  272. batch = doc.batch();
  273. setData( doc, '<paragraph>##[]</paragraph>' );
  274. doc.enqueueChanges( () => {
  275. batch.insert( doc.selection.getFirstPosition(), ' ' );
  276. } );
  277. expect( getData( doc ) ).to.equal( '<paragraph>## []</paragraph>' );
  278. } );
  279. } );
  280. } );
  281. } );