autoformat.js 17 KB

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