formatscommand.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 ModelTestEditor from '/tests/ckeditor5/_utils/modeltesteditor.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 ModelTestEditor.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. schema.registerItem( 'b', '$inline' );
  29. root = document.getRoot();
  30. } );
  31. } );
  32. afterEach( () => {
  33. command.destroy();
  34. } );
  35. describe( 'value', () => {
  36. for ( let format of formats ) {
  37. test( format );
  38. }
  39. function test( format ) {
  40. it( `equals ${ format.id } when collapsed selection is placed inside ${ format.id } element`, () => {
  41. setData( document, `<${ format.id }>foobar</${ format.id }>` );
  42. const element = root.getChild( 0 );
  43. document.selection.addRange( Range.createFromParentsAndOffsets( element, 3, element, 3 ) );
  44. expect( command.format ).to.equal( format );
  45. } );
  46. }
  47. } );
  48. describe( '_doExecute', () => {
  49. describe( 'collapsed selection', () => {
  50. let convertTo = formats[ formats.length - 1 ];
  51. for ( let format of formats ) {
  52. test( format, convertTo );
  53. convertTo = format;
  54. }
  55. it( 'uses paragraph as default value', () => {
  56. setData( document, '<heading1>foo<selection />bar</heading1>' );
  57. command._doExecute();
  58. expect( getData( document ) ).to.equal( '<paragraph>foo<selection />bar</paragraph>' );
  59. } );
  60. it( 'converts to default format when executed with already applied format', () => {
  61. setData( document, '<heading1>foo<selection />bar</heading1>' );
  62. command._doExecute( 'heading1' );
  63. expect( getData( document ) ).to.equal( '<paragraph>foo<selection />bar</paragraph>' );
  64. } );
  65. function test( from, to ) {
  66. it( `converts ${ from.id } to ${ to.id } on collapsed selection`, () => {
  67. setData( document, `<${ from.id }>foo<selection />bar</${ from.id }>` );
  68. command._doExecute( to.id );
  69. expect( getData( document ) ).to.equal( `<${ to.id }>foo<selection />bar</${ to.id }>` );
  70. } );
  71. }
  72. } );
  73. describe( 'non-collapsed selection', () => {
  74. let convertTo = formats[ formats.length - 1 ];
  75. for ( let format of formats ) {
  76. test( format, convertTo );
  77. convertTo = format;
  78. }
  79. it( 'converts all elements where selection is applied', () => {
  80. setData( document, '<heading1>foo<selection></heading1><heading2>bar</heading2><heading2></selection>baz</heading2>' );
  81. command._doExecute( 'paragraph' );
  82. expect( getData( document ) ).to.equal(
  83. '<paragraph>foo<selection></paragraph><paragraph>bar</paragraph><paragraph></selection>baz</paragraph>'
  84. );
  85. } );
  86. it( 'resets to default value all elements with same format', () => {
  87. setData( document, '<heading1>foo<selection></heading1><heading1>bar</heading1><heading2></selection>baz</heading2>' );
  88. command._doExecute( 'heading1' );
  89. expect( getData( document ) ).to.equal(
  90. '<paragraph>foo<selection></paragraph><paragraph>bar</paragraph><heading2></selection>baz</heading2>'
  91. );
  92. } );
  93. function test( from, to ) {
  94. it( `converts ${ from.id } to ${ to.id } on non-collapsed selection`, () => {
  95. setData( document, `<${ from.id }>foo<selection>bar</${ from.id }><${ from.id }>baz</selection>qux</${ from.id }>` );
  96. command._doExecute( to.id );
  97. expect( getData( document ) ).to.equal( `<${ to.id }>foo<selection>bar</${ to.id }><${ to.id }>baz</selection>qux</${ to.id }>` );
  98. } );
  99. }
  100. } );
  101. } );
  102. } );