utils.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import { isImageType, findOptimalInsertionPosition } from '../src/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.registerItem( 'paragraph', '$block' );
  34. model.schema.registerItem( 'image' );
  35. model.schema.registerItem( 'span' );
  36. model.schema.allow( { name: 'image', inside: '$root' } );
  37. model.schema.objects.add( 'image' );
  38. model.schema.allow( { name: 'span', inside: 'paragraph' } );
  39. model.schema.allow( { name: '$text', inside: 'span' } );
  40. } );
  41. it( 'returns position after selected element', () => {
  42. setData( model, '<paragraph>x</paragraph>[<image></image>]<paragraph>y</paragraph>' );
  43. const pos = findOptimalInsertionPosition( doc.selection );
  44. expect( pos.path ).to.deep.equal( [ 2 ] );
  45. } );
  46. it( 'returns position inside empty block', () => {
  47. setData( model, '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>y</paragraph>' );
  48. const pos = findOptimalInsertionPosition( doc.selection );
  49. expect( pos.path ).to.deep.equal( [ 1, 0 ] );
  50. } );
  51. it( 'returns position before block if at the beginning of that block', () => {
  52. setData( model, '<paragraph>x</paragraph><paragraph>[]foo</paragraph><paragraph>y</paragraph>' );
  53. const pos = findOptimalInsertionPosition( doc.selection );
  54. expect( pos.path ).to.deep.equal( [ 1 ] );
  55. } );
  56. it( 'returns position before block if in the middle of that block', () => {
  57. setData( model, '<paragraph>x</paragraph><paragraph>f[]oo</paragraph><paragraph>y</paragraph>' );
  58. const pos = findOptimalInsertionPosition( doc.selection );
  59. expect( pos.path ).to.deep.equal( [ 1 ] );
  60. } );
  61. it( 'returns position after block if at the end of that block', () => {
  62. setData( model, '<paragraph>x</paragraph><paragraph>foo[]</paragraph><paragraph>y</paragraph>' );
  63. const pos = findOptimalInsertionPosition( doc.selection );
  64. expect( pos.path ).to.deep.equal( [ 2 ] );
  65. } );
  66. // Checking if isTouching() was used.
  67. it( 'returns position after block if at the end of that block (deeply nested)', () => {
  68. setData( model, '<paragraph>x</paragraph><paragraph>foo<span>bar[]</span></paragraph><paragraph>y</paragraph>' );
  69. const pos = findOptimalInsertionPosition( doc.selection );
  70. expect( pos.path ).to.deep.equal( [ 2 ] );
  71. } );
  72. it( 'returns selection focus if not in a block', () => {
  73. model.schema.allow( { name: '$text', inside: '$root' } );
  74. setData( model, 'foo[]bar' );
  75. const pos = findOptimalInsertionPosition( doc.selection );
  76. expect( pos.path ).to.deep.equal( [ 3 ] );
  77. } );
  78. } );
  79. } );