/** * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license */ import Paragraph from '../src/paragraph'; import Clipboard from '@ckeditor/ckeditor5-clipboard/src/clipboard'; import UndoEditing from '@ckeditor/ckeditor5-undo/src/undoediting'; import HeadingEditing from '@ckeditor/ckeditor5-heading/src/headingediting'; import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor'; import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model'; import { parse as parseView } from '@ckeditor/ckeditor5-engine/src/dev-utils/view'; describe( 'Paragraph feature – integration', () => { describe( 'with clipboard', () => { it( 'pastes h1+h2+p as p+p+p when heading feature is not present', () => { return VirtualTestEditor .create( { plugins: [ Paragraph, Clipboard ] } ) .then( newEditor => { const editor = newEditor; const clipboard = editor.plugins.get( 'Clipboard' ); setModelData( editor.model, '[]' ); clipboard.fire( 'inputTransformation', { content: parseView( '

foo

bar

bom

' ) } ); expect( getModelData( editor.model ) ).to.equal( 'foobarbom[]' ); } ); } ); // Explainer: the heading feature is configured to handle h1-h4 elements, so h5 has no handler. it( 'pastes h1+h2+h5+p as h1+h2+p+p when heading feature is present', () => { return VirtualTestEditor .create( { plugins: [ Paragraph, Clipboard, HeadingEditing ] } ) .then( newEditor => { const editor = newEditor; const clipboard = editor.plugins.get( 'Clipboard' ); setModelData( editor.model, '[]' ); clipboard.fire( 'inputTransformation', { content: parseView( '

foo

bar

baz

bom

' ) } ); expect( getModelData( editor.model ) ).to.equal( 'foobarbazbom[]' ); } ); } ); it( 'pastes ul>li+li as p+p when list feature is not present', () => { return VirtualTestEditor .create( { plugins: [ Paragraph, Clipboard ] } ) .then( newEditor => { const editor = newEditor; const clipboard = editor.plugins.get( 'Clipboard' ); setModelData( editor.model, '[]' ); clipboard.fire( 'inputTransformation', { content: parseView( '' ) } ); expect( getModelData( editor.model ) ).to.equal( 'foobar[]' ); } ); } ); // Check whether the paragraph feature doesn't breaking pasting such content by trying to // handle the li element. it( 'pastes ul>li>h2+h3+p as h2+h3+p when heading feature is present', () => { return VirtualTestEditor .create( { plugins: [ Paragraph, Clipboard, HeadingEditing ] } ) .then( newEditor => { const editor = newEditor; const clipboard = editor.plugins.get( 'Clipboard' ); setModelData( editor.model, '[]' ); clipboard.fire( 'inputTransformation', { content: parseView( '' ) } ); expect( getModelData( editor.model ) ).to.equal( 'x' + 'foobarbom' + 'x[]' ); } ); } ); // See 'should convert ul>li>ul>li+li (in clipboard holder)' in clipboard.js. it( 'pastes ul>li>ul>li+li', () => { return VirtualTestEditor .create( { plugins: [ Paragraph, Clipboard ] } ) .then( newEditor => { const editor = newEditor; const clipboard = editor.plugins.get( 'Clipboard' ); setModelData( editor.model, '[]' ); clipboard.fire( 'inputTransformation', { content: parseView( '' ) } ); expect( getModelData( editor.model ) ).to.equal( 'a' + 'b' + 'c[]' ); } ); } ); // See 'should convert ul>li>p,text (in clipboard holder)' in clipboard.js. it( 'pastes ul>li>p,text', () => { return VirtualTestEditor .create( { plugins: [ Paragraph, Clipboard ] } ) .then( newEditor => { const editor = newEditor; const clipboard = editor.plugins.get( 'Clipboard' ); setModelData( editor.model, '[]' ); clipboard.fire( 'inputTransformation', { content: parseView( '' ) } ); expect( getModelData( editor.model ) ).to.equal( 'a' + 'b[]' ); } ); } ); } ); describe( 'with undo', () => { it( 'fixing empty roots should be transparent to undo', () => { return VirtualTestEditor .create( { plugins: [ Paragraph, UndoEditing ] } ) .then( newEditor => { const editor = newEditor; const doc = editor.model.document; const root = doc.getRoot(); expect( editor.getData( { trim: 'none' } ) ).to.equal( '

 

' ); expect( editor.commands.get( 'undo' ).isEnabled ).to.be.false; editor.setData( '

Foobar.

' ); editor.model.change( writer => { writer.remove( root.getChild( 0 ) ); } ); expect( editor.getData( { trim: 'none' } ) ).to.equal( '

 

' ); editor.execute( 'undo' ); expect( editor.getData( { trim: 'none' } ) ).to.equal( '

Foobar.

' ); editor.execute( 'redo' ); expect( editor.getData( { trim: 'none' } ) ).to.equal( '

 

' ); editor.execute( 'undo' ); expect( editor.getData( { trim: 'none' } ) ).to.equal( '

Foobar.

' ); } ); } ); it( 'fixing empty roots should be transparent to undo - multiple roots', () => { return VirtualTestEditor .create( { plugins: [ Paragraph, UndoEditing ] } ) .then( newEditor => { const editor = newEditor; const doc = editor.model.document; const root = doc.getRoot(); const otherRoot = doc.createRoot( '$root', 'otherRoot' ); editor.data.set( { main: '

Foobar.

' } ); editor.data.set( { otherRoot: '

Foobar.

' } ); editor.model.change( writer => { writer.remove( root.getChild( 0 ) ); } ); editor.model.change( writer => { writer.remove( otherRoot.getChild( 0 ) ); } ); expect( editor.data.get( { rootName: 'main', trim: 'none' } ) ).to.equal( '

 

' ); expect( editor.data.get( { rootName: 'otherRoot', trim: 'none' } ) ).to.equal( '

 

' ); editor.execute( 'undo' ); expect( editor.data.get( { rootName: 'main', trim: 'none' } ) ).to.equal( '

 

' ); expect( editor.data.get( { rootName: 'otherRoot', trim: 'none' } ) ).to.equal( '

Foobar.

' ); editor.execute( 'undo' ); expect( editor.data.get( { rootName: 'main', trim: 'none' } ) ).to.equal( '

Foobar.

' ); expect( editor.data.get( { rootName: 'otherRoot', trim: 'none' } ) ).to.equal( '

Foobar.

' ); } ); } ); } ); } );