8
0

horizontallinecommand.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /**
  2. * @license Copyright (c) 2003-2020, 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 HorizontalLineEditing from '../src/horizontallineediting';
  11. describe( 'HorizontalLineCommand', () => {
  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, HorizontalLineEditing ]
  20. } )
  21. .then( newEditor => {
  22. editor = newEditor;
  23. model = editor.model;
  24. command = editor.commands.get( 'horizontalLine' );
  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 line element', () => {
  57. setModelData( model, '[<horizontalLine></horizontalLine>]' );
  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 line', () => {
  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, isSelectable: 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 line', () => {
  77. model.schema.register( 'block', { inheritAllFrom: '$block' } );
  78. model.schema.extend( 'paragraph', { allowIn: 'block' } );
  79. // Block horizontal line in block.
  80. model.schema.addChildCheck( ( context, childDefinition ) => {
  81. if ( childDefinition.name === 'horizontalLine' && 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. beforeEach( () => {
  92. model.schema.register( 'heading1', { inheritAllFrom: '$block' } );
  93. editor.conversion.elementToElement( { model: 'heading1', view: 'h1' } );
  94. model.schema.register( 'media', { allowWhere: '$block' } );
  95. editor.conversion.elementToElement( { model: 'media', view: 'div' } );
  96. } );
  97. it( 'should create a single batch', () => {
  98. setModelData( model, '<paragraph>foo[]</paragraph>' );
  99. const spy = sinon.spy();
  100. model.document.on( 'change', spy );
  101. command.execute();
  102. sinon.assert.calledOnce( spy );
  103. } );
  104. it( 'should insert a horizontal line in an empty root and select it (a paragraph cannot be inserted)', () => {
  105. // Block a paragraph in $root.
  106. model.schema.addChildCheck( ( context, childDefinition ) => {
  107. if ( childDefinition.name === 'paragraph' && context.last.name === '$root' ) {
  108. return false;
  109. }
  110. } );
  111. setModelData( model, '[]' );
  112. command.execute();
  113. expect( getModelData( model ) ).to.equal( '[<horizontalLine></horizontalLine>]' );
  114. } );
  115. it( 'should split an element where selection is placed and insert a horizontal line (non-collapsed selection)', () => {
  116. setModelData( model, '<paragraph>f[o]o</paragraph>' );
  117. command.execute();
  118. expect( getModelData( model ) ).to.equal(
  119. '<paragraph>f</paragraph><horizontalLine></horizontalLine><paragraph>[]o</paragraph>'
  120. );
  121. } );
  122. it( 'should split an element where selection is placed and insert a horizontal line (collapsed selection)', () => {
  123. setModelData( model, '<paragraph>fo[]o</paragraph>' );
  124. command.execute();
  125. expect( getModelData( model ) ).to.equal(
  126. '<paragraph>fo</paragraph><horizontalLine></horizontalLine><paragraph>[]o</paragraph>'
  127. );
  128. } );
  129. it( 'should create an empty paragraph after inserting a horizontal line after a paragraph and place selection inside', () => {
  130. setModelData( model, '<paragraph>foo[]</paragraph>' );
  131. command.execute();
  132. expect( getModelData( model ) ).to.equal(
  133. '<paragraph>foo</paragraph><horizontalLine></horizontalLine><paragraph>[]</paragraph>'
  134. );
  135. } );
  136. it( 'should create an empty paragraph after inserting a horizontal line after a heading and place selection inside', () => {
  137. setModelData( model, '<heading1>foo[]</heading1>' );
  138. command.execute();
  139. expect( getModelData( model ) ).to.equal(
  140. '<heading1>foo</heading1><horizontalLine></horizontalLine><paragraph>[]</paragraph>'
  141. );
  142. } );
  143. it( 'should create an empty paragraph after inserting a horizontal line and next element must not having text', () => {
  144. setModelData( model, '<paragraph>foo[]</paragraph><media></media>' );
  145. command.execute();
  146. expect( getModelData( model ) ).to.equal(
  147. '<paragraph>foo</paragraph><horizontalLine></horizontalLine><paragraph>[]</paragraph><media></media>'
  148. );
  149. } );
  150. it( 'should create an empty paragraph after inserting a horizontal line in heading and next element must not having text', () => {
  151. setModelData( model, '<heading1>foo[]</heading1><media></media>' );
  152. command.execute();
  153. expect( getModelData( model ) ).to.equal(
  154. '<heading1>foo</heading1><horizontalLine></horizontalLine><paragraph>[]</paragraph><media></media>'
  155. );
  156. } );
  157. it( 'should not create an empty paragraph if a horizontal line split an element with text', () => {
  158. setModelData( model, '<heading1>foo[]bar</heading1>' );
  159. command.execute();
  160. expect( getModelData( model ) ).to.equal(
  161. '<heading1>foo</heading1><horizontalLine></horizontalLine><heading1>[]bar</heading1>'
  162. );
  163. } );
  164. it( 'should replace an empty paragraph with a horizontal line and insert another paragraph next to', () => {
  165. setModelData( model, '<paragraph>[]</paragraph>' );
  166. command.execute();
  167. expect( getModelData( model ) ).to.equal(
  168. '<horizontalLine></horizontalLine><paragraph>[]</paragraph>'
  169. );
  170. } );
  171. it( 'should replace an empty paragraph with a horizontal line and move the selection to next paragraph', () => {
  172. setModelData( model, '<paragraph>foo</paragraph><paragraph>[]</paragraph><paragraph>bar</paragraph>' );
  173. command.execute();
  174. expect( getModelData( model ) ).to.equal(
  175. '<paragraph>foo</paragraph><horizontalLine></horizontalLine><paragraph>[]bar</paragraph>'
  176. );
  177. } );
  178. it( 'should replace an empty paragraph with a horizontal line and move the selection to next element that has text', () => {
  179. setModelData( model, '<paragraph>foo</paragraph><paragraph>[]</paragraph><heading1>bar</heading1>' );
  180. command.execute();
  181. expect( getModelData( model ) ).to.equal(
  182. '<paragraph>foo</paragraph><horizontalLine></horizontalLine><heading1>[]bar</heading1>'
  183. );
  184. } );
  185. it( 'should replace an empty block element with a horizontal line and insert a paragraph next to', () => {
  186. setModelData( model, '<heading1>[]</heading1>' );
  187. command.execute();
  188. expect( getModelData( model ) ).to.equal(
  189. '<horizontalLine></horizontalLine><paragraph>[]</paragraph>'
  190. );
  191. } );
  192. it( 'should move the selection to next element if it allows having text (paragraph + heading)', () => {
  193. setModelData( model, '<paragraph>foo[]</paragraph><heading1>bar</heading1>' );
  194. command.execute();
  195. expect( getModelData( model ) ).to.equal(
  196. '<paragraph>foo</paragraph><horizontalLine></horizontalLine><heading1>[]bar</heading1>'
  197. );
  198. } );
  199. it( 'should move the selection to next element if it allows having text (heading + paragraph)', () => {
  200. setModelData( model, '<heading1>foo[]</heading1><paragraph>bar</paragraph>' );
  201. command.execute();
  202. expect( getModelData( model ) ).to.equal(
  203. '<heading1>foo</heading1><horizontalLine></horizontalLine><paragraph>[]bar</paragraph>'
  204. );
  205. } );
  206. } );
  207. } );