8
0

imageinsertcommand.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  6. import ImageInsertCommand from '../../src/image/imageinsertcommand';
  7. import Image from '../../src/image/imageediting';
  8. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  9. import { setData as setModelData, getData as getModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  10. describe( 'ImageInsertCommand', () => {
  11. let editor, command, model;
  12. beforeEach( () => {
  13. return VirtualTestEditor
  14. .create( {
  15. plugins: [ Image, Paragraph ]
  16. } )
  17. .then( newEditor => {
  18. editor = newEditor;
  19. model = editor.model;
  20. command = new ImageInsertCommand( editor );
  21. const schema = model.schema;
  22. schema.extend( 'image', { allowAttributes: 'uploadId' } );
  23. } );
  24. } );
  25. afterEach( () => {
  26. return editor.destroy();
  27. } );
  28. describe( 'isEnabled', () => {
  29. it( 'should be true when the selection directly in the root', () => {
  30. model.enqueueChange( 'transparent', () => {
  31. setModelData( model, '[]' );
  32. command.refresh();
  33. expect( command.isEnabled ).to.be.true;
  34. } );
  35. } );
  36. it( 'should be true when the selection is in empty block', () => {
  37. setModelData( model, '<paragraph>[]</paragraph>' );
  38. expect( command.isEnabled ).to.be.true;
  39. } );
  40. it( 'should be true when the selection directly in a paragraph', () => {
  41. setModelData( model, '<paragraph>foo[]</paragraph>' );
  42. expect( command.isEnabled ).to.be.true;
  43. } );
  44. it( 'should be true when the selection directly in a block', () => {
  45. model.schema.register( 'block', { inheritAllFrom: '$block' } );
  46. model.schema.extend( '$text', { allowIn: 'block' } );
  47. editor.conversion.for( 'downcast' ).elementToElement( { model: 'block', view: 'block' } );
  48. setModelData( model, '<block>foo[]</block>' );
  49. expect( command.isEnabled ).to.be.true;
  50. } );
  51. it( 'should be false when the selection is on other image', () => {
  52. setModelData( model, '[<image></image>]' );
  53. expect( command.isEnabled ).to.be.false;
  54. } );
  55. it( 'should be false when the selection is inside other image', () => {
  56. model.schema.register( 'caption', {
  57. allowIn: 'image',
  58. allowContentOf: '$block',
  59. isLimit: true
  60. } );
  61. editor.conversion.for( 'downcast' ).elementToElement( { model: 'caption', view: 'figcaption' } );
  62. setModelData( model, '<image><caption>[]</caption></image>' );
  63. expect( command.isEnabled ).to.be.false;
  64. } );
  65. it( 'should be false when the selection is on other object', () => {
  66. model.schema.register( 'object', { isObject: true, allowIn: '$root' } );
  67. editor.conversion.for( 'downcast' ).elementToElement( { model: 'object', view: 'object' } );
  68. setModelData( model, '[<object></object>]' );
  69. expect( command.isEnabled ).to.be.false;
  70. } );
  71. it( 'should be true when the selection is inside block element inside isLimit element which allows image', () => {
  72. model.schema.register( 'table', { allowWhere: '$block', isLimit: true, isObject: true, isBlock: true } );
  73. model.schema.register( 'tableRow', { allowIn: 'table', isLimit: true } );
  74. model.schema.register( 'tableCell', { allowIn: 'tableRow', isLimit: true, isSelectable: true } );
  75. model.schema.extend( '$block', { allowIn: 'tableCell' } );
  76. editor.conversion.for( 'downcast' ).elementToElement( { model: 'table', view: 'table' } );
  77. editor.conversion.for( 'downcast' ).elementToElement( { model: 'tableRow', view: 'tableRow' } );
  78. editor.conversion.for( 'downcast' ).elementToElement( { model: 'tableCell', view: 'tableCell' } );
  79. setModelData( model, '<table><tableRow><tableCell><paragraph>foo[]</paragraph></tableCell></tableRow></table>' );
  80. expect( command.isEnabled ).to.be.true;
  81. } );
  82. it( 'should be false when schema disallows image', () => {
  83. model.schema.register( 'block', { inheritAllFrom: '$block' } );
  84. model.schema.extend( 'paragraph', { allowIn: 'block' } );
  85. // Block image in block.
  86. model.schema.addChildCheck( ( context, childDefinition ) => {
  87. if ( childDefinition.name === 'image' && context.last.name === 'block' ) {
  88. return false;
  89. }
  90. } );
  91. editor.conversion.for( 'downcast' ).elementToElement( { model: 'block', view: 'block' } );
  92. setModelData( model, '<block><paragraph>[]</paragraph></block>' );
  93. expect( command.isEnabled ).to.be.false;
  94. } );
  95. } );
  96. describe( 'execute()', () => {
  97. it( 'should insert image at selection position as other widgets', () => {
  98. const imgSrc = 'foo/bar.jpg';
  99. setModelData( model, '<paragraph>f[o]o</paragraph>' );
  100. command.execute( { source: imgSrc } );
  101. expect( getModelData( model ) ).to.equal( `[<image src="${ imgSrc }"></image>]<paragraph>foo</paragraph>` );
  102. } );
  103. it( 'should insert multiple images at selection position as other widgets', () => {
  104. const imgSrc1 = 'foo/bar.jpg';
  105. const imgSrc2 = 'foo/baz.jpg';
  106. setModelData( model, '<paragraph>f[o]o</paragraph>' );
  107. command.execute( { source: [ imgSrc1, imgSrc2 ] } );
  108. expect( getModelData( model ) )
  109. .to.equal( `<image src="${ imgSrc1 }"></image>[<image src="${ imgSrc2 }"></image>]<paragraph>foo</paragraph>` );
  110. } );
  111. it( 'should not insert image nor crash when image could not be inserted', () => {
  112. const imgSrc = 'foo/bar.jpg';
  113. model.schema.register( 'other', {
  114. allowIn: '$root',
  115. isLimit: true
  116. } );
  117. model.schema.extend( '$text', { allowIn: 'other' } );
  118. editor.conversion.for( 'downcast' ).elementToElement( { model: 'other', view: 'p' } );
  119. setModelData( model, '<other>[]</other>' );
  120. command.execute( { source: imgSrc } );
  121. expect( getModelData( model ) ).to.equal( '<other>[]</other>' );
  122. } );
  123. } );
  124. } );