/** * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license */ /* globals document */ import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor'; import Clipboard from '../src/clipboard'; import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph'; import BlockQuote from '@ckeditor/ckeditor5-block-quote/src/blockquote'; import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold'; import Italic from '@ckeditor/ckeditor5-basic-styles/src/italic'; import Link from '@ckeditor/ckeditor5-link/src/link'; import { setData, getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model'; describe( 'Pasting – integration', () => { let element; beforeEach( () => { element = document.createElement( 'div' ); document.body.appendChild( element ); } ); afterEach( () => { element.remove(); } ); describe( 'inline styles', () => { // See https://github.com/ckeditor/ckeditor5/issues/477. it( 'pastes inline styles and links (no block)', () => { return ClassicTestEditor .create( element, { plugins: [ Clipboard, Paragraph, Bold, Italic, Link ] } ) .then( editor => { setData( editor.model, '[]' ); pasteHtml( editor, 'x bold italic link y' ); expect( getData( editor.model ) ).to.equal( '' + 'x <$text bold="true">bold <$text italic="true">italic ' + '<$text linkHref="x">link y[]' + '' ); return editor.destroy(); } ); } ); it( 'pastes inline styles and links (inside known block)', () => { return ClassicTestEditor .create( element, { plugins: [ Clipboard, Paragraph, Bold, Italic, Link ] } ) .then( editor => { setData( editor.model, '[]' ); pasteHtml( editor, '

x bold italic link y

' ); expect( getData( editor.model ) ).to.equal( '' + 'x <$text bold="true">bold <$text italic="true">italic ' + '<$text linkHref="x">link y[]' + '' ); return editor.destroy(); } ); } ); it( 'pastes inline styles and links (inside unknown block)', () => { return ClassicTestEditor .create( element, { plugins: [ Clipboard, Paragraph, Bold, Italic, Link ] } ) .then( editor => { setData( editor.model, '[]' ); pasteHtml( editor, '
x bold italic link y
' ); expect( getData( editor.model ) ).to.equal( '' + 'x <$text bold="true">bold <$text italic="true">italic ' + '<$text linkHref="x">link y[]' + '' ); return editor.destroy(); } ); } ); it( 'pastes inline styles and links (inside known block but on disallowed position)', () => { return ClassicTestEditor .create( element, { plugins: [ Clipboard, Paragraph, BlockQuote, Bold, Italic, Link ] } ) .then( editor => { setData( editor.model, '[]' ); pasteHtml( editor, '
x bold italic link y
' ); expect( getData( editor.model ) ).to.equal( '
' + '' + 'x <$text bold="true">bold <$text italic="true">italic ' + '<$text linkHref="x">link y[]' + '' + '
' ); return editor.destroy(); } ); } ); } ); describe( 'white spaces', () => { // See https://github.com/ckeditor/ckeditor5-clipboard/issues/2#issuecomment-310417731. it( 'keeps spaces around inline styles (Chrome)', () => { return ClassicTestEditor .create( element, { plugins: [ Clipboard, Paragraph, Bold, Italic, Link ] } ) .then( editor => { setData( editor.model, 'x[]y' ); pasteHtml( editor, '' + 'This is the\u00a0' + 'third developer preview' + '\u00a0of\u00a0' + 'CKEditor\u00a05' + '.' ); expect( getData( editor.model ) ).to.equal( '' + 'xThis is the ' + '<$text linkHref="url">third developer preview of <$text bold="true">CKEditor\u00a05' + '.[]y' + '' ); return editor.destroy(); } ); } ); // See https://github.com/ckeditor/ckeditor5-clipboard/issues/2#issuecomment-310417731. it( 'keeps spaces around inline styles (Safari)', () => { return ClassicTestEditor .create( element, { plugins: [ Clipboard, Paragraph, Bold, Italic, Link ] } ) .then( editor => { setData( editor.model, 'x[]y' ); /* eslint-disable max-len */ pasteHtml( editor, 'This is the\u00a0' + 'third developer preview' + '\u00a0of\u00a0' + 'CKEditor\u00a05' + '.' ); /* eslint-enable max-len */ expect( getData( editor.model ) ).to.equal( '' + 'xThis is the ' + '<$text linkHref="url">third developer preview of <$text bold="true">CKEditor\u00a05' + '.[]y' + '' ); return editor.destroy(); } ); } ); it( 'keeps spaces around inline styles (Firefox)', () => { return ClassicTestEditor .create( element, { plugins: [ Clipboard, Paragraph, Bold, Italic, Link ] } ) .then( editor => { setData( editor.model, 'x[]y' ); // Note, when copying the HTML from Firefox's console you'll see only normal spaces, // but when you check it later in the model it's still an nbsp. pasteHtml( editor, 'This is the third developer preview of CKEditor\u00a05.' ); expect( getData( editor.model ) ).to.equal( '' + 'xThis is the ' + '<$text linkHref="url">third developer preview of <$text bold="true">CKEditor\u00a05' + '.[]y' + '' ); return editor.destroy(); } ); } ); } ); } ); function pasteHtml( editor, html ) { editor.editing.view.document.fire( 'paste', { dataTransfer: createDataTransfer( { 'text/html': html } ), stopPropagation() {}, preventDefault() {} } ); } function createDataTransfer( data ) { return { getData( type ) { return data[ type ]; } }; }