autoformat.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Autoformat from '../src/autoformat';
  6. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  7. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  8. import Enter from '@ckeditor/ckeditor5-enter/src/enter';
  9. import { setData, getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  10. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  11. testUtils.createSinonSandbox();
  12. describe( 'Autoformat', () => {
  13. let editor, doc, batch;
  14. beforeEach( () => {
  15. return VirtualTestEditor.create( {
  16. plugins: [ Enter, Paragraph, Autoformat ]
  17. } )
  18. .then( newEditor => {
  19. editor = newEditor;
  20. doc = editor.document;
  21. batch = doc.batch();
  22. } );
  23. } );
  24. describe( 'Bulleted list', () => {
  25. it( 'should replace asterisk with bulleted list item', () => {
  26. setData( doc, '<paragraph>*[]</paragraph>' );
  27. doc.enqueueChanges( () => {
  28. batch.insert( doc.selection.getFirstPosition(), ' ' );
  29. } );
  30. expect( getData( doc ) ).to.equal( '<listItem indent="0" type="bulleted">[]</listItem>' );
  31. } );
  32. it( 'should replace minus character with bulleted list item', () => {
  33. setData( doc, '<paragraph>-[]</paragraph>' );
  34. doc.enqueueChanges( () => {
  35. batch.insert( doc.selection.getFirstPosition(), ' ' );
  36. } );
  37. expect( getData( doc ) ).to.equal( '<listItem indent="0" type="bulleted">[]</listItem>' );
  38. } );
  39. it( 'should not replace minus character when inside bulleted list item', () => {
  40. setData( doc, '<listItem indent="0" type="bulleted">-[]</listItem>' );
  41. doc.enqueueChanges( () => {
  42. batch.insert( doc.selection.getFirstPosition(), ' ' );
  43. } );
  44. expect( getData( doc ) ).to.equal( '<listItem indent="0" type="bulleted">- []</listItem>' );
  45. } );
  46. } );
  47. describe( 'Numbered list', () => {
  48. it( 'should replace digit with numbered list item', () => {
  49. setData( doc, '<paragraph>1.[]</paragraph>' );
  50. doc.enqueueChanges( () => {
  51. batch.insert( doc.selection.getFirstPosition(), ' ' );
  52. } );
  53. expect( getData( doc ) ).to.equal( '<listItem indent="0" type="numbered">[]</listItem>' );
  54. } );
  55. it( 'should not replace digit character when inside numbered list item', () => {
  56. setData( doc, '<listItem indent="0" type="numbered">1.[]</listItem>' );
  57. doc.enqueueChanges( () => {
  58. batch.insert( doc.selection.getFirstPosition(), ' ' );
  59. } );
  60. expect( getData( doc ) ).to.equal( '<listItem indent="0" type="numbered">1. []</listItem>' );
  61. } );
  62. } );
  63. describe( 'Heading', () => {
  64. it( 'should replace hash character with heading', () => {
  65. setData( doc, '<paragraph>#[]</paragraph>' );
  66. doc.enqueueChanges( () => {
  67. batch.insert( doc.selection.getFirstPosition(), ' ' );
  68. } );
  69. expect( getData( doc ) ).to.equal( '<heading1>[]</heading1>' );
  70. } );
  71. it( 'should replace two hash characters with heading level 2', () => {
  72. setData( doc, '<paragraph>##[]</paragraph>' );
  73. doc.enqueueChanges( () => {
  74. batch.insert( doc.selection.getFirstPosition(), ' ' );
  75. } );
  76. expect( getData( doc ) ).to.equal( '<heading2>[]</heading2>' );
  77. } );
  78. it( 'should not replace minus character when inside heading', () => {
  79. setData( doc, '<heading1>#[]</heading1>' );
  80. doc.enqueueChanges( () => {
  81. batch.insert( doc.selection.getFirstPosition(), ' ' );
  82. } );
  83. expect( getData( doc ) ).to.equal( '<heading1># []</heading1>' );
  84. } );
  85. } );
  86. describe( 'Inline autoformat', () => {
  87. it( 'should replace both `**` with bold', () => {
  88. setData( doc, '<paragraph>**foobar*[]</paragraph>' );
  89. doc.enqueueChanges( () => {
  90. batch.insert( doc.selection.getFirstPosition(), '*' );
  91. } );
  92. expect( getData( doc ) ).to.equal( '<paragraph><$text bold="true">foobar</$text>[]</paragraph>' );
  93. } );
  94. it( 'should replace both `*` with italic', () => {
  95. setData( doc, '<paragraph>*foobar[]</paragraph>' );
  96. doc.enqueueChanges( () => {
  97. batch.insert( doc.selection.getFirstPosition(), '*' );
  98. } );
  99. expect( getData( doc ) ).to.equal( '<paragraph><$text italic="true">foobar</$text>[]</paragraph>' );
  100. } );
  101. it( 'nothing should be replaces when typing `*`', () => {
  102. setData( doc, '<paragraph>foobar[]</paragraph>' );
  103. doc.enqueueChanges( () => {
  104. batch.insert( doc.selection.getFirstPosition(), '*' );
  105. } );
  106. expect( getData( doc ) ).to.equal( '<paragraph>foobar*[]</paragraph>' );
  107. } );
  108. it( 'should format inside the text', () => {
  109. setData( doc, '<paragraph>foo **bar*[] baz</paragraph>' );
  110. doc.enqueueChanges( () => {
  111. batch.insert( doc.selection.getFirstPosition(), '*' );
  112. } );
  113. expect( getData( doc ) ).to.equal( '<paragraph>foo <$text bold="true">bar</$text>[] baz</paragraph>' );
  114. } );
  115. } );
  116. } );