8
0

formatscommand.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 VirtualTestEditor from '/tests/ckeditor5/_utils/virtualtesteditor.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', default: true },
  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. return VirtualTestEditor.create()
  20. .then( newEditor => {
  21. editor = newEditor;
  22. document = editor.document;
  23. command = new FormatsCommand( editor, formats );
  24. const schema = document.schema;
  25. for ( let format of formats ) {
  26. schema.registerItem( format.id, '$block' );
  27. }
  28. root = document.getRoot();
  29. } );
  30. } );
  31. afterEach( () => {
  32. command.destroy();
  33. } );
  34. describe( 'value', () => {
  35. for ( let format of formats ) {
  36. test( format );
  37. }
  38. function test( format ) {
  39. it( `equals ${ format.id } when collapsed selection is placed inside ${ format.id } element`, () => {
  40. setData( document, `<${ format.id }>foobar</${ format.id }>` );
  41. const element = root.getChild( 0 );
  42. document.selection.addRange( Range.createFromParentsAndOffsets( element, 3, element, 3 ) );
  43. expect( command.format ).to.equal( format );
  44. } );
  45. }
  46. } );
  47. describe( '_doExecute', () => {
  48. let convertTo = formats[ formats.length - 1 ];
  49. for ( let format of formats ) {
  50. test( format, convertTo );
  51. convertTo = format;
  52. }
  53. it( 'uses paragraph as default value', () => {
  54. setData( document, '<heading1>foo<selection />bar</heading1>' );
  55. command._doExecute();
  56. expect( getData( document ) ).to.equal( '<paragraph>foo<selection />bar</paragraph>' );
  57. } );
  58. it( 'converts to default format when executed with already applied format', () => {
  59. setData( document, '<heading1>foo<selection />bar</heading1>' );
  60. command._doExecute( 'heading1' );
  61. expect( getData( document ) ).to.equal( '<paragraph>foo<selection />bar</paragraph>' );
  62. } );
  63. function test( from, to ) {
  64. it( `converts ${ from.id } to ${ to.id } on collapsed selection`, () => {
  65. setData( document, `<${ from.id }>foo<selection />bar</${ from.id }>` );
  66. command._doExecute( to.id );
  67. expect( getData( document ) ).to.equal( `<${ to.id }>foo<selection />bar</${ to.id }>` );
  68. } );
  69. }
  70. } );
  71. } );