autoformat.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  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. } );
  210. describe( 'Block quote', () => {
  211. it( 'should replace greater-than character with block quote', () => {
  212. setData( model, '<paragraph>>[]</paragraph>' );
  213. model.change( writer => {
  214. writer.insertText( ' ', doc.selection.getFirstPosition() );
  215. } );
  216. expect( getData( model ) ).to.equal( '<blockQuote><paragraph>[]</paragraph></blockQuote>' );
  217. } );
  218. it( 'should wrap the heading if greater-than character was used', () => {
  219. setData( model, '<heading1>>[]</heading1>' );
  220. model.change( writer => {
  221. writer.insertText( ' ', doc.selection.getFirstPosition() );
  222. } );
  223. expect( getData( model ) ).to.equal( '<blockQuote><heading1>[]</heading1></blockQuote>' );
  224. } );
  225. it( 'should not replace greater-than character when inside numbered list', () => {
  226. setData( model, '<listItem listIndent="0" listType="numbered">1. >[]</listItem>' );
  227. model.change( writer => {
  228. writer.insertText( ' ', doc.selection.getFirstPosition() );
  229. } );
  230. expect( getData( model ) ).to.equal( '<listItem listIndent="0" listType="numbered">1. > []</listItem>' );
  231. } );
  232. it( 'should not replace greater-than character when inside buletted list', () => {
  233. setData( model, '<listItem listIndent="0" listType="bulleted">1. >[]</listItem>' );
  234. model.change( writer => {
  235. writer.insertText( ' ', doc.selection.getFirstPosition() );
  236. } );
  237. expect( getData( model ) ).to.equal( '<listItem listIndent="0" listType="bulleted">1. > []</listItem>' );
  238. } );
  239. it( 'should not replace greater-than character after <softBreak>', () => {
  240. setData( model, '<paragraph>Foo<softBreak></softBreak>>[]</paragraph>' );
  241. model.change( writer => {
  242. writer.insertText( ' ', doc.selection.getFirstPosition() );
  243. } );
  244. expect( getData( model ) ).to.equal( '<paragraph>Foo<softBreak></softBreak>> []</paragraph>' );
  245. } );
  246. } );
  247. describe( 'Code block', () => {
  248. it( 'should replace triple grave accents with a code block', () => {
  249. setData( model, '<paragraph>``[]</paragraph>' );
  250. model.change( writer => {
  251. writer.insertText( '`', doc.selection.getFirstPosition() );
  252. } );
  253. expect( getData( model ) ).to.equal( '<codeBlock language="plaintext">[]</codeBlock>' );
  254. } );
  255. it( 'should replace triple grave accents in a heading', () => {
  256. setData( model, '<heading1>``[]</heading1>' );
  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 not replace triple grave accents when already in a code block', () => {
  263. setData( model, '<codeBlock language="plaintext">``[]</codeBlock>' );
  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 inside numbered list', () => {
  270. setData( model, '<listItem listIndent="0" listType="numbered">1. ``[]</listItem>' );
  271. model.change( writer => {
  272. writer.insertText( '`', doc.selection.getFirstPosition() );
  273. } );
  274. expect( getData( model ) ).to.equal( '<listItem listIndent="0" listType="numbered">1. ```[]</listItem>' );
  275. } );
  276. it( 'should not replace triple grave accents when inside buletted list', () => {
  277. setData( model, '<listItem listIndent="0" listType="bulleted">1. ``[]</listItem>' );
  278. model.change( writer => {
  279. writer.insertText( '`', doc.selection.getFirstPosition() );
  280. } );
  281. expect( getData( model ) ).to.equal( '<listItem listIndent="0" listType="bulleted">1. ```[]</listItem>' );
  282. } );
  283. } );
  284. describe( 'Inline autoformat', () => {
  285. it( 'should replace both "**" with bold', () => {
  286. setData( model, '<paragraph>**foobar*[]</paragraph>' );
  287. model.change( writer => {
  288. writer.insertText( '*', doc.selection.getFirstPosition() );
  289. } );
  290. expect( getData( model ) ).to.equal( '<paragraph><$text bold="true">foobar</$text>[]</paragraph>' );
  291. } );
  292. it( 'should replace both "*" with italic', () => {
  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 italic="true">foobar</$text>[]</paragraph>' );
  298. } );
  299. it( 'should replace both "`" with code', () => {
  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 code="true">foobar</$text>[]</paragraph>' );
  305. } );
  306. it( 'should replace both "~~" with strikethrough', () => {
  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 strikethrough="true">foobar</$text>[]</paragraph>' );
  312. } );
  313. it( 'nothing should be replaces when typing "*"', () => {
  314. setData( model, '<paragraph>foobar[]</paragraph>' );
  315. model.change( writer => {
  316. writer.insertText( '*', doc.selection.getFirstPosition() );
  317. } );
  318. expect( getData( model ) ).to.equal( '<paragraph>foobar*[]</paragraph>' );
  319. } );
  320. it( 'should format inside the text', () => {
  321. setData( model, '<paragraph>foo **bar*[] baz</paragraph>' );
  322. model.change( writer => {
  323. writer.insertText( '*', doc.selection.getFirstPosition() );
  324. } );
  325. expect( getData( model ) ).to.equal( '<paragraph>foo <$text bold="true">bar</$text>[] baz</paragraph>' );
  326. } );
  327. it( 'should not format if the command is not enabled', () => {
  328. model.schema.addAttributeCheck( ( context, attributeName ) => {
  329. if ( attributeName == 'bold' ) {
  330. return false;
  331. }
  332. } );
  333. setData( model, '<paragraph>**foobar*[]</paragraph>' );
  334. model.change( writer => {
  335. writer.insertText( '*', doc.selection.getFirstPosition() );
  336. } );
  337. expect( getData( model ) ).to.equal( '<paragraph>**foobar**[]</paragraph>' );
  338. } );
  339. it( 'should not format if the plugin is disabled', () => {
  340. editor.plugins.get( 'Autoformat' ).forceDisabled( 'Test' );
  341. setData( model, '<paragraph>**foobar*[]</paragraph>' );
  342. model.change( writer => {
  343. writer.insertText( '*', doc.selection.getFirstPosition() );
  344. } );
  345. expect( getData( model ) ).to.equal( '<paragraph>**foobar**[]</paragraph>' );
  346. } );
  347. describe( 'with code element', () => {
  348. describe( 'should not format', () => {
  349. it( '* inside', () => {
  350. setData( model, '<paragraph><$text code="true">fo*obar[]</$text></paragraph>' );
  351. model.change( writer => {
  352. writer.insertText( '*', { code: true }, doc.selection.getFirstPosition() );
  353. } );
  354. expect( getData( model ) ).to
  355. .equal( '<paragraph><$text code="true">fo*obar*[]</$text></paragraph>' );
  356. } );
  357. it( '__ inside', () => {
  358. setData( model, '<paragraph><$text code="true">fo__obar_[]</$text></paragraph>' );
  359. model.change( writer => {
  360. writer.insertText( '_', { code: true }, doc.selection.getFirstPosition() );
  361. } );
  362. expect( getData( model ) ).to
  363. .equal( '<paragraph><$text code="true">fo__obar__[]</$text></paragraph>' );
  364. } );
  365. it( '~~ inside', () => {
  366. setData( model, '<paragraph><$text code="true">fo~~obar~[]</$text></paragraph>' );
  367. model.change( writer => {
  368. writer.insertText( '~', { code: true }, doc.selection.getFirstPosition() );
  369. } );
  370. expect( getData( model ) ).to
  371. .equal( '<paragraph><$text code="true">fo~~obar~~[]</$text></paragraph>' );
  372. } );
  373. it( '` inside', () => {
  374. setData( model, '<paragraph><$text code="true">fo`obar[]</$text></paragraph>' );
  375. model.change( writer => {
  376. writer.insertText( '`', { code: true }, doc.selection.getFirstPosition() );
  377. } );
  378. expect( getData( model ) ).to
  379. .equal( '<paragraph><$text code="true">fo`obar`[]</$text></paragraph>' );
  380. } );
  381. } );
  382. describe( 'should not format', () => {
  383. it( '* across', () => {
  384. setData( model, '<paragraph><$text code="true">fo*o</$text>bar[]</paragraph>' );
  385. model.change( writer => {
  386. writer.insertText( '*', doc.selection.getFirstPosition() );
  387. } );
  388. expect( getData( model ) ).to
  389. .equal( '<paragraph><$text code="true">fo*o</$text>bar*[]</paragraph>' );
  390. } );
  391. it( '__ across', () => {
  392. setData( model, '<paragraph><$text code="true">fo__o</$text>bar_[]</paragraph>' );
  393. model.change( writer => {
  394. writer.insertText( '_', doc.selection.getFirstPosition() );
  395. } );
  396. expect( getData( model ) ).to
  397. .equal( '<paragraph><$text code="true">fo__o</$text>bar__[]</paragraph>' );
  398. } );
  399. it( '~~ across', () => {
  400. setData( model, '<paragraph><$text code="true">fo~~o</$text>bar~[]</paragraph>' );
  401. model.change( writer => {
  402. writer.insertText( '~', doc.selection.getFirstPosition() );
  403. } );
  404. expect( getData( model ) ).to
  405. .equal( '<paragraph><$text code="true">fo~~o</$text>bar~~[]</paragraph>' );
  406. } );
  407. it( '` across', () => {
  408. setData( model, '<paragraph><$text code="true">fo`o</$text>bar[]</paragraph>' );
  409. model.change( writer => {
  410. writer.insertText( '`', doc.selection.getFirstPosition() );
  411. } );
  412. expect( getData( model ) ).to
  413. .equal( '<paragraph><$text code="true">fo`o</$text>bar`[]</paragraph>' );
  414. } );
  415. } );
  416. describe( 'should format', () => {
  417. it( '* after', () => {
  418. setData( model, '<paragraph><$text code="true">fo*o</$text>b*ar[]</paragraph>' );
  419. model.change( writer => {
  420. writer.insertText( '*', doc.selection.getFirstPosition() );
  421. } );
  422. expect( getData( model ) ).to
  423. .equal( '<paragraph><$text code="true">fo*o</$text>b<$text italic="true">ar</$text>[]</paragraph>' );
  424. } );
  425. it( '__ after', () => {
  426. setData( model, '<paragraph><$text code="true">fo__o</$text>b__ar_[]</paragraph>' );
  427. model.change( writer => {
  428. writer.insertText( '_', doc.selection.getFirstPosition() );
  429. } );
  430. expect( getData( model ) ).to
  431. .equal( '<paragraph><$text code="true">fo__o</$text>b<$text bold="true">ar</$text>[]</paragraph>' );
  432. } );
  433. it( '~~ after', () => {
  434. setData( model, '<paragraph><$text code="true">fo~~o</$text>b~~ar~[]</paragraph>' );
  435. model.change( writer => {
  436. writer.insertText( '~', doc.selection.getFirstPosition() );
  437. } );
  438. expect( getData( model ) ).to
  439. .equal( '<paragraph><$text code="true">fo~~o</$text>b<$text strikethrough="true">ar</$text>[]</paragraph>' );
  440. } );
  441. it( '` after', () => {
  442. setData( model, '<paragraph><$text code="true">fo`o</$text>b`ar[]</paragraph>' );
  443. model.change( writer => {
  444. writer.insertText( '`', doc.selection.getFirstPosition() );
  445. } );
  446. expect( getData( model ) ).to
  447. .equal( '<paragraph><$text code="true">fo`o</$text>b<$text code="true">ar</$text>[]</paragraph>' );
  448. } );
  449. } );
  450. } );
  451. it( 'should work with <softBreak>s in paragraph', () => {
  452. setData( model, '<paragraph>foo<softBreak></softBreak>**barbaz*[]</paragraph>' );
  453. model.change( writer => {
  454. writer.insertText( '*', doc.selection.getFirstPosition() );
  455. } );
  456. expect( getData( model ) ).to.equal( '<paragraph>foo<softBreak></softBreak><$text bold="true">barbaz</$text>[]</paragraph>' );
  457. } );
  458. } );
  459. describe( 'without commands', () => {
  460. beforeEach( () => {
  461. return VirtualTestEditor
  462. .create( {
  463. plugins: [ Enter, Paragraph, Autoformat ]
  464. } )
  465. .then( newEditor => {
  466. editor = newEditor;
  467. model = editor.model;
  468. doc = model.document;
  469. } );
  470. } );
  471. it( 'should not replace asterisk with bulleted list item', () => {
  472. setData( model, '<paragraph>*[]</paragraph>' );
  473. model.change( writer => {
  474. writer.insertText( ' ', doc.selection.getFirstPosition() );
  475. } );
  476. expect( getData( model ) ).to.equal( '<paragraph>* []</paragraph>' );
  477. } );
  478. it( 'should not replace minus character 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 digit with numbered list item', () => {
  486. setData( model, '<paragraph>1.[]</paragraph>' );
  487. model.change( writer => {
  488. writer.insertText( ' ', doc.selection.getFirstPosition() );
  489. } );
  490. expect( getData( model ) ).to.equal( '<paragraph>1. []</paragraph>' );
  491. } );
  492. it( 'should not replace hash character with heading', () => {
  493. setData( model, '<paragraph>#[]</paragraph>' );
  494. model.change( writer => {
  495. writer.insertText( ' ', doc.selection.getFirstPosition() );
  496. } );
  497. expect( getData( model ) ).to.equal( '<paragraph># []</paragraph>' );
  498. } );
  499. it( 'should not replace two hash characters with heading level 2', () => {
  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 both "**" with bold', () => {
  507. setData( model, '<paragraph>**foobar*[]</paragraph>' );
  508. model.change( writer => {
  509. writer.insertText( '*', doc.selection.getFirstPosition() );
  510. } );
  511. expect( getData( model ) ).to.equal( '<paragraph>**foobar**[]</paragraph>' );
  512. } );
  513. it( 'should not replace both "*" with italic', () => {
  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 code', () => {
  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 ">" with block quote', () => {
  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 "```" with code block', () => {
  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 use only configured headings', () => {
  542. return VirtualTestEditor
  543. .create( {
  544. plugins: [ Enter, Paragraph, Autoformat, ListEditing, HeadingEditing ],
  545. heading: {
  546. options: [
  547. { model: 'paragraph' },
  548. { model: 'heading1', view: 'h2' }
  549. ]
  550. }
  551. } )
  552. .then( editor => {
  553. model = editor.model;
  554. doc = model.document;
  555. setData( model, '<paragraph>##[]</paragraph>' );
  556. model.change( writer => {
  557. writer.insertText( ' ', doc.selection.getFirstPosition() );
  558. } );
  559. expect( getData( model ) ).to.equal( '<paragraph>## []</paragraph>' );
  560. } );
  561. } );
  562. } );
  563. } );