horizontalrulecommand.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /* globals document */
  6. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  7. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  8. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  9. import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  10. import HorizontalRuleEditing from '../src/horizontalruleediting';
  11. describe( 'HorizontalRuleCommand', () => {
  12. let editor, model, editorElement, command;
  13. testUtils.createSinonSandbox();
  14. beforeEach( () => {
  15. editorElement = document.createElement( 'div' );
  16. document.body.appendChild( editorElement );
  17. return ClassicTestEditor
  18. .create( editorElement, {
  19. plugins: [ Paragraph, HorizontalRuleEditing ]
  20. } )
  21. .then( newEditor => {
  22. editor = newEditor;
  23. model = editor.model;
  24. command = editor.commands.get( 'horizontalRule' );
  25. } );
  26. } );
  27. afterEach( () => {
  28. return editor.destroy()
  29. .then( () => {
  30. editorElement.remove();
  31. } );
  32. } );
  33. describe( 'isEnabled', () => {
  34. it( 'should be true when the selection directly in the root', () => {
  35. model.enqueueChange( 'transparent', () => {
  36. setModelData( model, '[]' );
  37. command.refresh();
  38. expect( command.isEnabled ).to.be.true;
  39. } );
  40. } );
  41. it( 'should be true when the selection is in empty block', () => {
  42. setModelData( model, '<paragraph>[]</paragraph>' );
  43. expect( command.isEnabled ).to.be.true;
  44. } );
  45. it( 'should be true when the selection directly in a paragraph', () => {
  46. setModelData( model, '<paragraph>foo[]</paragraph>' );
  47. expect( command.isEnabled ).to.be.true;
  48. } );
  49. it( 'should be true when the selection directly in a block', () => {
  50. model.schema.register( 'block', { inheritAllFrom: '$block' } );
  51. model.schema.extend( '$text', { allowIn: 'block' } );
  52. editor.conversion.for( 'downcast' ).elementToElement( { model: 'block', view: 'block' } );
  53. setModelData( model, '<block>foo[]</block>' );
  54. expect( command.isEnabled ).to.be.true;
  55. } );
  56. it( 'should be false when the selection is on other horizontal rule element', () => {
  57. setModelData( model, '[<horizontalRule></horizontalRule>]' );
  58. expect( command.isEnabled ).to.be.false;
  59. } );
  60. it( 'should be false when the selection is on other object', () => {
  61. model.schema.register( 'object', { isObject: true, allowIn: '$root' } );
  62. editor.conversion.for( 'downcast' ).elementToElement( { model: 'object', view: 'object' } );
  63. setModelData( model, '[<object></object>]' );
  64. expect( command.isEnabled ).to.be.false;
  65. } );
  66. it( 'should be true when the selection is inside block element inside isLimit element which allows horizontal rule', () => {
  67. model.schema.register( 'table', { allowWhere: '$block', isLimit: true, isObject: true, isBlock: true } );
  68. model.schema.register( 'tableRow', { allowIn: 'table', isLimit: true } );
  69. model.schema.register( 'tableCell', { allowIn: 'tableRow', isLimit: true } );
  70. model.schema.extend( '$block', { allowIn: 'tableCell' } );
  71. editor.conversion.for( 'downcast' ).elementToElement( { model: 'table', view: 'table' } );
  72. editor.conversion.for( 'downcast' ).elementToElement( { model: 'tableRow', view: 'tableRow' } );
  73. editor.conversion.for( 'downcast' ).elementToElement( { model: 'tableCell', view: 'tableCell' } );
  74. setModelData( model, '<table><tableRow><tableCell><paragraph>foo[]</paragraph></tableCell></tableRow></table>' );
  75. } );
  76. it( 'should be false when schema disallows horizontal rule', () => {
  77. model.schema.register( 'block', { inheritAllFrom: '$block' } );
  78. model.schema.extend( 'paragraph', { allowIn: 'block' } );
  79. // Block horizontal rule in block.
  80. model.schema.addChildCheck( ( context, childDefinition ) => {
  81. if ( childDefinition.name === 'horizontalRule' && context.last.name === 'block' ) {
  82. return false;
  83. }
  84. } );
  85. editor.conversion.for( 'downcast' ).elementToElement( { model: 'block', view: 'block' } );
  86. setModelData( model, '<block><paragraph>[]</paragraph></block>' );
  87. expect( command.isEnabled ).to.be.false;
  88. } );
  89. } );
  90. describe( 'execute()', () => {
  91. it( 'should create a single batch', () => {
  92. setModelData( model, '<paragraph>foo[]</paragraph>' );
  93. const spy = sinon.spy();
  94. model.document.on( 'change', spy );
  95. command.execute();
  96. sinon.assert.calledOnce( spy );
  97. } );
  98. it( 'should insert a horizontal rule in an empty root and select it', () => {
  99. setModelData( model, '[]' );
  100. command.execute();
  101. expect( getModelData( model ) ).to.equal( '[<horizontalRule></horizontalRule>]' );
  102. } );
  103. it( 'should split an element where selection is placed and insert a horizontal rule (non-collapsed selection)', () => {
  104. setModelData( model, '<paragraph>f[o]o</paragraph>' );
  105. command.execute();
  106. expect( getModelData( model ) ).to.equal(
  107. '<paragraph>f</paragraph>[<horizontalRule></horizontalRule>]<paragraph>o</paragraph>'
  108. );
  109. } );
  110. it( 'should split an element where selection is placed and insert a horizontal rule (collapsed selection)', () => {
  111. setModelData( model, '<paragraph>fo[]o</paragraph>' );
  112. command.execute();
  113. expect( getModelData( model ) ).to.equal(
  114. '<paragraph>fo</paragraph>[<horizontalRule></horizontalRule>]<paragraph>o</paragraph>'
  115. );
  116. } );
  117. } );
  118. } );