imageinsertcommand.js 5.3 KB

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