/** * @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 ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor'; import Title from '../src/title'; import Heading from '../src/heading'; import Enter from '@ckeditor/ckeditor5-enter/src/enter'; import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph'; import BlockQuote from '@ckeditor/ckeditor5-block-quote/src/blockquote'; import Clipboard from '@ckeditor/ckeditor5-clipboard/src/clipboard'; import Image from '@ckeditor/ckeditor5-image/src/image'; import ImageUpload from '@ckeditor/ckeditor5-image/src/imageupload'; import Undo from '@ckeditor/ckeditor5-undo/src/undo'; import { setData, getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model'; import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard'; describe( 'Title', () => { let element, editor, model; beforeEach( () => { element = document.createElement( 'div' ); document.body.appendChild( element ); return ClassicTestEditor.create( element, { plugins: [ Title, Heading, BlockQuote, Clipboard, Image, ImageUpload, Enter, Undo ] } ).then( _editor => { editor = _editor; model = editor.model; } ); } ); afterEach( () => { return editor.destroy().then( () => element.remove() ); } ); it( 'should requires Paragraph plugin', () => { expect( Title.requires ).to.have.members( [ Paragraph ] ); } ); it( 'should have plugin name property', () => { expect( Title.pluginName ).to.equal( 'Title' ); } ); it( 'should set proper schema rules', () => { expect( model.schema.isRegistered( 'title' ) ).to.equal( true ); expect( model.schema.isRegistered( 'title-content' ) ).to.equal( true ); expect( model.schema.checkChild( 'title', '$text' ) ).to.equal( false ); expect( model.schema.checkChild( 'title', 'paragraph' ) ).to.equal( false ); expect( model.schema.checkChild( 'title', 'title-content' ) ).to.equal( true ); expect( model.schema.checkChild( '$root', 'title-content' ) ).to.equal( false ); expect( model.schema.checkChild( 'blockQuote', 'title-content' ) ).to.equal( false ); expect( model.schema.checkChild( 'title-content', '$text' ) ).to.equal( true ); expect( model.schema.checkChild( 'title-content', 'paragraph' ) ).to.equal( false ); expect( model.schema.checkChild( 'title-content', 'blockQuote' ) ).to.equal( false ); expect( model.schema.checkChild( '$root', 'title' ) ).to.equal( true ); expect( model.schema.checkChild( 'blockQuote', 'title' ) ).to.equal( false ); expect( model.schema.checkChild( 'paragraph', 'title' ) ).to.equal( false ); model.schema.extend( '$text', { allowAttributes: [ 'bold', 'foo' ] } ); expect( model.schema.checkAttribute( [ 'title-content', '$text' ], 'bold' ) ).to.equal( false ); expect( model.schema.checkAttribute( [ 'paragraph', '$text' ], 'bold' ) ).to.equal( true ); expect( model.schema.checkAttribute( [ 'title-content', '$text' ], 'foo' ) ).to.equal( false ); expect( model.schema.checkAttribute( [ 'paragraph', '$text' ], 'foo' ) ).to.equal( true ); } ); it( 'should convert title to h1', () => { setData( model, '<title-content>Foo</title-content>' + 'Bar' ); expect( editor.getData() ).to.equal( '

Foo

Bar

' ); } ); describe( 'model post-fixing', () => { it( 'should set title and content elements', () => { setData( model, '<title-content>Foo</title-content>' + 'Bar' ); expect( getData( model ) ).to.equal( '<title-content>[]Foo</title-content>' + 'Bar' ); } ); it( 'should create a content element when only title has been set', () => { setData( model, '<title-content>Foo</title-content>' ); expect( getData( model ) ).to.equal( '<title-content>[]Foo</title-content>' + '' ); } ); it( 'should create a title and content elements when are missing', () => { setData( model, '' ); expect( getData( model ) ).to.equal( '<title-content>[]</title-content>' + '' ); } ); it( 'should change heading1 element to title when is set as a first root child', () => { setData( model, 'Foo' + 'Bar' ); expect( getData( model ) ).to.equal( '<title-content>[]Foo</title-content>' + 'Bar' ); } ); it( 'should change paragraph element to title when is set as a first root child', () => { setData( model, 'Foo' + 'Bar' ); expect( getData( model ) ).to.equal( '<title-content>[]Foo</title-content>' + 'Bar' ); } ); it( 'should have list of title-like elements', () => { expect( Array.from( Title.titleLikeElements ) ).to.have.members( [ 'paragraph', 'heading1', 'heading2', 'heading3', 'heading4', 'heading5', 'heading6' ] ); } ); it( 'should change paragraph element to title when is set as a first root child', () => { setData( model, 'Foo' + 'Bar' ); expect( getData( model ) ).to.equal( '<title-content>[]Foo</title-content>' + 'Bar' ); } ); it( 'should change title element to a paragraph when is not a first root child #1', () => { setData( model, '<title-content>Foo</title-content>' + '<title-content>Bar</title-content>' ); expect( getData( model ) ).to.equal( '<title-content>[]Foo</title-content>' + 'Bar' ); } ); it( 'should change title element to a paragraph when is not a first root child #2', () => { setData( model, '<title-content>Foo</title-content>' + 'Bar' + '<title-content>Biz</title-content>' ); expect( getData( model ) ).to.equal( '<title-content>[]Foo</title-content>' + 'Bar' + 'Biz' ); } ); it( 'should move element after a title element when is not allowed to be a title', () => { setData( model, '
Foo
' + '<title-content>Bar</title-content>' ); expect( getData( model ) ).to.equal( '<title-content>[]Bar</title-content>' + '
Foo
' ); } ); it( 'should create a missing title element before an element that cannot to be a title element', () => { setData( model, '
Foo
' ); expect( getData( model ) ).to.equal( '<title-content>[]</title-content>' + '
Foo
' ); } ); it( 'should clear element from attributes when changing to title element', () => { model.schema.extend( '$text', { allowAttributes: 'bold' } ); setData( model, 'F<$text bold="true">oo' + 'B<$text bold="true">ar' ); expect( getData( model ) ).to.equal( '<title-content>[]Foo</title-content>' + 'B<$text bold="true">ar' ); } ); } ); describe( 'prevent extra paragraphing', () => { it( 'should remove the extra paragraph when pasting to the editor with body placeholder', () => { setData( model, '<title-content>[]</title-content>' ); const dataTransferMock = { getData: type => { if ( type === 'text/html' ) { return '

Title

Body

'; } }, types: [], files: [] }; editor.editing.view.document.fire( 'paste', { dataTransfer: dataTransferMock, preventDefault() {} } ); expect( getData( model ) ).to.equal( '<title-content>Title</title-content>' + 'Body[]' ); } ); it( 'should not remove the extra paragraph when pasting to the editor with directly created body element', () => { setData( model, '<title-content>[]</title-content>' + '' ); const dataTransferMock = { getData: type => { if ( type === 'text/html' ) { return '

Title

Body

'; } }, types: [], files: [] }; editor.editing.view.document.fire( 'paste', { dataTransfer: dataTransferMock, preventDefault() {} } ); expect( getData( model ) ).to.equal( '<title-content>Title</title-content>' + 'Body[]' + '' ); } ); it( 'should remove the extra paragraph when pressing enter in the title', () => { setData( model, '<title-content>fo[]o</title-content>' ); editor.execute( 'enter' ); expect( getData( model ) ).to.equal( '<title-content>fo</title-content>' + '[]o' ); } ); it( 'should not remove the extra paragraph when pressing enter in the title when body is created directly', () => { setData( model, '<title-content>fo[]o</title-content>' + '' ); editor.execute( 'enter' ); expect( getData( model ) ).to.equal( '<title-content>fo</title-content>' + '[]o' + '' ); } ); } ); describe( 'setTitle()', () => { it( 'should set new content of a title element', () => { setData( model, '<title-content></title-content>' + 'Bar' ); editor.plugins.get( 'Title' ).setTitle( 'Biz' ); expect( getData( model ) ).to.equal( '<title-content>[]Biz</title-content>' + 'Bar' ); } ); it( 'should replace old content of a title element', () => { setData( model, '<title-content>Foo</title-content>' + 'Bar' ); editor.plugins.get( 'Title' ).setTitle( 'Biz' ); expect( getData( model ) ).to.equal( '<title-content>[]Biz</title-content>' + 'Bar' ); } ); it( 'should clear content of a title element', () => { setData( model, '<title-content>Foo</title-content>' + 'Bar' ); editor.plugins.get( 'Title' ).setTitle( '' ); expect( getData( model ) ).to.equal( '<title-content>[]</title-content>' + 'Bar' ); } ); it( 'should properly handle HTML element', () => { setData( model, '<title-content></title-content>' + 'Bar' ); editor.plugins.get( 'Title' ).setTitle( '

Foo

' ); expect( getData( model ) ).to.equal( '<title-content>[]Foo</title-content>' + 'Bar' ); } ); it( 'should properly handle multiple HTML elements', () => { setData( model, '<title-content>Foo</title-content>' + 'Bar' ); editor.plugins.get( 'Title' ).setTitle( '

Foo

bar

' ); expect( getData( model ) ).to.equal( '<title-content>[]Foobar</title-content>' + 'Bar' ); } ); it( 'should do nothing when setting empty content to the empty title', () => { setData( model, '<title-content></title-content>' + 'Bar' ); editor.plugins.get( 'Title' ).setTitle( '' ); expect( getData( model ) ).to.equal( '<title-content>[]</title-content>' + 'Bar' ); } ); } ); describe( 'getTitle()', () => { it( 'should return content of a title element', () => { setData( model, '<title-content>Foo</title-content>' + 'Bar' ); expect( editor.plugins.get( 'Title' ).getTitle() ).to.equal( 'Foo' ); } ); it( 'should return content of an empty title element', () => { setData( model, '<title-content></title-content>' + 'Bar' ); expect( editor.plugins.get( 'Title' ).getTitle() ).to.equal( '' ); } ); } ); describe( 'setBody()', () => { it( 'should set new content to body', () => { setData( model, '<title-content>Foo</title-content>' + 'Bar' ); editor.plugins.get( 'Title' ).setBody( 'Biz' ); expect( getData( model ) ).to.equal( '<title-content>[]Foo</title-content>' + 'Biz' ); } ); it( 'should set empty content to body', () => { setData( model, '<title-content>Foo</title-content>' + 'Bar' ); editor.plugins.get( 'Title' ).setBody( '' ); expect( getData( model ) ).to.equal( '<title-content>[]Foo</title-content>' + '' ); } ); it( 'should set html content to body', () => { setData( model, '<title-content>Foo</title-content>' + 'Bar' ); editor.plugins.get( 'Title' ).setBody( '
Bar

Biz

' ); expect( getData( model ) ).to.equal( '<title-content>[]Foo</title-content>' + '
Bar
' + 'Biz' ); } ); } ); describe( 'getBody()', () => { it( 'should return all data except the title element', () => { setData( model, '<title-content>Foo</title-content>' + 'Bar' + 'Biz' ); expect( editor.plugins.get( 'Title' ).getBody() ).to.equal( '

Bar

Biz

' ); } ); it( 'should return empty paragraph when body is empty', () => { setData( model, '<title-content>Foo</title-content>' ); expect( editor.plugins.get( 'Title' ).getBody() ).to.equal( '

 

' ); } ); } ); describe( 'placeholders', () => { let viewRoot; beforeEach( () => { viewRoot = editor.editing.view.document.getRoot(); } ); it( 'should attach placeholder placeholder to title and body', () => { setData( model, '<title-content>Foo</title-content>' + 'Bar' ); const title = viewRoot.getChild( 0 ); const body = viewRoot.getChild( 1 ); expect( title.getAttribute( 'data-placeholder' ) ).to.equal( 'Title' ); expect( body.getAttribute( 'data-placeholder' ) ).to.equal( 'Body' ); expect( title.hasClass( 'ck-placeholder' ) ).to.equal( false ); expect( body.hasClass( 'ck-placeholder' ) ).to.equal( false ); } ); it( 'should show placeholder in empty title and body', () => { setData( model, '<title-content></title-content>' + '' ); const title = viewRoot.getChild( 0 ); const body = viewRoot.getChild( 1 ); expect( title.getAttribute( 'data-placeholder' ) ).to.equal( 'Title' ); expect( body.getAttribute( 'data-placeholder' ) ).to.equal( 'Body' ); expect( title.hasClass( 'ck-placeholder' ) ).to.equal( true ); expect( body.hasClass( 'ck-placeholder' ) ).to.equal( true ); } ); it( 'should hide placeholder from body with more than one child elements', () => { setData( editor.model, '<title-content>Foo</title-content>' + '' + '' ); const body = viewRoot.getChild( 1 ); expect( body.getAttribute( 'data-placeholder' ) ).to.equal( 'Body' ); expect( body.hasClass( 'ck-placeholder' ) ).to.equal( false ); } ); it( 'should hide placeholder from body with element other than paragraph', () => { setData( editor.model, '<title-content>Foo</title-content>' + '' ); const body = viewRoot.getChild( 1 ); expect( body.hasAttribute( 'data-placeholder' ) ).to.equal( true ); expect( body.hasClass( 'ck-placeholder' ) ).to.equal( false ); } ); it( 'should hide placeholder when title element become not empty', () => { setData( model, '<title-content></title-content>' + '[]' ); expect( viewRoot.getChild( 0 ).hasClass( 'ck-placeholder' ) ).to.equal( true ); model.change( writer => { writer.appendText( 'Bar', null, model.document.getRoot().getChild( 0 ).getChild( 0 ) ); } ); expect( viewRoot.getChild( 0 ).hasClass( 'ck-placeholder' ) ).to.equal( false ); } ); it( 'should hide placeholder when body element become not empty', () => { setData( model, '<title-content>Foo</title-content>' + '' ); expect( viewRoot.getChild( 1 ).hasClass( 'ck-placeholder' ) ).to.equal( true ); model.change( writer => { writer.appendText( 'Bar', null, model.document.getRoot().getChild( 1 ) ); } ); expect( viewRoot.getChild( 1 ).hasClass( 'ck-placeholder' ) ).to.equal( false ); } ); it( 'should properly map the body placeholder in DOM when undoing', () => { const viewRoot = editor.editing.view.document.getRoot(); const domConverter = editor.editing.view.domConverter; let bodyDomElement; setData( editor.model, '<title-content>[Foo</title-content>' + 'Bar' + 'Baz]' ); editor.model.deleteContent( editor.model.document.selection ); bodyDomElement = domConverter.mapViewToDom( viewRoot.getChild( 1 ) ); expect( bodyDomElement.dataset.placeholder ).to.equal( 'Body' ); expect( bodyDomElement.classList.contains( 'ck-placeholder' ) ).to.be.true; editor.execute( 'undo' ); bodyDomElement = domConverter.mapViewToDom( viewRoot.getChild( 1 ) ); expect( bodyDomElement.dataset.placeholder ).to.equal( 'Body' ); expect( bodyDomElement.classList.contains( 'ck-placeholder' ) ).to.be.false; } ); it( 'should use placeholder defined through EditorConfig as a Body placeholder', () => { const element = document.createElement( 'div' ); document.body.appendChild( element ); return ClassicTestEditor.create( element, { plugins: [ Title ], placeholder: 'Custom editor placeholder' } ).then( editor => { const viewRoot = editor.editing.view.document.getRoot(); const title = viewRoot.getChild( 0 ); const body = viewRoot.getChild( 1 ); expect( title.getAttribute( 'data-placeholder' ) ).to.equal( 'Title' ); expect( title.hasClass( 'ck-placeholder' ) ).to.equal( true ); expect( body.getAttribute( 'data-placeholder' ) ).to.equal( 'Custom editor placeholder' ); expect( body.hasClass( 'ck-placeholder' ) ).to.equal( true ); return editor.destroy().then( () => element.remove() ); } ); } ); } ); describe( 'Tab press handling', () => { it( 'should handle tab key when the selection is at the beginning of the title', () => { setData( model, '<title-content>[]foo</title-content>' + 'bar' ); const eventData = getEventData( keyCodes.tab ); editor.keystrokes.press( eventData ); sinon.assert.calledOnce( eventData.preventDefault ); sinon.assert.calledOnce( eventData.stopPropagation ); expect( getData( model ) ).to.equal( '<title-content>foo</title-content>' + '[]bar' ); } ); it( 'should handle tab key when the selection is at the end of the title', () => { setData( model, '<title-content>foo[]</title-content>' + 'bar' ); const eventData = getEventData( keyCodes.tab ); editor.keystrokes.press( eventData ); sinon.assert.calledOnce( eventData.preventDefault ); sinon.assert.calledOnce( eventData.stopPropagation ); expect( getData( model ) ).to.equal( '<title-content>foo</title-content>' + '[]bar' ); } ); it( 'should not handle tab key when the selection is in the title and body', () => { setData( model, '<title-content>fo[o</title-content>' + 'b]ar' ); const eventData = getEventData( keyCodes.tab ); editor.keystrokes.press( eventData ); sinon.assert.notCalled( eventData.preventDefault ); sinon.assert.notCalled( eventData.stopPropagation ); expect( getData( model ) ).to.equal( '<title-content>fo[o</title-content>' + 'b]ar' ); } ); it( 'should not handle tab key when the selection is in the body', () => { setData( model, '<title-content>foo</title-content>' + '[]bar' ); const eventData = getEventData( keyCodes.tab ); editor.keystrokes.press( eventData ); sinon.assert.notCalled( eventData.preventDefault ); sinon.assert.notCalled( eventData.stopPropagation ); expect( getData( model ) ).to.equal( '<title-content>foo</title-content>' + '[]bar' ); } ); } ); describe( 'Shift + Tab press handling', () => { it( 'should handle shift + tab keys when the selection is at the beginning of the body', () => { setData( model, '<title-content>foo</title-content>' + '[]bar' ); const eventData = getEventData( keyCodes.tab, { shiftKey: true } ); editor.keystrokes.press( eventData ); sinon.assert.calledOnce( eventData.preventDefault ); sinon.assert.calledOnce( eventData.stopPropagation ); expect( getData( model ) ).to.equal( '<title-content>[]foo</title-content>' + 'bar' ); } ); it( 'should not handle shift + tab keys when the selection is not at the beginning of the body', () => { setData( model, '<title-content>foo</title-content>' + 'b[]ar' ); const eventData = getEventData( keyCodes.tab, { shiftKey: true } ); editor.keystrokes.press( eventData ); sinon.assert.notCalled( eventData.preventDefault ); sinon.assert.notCalled( eventData.stopPropagation ); expect( getData( model ) ).to.equal( '<title-content>foo</title-content>' + 'b[]ar' ); } ); it( 'should not handle shift + tab keys when the selection is not collapsed', () => { setData( model, '<title-content>foo</title-content>' + '[b]ar' ); const eventData = getEventData( keyCodes.tab, { shiftKey: true } ); editor.keystrokes.press( eventData ); sinon.assert.notCalled( eventData.preventDefault ); sinon.assert.notCalled( eventData.stopPropagation ); expect( getData( model ) ).to.equal( '<title-content>foo</title-content>' + '[b]ar' ); } ); it( 'should not handle shift + tab keys when the selection is in the title', () => { setData( model, '<title-content>[]foo</title-content>' + 'bar' ); const eventData = getEventData( keyCodes.tab, { shiftKey: true } ); editor.keystrokes.press( eventData ); sinon.assert.notCalled( eventData.preventDefault ); sinon.assert.notCalled( eventData.stopPropagation ); expect( getData( model ) ).to.equal( '<title-content>[]foo</title-content>' + 'bar' ); } ); } ); } ); function getEventData( keyCode, { shiftKey = false } = {} ) { return { keyCode, shiftKey, preventDefault: sinon.spy(), stopPropagation: sinon.spy() }; }