imageinsertcommand.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 true when the selection is inside isLimit element which allows image', () => {
  73. model.schema.register( 'outerObject', { isObject: true, isBlock: true, allowIn: '$root' } );
  74. model.schema.register( 'limit', { isLimit: true, allowIn: 'outerObject' } );
  75. model.schema.extend( '$block', { allowIn: 'limit' } );
  76. editor.conversion.for( 'downcast' ).add( downcastElementToElement( { model: 'outerObject', view: 'outerObject' } ) );
  77. editor.conversion.for( 'downcast' ).add( downcastElementToElement( { model: 'limit', view: 'limit' } ) );
  78. setModelData( model, '<outerObject><limit>[]</limit></outerObject>' );
  79. expect( command.isEnabled ).to.be.true;
  80. } );
  81. it( 'should be true when the selection is inside isLimit element which allows image', () => {
  82. model.schema.register( 'outerObject', { isObject: true, isBlock: true, allowIn: '$root' } );
  83. model.schema.register( 'limit', { isLimit: true, allowIn: 'outerObject' } );
  84. model.schema.extend( '$block', { allowIn: 'limit' } );
  85. editor.conversion.for( 'downcast' ).add( downcastElementToElement( { model: 'outerObject', view: 'outerObject' } ) );
  86. editor.conversion.for( 'downcast' ).add( downcastElementToElement( { model: 'limit', view: 'limit' } ) );
  87. setModelData( model, '<outerObject><limit><paragraph>foo[]</paragraph></limit></outerObject>' );
  88. expect( command.isEnabled ).to.be.true;
  89. } );
  90. it( 'should be false when schema disallows image', () => {
  91. model.schema.register( 'block', { inheritAllFrom: '$block' } );
  92. model.schema.extend( 'paragraph', { allowIn: 'block' } );
  93. // Block image in block.
  94. model.schema.addChildCheck( ( context, childDefinition ) => {
  95. if ( childDefinition.name === 'image' && context.last.name === 'block' ) {
  96. return false;
  97. }
  98. } );
  99. editor.conversion.for( 'downcast' ).add( downcastElementToElement( { model: 'block', view: 'block' } ) );
  100. setModelData( model, '<block><paragraph>[]</paragraph></block>' );
  101. expect( command.isEnabled ).to.be.false;
  102. } );
  103. } );
  104. describe( 'execute()', () => {
  105. it( 'should insert image at selection position as other widgets', () => {
  106. const imgSrc = 'foo/bar.jpg';
  107. setModelData( model, '<paragraph>f[o]o</paragraph>' );
  108. command.execute( { source: imgSrc } );
  109. expect( getModelData( model ) ).to.equal( `[<image src="${ imgSrc }"></image>]<paragraph>foo</paragraph>` );
  110. } );
  111. it( 'should insert multiple images at selection position as other widgets', () => {
  112. const imgSrc1 = 'foo/bar.jpg';
  113. const imgSrc2 = 'foo/baz.jpg';
  114. setModelData( model, '<paragraph>f[o]o</paragraph>' );
  115. command.execute( { source: [ imgSrc1, imgSrc2 ] } );
  116. expect( getModelData( model ) )
  117. .to.equal( `<image src="${ imgSrc1 }"></image>[<image src="${ imgSrc2 }"></image>]<paragraph>foo</paragraph>` );
  118. } );
  119. it( 'should not insert image nor crash when image could not be inserted', () => {
  120. const imgSrc = 'foo/bar.jpg';
  121. model.schema.register( 'other', {
  122. allowIn: '$root',
  123. isLimit: true
  124. } );
  125. model.schema.extend( '$text', { allowIn: 'other' } );
  126. editor.conversion.for( 'downcast' ).add( downcastElementToElement( { model: 'other', view: 'p' } ) );
  127. setModelData( model, '<other>[]</other>' );
  128. command.execute( { source: imgSrc } );
  129. expect( getModelData( model ) ).to.equal( '<other>[]</other>' );
  130. } );
  131. } );
  132. } );