utils.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import { isImageType, findOptimalInsertionPosition } from '../../src/imageupload/utils';
  6. import Model from '@ckeditor/ckeditor5-engine/src/model/model';
  7. import { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  8. describe( 'upload utils', () => {
  9. describe( 'isImageType()', () => {
  10. it( 'should return true for png mime type', () => {
  11. expect( isImageType( { type: 'image/png' } ) ).to.be.true;
  12. } );
  13. it( 'should return true for jpeg mime type', () => {
  14. expect( isImageType( { type: 'image/jpeg' } ) ).to.be.true;
  15. } );
  16. it( 'should return true for gif mime type', () => {
  17. expect( isImageType( { type: 'image/gif' } ) ).to.be.true;
  18. } );
  19. it( 'should return true for bmp mime type', () => {
  20. expect( isImageType( { type: 'image/bmp' } ) ).to.be.true;
  21. } );
  22. it( 'should return false for other mime types', () => {
  23. expect( isImageType( { type: 'audio/mp3' } ) ).to.be.false;
  24. expect( isImageType( { type: 'video/mpeg' } ) ).to.be.false;
  25. } );
  26. } );
  27. describe( 'findOptimalInsertionPosition()', () => {
  28. let model, doc;
  29. beforeEach( () => {
  30. model = new Model();
  31. doc = model.document;
  32. doc.createRoot();
  33. model.schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  34. model.schema.register( 'image' );
  35. model.schema.register( 'span' );
  36. model.schema.extend( 'image', {
  37. allowIn: '$root',
  38. isObject: true
  39. } );
  40. model.schema.extend( 'span', { allowIn: 'paragraph' } );
  41. model.schema.extend( '$text', { allowIn: 'span' } );
  42. } );
  43. it( 'returns position after selected element', () => {
  44. setData( model, '<paragraph>x</paragraph>[<image></image>]<paragraph>y</paragraph>' );
  45. const pos = findOptimalInsertionPosition( doc.selection );
  46. expect( pos.path ).to.deep.equal( [ 2 ] );
  47. } );
  48. it( 'returns position inside empty block', () => {
  49. setData( model, '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>y</paragraph>' );
  50. const pos = findOptimalInsertionPosition( doc.selection );
  51. expect( pos.path ).to.deep.equal( [ 1, 0 ] );
  52. } );
  53. it( 'returns position before block if at the beginning of that block', () => {
  54. setData( model, '<paragraph>x</paragraph><paragraph>[]foo</paragraph><paragraph>y</paragraph>' );
  55. const pos = findOptimalInsertionPosition( doc.selection );
  56. expect( pos.path ).to.deep.equal( [ 1 ] );
  57. } );
  58. it( 'returns position before block if in the middle of that block', () => {
  59. setData( model, '<paragraph>x</paragraph><paragraph>f[]oo</paragraph><paragraph>y</paragraph>' );
  60. const pos = findOptimalInsertionPosition( doc.selection );
  61. expect( pos.path ).to.deep.equal( [ 1 ] );
  62. } );
  63. it( 'returns position after block if at the end of that block', () => {
  64. setData( model, '<paragraph>x</paragraph><paragraph>foo[]</paragraph><paragraph>y</paragraph>' );
  65. const pos = findOptimalInsertionPosition( doc.selection );
  66. expect( pos.path ).to.deep.equal( [ 2 ] );
  67. } );
  68. // Checking if isTouching() was used.
  69. it( 'returns position after block if at the end of that block (deeply nested)', () => {
  70. setData( model, '<paragraph>x</paragraph><paragraph>foo<span>bar[]</span></paragraph><paragraph>y</paragraph>' );
  71. const pos = findOptimalInsertionPosition( doc.selection );
  72. expect( pos.path ).to.deep.equal( [ 2 ] );
  73. } );
  74. it( 'returns selection focus if not in a block', () => {
  75. model.schema.extend( '$text', { allowIn: '$root' } );
  76. setData( model, 'foo[]bar' );
  77. const pos = findOptimalInsertionPosition( doc.selection );
  78. expect( pos.path ).to.deep.equal( [ 3 ] );
  79. } );
  80. } );
  81. } );