8
0

autoformat.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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 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. it( 'should not replace asterisk character after <softBreak>', () => {
  72. setData( model, '<paragraph>Foo<softBreak></softBreak>*[]</paragraph>' );
  73. model.change( writer => {
  74. writer.insertText( ' ', doc.selection.getFirstPosition() );
  75. } );
  76. expect( getData( model ) ).to.equal( '<paragraph>Foo<softBreak></softBreak>* []</paragraph>' );
  77. } );
  78. } );
  79. describe( 'Numbered list', () => {
  80. it( 'should replace digit with numbered list item using the dot 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 replace digit with numbered list item using the parenthesis format', () => {
  88. setData( model, '<paragraph>1)[]</paragraph>' );
  89. model.change( writer => {
  90. writer.insertText( ' ', doc.selection.getFirstPosition() );
  91. } );
  92. expect( getData( model ) ).to.equal( '<listItem listIndent="0" listType="numbered">[]</listItem>' );
  93. } );
  94. it( 'should not replace digit character when there is no . or ) in the format', () => {
  95. setData( model, '<paragraph>1[]</paragraph>' );
  96. model.change( writer => {
  97. writer.insertText( ' ', doc.selection.getFirstPosition() );
  98. } );
  99. expect( getData( model ) ).to.equal( '<paragraph>1 []</paragraph>' );
  100. } );
  101. it( 'should not replace digit character when inside numbered list item', () => {
  102. setData( model, '<listItem listIndent="0" listType="numbered">1.[]</listItem>' );
  103. model.change( writer => {
  104. writer.insertText( ' ', doc.selection.getFirstPosition() );
  105. } );
  106. expect( getData( model ) ).to.equal( '<listItem listIndent="0" listType="numbered">1. []</listItem>' );
  107. } );
  108. it( 'should not replace digit with numbered list item when digit is different than "1"', () => {
  109. setData( model, '<paragraph>3.[]</paragraph>' );
  110. model.change( writer => {
  111. writer.insertText( ' ', doc.selection.getFirstPosition() );
  112. } );
  113. expect( getData( model ) ).to.equal( '<paragraph>3. []</paragraph>' );
  114. } );
  115. it( 'should not replace digit character after <softBreak>', () => {
  116. setData( model, '<paragraph>Foo<softBreak></softBreak>1.[]</paragraph>' );
  117. model.change( writer => {
  118. writer.insertText( ' ', doc.selection.getFirstPosition() );
  119. } );
  120. expect( getData( model ) ).to.equal( '<paragraph>Foo<softBreak></softBreak>1. []</paragraph>' );
  121. } );
  122. } );
  123. describe( 'Heading', () => {
  124. it( 'should replace hash character with heading', () => {
  125. setData( model, '<paragraph>#[]</paragraph>' );
  126. model.change( writer => {
  127. writer.insertText( ' ', doc.selection.getFirstPosition() );
  128. } );
  129. expect( getData( model ) ).to.equal( '<heading1>[]</heading1>' );
  130. } );
  131. it( 'should replace two hash characters with heading level 2', () => {
  132. setData( model, '<paragraph>##[]</paragraph>' );
  133. model.change( writer => {
  134. writer.insertText( ' ', doc.selection.getFirstPosition() );
  135. } );
  136. expect( getData( model ) ).to.equal( '<heading2>[]</heading2>' );
  137. } );
  138. it( 'should not replace hash character when inside heading', () => {
  139. setData( model, '<heading1>#[]</heading1>' );
  140. model.change( writer => {
  141. writer.insertText( ' ', doc.selection.getFirstPosition() );
  142. } );
  143. expect( getData( model ) ).to.equal( '<heading1># []</heading1>' );
  144. } );
  145. it( 'should work with heading1-heading6 commands regardless of the config of the heading feature', () => {
  146. const command = new HeadingCommand( editor, [ 'heading1', 'heading6' ] );
  147. const spy = sinon.spy( command, 'execute' );
  148. function HeadingPlugin( editor ) {
  149. editor.commands.add( 'heading', command );
  150. command.refresh();
  151. }
  152. return VirtualTestEditor
  153. .create( {
  154. plugins: [
  155. Paragraph, Autoformat, HeadingPlugin
  156. ]
  157. } )
  158. .then( editor => {
  159. const model = editor.model;
  160. const doc = model.document;
  161. setData( model, '<paragraph>#[]</paragraph>' );
  162. model.change( writer => {
  163. writer.insertText( ' ', doc.selection.getFirstPosition() );
  164. } );
  165. sinon.assert.calledOnce( spy );
  166. sinon.assert.calledWithExactly( spy, { value: 'heading1' } );
  167. spy.resetHistory();
  168. setData( model, '<paragraph>######[]</paragraph>' );
  169. model.change( writer => {
  170. writer.insertText( ' ', doc.selection.getFirstPosition() );
  171. } );
  172. sinon.assert.calledOnce( spy );
  173. sinon.assert.calledWithExactly( spy, { value: 'heading6' } );
  174. return editor.destroy();
  175. } );
  176. } );
  177. it( 'should not replace if heading command is disabled', () => {
  178. setData( model, '<paragraph>#[]</paragraph>' );
  179. model.change( writer => {
  180. editor.commands.get( 'heading' ).refresh = () => {};
  181. editor.commands.get( 'heading' ).isEnabled = false;
  182. writer.insertText( ' ', doc.selection.getFirstPosition() );
  183. } );
  184. expect( getData( model ) ).to.equal( '<paragraph># []</paragraph>' );
  185. } );
  186. it( 'should not replace hash character after <softBreak>', () => {
  187. setData( model, '<paragraph>Foo<softBreak></softBreak>#[]</paragraph>' );
  188. model.change( writer => {
  189. writer.insertText( ' ', doc.selection.getFirstPosition() );
  190. } );
  191. expect( getData( model ) ).to.equal( '<paragraph>Foo<softBreak></softBreak># []</paragraph>' );
  192. } );
  193. } );
  194. describe( 'Block quote', () => {
  195. it( 'should replace greater-than character with block quote', () => {
  196. setData( model, '<paragraph>>[]</paragraph>' );
  197. model.change( writer => {
  198. writer.insertText( ' ', doc.selection.getFirstPosition() );
  199. } );
  200. expect( getData( model ) ).to.equal( '<blockQuote><paragraph>[]</paragraph></blockQuote>' );
  201. } );
  202. it( 'should not replace greater-than character when inside heading', () => {
  203. setData( model, '<heading1>>[]</heading1>' );
  204. model.change( writer => {
  205. writer.insertText( ' ', doc.selection.getFirstPosition() );
  206. } );
  207. expect( getData( model ) ).to.equal( '<heading1>> []</heading1>' );
  208. } );
  209. it( 'should not replace greater-than character when inside numbered list', () => {
  210. setData( model, '<listItem listIndent="0" listType="numbered">1. >[]</listItem>' );
  211. model.change( writer => {
  212. writer.insertText( ' ', doc.selection.getFirstPosition() );
  213. } );
  214. expect( getData( model ) ).to.equal( '<listItem listIndent="0" listType="numbered">1. > []</listItem>' );
  215. } );
  216. it( 'should not replace greater-than character when inside buletted list', () => {
  217. setData( model, '<listItem listIndent="0" listType="bulleted">1. >[]</listItem>' );
  218. model.change( writer => {
  219. writer.insertText( ' ', doc.selection.getFirstPosition() );
  220. } );
  221. expect( getData( model ) ).to.equal( '<listItem listIndent="0" listType="bulleted">1. > []</listItem>' );
  222. } );
  223. it( 'should not replace greater-than character after <softBreak>', () => {
  224. setData( model, '<paragraph>Foo<softBreak></softBreak>>[]</paragraph>' );
  225. model.change( writer => {
  226. writer.insertText( ' ', doc.selection.getFirstPosition() );
  227. } );
  228. expect( getData( model ) ).to.equal( '<paragraph>Foo<softBreak></softBreak>> []</paragraph>' );
  229. } );
  230. } );
  231. describe( 'Code block', () => {
  232. it( 'should replace triple grave accents with a code block', () => {
  233. setData( model, '<paragraph>``[]</paragraph>' );
  234. model.change( writer => {
  235. writer.insertText( '`', doc.selection.getFirstPosition() );
  236. } );
  237. expect( getData( model ) ).to.equal( '<codeBlock language="plaintext">[]</codeBlock>' );
  238. } );
  239. it( 'should not replace triple grave accents when already in a code block', () => {
  240. setData( model, '<codeBlock language="plaintext">``[]</codeBlock>' );
  241. model.change( writer => {
  242. writer.insertText( '`', doc.selection.getFirstPosition() );
  243. } );
  244. expect( getData( model ) ).to.equal( '<codeBlock language="plaintext">```[]</codeBlock>' );
  245. } );
  246. it( 'should not replace triple grave accents when inside heading', () => {
  247. setData( model, '<heading1>``[]</heading1>' );
  248. model.change( writer => {
  249. writer.insertText( '`', doc.selection.getFirstPosition() );
  250. } );
  251. expect( getData( model ) ).to.equal( '<heading1>```[]</heading1>' );
  252. } );
  253. it( 'should not replace triple grave accents when inside numbered list', () => {
  254. setData( model, '<listItem listIndent="0" listType="numbered">1. ``[]</listItem>' );
  255. model.change( writer => {
  256. writer.insertText( '`', doc.selection.getFirstPosition() );
  257. } );
  258. expect( getData( model ) ).to.equal( '<listItem listIndent="0" listType="numbered">1. ```[]</listItem>' );
  259. } );
  260. it( 'should not replace triple grave accents when inside buletted list', () => {
  261. setData( model, '<listItem listIndent="0" listType="bulleted">1. ``[]</listItem>' );
  262. model.change( writer => {
  263. writer.insertText( '`', doc.selection.getFirstPosition() );
  264. } );
  265. expect( getData( model ) ).to.equal( '<listItem listIndent="0" listType="bulleted">1. ```[]</listItem>' );
  266. } );
  267. } );
  268. describe( 'Inline autoformat', () => {
  269. it( 'should replace both "**" with bold', () => {
  270. setData( model, '<paragraph>**foobar*[]</paragraph>' );
  271. model.change( writer => {
  272. writer.insertText( '*', doc.selection.getFirstPosition() );
  273. } );
  274. expect( getData( model ) ).to.equal( '<paragraph><$text bold="true">foobar</$text>[]</paragraph>' );
  275. } );
  276. it( 'should replace both "*" with italic', () => {
  277. setData( model, '<paragraph>*foobar[]</paragraph>' );
  278. model.change( writer => {
  279. writer.insertText( '*', doc.selection.getFirstPosition() );
  280. } );
  281. expect( getData( model ) ).to.equal( '<paragraph><$text italic="true">foobar</$text>[]</paragraph>' );
  282. } );
  283. it( 'should replace both "`" with code', () => {
  284. setData( model, '<paragraph>`foobar[]</paragraph>' );
  285. model.change( writer => {
  286. writer.insertText( '`', doc.selection.getFirstPosition() );
  287. } );
  288. expect( getData( model ) ).to.equal( '<paragraph><$text code="true">foobar</$text>[]</paragraph>' );
  289. } );
  290. it( 'nothing should be replaces when typing "*"', () => {
  291. setData( model, '<paragraph>foobar[]</paragraph>' );
  292. model.change( writer => {
  293. writer.insertText( '*', doc.selection.getFirstPosition() );
  294. } );
  295. expect( getData( model ) ).to.equal( '<paragraph>foobar*[]</paragraph>' );
  296. } );
  297. it( 'should format inside the text', () => {
  298. setData( model, '<paragraph>foo **bar*[] baz</paragraph>' );
  299. model.change( writer => {
  300. writer.insertText( '*', doc.selection.getFirstPosition() );
  301. } );
  302. expect( getData( model ) ).to.equal( '<paragraph>foo <$text bold="true">bar</$text>[] baz</paragraph>' );
  303. } );
  304. it( 'should not format if the command is not enabled', () => {
  305. model.schema.addAttributeCheck( ( context, attributeName ) => {
  306. if ( attributeName == 'bold' ) {
  307. return false;
  308. }
  309. } );
  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 work with <softBreak>s in paragraph', () => {
  317. setData( model, '<paragraph>foo<softBreak></softBreak>**barbaz*[]</paragraph>' );
  318. model.change( writer => {
  319. writer.insertText( '*', doc.selection.getFirstPosition() );
  320. } );
  321. expect( getData( model ) ).to.equal( '<paragraph>foo<softBreak></softBreak><$text bold="true">barbaz</$text>[]</paragraph>' );
  322. } );
  323. } );
  324. describe( 'without commands', () => {
  325. beforeEach( () => {
  326. return VirtualTestEditor
  327. .create( {
  328. plugins: [ Enter, Paragraph, Autoformat ]
  329. } )
  330. .then( newEditor => {
  331. editor = newEditor;
  332. model = editor.model;
  333. doc = model.document;
  334. } );
  335. } );
  336. it( 'should not replace asterisk with bulleted list item', () => {
  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 minus character with bulleted list item', () => {
  344. setData( model, '<paragraph>-[]</paragraph>' );
  345. model.change( writer => {
  346. writer.insertText( ' ', doc.selection.getFirstPosition() );
  347. } );
  348. expect( getData( model ) ).to.equal( '<paragraph>- []</paragraph>' );
  349. } );
  350. it( 'should not replace digit with numbered list item', () => {
  351. setData( model, '<paragraph>1.[]</paragraph>' );
  352. model.change( writer => {
  353. writer.insertText( ' ', doc.selection.getFirstPosition() );
  354. } );
  355. expect( getData( model ) ).to.equal( '<paragraph>1. []</paragraph>' );
  356. } );
  357. it( 'should not replace hash character with heading', () => {
  358. setData( model, '<paragraph>#[]</paragraph>' );
  359. model.change( writer => {
  360. writer.insertText( ' ', doc.selection.getFirstPosition() );
  361. } );
  362. expect( getData( model ) ).to.equal( '<paragraph># []</paragraph>' );
  363. } );
  364. it( 'should not replace two hash characters with heading level 2', () => {
  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 both "**" with bold', () => {
  372. setData( model, '<paragraph>**foobar*[]</paragraph>' );
  373. model.change( writer => {
  374. writer.insertText( '*', doc.selection.getFirstPosition() );
  375. } );
  376. expect( getData( model ) ).to.equal( '<paragraph>**foobar**[]</paragraph>' );
  377. } );
  378. it( 'should not replace both "*" with italic', () => {
  379. setData( model, '<paragraph>*foobar[]</paragraph>' );
  380. model.change( writer => {
  381. writer.insertText( '*', doc.selection.getFirstPosition() );
  382. } );
  383. expect( getData( model ) ).to.equal( '<paragraph>*foobar*[]</paragraph>' );
  384. } );
  385. it( 'should not replace both "`" with code', () => {
  386. setData( model, '<paragraph>`foobar[]</paragraph>' );
  387. model.change( writer => {
  388. writer.insertText( '`', doc.selection.getFirstPosition() );
  389. } );
  390. expect( getData( model ) ).to.equal( '<paragraph>`foobar`[]</paragraph>' );
  391. } );
  392. it( 'should not replace ">" with block quote', () => {
  393. setData( model, '<paragraph>>[]</paragraph>' );
  394. model.change( writer => {
  395. writer.insertText( ' ', doc.selection.getFirstPosition() );
  396. } );
  397. expect( getData( model ) ).to.equal( '<paragraph>> []</paragraph>' );
  398. } );
  399. it( 'should not replace "```" with code block', () => {
  400. setData( model, '<paragraph>``[]</paragraph>' );
  401. model.change( writer => {
  402. writer.insertText( '`', doc.selection.getFirstPosition() );
  403. } );
  404. expect( getData( model ) ).to.equal( '<paragraph>```[]</paragraph>' );
  405. } );
  406. it( 'should use only configured headings', () => {
  407. return VirtualTestEditor
  408. .create( {
  409. plugins: [ Enter, Paragraph, Autoformat, ListEditing, HeadingEditing ],
  410. heading: {
  411. options: [
  412. { model: 'paragraph' },
  413. { model: 'heading1', view: 'h2' }
  414. ]
  415. }
  416. } )
  417. .then( editor => {
  418. model = editor.model;
  419. doc = model.document;
  420. setData( model, '<paragraph>##[]</paragraph>' );
  421. model.change( writer => {
  422. writer.insertText( ' ', doc.selection.getFirstPosition() );
  423. } );
  424. expect( getData( model ) ).to.equal( '<paragraph>## []</paragraph>' );
  425. } );
  426. } );
  427. } );
  428. } );