autoformat.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  1. /**
  2. * @license Copyright (c) 2003-2020, 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 StrikethroughEditing from '@ckeditor/ckeditor5-basic-styles/src/strikethrough/strikethroughediting';
  11. import CodeEditing from '@ckeditor/ckeditor5-basic-styles/src/code/codeediting';
  12. import ItalicEditing from '@ckeditor/ckeditor5-basic-styles/src/italic/italicediting';
  13. import BlockQuoteEditing from '@ckeditor/ckeditor5-block-quote/src/blockquoteediting';
  14. import CodeBlockEditing from '@ckeditor/ckeditor5-code-block/src/codeblockediting';
  15. import Enter from '@ckeditor/ckeditor5-enter/src/enter';
  16. import ShiftEnter from '@ckeditor/ckeditor5-enter/src/shiftenter';
  17. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  18. import { setData, getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  19. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  20. import HeadingCommand from '@ckeditor/ckeditor5-heading/src/headingcommand';
  21. describe( 'Autoformat', () => {
  22. let editor, model, doc;
  23. testUtils.createSinonSandbox();
  24. beforeEach( () => {
  25. return VirtualTestEditor
  26. .create( {
  27. plugins: [
  28. Enter,
  29. Paragraph,
  30. Autoformat,
  31. ListEditing,
  32. HeadingEditing,
  33. BoldEditing,
  34. ItalicEditing,
  35. CodeEditing,
  36. StrikethroughEditing,
  37. BlockQuoteEditing,
  38. CodeBlockEditing,
  39. ShiftEnter
  40. ]
  41. } )
  42. .then( newEditor => {
  43. editor = newEditor;
  44. model = editor.model;
  45. doc = model.document;
  46. } );
  47. } );
  48. afterEach( () => {
  49. return editor.destroy();
  50. } );
  51. describe( 'Bulleted list', () => {
  52. it( 'should replace asterisk with bulleted list item', () => {
  53. setData( model, '<paragraph>*[]</paragraph>' );
  54. model.change( writer => {
  55. writer.insertText( ' ', doc.selection.getFirstPosition() );
  56. } );
  57. expect( getData( model ) ).to.equal( '<listItem listIndent="0" listType="bulleted">[]</listItem>' );
  58. } );
  59. it( 'should replace minus character with bulleted list item', () => {
  60. setData( model, '<paragraph>-[]</paragraph>' );
  61. model.change( writer => {
  62. writer.insertText( ' ', doc.selection.getFirstPosition() );
  63. } );
  64. expect( getData( model ) ).to.equal( '<listItem listIndent="0" listType="bulleted">[]</listItem>' );
  65. } );
  66. it( 'should replace a non-empty paragraph using the asterisk', () => {
  67. setData( model, '<paragraph>*[]sample text</paragraph>' );
  68. model.change( writer => {
  69. writer.insertText( ' ', doc.selection.getFirstPosition() );
  70. } );
  71. expect( getData( model ) ).to.equal( '<listItem listIndent="0" listType="bulleted">[]sample text</listItem>' );
  72. } );
  73. it( 'should not replace minus character when inside bulleted list item', () => {
  74. setData( model, '<listItem listIndent="0" listType="bulleted">-[]</listItem>' );
  75. model.change( writer => {
  76. writer.insertText( ' ', doc.selection.getFirstPosition() );
  77. } );
  78. expect( getData( model ) ).to.equal( '<listItem listIndent="0" listType="bulleted">- []</listItem>' );
  79. } );
  80. it( 'should not replace asterisk character after <softBreak>', () => {
  81. setData( model, '<paragraph>Foo<softBreak></softBreak>*[]</paragraph>' );
  82. model.change( writer => {
  83. writer.insertText( ' ', doc.selection.getFirstPosition() );
  84. } );
  85. expect( getData( model ) ).to.equal( '<paragraph>Foo<softBreak></softBreak>* []</paragraph>' );
  86. } );
  87. } );
  88. describe( 'Numbered list', () => {
  89. it( 'should replace digit with numbered list item using the dot format', () => {
  90. setData( model, '<paragraph>1.[]</paragraph>' );
  91. model.change( writer => {
  92. writer.insertText( ' ', doc.selection.getFirstPosition() );
  93. } );
  94. expect( getData( model ) ).to.equal( '<listItem listIndent="0" listType="numbered">[]</listItem>' );
  95. } );
  96. it( 'should replace digit with numbered list item using the parenthesis format', () => {
  97. setData( model, '<paragraph>1)[]</paragraph>' );
  98. model.change( writer => {
  99. writer.insertText( ' ', doc.selection.getFirstPosition() );
  100. } );
  101. expect( getData( model ) ).to.equal( '<listItem listIndent="0" listType="numbered">[]</listItem>' );
  102. } );
  103. it( 'should replace a non-empty paragraph using the parenthesis format', () => {
  104. setData( model, '<paragraph>1)[]sample text</paragraph>' );
  105. model.change( writer => {
  106. writer.insertText( ' ', doc.selection.getFirstPosition() );
  107. } );
  108. expect( getData( model ) ).to.equal( '<listItem listIndent="0" listType="numbered">[]sample text</listItem>' );
  109. } );
  110. it( 'should not replace digit character when there is no . or ) in the format', () => {
  111. setData( model, '<paragraph>1[]</paragraph>' );
  112. model.change( writer => {
  113. writer.insertText( ' ', doc.selection.getFirstPosition() );
  114. } );
  115. expect( getData( model ) ).to.equal( '<paragraph>1 []</paragraph>' );
  116. } );
  117. it( 'should not replace digit character when inside numbered list item', () => {
  118. setData( model, '<listItem listIndent="0" listType="numbered">1.[]</listItem>' );
  119. model.change( writer => {
  120. writer.insertText( ' ', doc.selection.getFirstPosition() );
  121. } );
  122. expect( getData( model ) ).to.equal( '<listItem listIndent="0" listType="numbered">1. []</listItem>' );
  123. } );
  124. it( 'should not replace digit with numbered list item when digit is different than "1"', () => {
  125. setData( model, '<paragraph>3.[]</paragraph>' );
  126. model.change( writer => {
  127. writer.insertText( ' ', doc.selection.getFirstPosition() );
  128. } );
  129. expect( getData( model ) ).to.equal( '<paragraph>3. []</paragraph>' );
  130. } );
  131. it( 'should not replace digit character after <softBreak>', () => {
  132. setData( model, '<paragraph>Foo<softBreak></softBreak>1.[]</paragraph>' );
  133. model.change( writer => {
  134. writer.insertText( ' ', doc.selection.getFirstPosition() );
  135. } );
  136. expect( getData( model ) ).to.equal( '<paragraph>Foo<softBreak></softBreak>1. []</paragraph>' );
  137. } );
  138. it( 'should be converted from a header', () => {
  139. setData( model, '<heading1>1.[]</heading1>' );
  140. model.change( writer => {
  141. writer.insertText( ' ', doc.selection.getFirstPosition() );
  142. } );
  143. expect( getData( model ) ).to.equal( '<listItem listIndent="0" listType="numbered">[]</listItem>' );
  144. } );
  145. it( 'should be converted from a bulleted list', () => {
  146. setData( model, '<listItem listIndent="0" listType="bulleted">1.[]</listItem>' );
  147. model.change( writer => {
  148. writer.insertText( ' ', doc.selection.getFirstPosition() );
  149. } );
  150. expect( getData( model ) ).to.equal( '<listItem listIndent="0" listType="numbered">[]</listItem>' );
  151. } );
  152. } );
  153. describe( 'Heading', () => {
  154. it( 'should replace hash character with heading', () => {
  155. setData( model, '<paragraph>#[]</paragraph>' );
  156. model.change( writer => {
  157. writer.insertText( ' ', doc.selection.getFirstPosition() );
  158. } );
  159. expect( getData( model ) ).to.equal( '<heading1>[]</heading1>' );
  160. } );
  161. it( 'should replace two hash characters with heading level 2', () => {
  162. setData( model, '<paragraph>##[]</paragraph>' );
  163. model.change( writer => {
  164. writer.insertText( ' ', doc.selection.getFirstPosition() );
  165. } );
  166. expect( getData( model ) ).to.equal( '<heading2>[]</heading2>' );
  167. } );
  168. it( 'should not replace hash 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 work with heading1-heading6 commands regardless of the config of the heading feature', () => {
  176. const command = new HeadingCommand( editor, [ 'heading1', 'heading6' ] );
  177. const spy = sinon.spy( command, 'execute' );
  178. function HeadingPlugin( editor ) {
  179. editor.commands.add( 'heading', command );
  180. command.refresh();
  181. }
  182. return VirtualTestEditor
  183. .create( {
  184. plugins: [
  185. Paragraph, Autoformat, HeadingPlugin
  186. ]
  187. } )
  188. .then( editor => {
  189. const model = editor.model;
  190. const doc = model.document;
  191. setData( model, '<paragraph>#[]</paragraph>' );
  192. model.change( writer => {
  193. writer.insertText( ' ', doc.selection.getFirstPosition() );
  194. } );
  195. sinon.assert.calledOnce( spy );
  196. sinon.assert.calledWithExactly( spy, { value: 'heading1' } );
  197. spy.resetHistory();
  198. setData( model, '<paragraph>######[]</paragraph>' );
  199. model.change( writer => {
  200. writer.insertText( ' ', doc.selection.getFirstPosition() );
  201. } );
  202. sinon.assert.calledOnce( spy );
  203. sinon.assert.calledWithExactly( spy, { value: 'heading6' } );
  204. return editor.destroy();
  205. } );
  206. } );
  207. it( 'should not replace if heading command is disabled', () => {
  208. setData( model, '<paragraph>#[]</paragraph>' );
  209. model.change( writer => {
  210. editor.commands.get( 'heading' ).refresh = () => {};
  211. editor.commands.get( 'heading' ).isEnabled = false;
  212. writer.insertText( ' ', doc.selection.getFirstPosition() );
  213. } );
  214. expect( getData( model ) ).to.equal( '<paragraph># []</paragraph>' );
  215. } );
  216. it( 'should not replace hash character after <softBreak>', () => {
  217. setData( model, '<paragraph>Foo<softBreak></softBreak>#[]</paragraph>' );
  218. model.change( writer => {
  219. writer.insertText( ' ', doc.selection.getFirstPosition() );
  220. } );
  221. expect( getData( model ) ).to.equal( '<paragraph>Foo<softBreak></softBreak># []</paragraph>' );
  222. } );
  223. it( 'should convert a header that already contains a text', () => {
  224. setData( model, '<heading1>###[]foo</heading1>' );
  225. model.change( writer => {
  226. writer.insertText( ' ', doc.selection.getFirstPosition() );
  227. } );
  228. expect( getData( model ) ).to.equal( '<heading3>[]foo</heading3>' );
  229. } );
  230. } );
  231. describe( 'Block quote', () => {
  232. it( 'should replace greater-than character with block quote', () => {
  233. setData( model, '<paragraph>>[]</paragraph>' );
  234. model.change( writer => {
  235. writer.insertText( ' ', doc.selection.getFirstPosition() );
  236. } );
  237. expect( getData( model ) ).to.equal( '<blockQuote><paragraph>[]</paragraph></blockQuote>' );
  238. } );
  239. it( 'should replace greater-than character in a non-empty paragraph', () => {
  240. setData( model, '<paragraph>>[]foo</paragraph>' );
  241. model.change( writer => {
  242. writer.insertText( ' ', doc.selection.getFirstPosition() );
  243. } );
  244. expect( getData( model ) ).to.equal( '<blockQuote><paragraph>[]foo</paragraph></blockQuote>' );
  245. } );
  246. it( 'should wrap the heading if greater-than character was used', () => {
  247. setData( model, '<heading1>>[]</heading1>' );
  248. model.change( writer => {
  249. writer.insertText( ' ', doc.selection.getFirstPosition() );
  250. } );
  251. expect( getData( model ) ).to.equal( '<blockQuote><heading1>[]</heading1></blockQuote>' );
  252. } );
  253. it( 'should not replace greater-than character when inside numbered list', () => {
  254. setData( model, '<listItem listIndent="0" listType="numbered">1. >[]</listItem>' );
  255. model.change( writer => {
  256. writer.insertText( ' ', doc.selection.getFirstPosition() );
  257. } );
  258. expect( getData( model ) ).to.equal( '<listItem listIndent="0" listType="numbered">1. > []</listItem>' );
  259. } );
  260. it( 'should not replace greater-than character when inside buletted list', () => {
  261. setData( model, '<listItem listIndent="0" listType="bulleted">1. >[]</listItem>' );
  262. model.change( writer => {
  263. writer.insertText( ' ', doc.selection.getFirstPosition() );
  264. } );
  265. expect( getData( model ) ).to.equal( '<listItem listIndent="0" listType="bulleted">1. > []</listItem>' );
  266. } );
  267. it( 'should not replace greater-than character after <softBreak>', () => {
  268. setData( model, '<paragraph>Foo<softBreak></softBreak>>[]</paragraph>' );
  269. model.change( writer => {
  270. writer.insertText( ' ', doc.selection.getFirstPosition() );
  271. } );
  272. expect( getData( model ) ).to.equal( '<paragraph>Foo<softBreak></softBreak>> []</paragraph>' );
  273. } );
  274. } );
  275. describe( 'Code block', () => {
  276. it( 'should replace triple grave accents with a code block', () => {
  277. setData( model, '<paragraph>``[]</paragraph>' );
  278. model.change( writer => {
  279. writer.insertText( '`', doc.selection.getFirstPosition() );
  280. } );
  281. expect( getData( model ) ).to.equal( '<codeBlock language="plaintext">[]</codeBlock>' );
  282. } );
  283. it( 'should replace triple grave accents in a heading', () => {
  284. setData( model, '<heading1>``[]</heading1>' );
  285. model.change( writer => {
  286. writer.insertText( '`', doc.selection.getFirstPosition() );
  287. } );
  288. expect( getData( model ) ).to.equal( '<codeBlock language="plaintext">[]</codeBlock>' );
  289. } );
  290. it( 'should replace triple grave accents in a non-empty paragraph', () => {
  291. setData( model, '<paragraph>``[]let foo = 1;</paragraph>' );
  292. model.change( writer => {
  293. writer.insertText( '`', doc.selection.getFirstPosition() );
  294. } );
  295. expect( getData( model ) ).to.equal( '<codeBlock language="plaintext">[]let foo = 1;</codeBlock>' );
  296. } );
  297. it( 'should not replace triple grave accents when already in a code block', () => {
  298. setData( model, '<codeBlock language="plaintext">``[]</codeBlock>' );
  299. model.change( writer => {
  300. writer.insertText( '`', doc.selection.getFirstPosition() );
  301. } );
  302. expect( getData( model ) ).to.equal( '<codeBlock language="plaintext">```[]</codeBlock>' );
  303. } );
  304. it( 'should not replace triple grave accents when inside numbered list', () => {
  305. setData( model, '<listItem listIndent="0" listType="numbered">1. ``[]</listItem>' );
  306. model.change( writer => {
  307. writer.insertText( '`', doc.selection.getFirstPosition() );
  308. } );
  309. expect( getData( model ) ).to.equal( '<listItem listIndent="0" listType="numbered">1. ```[]</listItem>' );
  310. } );
  311. it( 'should not replace triple grave accents when inside buletted list', () => {
  312. setData( model, '<listItem listIndent="0" listType="bulleted">1. ``[]</listItem>' );
  313. model.change( writer => {
  314. writer.insertText( '`', doc.selection.getFirstPosition() );
  315. } );
  316. expect( getData( model ) ).to.equal( '<listItem listIndent="0" listType="bulleted">1. ```[]</listItem>' );
  317. } );
  318. } );
  319. describe( 'Inline autoformat', () => {
  320. it( 'should replace both "**" with bold', () => {
  321. setData( model, '<paragraph>**foobar*[]</paragraph>' );
  322. model.change( writer => {
  323. writer.insertText( '*', doc.selection.getFirstPosition() );
  324. } );
  325. expect( getData( model ) ).to.equal( '<paragraph><$text bold="true">foobar</$text>[]</paragraph>' );
  326. } );
  327. it( 'should replace both "*" with italic', () => {
  328. setData( model, '<paragraph>*foobar[]</paragraph>' );
  329. model.change( writer => {
  330. writer.insertText( '*', doc.selection.getFirstPosition() );
  331. } );
  332. expect( getData( model ) ).to.equal( '<paragraph><$text italic="true">foobar</$text>[]</paragraph>' );
  333. } );
  334. it( 'should replace both "`" with code', () => {
  335. setData( model, '<paragraph>`foobar[]</paragraph>' );
  336. model.change( writer => {
  337. writer.insertText( '`', doc.selection.getFirstPosition() );
  338. } );
  339. expect( getData( model ) ).to.equal( '<paragraph><$text code="true">foobar</$text>[]</paragraph>' );
  340. } );
  341. it( 'should replace both "~~" with strikethrough', () => {
  342. setData( model, '<paragraph>~~foobar~[]</paragraph>' );
  343. model.change( writer => {
  344. writer.insertText( '~', doc.selection.getFirstPosition() );
  345. } );
  346. expect( getData( model ) ).to.equal( '<paragraph><$text strikethrough="true">foobar</$text>[]</paragraph>' );
  347. } );
  348. it( 'nothing should be replaces when typing "*"', () => {
  349. setData( model, '<paragraph>foobar[]</paragraph>' );
  350. model.change( writer => {
  351. writer.insertText( '*', doc.selection.getFirstPosition() );
  352. } );
  353. expect( getData( model ) ).to.equal( '<paragraph>foobar*[]</paragraph>' );
  354. } );
  355. it( 'should format inside the text', () => {
  356. setData( model, '<paragraph>foo **bar*[] baz</paragraph>' );
  357. model.change( writer => {
  358. writer.insertText( '*', doc.selection.getFirstPosition() );
  359. } );
  360. expect( getData( model ) ).to.equal( '<paragraph>foo <$text bold="true">bar</$text>[] baz</paragraph>' );
  361. } );
  362. it( 'should not format if the command is not enabled', () => {
  363. model.schema.addAttributeCheck( ( context, attributeName ) => {
  364. if ( attributeName == 'bold' ) {
  365. return false;
  366. }
  367. } );
  368. setData( model, '<paragraph>**foobar*[]</paragraph>' );
  369. model.change( writer => {
  370. writer.insertText( '*', doc.selection.getFirstPosition() );
  371. } );
  372. expect( getData( model ) ).to.equal( '<paragraph>**foobar**[]</paragraph>' );
  373. } );
  374. it( 'should not format if the plugin is disabled', () => {
  375. editor.plugins.get( 'Autoformat' ).forceDisabled( 'Test' );
  376. setData( model, '<paragraph>**foobar*[]</paragraph>' );
  377. model.change( writer => {
  378. writer.insertText( '*', doc.selection.getFirstPosition() );
  379. } );
  380. expect( getData( model ) ).to.equal( '<paragraph>**foobar**[]</paragraph>' );
  381. } );
  382. describe( 'with code element', () => {
  383. describe( 'should not format (inside)', () => {
  384. it( '* inside', () => {
  385. setData( model, '<paragraph><$text code="true">fo*obar[]</$text></paragraph>' );
  386. model.change( writer => {
  387. writer.insertText( '*', { code: true }, doc.selection.getFirstPosition() );
  388. } );
  389. expect( getData( model ) ).to
  390. .equal( '<paragraph><$text code="true">fo*obar*[]</$text></paragraph>' );
  391. } );
  392. it( '__ inside', () => {
  393. setData( model, '<paragraph><$text code="true">fo__obar_[]</$text></paragraph>' );
  394. model.change( writer => {
  395. writer.insertText( '_', { code: true }, doc.selection.getFirstPosition() );
  396. } );
  397. expect( getData( model ) ).to
  398. .equal( '<paragraph><$text code="true">fo__obar__[]</$text></paragraph>' );
  399. } );
  400. it( '~~ inside', () => {
  401. setData( model, '<paragraph><$text code="true">fo~~obar~[]</$text></paragraph>' );
  402. model.change( writer => {
  403. writer.insertText( '~', { code: true }, doc.selection.getFirstPosition() );
  404. } );
  405. expect( getData( model ) ).to
  406. .equal( '<paragraph><$text code="true">fo~~obar~~[]</$text></paragraph>' );
  407. } );
  408. it( '` inside', () => {
  409. setData( model, '<paragraph><$text code="true">fo`obar[]</$text></paragraph>' );
  410. model.change( writer => {
  411. writer.insertText( '`', { code: true }, doc.selection.getFirstPosition() );
  412. } );
  413. expect( getData( model ) ).to
  414. .equal( '<paragraph><$text code="true">fo`obar`[]</$text></paragraph>' );
  415. } );
  416. } );
  417. describe( 'should not format (across)', () => {
  418. it( '* across', () => {
  419. setData( model, '<paragraph><$text code="true">fo*o</$text>bar[]</paragraph>' );
  420. model.change( writer => {
  421. writer.insertText( '*', doc.selection.getFirstPosition() );
  422. } );
  423. expect( getData( model ) ).to
  424. .equal( '<paragraph><$text code="true">fo*o</$text>bar*[]</paragraph>' );
  425. } );
  426. it( '__ across', () => {
  427. setData( model, '<paragraph><$text code="true">fo__o</$text>bar_[]</paragraph>' );
  428. model.change( writer => {
  429. writer.insertText( '_', doc.selection.getFirstPosition() );
  430. } );
  431. expect( getData( model ) ).to
  432. .equal( '<paragraph><$text code="true">fo__o</$text>bar__[]</paragraph>' );
  433. } );
  434. it( '~~ across', () => {
  435. setData( model, '<paragraph><$text code="true">fo~~o</$text>bar~[]</paragraph>' );
  436. model.change( writer => {
  437. writer.insertText( '~', doc.selection.getFirstPosition() );
  438. } );
  439. expect( getData( model ) ).to
  440. .equal( '<paragraph><$text code="true">fo~~o</$text>bar~~[]</paragraph>' );
  441. } );
  442. it( '` across', () => {
  443. setData( model, '<paragraph><$text code="true">fo`o</$text>bar[]</paragraph>' );
  444. model.change( writer => {
  445. writer.insertText( '`', doc.selection.getFirstPosition() );
  446. } );
  447. expect( getData( model ) ).to
  448. .equal( '<paragraph><$text code="true">fo`o</$text>bar`[]</paragraph>' );
  449. } );
  450. } );
  451. describe( 'should format', () => {
  452. it( '* after', () => {
  453. setData( model, '<paragraph><$text code="true">fo*o</$text>b*ar[]</paragraph>' );
  454. model.change( writer => {
  455. writer.insertText( '*', doc.selection.getFirstPosition() );
  456. } );
  457. expect( getData( model ) ).to
  458. .equal( '<paragraph><$text code="true">fo*o</$text>b<$text italic="true">ar</$text>[]</paragraph>' );
  459. } );
  460. it( '__ after', () => {
  461. setData( model, '<paragraph><$text code="true">fo__o</$text>b__ar_[]</paragraph>' );
  462. model.change( writer => {
  463. writer.insertText( '_', doc.selection.getFirstPosition() );
  464. } );
  465. expect( getData( model ) ).to
  466. .equal( '<paragraph><$text code="true">fo__o</$text>b<$text bold="true">ar</$text>[]</paragraph>' );
  467. } );
  468. it( '~~ after', () => {
  469. setData( model, '<paragraph><$text code="true">fo~~o</$text>b~~ar~[]</paragraph>' );
  470. model.change( writer => {
  471. writer.insertText( '~', doc.selection.getFirstPosition() );
  472. } );
  473. expect( getData( model ) ).to
  474. .equal( '<paragraph><$text code="true">fo~~o</$text>b<$text strikethrough="true">ar</$text>[]</paragraph>' );
  475. } );
  476. it( '` after', () => {
  477. setData( model, '<paragraph><$text code="true">fo`o</$text>b`ar[]</paragraph>' );
  478. model.change( writer => {
  479. writer.insertText( '`', doc.selection.getFirstPosition() );
  480. } );
  481. expect( getData( model ) ).to
  482. .equal( '<paragraph><$text code="true">fo`o</$text>b<$text code="true">ar</$text>[]</paragraph>' );
  483. } );
  484. } );
  485. } );
  486. it( 'should work with <softBreak>s in paragraph', () => {
  487. setData( model, '<paragraph>foo<softBreak></softBreak>**barbaz*[]</paragraph>' );
  488. model.change( writer => {
  489. writer.insertText( '*', doc.selection.getFirstPosition() );
  490. } );
  491. expect( getData( model ) ).to.equal( '<paragraph>foo<softBreak></softBreak><$text bold="true">barbaz</$text>[]</paragraph>' );
  492. } );
  493. } );
  494. describe( 'without commands', () => {
  495. beforeEach( () => {
  496. return VirtualTestEditor
  497. .create( {
  498. plugins: [ Enter, Paragraph, Autoformat ]
  499. } )
  500. .then( newEditor => {
  501. editor = newEditor;
  502. model = editor.model;
  503. doc = model.document;
  504. } );
  505. } );
  506. it( 'should not replace asterisk with bulleted list item', () => {
  507. setData( model, '<paragraph>*[]</paragraph>' );
  508. model.change( writer => {
  509. writer.insertText( ' ', doc.selection.getFirstPosition() );
  510. } );
  511. expect( getData( model ) ).to.equal( '<paragraph>* []</paragraph>' );
  512. } );
  513. it( 'should not replace minus character with bulleted list item', () => {
  514. setData( model, '<paragraph>-[]</paragraph>' );
  515. model.change( writer => {
  516. writer.insertText( ' ', doc.selection.getFirstPosition() );
  517. } );
  518. expect( getData( model ) ).to.equal( '<paragraph>- []</paragraph>' );
  519. } );
  520. it( 'should not replace digit with numbered list item', () => {
  521. setData( model, '<paragraph>1.[]</paragraph>' );
  522. model.change( writer => {
  523. writer.insertText( ' ', doc.selection.getFirstPosition() );
  524. } );
  525. expect( getData( model ) ).to.equal( '<paragraph>1. []</paragraph>' );
  526. } );
  527. it( 'should not replace hash character with heading', () => {
  528. setData( model, '<paragraph>#[]</paragraph>' );
  529. model.change( writer => {
  530. writer.insertText( ' ', doc.selection.getFirstPosition() );
  531. } );
  532. expect( getData( model ) ).to.equal( '<paragraph># []</paragraph>' );
  533. } );
  534. it( 'should not replace two hash characters with heading level 2', () => {
  535. setData( model, '<paragraph>##[]</paragraph>' );
  536. model.change( writer => {
  537. writer.insertText( ' ', doc.selection.getFirstPosition() );
  538. } );
  539. expect( getData( model ) ).to.equal( '<paragraph>## []</paragraph>' );
  540. } );
  541. it( 'should not replace both "**" with bold', () => {
  542. setData( model, '<paragraph>**foobar*[]</paragraph>' );
  543. model.change( writer => {
  544. writer.insertText( '*', doc.selection.getFirstPosition() );
  545. } );
  546. expect( getData( model ) ).to.equal( '<paragraph>**foobar**[]</paragraph>' );
  547. } );
  548. it( 'should not replace both "*" with italic', () => {
  549. setData( model, '<paragraph>*foobar[]</paragraph>' );
  550. model.change( writer => {
  551. writer.insertText( '*', doc.selection.getFirstPosition() );
  552. } );
  553. expect( getData( model ) ).to.equal( '<paragraph>*foobar*[]</paragraph>' );
  554. } );
  555. it( 'should not replace both "`" with code', () => {
  556. setData( model, '<paragraph>`foobar[]</paragraph>' );
  557. model.change( writer => {
  558. writer.insertText( '`', doc.selection.getFirstPosition() );
  559. } );
  560. expect( getData( model ) ).to.equal( '<paragraph>`foobar`[]</paragraph>' );
  561. } );
  562. it( 'should not replace ">" with block quote', () => {
  563. setData( model, '<paragraph>>[]</paragraph>' );
  564. model.change( writer => {
  565. writer.insertText( ' ', doc.selection.getFirstPosition() );
  566. } );
  567. expect( getData( model ) ).to.equal( '<paragraph>> []</paragraph>' );
  568. } );
  569. it( 'should not replace "```" with code block', () => {
  570. setData( model, '<paragraph>``[]</paragraph>' );
  571. model.change( writer => {
  572. writer.insertText( '`', doc.selection.getFirstPosition() );
  573. } );
  574. expect( getData( model ) ).to.equal( '<paragraph>```[]</paragraph>' );
  575. } );
  576. it( 'should use only configured headings', () => {
  577. return VirtualTestEditor
  578. .create( {
  579. plugins: [ Enter, Paragraph, Autoformat, ListEditing, HeadingEditing ],
  580. heading: {
  581. options: [
  582. { model: 'paragraph' },
  583. { model: 'heading1', view: 'h2' }
  584. ]
  585. }
  586. } )
  587. .then( editor => {
  588. model = editor.model;
  589. doc = model.document;
  590. setData( model, '<paragraph>##[]</paragraph>' );
  591. model.change( writer => {
  592. writer.insertText( ' ', doc.selection.getFirstPosition() );
  593. } );
  594. expect( getData( model ) ).to.equal( '<paragraph>## []</paragraph>' );
  595. } );
  596. } );
  597. } );
  598. } );