| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- /**
- * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
- import HeadingEditing from '../src/headingediting';
- import HeadingCommand from '../src/headingcommand';
- import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
- import ParagraphCommand from '@ckeditor/ckeditor5-paragraph/src/paragraphcommand';
- import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
- import { getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
- describe( 'HeadingEditing', () => {
- let editor, model;
- beforeEach( () => {
- return VirtualTestEditor
- .create( {
- plugins: [ HeadingEditing ]
- } )
- .then( newEditor => {
- editor = newEditor;
- model = editor.model;
- } );
- } );
- it( 'should be loaded', () => {
- expect( editor.plugins.get( HeadingEditing ) ).to.be.instanceOf( HeadingEditing );
- } );
- it( 'should load paragraph feature', () => {
- expect( editor.plugins.get( Paragraph ) ).to.be.instanceOf( Paragraph );
- } );
- it( 'should set proper schema rules', () => {
- expect( model.schema.isRegistered( 'heading1' ) ).to.be.true;
- expect( model.schema.isRegistered( 'heading2' ) ).to.be.true;
- expect( model.schema.isRegistered( 'heading3' ) ).to.be.true;
- expect( model.schema.checkChild( [ '$root' ], 'heading1' ) ).to.be.true;
- expect( model.schema.checkChild( [ 'heading1' ], '$text' ) ).to.be.true;
- expect( model.schema.checkChild( [ '$root' ], 'heading2' ) ).to.be.true;
- expect( model.schema.checkChild( [ 'heading2' ], '$text' ) ).to.be.true;
- expect( model.schema.checkChild( [ '$root' ], 'heading3' ) ).to.be.true;
- expect( model.schema.checkChild( [ 'heading3' ], '$text' ) ).to.be.true;
- } );
- it( 'should register #commands', () => {
- expect( editor.commands.get( 'paragraph' ) ).to.be.instanceOf( ParagraphCommand );
- expect( editor.commands.get( 'heading1' ) ).to.be.instanceOf( HeadingCommand );
- expect( editor.commands.get( 'heading2' ) ).to.be.instanceOf( HeadingCommand );
- expect( editor.commands.get( 'heading3' ) ).to.be.instanceOf( HeadingCommand );
- } );
- it( 'should convert heading1', () => {
- editor.setData( '<h2>foobar</h2>' );
- expect( getData( model, { withoutSelection: true } ) ).to.equal( '<heading1>foobar</heading1>' );
- expect( editor.getData() ).to.equal( '<h2>foobar</h2>' );
- } );
- it( 'should convert heading2', () => {
- editor.setData( '<h3>foobar</h3>' );
- expect( getData( model, { withoutSelection: true } ) ).to.equal( '<heading2>foobar</heading2>' );
- expect( editor.getData() ).to.equal( '<h3>foobar</h3>' );
- } );
- it( 'should convert heading3', () => {
- editor.setData( '<h4>foobar</h4>' );
- expect( getData( model, { withoutSelection: true } ) ).to.equal( '<heading3>foobar</heading3>' );
- expect( editor.getData() ).to.equal( '<h4>foobar</h4>' );
- } );
- describe( 'user defined', () => {
- beforeEach( () => {
- return VirtualTestEditor
- .create( {
- plugins: [ HeadingEditing ],
- heading: {
- options: [
- { model: 'paragraph', title: 'paragraph' },
- {
- model: 'heading1',
- view: 'h1',
- upcastAlso: [
- { name: 'p', attribute: { 'data-heading': 'h1' } }
- ],
- title: 'User H1',
- priority: 'high'
- }
- ]
- }
- } )
- .then( newEditor => {
- editor = newEditor;
- model = editor.model;
- } );
- } );
- it( 'should convert from defined h element', () => {
- editor.setData( '<h1>foobar</h1>' );
- expect( getData( model, { withoutSelection: true } ) ).to.equal( '<heading1>foobar</heading1>' );
- expect( editor.getData() ).to.equal( '<h1>foobar</h1>' );
- } );
- it( 'should convert from defined paragraph with attributes', () => {
- editor.setData( '<p data-heading="h1">foobar</p><p>Normal paragraph</p>' );
- expect( getData( model, { withoutSelection: true } ) )
- .to.equal( '<heading1>foobar</heading1><paragraph>Normal paragraph</paragraph>' );
- expect( editor.getData() ).to.equal( '<h1>foobar</h1><p>Normal paragraph</p>' );
- } );
- } );
- it( 'should not blow up if there\'s no enter command in the editor', () => {
- return VirtualTestEditor
- .create( {
- plugins: [ HeadingEditing ]
- } );
- } );
- describe( 'config', () => {
- describe( 'options', () => {
- describe( 'default value', () => {
- it( 'should be set', () => {
- expect( editor.config.get( 'heading.options' ) ).to.deep.equal( [
- { model: 'paragraph', title: 'Paragraph', class: 'ck-heading_paragraph' },
- { model: 'heading1', view: 'h2', title: 'Heading 1', class: 'ck-heading_heading1' },
- { model: 'heading2', view: 'h3', title: 'Heading 2', class: 'ck-heading_heading2' },
- { model: 'heading3', view: 'h4', title: 'Heading 3', class: 'ck-heading_heading3' }
- ] );
- } );
- } );
- it( 'should customize options', () => {
- const options = [
- { model: 'paragraph', title: 'Paragraph' },
- { model: 'h4', view: { name: 'h4' }, title: 'H4' }
- ];
- return VirtualTestEditor
- .create( {
- plugins: [ HeadingEditing ],
- heading: {
- options
- }
- } )
- .then( editor => {
- model = editor.model;
- expect( editor.commands.get( 'h4' ) ).to.be.instanceOf( HeadingCommand );
- expect( editor.commands.get( 'paragraph' ) ).to.be.instanceOf( ParagraphCommand );
- expect( model.schema.isRegistered( 'paragraph' ) ).to.be.true;
- expect( model.schema.isRegistered( 'h4' ) ).to.be.true;
- expect( model.schema.isRegistered( 'heading1' ) ).to.be.false;
- expect( model.schema.isRegistered( 'heading2' ) ).to.be.false;
- expect( model.schema.isRegistered( 'heading3' ) ).to.be.false;
- } );
- } );
- } );
- } );
- } );
|