autoformat.js 12 KB

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