utils.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 Document from '@ckeditor/ckeditor5-engine/src/model/document';
  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 doc;
  29. beforeEach( () => {
  30. doc = new Document();
  31. doc.createRoot();
  32. doc.schema.registerItem( 'paragraph', '$block' );
  33. doc.schema.registerItem( 'image' );
  34. doc.schema.registerItem( 'span' );
  35. doc.schema.allow( { name: 'image', inside: '$root' } );
  36. doc.schema.objects.add( 'image' );
  37. doc.schema.allow( { name: 'span', inside: 'paragraph' } );
  38. doc.schema.allow( { name: '$text', inside: 'span' } );
  39. } );
  40. it( 'returns position after selected element', () => {
  41. setData( doc, '<paragraph>x</paragraph>[<image></image>]<paragraph>y</paragraph>' );
  42. const pos = findOptimalInsertionPosition( doc.selection );
  43. expect( pos.path ).to.deep.equal( [ 2 ] );
  44. } );
  45. it( 'returns position inside empty block', () => {
  46. setData( doc, '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>y</paragraph>' );
  47. const pos = findOptimalInsertionPosition( doc.selection );
  48. expect( pos.path ).to.deep.equal( [ 1, 0 ] );
  49. } );
  50. it( 'returns position before block if at the beginning of that block', () => {
  51. setData( doc, '<paragraph>x</paragraph><paragraph>[]foo</paragraph><paragraph>y</paragraph>' );
  52. const pos = findOptimalInsertionPosition( doc.selection );
  53. expect( pos.path ).to.deep.equal( [ 1 ] );
  54. } );
  55. it( 'returns position before block if in the middle of that block', () => {
  56. setData( doc, '<paragraph>x</paragraph><paragraph>f[]oo</paragraph><paragraph>y</paragraph>' );
  57. const pos = findOptimalInsertionPosition( doc.selection );
  58. expect( pos.path ).to.deep.equal( [ 1 ] );
  59. } );
  60. it( 'returns position after block if at the end of that block', () => {
  61. setData( doc, '<paragraph>x</paragraph><paragraph>foo[]</paragraph><paragraph>y</paragraph>' );
  62. const pos = findOptimalInsertionPosition( doc.selection );
  63. expect( pos.path ).to.deep.equal( [ 2 ] );
  64. } );
  65. // Checking if isTouching() was used.
  66. it( 'returns position after block if at the end of that block (deeply nested)', () => {
  67. setData( doc, '<paragraph>x</paragraph><paragraph>foo<span>bar[]</span></paragraph><paragraph>y</paragraph>' );
  68. const pos = findOptimalInsertionPosition( doc.selection );
  69. expect( pos.path ).to.deep.equal( [ 2 ] );
  70. } );
  71. it( 'returns selection focus if not in a block', () => {
  72. doc.schema.allow( { name: '$text', inside: '$root' } );
  73. setData( doc, 'foo[]bar' );
  74. const pos = findOptimalInsertionPosition( doc.selection );
  75. expect( pos.path ).to.deep.equal( [ 3 ] );
  76. } );
  77. } );
  78. } );