autoformat.js 13 KB

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