autoformat.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  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. it( 'should not replace digit with numbered list item when digit is different than "1"', () => {
  98. setData( model, '<paragraph>3.[]</paragraph>' );
  99. model.change( writer => {
  100. writer.insertText( ' ', doc.selection.getFirstPosition() );
  101. } );
  102. expect( getData( model ) ).to.equal( '<paragraph>3. []</paragraph>' );
  103. } );
  104. } );
  105. describe( 'Heading', () => {
  106. it( 'should replace hash character with heading', () => {
  107. setData( model, '<paragraph>#[]</paragraph>' );
  108. model.change( writer => {
  109. writer.insertText( ' ', doc.selection.getFirstPosition() );
  110. } );
  111. expect( getData( model ) ).to.equal( '<heading1>[]</heading1>' );
  112. } );
  113. it( 'should replace two hash characters with heading level 2', () => {
  114. setData( model, '<paragraph>##[]</paragraph>' );
  115. model.change( writer => {
  116. writer.insertText( ' ', doc.selection.getFirstPosition() );
  117. } );
  118. expect( getData( model ) ).to.equal( '<heading2>[]</heading2>' );
  119. } );
  120. it( 'should not replace hash character when inside heading', () => {
  121. setData( model, '<heading1>#[]</heading1>' );
  122. model.change( writer => {
  123. writer.insertText( ' ', doc.selection.getFirstPosition() );
  124. } );
  125. expect( getData( model ) ).to.equal( '<heading1># []</heading1>' );
  126. } );
  127. it( 'should work with heading1-heading6 commands regardless of the config of the heading feature', () => {
  128. const command = new HeadingCommand( editor, [ 'heading1', 'heading6' ] );
  129. const spy = sinon.spy( command, 'execute' );
  130. function HeadingPlugin( editor ) {
  131. editor.commands.add( 'heading', command );
  132. command.refresh();
  133. }
  134. return VirtualTestEditor
  135. .create( {
  136. plugins: [
  137. Paragraph, Autoformat, HeadingPlugin
  138. ]
  139. } )
  140. .then( editor => {
  141. const model = editor.model;
  142. const doc = model.document;
  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: 'heading1' } );
  149. spy.resetHistory();
  150. setData( model, '<paragraph>######[]</paragraph>' );
  151. model.change( writer => {
  152. writer.insertText( ' ', doc.selection.getFirstPosition() );
  153. } );
  154. sinon.assert.calledOnce( spy );
  155. sinon.assert.calledWithExactly( spy, { value: 'heading6' } );
  156. return editor.destroy();
  157. } );
  158. } );
  159. it( 'should not replace if heading command is disabled', () => {
  160. setData( model, '<paragraph>#[]</paragraph>' );
  161. model.change( writer => {
  162. editor.commands.get( 'heading' ).refresh = () => {};
  163. editor.commands.get( 'heading' ).isEnabled = false;
  164. writer.insertText( ' ', doc.selection.getFirstPosition() );
  165. } );
  166. expect( getData( model ) ).to.equal( '<paragraph># []</paragraph>' );
  167. } );
  168. } );
  169. describe( 'Block quote', () => {
  170. it( 'should replace greater-than character with block quote', () => {
  171. setData( model, '<paragraph>>[]</paragraph>' );
  172. model.change( writer => {
  173. writer.insertText( ' ', doc.selection.getFirstPosition() );
  174. } );
  175. expect( getData( model ) ).to.equal( '<blockQuote><paragraph>[]</paragraph></blockQuote>' );
  176. } );
  177. it( 'should not replace greater-than character when inside heading', () => {
  178. setData( model, '<heading1>>[]</heading1>' );
  179. model.change( writer => {
  180. writer.insertText( ' ', doc.selection.getFirstPosition() );
  181. } );
  182. expect( getData( model ) ).to.equal( '<heading1>> []</heading1>' );
  183. } );
  184. it( 'should not replace greater-than character when inside numbered list', () => {
  185. setData( model, '<listItem listIndent="0" listType="numbered">1. >[]</listItem>' );
  186. model.change( writer => {
  187. writer.insertText( ' ', doc.selection.getFirstPosition() );
  188. } );
  189. expect( getData( model ) ).to.equal( '<listItem listIndent="0" listType="numbered">1. > []</listItem>' );
  190. } );
  191. it( 'should not replace greater-than character when inside buletted list', () => {
  192. setData( model, '<listItem listIndent="0" listType="bulleted">1. >[]</listItem>' );
  193. model.change( writer => {
  194. writer.insertText( ' ', doc.selection.getFirstPosition() );
  195. } );
  196. expect( getData( model ) ).to.equal( '<listItem listIndent="0" listType="bulleted">1. > []</listItem>' );
  197. } );
  198. } );
  199. describe( 'Inline autoformat', () => {
  200. it( 'should replace both "**" with bold', () => {
  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 bold="true">foobar</$text>[]</paragraph>' );
  206. } );
  207. it( 'should replace both "*" with italic', () => {
  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 italic="true">foobar</$text>[]</paragraph>' );
  213. } );
  214. it( 'should replace both "`" with code', () => {
  215. setData( model, '<paragraph>`foobar[]</paragraph>' );
  216. model.change( writer => {
  217. writer.insertText( '`', doc.selection.getFirstPosition() );
  218. } );
  219. expect( getData( model ) ).to.equal( '<paragraph><$text code="true">foobar</$text>[]</paragraph>' );
  220. } );
  221. it( 'nothing should be replaces when typing "*"', () => {
  222. setData( model, '<paragraph>foobar[]</paragraph>' );
  223. model.change( writer => {
  224. writer.insertText( '*', doc.selection.getFirstPosition() );
  225. } );
  226. expect( getData( model ) ).to.equal( '<paragraph>foobar*[]</paragraph>' );
  227. } );
  228. it( 'should format inside the text', () => {
  229. setData( model, '<paragraph>foo **bar*[] baz</paragraph>' );
  230. model.change( writer => {
  231. writer.insertText( '*', doc.selection.getFirstPosition() );
  232. } );
  233. expect( getData( model ) ).to.equal( '<paragraph>foo <$text bold="true">bar</$text>[] baz</paragraph>' );
  234. } );
  235. it( 'should not format if the command is not enabled', () => {
  236. model.schema.addAttributeCheck( ( context, attributeName ) => {
  237. if ( attributeName == 'bold' ) {
  238. return false;
  239. }
  240. } );
  241. setData( model, '<paragraph>**foobar*[]</paragraph>' );
  242. model.change( writer => {
  243. writer.insertText( '*', doc.selection.getFirstPosition() );
  244. } );
  245. expect( getData( model ) ).to.equal( '<paragraph>**foobar**[]</paragraph>' );
  246. } );
  247. } );
  248. describe( 'without commands', () => {
  249. beforeEach( () => {
  250. return VirtualTestEditor
  251. .create( {
  252. plugins: [ Enter, Paragraph, Autoformat ]
  253. } )
  254. .then( newEditor => {
  255. editor = newEditor;
  256. model = editor.model;
  257. doc = model.document;
  258. } );
  259. } );
  260. it( 'should not replace asterisk 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 minus character with bulleted list item', () => {
  268. setData( model, '<paragraph>-[]</paragraph>' );
  269. model.change( writer => {
  270. writer.insertText( ' ', doc.selection.getFirstPosition() );
  271. } );
  272. expect( getData( model ) ).to.equal( '<paragraph>- []</paragraph>' );
  273. } );
  274. it( 'should not replace digit with numbered list item', () => {
  275. setData( model, '<paragraph>1.[]</paragraph>' );
  276. model.change( writer => {
  277. writer.insertText( ' ', doc.selection.getFirstPosition() );
  278. } );
  279. expect( getData( model ) ).to.equal( '<paragraph>1. []</paragraph>' );
  280. } );
  281. it( 'should not replace hash character with heading', () => {
  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 two hash characters with heading level 2', () => {
  289. setData( model, '<paragraph>##[]</paragraph>' );
  290. model.change( writer => {
  291. writer.insertText( ' ', doc.selection.getFirstPosition() );
  292. } );
  293. expect( getData( model ) ).to.equal( '<paragraph>## []</paragraph>' );
  294. } );
  295. it( 'should not replace both "**" with bold', () => {
  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 italic', () => {
  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 both "`" with code', () => {
  310. setData( model, '<paragraph>`foobar[]</paragraph>' );
  311. model.change( writer => {
  312. writer.insertText( '`', doc.selection.getFirstPosition() );
  313. } );
  314. expect( getData( model ) ).to.equal( '<paragraph>`foobar`[]</paragraph>' );
  315. } );
  316. it( 'should not replace ">" with block quote', () => {
  317. setData( model, '<paragraph>>[]</paragraph>' );
  318. model.change( writer => {
  319. writer.insertText( ' ', doc.selection.getFirstPosition() );
  320. } );
  321. expect( getData( model ) ).to.equal( '<paragraph>> []</paragraph>' );
  322. } );
  323. it( 'should use only configured headings', () => {
  324. return VirtualTestEditor
  325. .create( {
  326. plugins: [ Enter, Paragraph, Autoformat, ListEditing, HeadingEditing ],
  327. heading: {
  328. options: [
  329. { model: 'paragraph' },
  330. { model: 'heading1', view: 'h2' }
  331. ]
  332. }
  333. } )
  334. .then( editor => {
  335. model = editor.model;
  336. doc = model.document;
  337. setData( model, '<paragraph>##[]</paragraph>' );
  338. model.change( writer => {
  339. writer.insertText( ' ', doc.selection.getFirstPosition() );
  340. } );
  341. expect( getData( model ) ).to.equal( '<paragraph>## []</paragraph>' );
  342. } );
  343. } );
  344. } );
  345. } );