8
0

autoformat.js 12 KB

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