autoformat.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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 Command from '@ckeditor/ckeditor5-core/src/command';
  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 indent="0" type="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 indent="0" type="bulleted">[]</listItem>' );
  59. } );
  60. it( 'should not replace minus character when inside bulleted list item', () => {
  61. setData( model, '<listItem indent="0" type="bulleted">-[]</listItem>' );
  62. model.change( writer => {
  63. writer.insertText( ' ', doc.selection.getFirstPosition() );
  64. } );
  65. expect( getData( model ) ).to.equal( '<listItem indent="0" type="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 indent="0" type="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 indent="0" type="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 indent="0" type="numbered">1.[]</listItem>' );
  92. model.change( writer => {
  93. writer.insertText( ' ', doc.selection.getFirstPosition() );
  94. } );
  95. expect( getData( model ) ).to.equal( '<listItem indent="0" type="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 spy1 = sinon.spy();
  122. const spy6 = sinon.spy();
  123. class Heading6 extends Command {
  124. execute() {
  125. spy6();
  126. }
  127. }
  128. class Heading1 extends Command {
  129. execute() {
  130. spy1();
  131. }
  132. }
  133. function HeadingPlugin( editor ) {
  134. editor.commands.add( 'heading1', new Heading1( editor ) );
  135. editor.commands.add( 'heading6', new Heading6( editor ) );
  136. }
  137. return VirtualTestEditor
  138. .create( {
  139. plugins: [
  140. Paragraph, Autoformat, HeadingPlugin
  141. ]
  142. } )
  143. .then( editor => {
  144. const model = editor.model;
  145. const doc = model.document;
  146. setData( model, '<paragraph>#[]</paragraph>' );
  147. model.change( writer => {
  148. writer.insertText( ' ', doc.selection.getFirstPosition() );
  149. } );
  150. expect( spy1.calledOnce ).to.be.true;
  151. setData( model, '<paragraph>######[]</paragraph>' );
  152. model.change( writer => {
  153. writer.insertText( ' ', doc.selection.getFirstPosition() );
  154. } );
  155. expect( spy6.calledOnce ).to.be.true;
  156. return editor.destroy();
  157. } );
  158. } );
  159. } );
  160. describe( 'Block quote', () => {
  161. it( 'should replace greater-than character with heading', () => {
  162. setData( model, '<paragraph>>[]</paragraph>' );
  163. model.change( writer => {
  164. writer.insertText( ' ', doc.selection.getFirstPosition() );
  165. } );
  166. expect( getData( model ) ).to.equal( '<blockQuote><paragraph>[]</paragraph></blockQuote>' );
  167. } );
  168. it( 'should not replace greater-than character when inside heading', () => {
  169. setData( model, '<heading1>>[]</heading1>' );
  170. model.change( writer => {
  171. writer.insertText( ' ', doc.selection.getFirstPosition() );
  172. } );
  173. expect( getData( model ) ).to.equal( '<heading1>> []</heading1>' );
  174. } );
  175. it( 'should not replace greater-than character when inside numbered list', () => {
  176. setData( model, '<listItem indent="0" type="numbered">1. >[]</listItem>' );
  177. model.change( writer => {
  178. writer.insertText( ' ', doc.selection.getFirstPosition() );
  179. } );
  180. expect( getData( model ) ).to.equal( '<listItem indent="0" type="numbered">1. > []</listItem>' );
  181. } );
  182. it( 'should not replace greater-than character when inside buletted list', () => {
  183. setData( model, '<listItem indent="0" type="bulleted">1. >[]</listItem>' );
  184. model.change( writer => {
  185. writer.insertText( ' ', doc.selection.getFirstPosition() );
  186. } );
  187. expect( getData( model ) ).to.equal( '<listItem indent="0" type="bulleted">1. > []</listItem>' );
  188. } );
  189. } );
  190. describe( 'Inline autoformat', () => {
  191. it( 'should replace both "**" with bold', () => {
  192. setData( model, '<paragraph>**foobar*[]</paragraph>' );
  193. model.change( writer => {
  194. writer.insertText( '*', doc.selection.getFirstPosition() );
  195. } );
  196. expect( getData( model ) ).to.equal( '<paragraph><$text bold="true">foobar</$text>[]</paragraph>' );
  197. } );
  198. it( 'should replace both "*" with italic', () => {
  199. setData( model, '<paragraph>*foobar[]</paragraph>' );
  200. model.change( writer => {
  201. writer.insertText( '*', doc.selection.getFirstPosition() );
  202. } );
  203. expect( getData( model ) ).to.equal( '<paragraph><$text italic="true">foobar</$text>[]</paragraph>' );
  204. } );
  205. it( 'should replace both "`" with code', () => {
  206. setData( model, '<paragraph>`foobar[]</paragraph>' );
  207. model.change( writer => {
  208. writer.insertText( '`', doc.selection.getFirstPosition() );
  209. } );
  210. expect( getData( model ) ).to.equal( '<paragraph><$text code="true">foobar</$text>[]</paragraph>' );
  211. } );
  212. it( 'nothing should be replaces when typing "*"', () => {
  213. setData( model, '<paragraph>foobar[]</paragraph>' );
  214. model.change( writer => {
  215. writer.insertText( '*', doc.selection.getFirstPosition() );
  216. } );
  217. expect( getData( model ) ).to.equal( '<paragraph>foobar*[]</paragraph>' );
  218. } );
  219. it( 'should format inside the text', () => {
  220. setData( model, '<paragraph>foo **bar*[] baz</paragraph>' );
  221. model.change( writer => {
  222. writer.insertText( '*', doc.selection.getFirstPosition() );
  223. } );
  224. expect( getData( model ) ).to.equal( '<paragraph>foo <$text bold="true">bar</$text>[] baz</paragraph>' );
  225. } );
  226. } );
  227. describe( 'without commands', () => {
  228. beforeEach( () => {
  229. return VirtualTestEditor
  230. .create( {
  231. plugins: [ Enter, Paragraph, Autoformat ]
  232. } )
  233. .then( newEditor => {
  234. editor = newEditor;
  235. model = editor.model;
  236. doc = model.document;
  237. } );
  238. } );
  239. it( 'should not replace asterisk with bulleted list item', () => {
  240. setData( model, '<paragraph>*[]</paragraph>' );
  241. model.change( writer => {
  242. writer.insertText( ' ', doc.selection.getFirstPosition() );
  243. } );
  244. expect( getData( model ) ).to.equal( '<paragraph>* []</paragraph>' );
  245. } );
  246. it( 'should not replace minus character with bulleted list item', () => {
  247. setData( model, '<paragraph>-[]</paragraph>' );
  248. model.change( writer => {
  249. writer.insertText( ' ', doc.selection.getFirstPosition() );
  250. } );
  251. expect( getData( model ) ).to.equal( '<paragraph>- []</paragraph>' );
  252. } );
  253. it( 'should not replace digit with numbered list item', () => {
  254. setData( model, '<paragraph>1.[]</paragraph>' );
  255. model.change( writer => {
  256. writer.insertText( ' ', doc.selection.getFirstPosition() );
  257. } );
  258. expect( getData( model ) ).to.equal( '<paragraph>1. []</paragraph>' );
  259. } );
  260. it( 'should not replace hash character with heading', () => {
  261. setData( model, '<paragraph>#[]</paragraph>' );
  262. model.change( writer => {
  263. writer.insertText( ' ', doc.selection.getFirstPosition() );
  264. } );
  265. expect( getData( model ) ).to.equal( '<paragraph># []</paragraph>' );
  266. } );
  267. it( 'should not replace two hash characters with heading level 2', () => {
  268. setData( model, '<paragraph>##[]</paragraph>' );
  269. model.change( writer => {
  270. writer.insertText( ' ', doc.selection.getFirstPosition() );
  271. } );
  272. expect( getData( model ) ).to.equal( '<paragraph>## []</paragraph>' );
  273. } );
  274. it( 'should not replace both "**" with bold', () => {
  275. setData( model, '<paragraph>**foobar*[]</paragraph>' );
  276. model.change( writer => {
  277. writer.insertText( '*', doc.selection.getFirstPosition() );
  278. } );
  279. expect( getData( model ) ).to.equal( '<paragraph>**foobar**[]</paragraph>' );
  280. } );
  281. it( 'should not replace both "*" with italic', () => {
  282. setData( model, '<paragraph>*foobar[]</paragraph>' );
  283. model.change( writer => {
  284. writer.insertText( '*', doc.selection.getFirstPosition() );
  285. } );
  286. expect( getData( model ) ).to.equal( '<paragraph>*foobar*[]</paragraph>' );
  287. } );
  288. it( 'should not replace both "`" with code', () => {
  289. setData( model, '<paragraph>`foobar[]</paragraph>' );
  290. model.change( writer => {
  291. writer.insertText( '`', doc.selection.getFirstPosition() );
  292. } );
  293. expect( getData( model ) ).to.equal( '<paragraph>`foobar`[]</paragraph>' );
  294. } );
  295. it( 'should not replace ">" with block quote', () => {
  296. setData( model, '<paragraph>>[]</paragraph>' );
  297. model.change( writer => {
  298. writer.insertText( ' ', doc.selection.getFirstPosition() );
  299. } );
  300. expect( getData( model ) ).to.equal( '<paragraph>> []</paragraph>' );
  301. } );
  302. it( 'should use only configured headings', () => {
  303. return VirtualTestEditor
  304. .create( {
  305. plugins: [ Enter, Paragraph, Autoformat, ListEditing, HeadingEditing ],
  306. heading: {
  307. options: [
  308. { model: 'paragraph' },
  309. { model: 'heading1', view: 'h2' }
  310. ]
  311. }
  312. } )
  313. .then( editor => {
  314. model = editor.model;
  315. doc = model.document;
  316. setData( model, '<paragraph>##[]</paragraph>' );
  317. model.change( writer => {
  318. writer.insertText( ' ', doc.selection.getFirstPosition() );
  319. } );
  320. expect( getData( model ) ).to.equal( '<paragraph>## []</paragraph>' );
  321. } );
  322. } );
  323. } );
  324. } );