8
0

autoformat.js 24 KB

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