formatscommand.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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.value ).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. it( 'converts topmost blocks', () => {
  66. setData( document, '<heading1><b>foo<selection /></b>bar</heading1>' );
  67. command._doExecute( 'heading1' );
  68. expect( getData( document ) ).to.equal( '<paragraph><b>foo<selection /></b>bar</paragraph>' );
  69. } );
  70. function test( from, to ) {
  71. it( `converts ${ from.id } to ${ to.id } on collapsed selection`, () => {
  72. setData( document, `<${ from.id }>foo<selection />bar</${ from.id }>` );
  73. command._doExecute( to.id );
  74. expect( getData( document ) ).to.equal( `<${ to.id }>foo<selection />bar</${ to.id }>` );
  75. } );
  76. }
  77. } );
  78. describe( 'non-collapsed selection', () => {
  79. let convertTo = formats[ formats.length - 1 ];
  80. for ( let format of formats ) {
  81. test( format, convertTo );
  82. convertTo = format;
  83. }
  84. it( 'converts all elements where selection is applied', () => {
  85. setData( document, '<heading1>foo<selection></heading1><heading2>bar</heading2><heading2></selection>baz</heading2>' );
  86. command._doExecute( 'paragraph' );
  87. expect( getData( document ) ).to.equal(
  88. '<paragraph>foo<selection></paragraph><paragraph>bar</paragraph><paragraph></selection>baz</paragraph>'
  89. );
  90. } );
  91. it( 'resets to default value all elements with same format', () => {
  92. setData( document, '<heading1>foo<selection></heading1><heading1>bar</heading1><heading2>baz</heading2></selection>' );
  93. command._doExecute( 'heading1' );
  94. expect( getData( document ) ).to.equal(
  95. '<paragraph>foo<selection></paragraph><paragraph>bar</paragraph><heading2>baz</heading2></selection>'
  96. );
  97. } );
  98. function test( from, to ) {
  99. it( `converts ${ from.id } to ${ to.id } on non-collapsed selection`, () => {
  100. setData( document, `<${ from.id }>foo<selection>bar</${ from.id }><${ from.id }>baz</selection>qux</${ from.id }>` );
  101. command._doExecute( to.id );
  102. expect( getData( document ) ).to.equal( `<${ to.id }>foo<selection>bar</${ to.id }><${ to.id }>baz</selection>qux</${ to.id }>` );
  103. } );
  104. }
  105. } );
  106. } );
  107. } );