| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515 |
- /**
- * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
- */
- /* global document */
- import CodeBlockEditing from '../src/codeblockediting';
- import CodeBlockCommand from '../src/codeblockcommand';
- import AlignmentEditing from '@ckeditor/ckeditor5-alignment/src/alignmentediting';
- import BoldEditing from '@ckeditor/ckeditor5-basic-styles/src/bold/boldediting';
- import Enter from '@ckeditor/ckeditor5-enter/src/enter';
- import ShiftEnter from '@ckeditor/ckeditor5-enter/src/shiftenter';
- import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
- import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
- import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
- import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
- import { _clear as clearTranslations, add as addTranslations } from '@ckeditor/ckeditor5-utils/src/translation-service';
- describe( 'CodeBlockEditing', () => {
- let editor, element, model, view;
- before( () => {
- addTranslations( 'en', {
- 'Plain text': 'Plain text'
- } );
- addTranslations( 'pl', {
- 'Plain text': 'Zwykły tekst'
- } );
- } );
- after( () => {
- clearTranslations();
- } );
- beforeEach( () => {
- element = document.createElement( 'div' );
- document.body.appendChild( element );
- return ClassicTestEditor
- .create( element, {
- language: 'en',
- plugins: [ CodeBlockEditing, AlignmentEditing, BoldEditing, Enter, Paragraph ]
- } )
- .then( newEditor => {
- editor = newEditor;
- model = editor.model;
- view = editor.editing.view;
- } );
- } );
- afterEach( () => {
- return editor.destroy().then( () => element.remove() );
- } );
- it( 'defines plugin name', () => {
- expect( CodeBlockEditing.pluginName ).to.equal( 'CodeBlockEditing' );
- } );
- it( 'defines plugin dependencies', () => {
- expect( CodeBlockEditing.requires ).to.have.members( [ ShiftEnter ] );
- } );
- describe( 'config', () => {
- describe( 'languages', () => {
- describe( 'default value', () => {
- it( 'should be set', () => {
- expect( editor.config.get( 'codeBlock.languages' ) ).to.deep.equal( [
- { class: 'plaintext', label: 'Plain text' },
- { class: 'c', label: 'C' },
- { class: 'cs', label: 'C#' },
- { class: 'cpp', label: 'C++' },
- { class: 'css', label: 'CSS' },
- { class: 'diff', label: 'Diff' },
- { class: 'xml', label: 'HTML/XML' },
- { class: 'java', label: 'Java' },
- { class: 'javascript', label: 'JavaScript' },
- { class: 'php', label: 'PHP' },
- { class: 'python', label: 'Python' },
- { class: 'ruby', label: 'Ruby' },
- { class: 'typescript', label: 'TypeScript' }
- ] );
- } );
- } );
- it( 'should be recognized when loading data', () => {
- return ClassicTestEditor.create(
- '<pre><code class="foo">bar</code></pre>' +
- '<pre><code class="bar">baz</code></pre>',
- {
- plugins: [ CodeBlockEditing ],
- codeBlock: {
- languages: [
- { class: 'foo', label: 'Foo' },
- { class: 'bar', label: 'Bar' },
- ]
- }
- } )
- .then( editor => {
- model = editor.model;
- expect( getModelData( model ) ).to.equal(
- '<codeBlock language="foo">[]bar</codeBlock>' +
- '<codeBlock language="bar">baz</codeBlock>'
- );
- return editor.destroy();
- } );
- } );
- it( 'should use the first if the code in data has no language', () => {
- return ClassicTestEditor
- .create( '<pre><code>bar</code></pre>', {
- plugins: [ CodeBlockEditing ],
- codeBlock: {
- languages: [
- { class: 'foo', label: 'Foo' },
- { class: 'bar', label: 'Bar' }
- ]
- }
- } )
- .then( editor => {
- model = editor.model;
- expect( getModelData( model ) ).to.equal( '<codeBlock language="foo">[]bar</codeBlock>' );
- return editor.destroy();
- } );
- } );
- it( 'should use the first if the code in data has an invalid language', () => {
- return ClassicTestEditor
- .create( '<pre><code class="baz">bar</code></pre>', {
- plugins: [ CodeBlockEditing ],
- codeBlock: {
- languages: [
- { class: 'foo', label: 'Foo' },
- { class: 'bar', label: 'Bar' }
- ]
- }
- } )
- .then( editor => {
- model = editor.model;
- expect( getModelData( model ) ).to.equal( '<codeBlock language="foo">[]bar</codeBlock>' );
- return editor.destroy();
- } );
- } );
- } );
- } );
- it( 'adds a codeBlock command', () => {
- expect( editor.commands.get( 'codeBlock' ) ).to.be.instanceOf( CodeBlockCommand );
- } );
- it( 'allows for codeBlock in the $root', () => {
- expect( model.schema.checkChild( [ '$root' ], 'codeBlock' ) ).to.be.true;
- } );
- it( 'allows only for $text in codeBlock', () => {
- expect( model.schema.checkChild( [ '$root', 'codeBlock' ], '$text' ) ).to.equal( true );
- expect( model.schema.checkChild( [ '$root', 'codeBlock' ], '$block' ) ).to.equal( false );
- expect( model.schema.checkChild( [ '$root', 'codeBlock' ], 'codeBlock' ) ).to.equal( false );
- } );
- it( 'disallows all attributes (except "language") for codeBlock', () => {
- setModelData( model, '<codeBlock language="css">f[o]o</codeBlock>' );
- editor.execute( 'alignment', { value: 'right' } );
- editor.execute( 'bold' );
- expect( getModelData( model ) ).to.equal( '<codeBlock language="css">f[o]o</codeBlock>' );
- } );
- it( 'should force shiftEnter command when pressing enter inside a codeBlock', () => {
- const enterCommand = editor.commands.get( 'enter' );
- const shiftEnterCommand = editor.commands.get( 'shiftEnter' );
- sinon.spy( enterCommand, 'execute' );
- sinon.spy( shiftEnterCommand, 'execute' );
- setModelData( model, '<codeBlock>foo[]bar</codeBlock>' );
- editor.editing.view.document.fire( 'enter', {
- preventDefault: () => {}
- } );
- expect( getModelData( model ) ).to.equal( '<codeBlock>foo<softBreak></softBreak>[]bar</codeBlock>' );
- sinon.assert.calledOnce( shiftEnterCommand.execute );
- sinon.assert.notCalled( enterCommand.execute );
- } );
- it( 'should execute enter command when pressing enter out of codeBlock', () => {
- const enterCommand = editor.commands.get( 'enter' );
- const shiftEnterCommand = editor.commands.get( 'shiftEnter' );
- sinon.spy( enterCommand, 'execute' );
- sinon.spy( shiftEnterCommand, 'execute' );
- setModelData( model, '<paragraph>foo[]bar</paragraph>' );
- editor.editing.view.document.fire( 'enter', {
- preventDefault: () => {}
- } );
- expect( getModelData( model ) ).to.equal( '<paragraph>foo</paragraph><paragraph>[]bar</paragraph>' );
- sinon.assert.calledOnce( enterCommand.execute );
- sinon.assert.notCalled( shiftEnterCommand.execute );
- } );
- describe( 'editing pipeline m -> v', () => {
- it( 'should convert empty codeBlock to empty pre tag', () => {
- setModelData( model, '<codeBlock language="plaintext"></codeBlock>' );
- expect( getViewData( view ) ).to.equal( '<pre data-language="Plain text"><code class="plaintext">[]</code></pre>' );
- } );
- it( 'should convert non-empty codeBlock to pre tag', () => {
- setModelData( model, '<codeBlock language="plaintext">Foo</codeBlock>' );
- expect( getViewData( view ) ).to.equal( '<pre data-language="Plain text"><code class="plaintext">{}Foo</code></pre>' );
- } );
- it( 'should convert codeBlock with softBreaks to pre tag #1', () => {
- setModelData( model,
- '<codeBlock language="plaintext">' +
- 'Foo<softBreak></softBreak>' +
- 'Bar<softBreak></softBreak>' +
- 'Biz' +
- '</codeBlock>'
- );
- expect( getViewData( view ) ).to.equal(
- '<pre data-language="Plain text">' +
- '<code class="plaintext">{}Foo<br></br>Bar<br></br>Biz</code>' +
- '</pre>' );
- } );
- it( 'should convert codeBlock with softBreaks to pre tag #2', () => {
- setModelData( model,
- '<codeBlock language="plaintext">' +
- '<softBreak></softBreak>' +
- '<softBreak></softBreak>' +
- 'Foo' +
- '<softBreak></softBreak>' +
- '<softBreak></softBreak>' +
- '</codeBlock>'
- );
- expect( getViewData( view ) ).to.equal(
- '<pre data-language="Plain text">' +
- '<code class="plaintext">[]<br></br><br></br>Foo<br></br><br></br></code>' +
- '</pre>' );
- } );
- it( 'should use localized "Plain text" label', () => {
- const element = document.createElement( 'div' );
- document.body.appendChild( element );
- return ClassicTestEditor
- .create( element, {
- language: 'pl',
- plugins: [ CodeBlockEditing, AlignmentEditing, BoldEditing, Enter, Paragraph ]
- } )
- .then( newEditor => {
- const editor = newEditor;
- const model = editor.model;
- const view = editor.editing.view;
- setModelData( model,
- '<codeBlock language="plaintext">foo</codeBlock>'
- );
- expect( getViewData( view ) ).to.equal(
- '<pre data-language="Zwykły tekst">' +
- '<code class="plaintext">{}foo</code>' +
- '</pre>' );
- element.remove();
- return editor.destroy();
- } );
- } );
- } );
- describe( 'data pipeline m -> v conversion ', () => {
- it( 'should convert empty codeBlock to empty pre tag', () => {
- setModelData( model, '<codeBlock language="plaintext"></codeBlock>' );
- expect( editor.getData( { trim: 'none' } ) ).to.equal( '<pre><code class="plaintext"> </code></pre>' );
- } );
- it( 'should convert non-empty codeBlock to pre tag', () => {
- setModelData( model, '<codeBlock language="plaintext">Foo</codeBlock>' );
- expect( editor.getData() ).to.equal( '<pre><code class="plaintext">Foo</code></pre>' );
- } );
- it( 'should convert codeBlock with softBreaks to pre tag #1', () => {
- setModelData( model,
- '<codeBlock language="plaintext">' +
- 'Foo<softBreak></softBreak>' +
- 'Bar<softBreak></softBreak>' +
- 'Biz' +
- '</codeBlock>' +
- '<paragraph>A<softBreak></softBreak>B</paragraph>'
- );
- expect( editor.getData() ).to.equal( '<pre><code class="plaintext">Foo\nBar\nBiz</code></pre><p>A<br>B</p>' );
- } );
- it( 'should convert codeBlock with softBreaks to pre tag #2', () => {
- setModelData( model,
- '<codeBlock language="plaintext">' +
- '<softBreak></softBreak>' +
- '<softBreak></softBreak>' +
- 'Foo' +
- '<softBreak></softBreak>' +
- '<softBreak></softBreak>' +
- '</codeBlock>'
- );
- expect( editor.getData() ).to.equal( '<pre><code class="plaintext">\n\nFoo\n\n</code></pre>' );
- } );
- it( 'should convert codeBlock with html content', () => {
- setModelData( model, '<codeBlock language="plaintext">[]</codeBlock>' );
- model.change( writer => writer.insertText( '<div><p>Foo</p></div>', model.document.selection.getFirstPosition() ) );
- expect( editor.getData() ).to.equal(
- '<pre>' +
- '<code class="plaintext"><div><p>Foo</p></div></code>' +
- '</pre>' );
- } );
- it( 'should be overridable', () => {
- editor.data.downcastDispatcher.on( 'insert:codeBlock', ( evt, data, api ) => {
- const targetViewPosition = api.mapper.toViewPosition( model.createPositionBefore( data.item ) );
- const code = api.writer.createContainerElement( 'code' );
- api.consumable.consume( data.item, 'insert' );
- api.writer.insert( targetViewPosition, code );
- api.mapper.bindElements( data.item, code );
- }, { priority: 'high' } );
- editor.data.downcastDispatcher.on( 'insert:softBreak', ( evt, data, api ) => {
- const position = api.mapper.toViewPosition( model.createPositionBefore( data.item ) );
- api.consumable.consume( data.item, 'insert' );
- api.writer.insert( position, api.writer.createText( '\n' ) );
- }, { priority: 'highest' } );
- setModelData( model, '<codeBlock language="plaintext">Foo<softBreak></softBreak>Bar</codeBlock>' );
- expect( editor.getData() ).to.equal( '<code>Foo\nBar</code>' );
- } );
- } );
- describe( 'data pipeline v -> m conversion ', () => {
- it( 'should not convert empty pre tag to code block', () => {
- editor.setData( '<pre></pre>' );
- expect( getModelData( model ) ).to.equal( '<paragraph>[]</paragraph>' );
- } );
- it( 'should not convert pre with no code child to code block', () => {
- editor.setData( '<pre><samp></samp></pre>' );
- expect( getModelData( model ) ).to.equal( '<paragraph>[]</paragraph>' );
- } );
- it( 'should convert pre > code to code block', () => {
- editor.setData( '<pre><code></code></pre>' );
- expect( getModelData( model ) ).to.equal( '<codeBlock language="plaintext">[]</codeBlock>' );
- } );
- it( 'should convert pre > code with multi-line text to code block #1', () => {
- editor.setData( '<pre><code>foo\nbar</code></pre>' );
- expect( getModelData( model ) ).to.equal(
- '<codeBlock language="plaintext">[]' +
- 'foo' +
- '<softBreak></softBreak>' +
- 'bar' +
- '</codeBlock>'
- );
- } );
- it( 'should convert pre > code with multi-line text to code block #2', () => {
- editor.setData( '<pre><code>\n\nfoo\n\n</code></pre>' );
- expect( getModelData( model ) ).to.equal(
- '<codeBlock language="plaintext">[]' +
- '<softBreak></softBreak>' +
- '<softBreak></softBreak>' +
- 'foo' +
- '<softBreak></softBreak>' +
- '<softBreak></softBreak>' +
- '</codeBlock>'
- );
- } );
- it( 'should convert pre > code with HTML inside', () => {
- editor.setData( '<pre><code><p>Foo</p>\n<p>Bar</p></code></pre>' );
- expect( getModelData( model ) ).to.equal(
- '<codeBlock language="plaintext">[]' +
- '<p>Foo</p>' +
- '<softBreak></softBreak>' +
- '<p>Bar</p>' +
- '</codeBlock>'
- );
- } );
- it( 'should convert pre > code tag with HTML and nested pre > code tag', () => {
- editor.setData( '<pre><code><p>Foo</p><pre>Bar</pre><p>Biz</p></code></pre>' );
- expect( getModelData( model ) ).to.equal( '<codeBlock language="plaintext">[]<p>Foo</p><pre>Bar</pre><p>Biz</p></codeBlock>' );
- } );
- it( 'should convert pre > code tag with escaped html content', () => {
- editor.setData( '<pre><code><div><p>Foo</p></div></code></pre>' );
- expect( getModelData( model ) ).to.equal( '<codeBlock language="plaintext">[]<div><p>Foo</p></div></codeBlock>' );
- } );
- it( 'should be overridable', () => {
- editor.data.upcastDispatcher.on( 'element:pre', ( evt, data, api ) => {
- const modelItem = api.writer.createElement( 'codeBlock' );
- api.writer.appendText( 'Hello World!', modelItem );
- api.writer.insert( modelItem, data.modelCursor );
- api.consumable.consume( data.viewItem, { name: true } );
- data.modelCursor = api.writer.createPositionAfter( modelItem );
- data.modelRange = api.writer.createRangeOn( modelItem );
- }, { priority: 'high' } );
- editor.setData( '<pre><code>Foo Bar</code></pre>' );
- expect( getModelData( model ) ).to.equal( '<codeBlock>[]Hello World!</codeBlock>' );
- } );
- it( 'should split parents to correctly upcast the code block', () => {
- editor.setData( '<p>foo<pre><code>code</code></pre>bar</p>' );
- expect( getModelData( model ) ).to.equal(
- '<paragraph>[]foo</paragraph><codeBlock language="plaintext">codecode</codeBlock><paragraph>bar</paragraph>' );
- } );
- it( 'should upcast two code blocks in a row (#1)', () => {
- editor.setData( '<pre><code>foo</code></pre><pre><code>bar</code></pre>' );
- expect( getModelData( model ) ).to.equal(
- '<codeBlock language="plaintext">[]foo</codeBlock><codeBlock language="plaintext">bar</codeBlock>' );
- } );
- it( 'should upcast two code blocks in a row (#2)', () => {
- editor.setData( `<pre><code>foo</code></pre>
- <pre><code>bar</code></pre>` );
- expect( getModelData( model ) ).to.equal(
- '<codeBlock language="plaintext">[]foo</codeBlock><codeBlock language="plaintext">bar</codeBlock>' );
- } );
- } );
- describe( 'clipboard integration', () => {
- it( 'should not intercept input when selection anchored outside any code block', () => {
- setModelData( model, '<paragraph>f[]oo</paragraph>' );
- const dataTransferMock = {
- getData: sinon.stub().withArgs( 'text/plain' ).returns( 'bar' )
- };
- editor.editing.view.document.fire( 'clipboardInput', {
- dataTransfer: dataTransferMock,
- stop: sinon.spy()
- } );
- expect( getModelData( model ) ).to.equal( '<paragraph>f[]oo</paragraph>' );
- sinon.assert.notCalled( dataTransferMock.getData );
- } );
- it( 'should intercept input when selection anchored in the code block', () => {
- setModelData( model, '<codeBlock language="css">f[o]o</codeBlock>' );
- const dataTransferMock = {
- getData: sinon.stub().withArgs( 'text/plain' ).returns( 'bar\nbaz\n' )
- };
- editor.editing.view.document.fire( 'clipboardInput', {
- dataTransfer: dataTransferMock,
- stop: sinon.spy()
- } );
- expect( getModelData( model ) ).to.equal(
- '<codeBlock language="css">' +
- 'fbar' +
- '<softBreak></softBreak>' +
- 'baz' +
- '<softBreak></softBreak>' +
- '[]o' +
- '</codeBlock>' );
- sinon.assert.calledOnce( dataTransferMock.getData );
- } );
- } );
- } );
|