formatscommand.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. import StandardEditor from '/ckeditor5/editor/standardeditor.js';
  7. import FormatsCommand from '/ckeditor5/formats/formatscommand.js';
  8. import Range from '/ckeditor5/engine/model/range.js';
  9. import { setData, getData } from '/tests/engine/_utils/model.js';
  10. const formats = [
  11. { id: 'paragraph', viewElement: 'p' },
  12. { id: 'heading1', viewElement: 'h2' },
  13. { id: 'heading2', viewElement: 'h3' },
  14. { id: 'heading3', viewElement: 'h4' }
  15. ];
  16. describe( 'FormatsCommand', () => {
  17. let editor, document, command, root;
  18. beforeEach( () => {
  19. editor = new StandardEditor( null );
  20. document = editor.document;
  21. command = new FormatsCommand( editor, formats );
  22. const schema = document.schema;
  23. for ( let format of formats ) {
  24. schema.registerItem( format.id, '$block' );
  25. }
  26. root = document.createRoot();
  27. } );
  28. afterEach( () => {
  29. command.destroy();
  30. } );
  31. describe( 'value', () => {
  32. for ( let format of formats ) {
  33. test( format );
  34. }
  35. function test( format ) {
  36. it( `equals ${ format.id } when collapsed selection is placed inside ${ format.id } element`, () => {
  37. setData( document, `<${ format.id }>foobar</${ format.id }>` );
  38. const element = root.getChild( 0 );
  39. document.selection.addRange( Range.createFromParentsAndOffsets( element, 3, element, 3 ) );
  40. expect( command.value ).to.equal( format.id );
  41. } );
  42. }
  43. } );
  44. describe( '_doExecute', () => {
  45. let convertTo = formats[ formats.length - 1 ];
  46. for ( let format of formats ) {
  47. test( format, convertTo );
  48. convertTo = format;
  49. }
  50. it( 'uses paragraph as default value', () => {
  51. setData( document, '<heading1>foo<selection />bar</heading1>' );
  52. command._doExecute();
  53. expect( getData( document ) ).to.equal( '<paragraph>foo<selection />bar</paragraph>' );
  54. } );
  55. it( 'converts to default format when executed with already applied format', () => {
  56. setData( document, '<heading1>foo<selection />bar</heading1>' );
  57. command._doExecute( 'heading1' );
  58. expect( getData( document ) ).to.equal( '<paragraph>foo<selection />bar</paragraph>' );
  59. } );
  60. function test( from, to ) {
  61. it( `converts ${ from.id } to ${ to.id } on collapsed selection`, () => {
  62. setData( document, `<${ from.id }>foo<selection />bar</${ from.id }>` );
  63. command._doExecute( to.id );
  64. expect( getData( document ) ).to.equal( `<${ to.id }>foo<selection />bar</${ to.id }>` );
  65. } );
  66. }
  67. } );
  68. } );