autoformat.js 14 KB

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