8
0

autoformat.js 18 KB

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