autoformat.js 14 KB

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