inlineautoformatengine.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import InlineAutoformatEngine from '/ckeditor5/autoformat/inlineautoformatengine.js';
  6. import Paragraph from '/ckeditor5/paragraph/paragraph.js';
  7. import VirtualTestEditor from '/tests/core/_utils/virtualtesteditor.js';
  8. import Enter from '/ckeditor5/enter/enter.js';
  9. import { setData, getData } from '/ckeditor5/engine/dev-utils/model.js';
  10. import testUtils from '/tests/core/_utils/utils.js';
  11. testUtils.createSinonSandbox();
  12. describe( 'InlineAutoformatEngine', () => {
  13. let editor, doc, batch;
  14. beforeEach( () => {
  15. return VirtualTestEditor.create( {
  16. features: [ Enter, Paragraph ]
  17. } )
  18. .then( newEditor => {
  19. editor = newEditor;
  20. doc = editor.document;
  21. batch = doc.batch();
  22. doc.schema.allow( { name: '$inline', attributes: [ 'testAttribute' ] } );
  23. } );
  24. } );
  25. describe( 'attribute', () => {
  26. it( 'should stop early if there are less than 3 capture groups', () => {
  27. new InlineAutoformatEngine( editor, /(\*)(.+?)\*/g, 'testAttribute' );
  28. setData( doc, '<paragraph>*foobar[]</paragraph>' );
  29. doc.enqueueChanges( () => {
  30. batch.insert( doc.selection.getFirstPosition(), '*' );
  31. } );
  32. expect( getData( doc ) ).to.equal( '<paragraph>*foobar*[]</paragraph>' );
  33. } );
  34. it( 'should apply an attribute when the pattern is matched', () => {
  35. new InlineAutoformatEngine( editor, /(\*)(.+?)(\*)/g, 'testAttribute' );
  36. setData( doc, '<paragraph>*foobar[]</paragraph>' );
  37. doc.enqueueChanges( () => {
  38. batch.insert( doc.selection.getFirstPosition(), '*' );
  39. } );
  40. expect( getData( doc ) ).to.equal( '<paragraph><$text testAttribute="true">foobar</$text>[]</paragraph>' );
  41. } );
  42. it( 'should stop early if selection is not collapsed', () => {
  43. new InlineAutoformatEngine( editor, /(\*)(.+?)\*/g, 'testAttribute' );
  44. setData( doc, '<paragraph>*foob[ar]</paragraph>' );
  45. doc.enqueueChanges( () => {
  46. batch.insert( doc.selection.getFirstPosition(), '*' );
  47. } );
  48. expect( getData( doc ) ).to.equal( '<paragraph>*foob[*ar]</paragraph>' );
  49. } );
  50. it( 'should stop early if there are block elements in the way', () => {
  51. new InlineAutoformatEngine( editor, /(\*)(.+?)(\*)/g, 'testAttribute' );
  52. doc.schema.registerItem( 'widget', '$block' );
  53. setData( doc, '<paragraph>*foo<paragraph>baz</paragraph>bar[]</paragraph>' );
  54. doc.enqueueChanges( () => {
  55. batch.insert( doc.selection.getFirstPosition(), '*' );
  56. } );
  57. expect( getData( doc ) ).to.equal( '<paragraph>*foo<paragraph>baz</paragraph>bar*[]</paragraph>' );
  58. } );
  59. } );
  60. describe( 'Callback', () => {
  61. it( 'should stop when there are no format ranges returned from testCallback', () => {
  62. const formatSpy = testUtils.sinon.spy();
  63. const testStub = testUtils.sinon.stub().returns( {
  64. format: [ [] ],
  65. remove: []
  66. } );
  67. new InlineAutoformatEngine( editor, testStub, formatSpy );
  68. setData( doc, '<paragraph>*[]</paragraph>' );
  69. doc.enqueueChanges( () => {
  70. batch.insert( doc.selection.getFirstPosition(), ' ' );
  71. } );
  72. sinon.assert.notCalled( formatSpy );
  73. } );
  74. it( 'should stop when there are no remove ranges returned from testCallback', () => {
  75. const formatSpy = testUtils.sinon.spy();
  76. const testStub = testUtils.sinon.stub().returns( {
  77. format: [],
  78. remove: [ [] ]
  79. } );
  80. new InlineAutoformatEngine( editor, testStub, formatSpy );
  81. setData( doc, '<paragraph>*[]</paragraph>' );
  82. doc.enqueueChanges( () => {
  83. batch.insert( doc.selection.getFirstPosition(), ' ' );
  84. } );
  85. sinon.assert.notCalled( formatSpy );
  86. } );
  87. it( 'should stop early when there is no text', () => {
  88. const formatSpy = testUtils.sinon.spy();
  89. const testStub = testUtils.sinon.stub().returns( {
  90. format: [],
  91. remove: [ [] ]
  92. } );
  93. new InlineAutoformatEngine( editor, testStub, formatSpy );
  94. setData( doc, '<paragraph>[]</paragraph>' );
  95. doc.enqueueChanges( () => {
  96. batch.insert( doc.selection.getFirstPosition(), ' ' );
  97. } );
  98. sinon.assert.notCalled( formatSpy );
  99. } );
  100. } );
  101. } );