inserthtmlembedcommand.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  8. import HtmlEmbedEditing from '../src/htmlembedediting';
  9. import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  10. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  11. describe( 'InsertHtmlEmbedCommand', () => {
  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: [ HtmlEmbedEditing, Paragraph ]
  20. } )
  21. .then( newEditor => {
  22. editor = newEditor;
  23. model = editor.model;
  24. command = editor.commands.get( 'insertHtmlEmbed' );
  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 raw html element', () => {
  57. setModelData( model, '[<rawHtml></rawHtml>]' );
  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 raw html', () => {
  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 raw html', () => {
  77. model.schema.register( 'block', { inheritAllFrom: '$block' } );
  78. model.schema.extend( 'paragraph', { allowIn: 'block' } );
  79. // Block raw html in block.
  80. model.schema.addChildCheck( ( context, childDefinition ) => {
  81. if ( childDefinition.name === 'rawHtml' && 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 raw html 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( '[<rawHtml></rawHtml>]' );
  114. } );
  115. it( 'should split an element where selection is placed and insert a raw html (non-collapsed selection)', () => {
  116. setModelData( model, '<paragraph>f[o]o</paragraph>' );
  117. command.execute();
  118. expect( getModelData( model ) ).to.equal(
  119. '<paragraph>f</paragraph>[<rawHtml></rawHtml>]<paragraph>o</paragraph>'
  120. );
  121. } );
  122. it( 'should split an element where selection is placed and insert a raw html (collapsed selection)', () => {
  123. setModelData( model, '<paragraph>fo[]o</paragraph>' );
  124. command.execute();
  125. expect( getModelData( model ) ).to.equal(
  126. '<paragraph>fo</paragraph>[<rawHtml></rawHtml>]<paragraph>o</paragraph>'
  127. );
  128. } );
  129. } );
  130. } );