| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- /**
- * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
- import InlineAutoformatEngine from '/ckeditor5/autoformat/inlineautoformatengine.js';
- import Paragraph from '/ckeditor5/paragraph/paragraph.js';
- import VirtualTestEditor from '/tests/core/_utils/virtualtesteditor.js';
- import Enter from '/ckeditor5/enter/enter.js';
- import { setData, getData } from '/ckeditor5/engine/dev-utils/model.js';
- import testUtils from '/tests/core/_utils/utils.js';
- testUtils.createSinonSandbox();
- describe( 'InlineAutoformatEngine', () => {
- let editor, doc, batch;
- beforeEach( () => {
- return VirtualTestEditor.create( {
- features: [ Enter, Paragraph ]
- } )
- .then( newEditor => {
- editor = newEditor;
- doc = editor.document;
- batch = doc.batch();
- doc.schema.allow( { name: '$inline', attributes: [ 'testAttribute' ] } );
- } );
- } );
- describe( 'attribute', () => {
- it( 'should stop early if there are less than 3 capture groups', () => {
- new InlineAutoformatEngine( editor, /(\*)(.+?)\*/g, 'testAttribute' );
- setData( doc, '<paragraph>*foobar[]</paragraph>' );
- doc.enqueueChanges( () => {
- batch.insert( doc.selection.getFirstPosition(), '*' );
- } );
- expect( getData( doc ) ).to.equal( '<paragraph>*foobar*[]</paragraph>' );
- } );
- it( 'should apply an attribute when the pattern is matched', () => {
- new InlineAutoformatEngine( editor, /(\*)(.+?)(\*)/g, 'testAttribute' );
- setData( doc, '<paragraph>*foobar[]</paragraph>' );
- doc.enqueueChanges( () => {
- batch.insert( doc.selection.getFirstPosition(), '*' );
- } );
- expect( getData( doc ) ).to.equal( '<paragraph><$text testAttribute="true">foobar</$text>[]</paragraph>' );
- } );
- it( 'should stop early if selection is not collapsed', () => {
- new InlineAutoformatEngine( editor, /(\*)(.+?)\*/g, 'testAttribute' );
- setData( doc, '<paragraph>*foob[ar]</paragraph>' );
- doc.enqueueChanges( () => {
- batch.insert( doc.selection.getFirstPosition(), '*' );
- } );
- expect( getData( doc ) ).to.equal( '<paragraph>*foob[*ar]</paragraph>' );
- } );
- it( 'should stop early if there are block elements in the way', () => {
- new InlineAutoformatEngine( editor, /(\*)(.+?)(\*)/g, 'testAttribute' );
- doc.schema.registerItem( 'widget', '$block' );
- setData( doc, '<paragraph>*foo<paragraph>baz</paragraph>bar[]</paragraph>' );
- doc.enqueueChanges( () => {
- batch.insert( doc.selection.getFirstPosition(), '*' );
- } );
- expect( getData( doc ) ).to.equal( '<paragraph>*foo<paragraph>baz</paragraph>bar*[]</paragraph>' );
- } );
- } );
- describe( 'Callback', () => {
- it( 'should stop when there are no format ranges returned from testCallback', () => {
- const formatSpy = testUtils.sinon.spy();
- const testStub = testUtils.sinon.stub().returns( {
- format: [ [] ],
- remove: []
- } );
- new InlineAutoformatEngine( editor, testStub, formatSpy );
- setData( doc, '<paragraph>*[]</paragraph>' );
- doc.enqueueChanges( () => {
- batch.insert( doc.selection.getFirstPosition(), ' ' );
- } );
- sinon.assert.notCalled( formatSpy );
- } );
- it( 'should stop when there are no remove ranges returned from testCallback', () => {
- const formatSpy = testUtils.sinon.spy();
- const testStub = testUtils.sinon.stub().returns( {
- format: [],
- remove: [ [] ]
- } );
- new InlineAutoformatEngine( editor, testStub, formatSpy );
- setData( doc, '<paragraph>*[]</paragraph>' );
- doc.enqueueChanges( () => {
- batch.insert( doc.selection.getFirstPosition(), ' ' );
- } );
- sinon.assert.notCalled( formatSpy );
- } );
- it( 'should stop early when there is no text', () => {
- const formatSpy = testUtils.sinon.spy();
- const testStub = testUtils.sinon.stub().returns( {
- format: [],
- remove: [ [] ]
- } );
- new InlineAutoformatEngine( editor, testStub, formatSpy );
- setData( doc, '<paragraph>[]</paragraph>' );
- doc.enqueueChanges( () => {
- batch.insert( doc.selection.getFirstPosition(), ' ' );
- } );
- sinon.assert.notCalled( formatSpy );
- } );
- } );
- } );
|