8
0

converters.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import {
  6. viewFigureToModel,
  7. modelToViewAttributeConverter
  8. } from '../../src/image/converters';
  9. import { toImageWidget } from '../../src/image/utils';
  10. import { createImageViewElement } from '../../src/image/imageediting';
  11. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  12. import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  13. import { setData as setModelData, getData as getModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  14. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  15. describe( 'Image converters', () => {
  16. let editor, model, document, viewDocument;
  17. testUtils.createSinonSandbox();
  18. beforeEach( () => {
  19. return VirtualTestEditor.create()
  20. .then( newEditor => {
  21. editor = newEditor;
  22. model = editor.model;
  23. document = model.document;
  24. viewDocument = editor.editing.view;
  25. const schema = model.schema;
  26. schema.register( 'image', {
  27. allowWhere: '$block',
  28. allowAttributes: [ 'alt', 'src' ],
  29. isObject: true,
  30. isBlock: true
  31. } );
  32. const editingElementCreator = ( modelElement, viewWriter ) =>
  33. toImageWidget( createImageViewElement( viewWriter ), viewWriter, '' );
  34. editor.conversion.for( 'editingDowncast' ).elementToElement( {
  35. model: 'image',
  36. view: editingElementCreator
  37. } );
  38. editor.conversion.for( 'downcast' )
  39. .add( modelToViewAttributeConverter( 'src' ) )
  40. .add( modelToViewAttributeConverter( 'alt' ) );
  41. } );
  42. } );
  43. describe( 'viewFigureToModel', () => {
  44. function expectModel( data ) {
  45. expect( getModelData( model, { withoutSelection: true } ) ).to.equal( data );
  46. }
  47. let schema, imgConverterCalled;
  48. beforeEach( () => {
  49. // Since this part of test tests only view->model conversion editing pipeline is not necessary
  50. // so defining model->view converters won't be necessary.
  51. editor.editing.destroy();
  52. schema = model.schema;
  53. schema.extend( '$text', { allowIn: 'image' } );
  54. editor.conversion.for( 'upcast' )
  55. .add( viewFigureToModel() )
  56. .elementToElement( {
  57. view: {
  58. name: 'img',
  59. attributes: {
  60. src: true
  61. }
  62. },
  63. model: ( viewImage, writer ) => {
  64. imgConverterCalled = true;
  65. return writer.createElement( 'image', { src: viewImage.getAttribute( 'src' ) } );
  66. }
  67. } );
  68. } );
  69. it( 'should find img element among children and convert it using already defined converters', () => {
  70. editor.setData( '<figure class="image"><img src="/assets/sample.png" /></figure>' );
  71. expectModel( '<image src="/assets/sample.png"></image>' );
  72. expect( imgConverterCalled ).to.be.true;
  73. } );
  74. it( 'should convert children allowed by schema and omit disallowed', () => {
  75. editor.conversion.for( 'upcast' ).elementToElement( { view: 'foo', model: 'foo' } );
  76. editor.conversion.for( 'upcast' ).elementToElement( { view: 'bar', model: 'bar' } );
  77. schema.register( 'foo', { allowIn: 'image' } );
  78. // Is allowed in root, but should not try to split image element.
  79. schema.register( 'bar', { allowIn: '$root' } );
  80. editor.setData( '<figure class="image">x<img src="/assets/sample.png" />y<foo></foo><bar></bar></figure>' );
  81. // Element bar not converted because schema does not allow it.
  82. expectModel( '<image src="/assets/sample.png">xy<foo></foo></image>' );
  83. } );
  84. it( 'should split parent element when image is not allowed - in the middle', () => {
  85. editor.conversion.for( 'upcast' ).elementToElement( { view: 'div', model: 'div' } );
  86. schema.register( 'div', { inheritAllFrom: '$block' } );
  87. schema.extend( 'image', { disallowIn: 'div' } );
  88. editor.setData(
  89. '<div>' +
  90. 'abc' +
  91. '<figure class="image">' +
  92. '<img src="foo.jpg"/>' +
  93. '</figure>' +
  94. 'def' +
  95. '</div>'
  96. );
  97. expectModel( '<div>abc</div><image src="foo.jpg"></image><div>def</div>' );
  98. } );
  99. it( 'should split parent element when image is not allowed - at the end', () => {
  100. editor.conversion.elementToElement( { model: 'div', view: 'div' } );
  101. schema.register( 'div', { inheritAllFrom: '$block' } );
  102. schema.extend( 'image', { disallowIn: 'div' } );
  103. editor.setData(
  104. '<div>' +
  105. 'abc' +
  106. '<figure class="image">' +
  107. '<img src="foo.jpg"/>' +
  108. '</figure>' +
  109. '</div>'
  110. );
  111. expectModel( '<div>abc</div><image src="foo.jpg"></image>' );
  112. } );
  113. it( 'should split parent element when image is not allowed - at the beginning', () => {
  114. editor.conversion.elementToElement( { model: 'div', view: 'div' } );
  115. schema.register( 'div', { inheritAllFrom: '$block' } );
  116. schema.extend( 'image', { disallowIn: 'div' } );
  117. editor.setData(
  118. '<div>' +
  119. '<figure class="image">' +
  120. '<img src="foo.jpg"/>' +
  121. '</figure>' +
  122. 'def' +
  123. '</div>'
  124. );
  125. expectModel( '<image src="foo.jpg"></image><div>def</div>' );
  126. } );
  127. it( 'should be possible to overwrite', () => {
  128. editor.data.upcastDispatcher.on( 'element:figure', ( evt, data, conversionApi ) => {
  129. conversionApi.consumable.consume( data.viewItem, { name: true } );
  130. conversionApi.consumable.consume( data.viewItem.getChild( 0 ), { name: true } );
  131. const element = conversionApi.writer.createElement( 'myImage', {
  132. data: {
  133. src: data.viewItem.getChild( 0 ).getAttribute( 'src' )
  134. }
  135. } );
  136. conversionApi.writer.insert( element, data.modelCursor );
  137. data.modelRange = conversionApi.writer.createRangeOn( element );
  138. data.modelCursor = data.modelRange.end;
  139. }, { priority: 'high' } );
  140. editor.setData( '<figure class="image"><img src="/assets/sample.png" />xyz</figure>' );
  141. expectModel( '<myImage data="{"src":"/assets/sample.png"}"></myImage>' );
  142. } );
  143. // Test exactly what figure converter does, which is putting it's children element to image element.
  144. // If this has not been done, it means that figure converter was not used.
  145. it( 'should not convert if figure do not have class="image" attribute', () => {
  146. editor.setData( '<figure><img src="/assets/sample.png" />xyz</figure>' );
  147. // Default image converter will be fired.
  148. expectModel( '<image src="/assets/sample.png"></image>' );
  149. } );
  150. it( 'should not convert if there is no img element among children', () => {
  151. editor.setData( '<figure class="image">xyz</figure>' );
  152. // Figure converter outputs nothing and text is disallowed in root.
  153. expectModel( '' );
  154. } );
  155. it( 'should not convert if img element was not converted', () => {
  156. // Image element missing src attribute.
  157. editor.setData( '<figure class="image"><img alt="abc" />xyz</figure>' );
  158. // Figure converter outputs nothing and text is disallowed in root.
  159. expectModel( '' );
  160. } );
  161. } );
  162. describe( 'modelToViewAttributeConverter', () => {
  163. it( 'should convert adding attribute to image', () => {
  164. setModelData( model, '<image src=""></image>' );
  165. const image = document.getRoot().getChild( 0 );
  166. model.change( writer => {
  167. writer.setAttribute( 'alt', 'foo bar', image );
  168. } );
  169. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  170. '<figure class="ck-widget image" contenteditable="false"><img alt="foo bar" src=""></img></figure>'
  171. );
  172. } );
  173. it( 'should convert an empty "src" attribute from image even if removed', () => {
  174. setModelData( model, '<image src="" alt="foo bar"></image>' );
  175. const image = document.getRoot().getChild( 0 );
  176. model.change( writer => {
  177. writer.removeAttribute( 'src', image );
  178. } );
  179. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  180. '<figure class="ck-widget image" contenteditable="false"><img alt="foo bar" src=""></img></figure>'
  181. );
  182. } );
  183. it( 'should convert an empty "alt" attribute from image even if removed', () => {
  184. setModelData( model, '<image src="" alt="foo bar"></image>' );
  185. const image = document.getRoot().getChild( 0 );
  186. model.change( writer => {
  187. writer.removeAttribute( 'alt', image );
  188. } );
  189. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  190. '<figure class="ck-widget image" contenteditable="false"><img alt="" src=""></img></figure>'
  191. );
  192. } );
  193. it( 'should convert change of attribute image', () => {
  194. setModelData( model, '<image src="" alt="foo bar"></image>' );
  195. const image = document.getRoot().getChild( 0 );
  196. model.change( writer => {
  197. writer.setAttribute( 'alt', 'baz quix', image );
  198. } );
  199. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  200. '<figure class="ck-widget image" contenteditable="false"><img alt="baz quix" src=""></img></figure>'
  201. );
  202. } );
  203. it( 'should not set attribute if change was already consumed', () => {
  204. editor.editing.downcastDispatcher.on( 'attribute:alt:image', ( evt, data, conversionApi ) => {
  205. conversionApi.consumable.consume( data.item, 'attribute:alt' );
  206. }, { priority: 'high' } );
  207. setModelData( model, '<image src="" alt="foo bar"></image>' );
  208. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  209. '<figure class="ck-widget image" contenteditable="false"><img src=""></img></figure>'
  210. );
  211. } );
  212. it( 'should set attribute on <img> even if other element is present inside figure', () => {
  213. editor.model.schema.register( 'foo', {
  214. allowIn: 'image'
  215. } );
  216. editor.conversion.for( 'downcast' ).elementToElement( { model: 'foo', view: 'foo' } );
  217. setModelData( model, '<image src=""><foo></foo></image>' );
  218. const image = document.getRoot().getChild( 0 );
  219. model.change( writer => {
  220. writer.setAttribute( 'alt', 'foo bar', image );
  221. } );
  222. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  223. '<figure class="ck-widget image" contenteditable="false"><foo></foo><img alt="foo bar" src=""></img></figure>'
  224. );
  225. } );
  226. } );
  227. } );