autoformat.js 16 KB

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