imageengine.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import VirtualTestEditor from 'tests/core/_utils/virtualtesteditor.js';
  6. import ImageEngine from 'ckeditor5/image/imageengine.js';
  7. import { getData as getModelData, setData as setModelData } from 'ckeditor5/engine/dev-utils/model.js';
  8. import buildViewConverter from 'ckeditor5/engine/conversion/buildviewconverter.js';
  9. import buildModelConverter from 'ckeditor5/engine/conversion/buildmodelconverter.js';
  10. describe( `ImageEngine`, () => {
  11. let editor, document;
  12. beforeEach( () => {
  13. return VirtualTestEditor.create( {
  14. features: [ ImageEngine ]
  15. } )
  16. .then( newEditor => {
  17. editor = newEditor;
  18. document = editor.document;
  19. } );
  20. } );
  21. it( 'should be loaded', () => {
  22. expect( editor.plugins.get( ImageEngine ) ).to.be.instanceOf( ImageEngine );
  23. } );
  24. it( 'should set proper schema rules', () => {
  25. expect( document.schema.check( { name: 'image', attributes: [ 'src', 'alt' ] } ) ).to.be.true;
  26. } );
  27. describe( 'conversion in data pipeline', () => {
  28. describe( 'model to view', () => {
  29. it( 'should convert', () => {
  30. setModelData( document, '<image src="foo.png" alt="alt text"></image>' );
  31. expect( editor.getData() ).to.equal( '<figure class="image"><img src="foo.png" alt="alt text"></figure>' );
  32. } );
  33. it( 'should convert without alt attribute', () => {
  34. setModelData( document, '<image src="foo.png"></image>' );
  35. expect( editor.getData() ).to.equal( '<figure class="image"><img src="foo.png"></figure>' );
  36. } );
  37. it( 'should convert without src attribute', () => {
  38. setModelData( document, '<image alt="alt text"></image>' );
  39. expect( editor.getData() ).to.equal( '<figure class="image"><img alt="alt text"></figure>' );
  40. } );
  41. } );
  42. describe( 'view to model', () => {
  43. it( 'should convert image figure', () => {
  44. editor.setData( '<figure class="image"><img src="foo.png" alt="alt text" /></figure>' );
  45. expect( getModelData( document, { withoutSelection: true } ) )
  46. .to.equal( '<image alt="alt text" src="foo.png"></image>' );
  47. } );
  48. it( 'should not convert if there is no image class', () => {
  49. editor.setData( '<figure><img src="foo.png" alt="alt text" /></figure>' );
  50. expect( getModelData( document, { withoutSelection: true } ) )
  51. .to.equal( '' );
  52. } );
  53. it( 'should not convert if there is no img inside #1', () => {
  54. editor.setData( '<figure class="image"></figure>' );
  55. expect( getModelData( document, { withoutSelection: true } ) )
  56. .to.equal( '' );
  57. } );
  58. it( 'should not convert if there is no img inside #2', () => {
  59. editor.setData( '<figure class="image">test</figure>' );
  60. expect( getModelData( document, { withoutSelection: true } ) )
  61. .to.equal( '' );
  62. } );
  63. it( 'should convert without alt attribute', () => {
  64. editor.setData( '<figure class="image"><img src="foo.png" /></figure>' );
  65. expect( getModelData( document, { withoutSelection: true } ) )
  66. .to.equal( '<image src="foo.png"></image>' );
  67. } );
  68. it( 'should convert without src attribute', () => {
  69. editor.setData( '<figure class="image"><img alt="alt text" /></figure>' );
  70. expect( getModelData( document, { withoutSelection: true } ) )
  71. .to.equal( '<image alt="alt text"></image>' );
  72. } );
  73. it( 'should not convert in wrong context', () => {
  74. const data = editor.data;
  75. const editing = editor.editing;
  76. document.schema.registerItem( 'div', '$block' );
  77. buildModelConverter().for( data.modelToView, editing.modelToView ).fromElement( 'div' ).toElement( 'div' );
  78. buildViewConverter().for( data.viewToModel ).fromElement( 'div' ).toElement( 'div' );
  79. document.schema.disallow( { name: 'image', inside: 'div' } );
  80. editor.setData( '<div><figure class="image"><img src="foo.png" alt="alt text" /></figure></div>' );
  81. expect( getModelData( document, { withoutSelection: true } ) )
  82. .to.equal( '<div></div>' );
  83. } );
  84. it( 'should not convert if img is already consumed', () => {
  85. editor.data.viewToModel.on( 'element:figure', ( evt, data, consumable ) => {
  86. const img = data.input.getChild( 0 );
  87. consumable.consume( img, { name: true } );
  88. }, { priority: 'high' } );
  89. editor.setData( '<figure class="image"><img src="foo.png" alt="alt text" /></figure>' );
  90. expect( getModelData( document, { withoutSelection: true } ) )
  91. .to.equal( '' );
  92. } );
  93. it( 'should not convert if figure is already consumed', () => {
  94. editor.data.viewToModel.on( 'element:figure', ( evt, data, consumable ) => {
  95. const figure = data.input;
  96. consumable.consume( figure, { name: true, class: 'image' } );
  97. }, { priority: 'high' } );
  98. editor.setData( '<figure class="image"><img src="foo.png" alt="alt text" /></figure>' );
  99. expect( getModelData( document, { withoutSelection: true } ) )
  100. .to.equal( '' );
  101. } );
  102. } );
  103. } );
  104. } );