converters.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import {
  6. viewFigureToModel,
  7. createImageAttributeConverter,
  8. convertHoistableImage,
  9. hoistImageThroughElement
  10. } from '../../src/image/converters';
  11. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  12. import { createImageViewElement } from '../../src/image/imageengine';
  13. import { toImageWidget } from '../../src/image/utils';
  14. import buildModelConverter from '@ckeditor/ckeditor5-engine/src/conversion/buildmodelconverter';
  15. import buildViewConverter from '@ckeditor/ckeditor5-engine/src/conversion/buildviewconverter';
  16. import ModelElement from '@ckeditor/ckeditor5-engine/src/model/element';
  17. import ViewEmptyElement from '@ckeditor/ckeditor5-engine/src/view/emptyelement';
  18. import ViewContainerElement from '@ckeditor/ckeditor5-engine/src/view/containerelement';
  19. import { convertToModelFragment } from '@ckeditor/ckeditor5-engine/src/conversion/view-to-model-converters';
  20. import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  21. import { setData as setModelData, getData as getModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  22. describe( 'Image converters', () => {
  23. let editor, document, viewDocument;
  24. beforeEach( () => {
  25. return VirtualTestEditor.create()
  26. .then( newEditor => {
  27. editor = newEditor;
  28. document = editor.document;
  29. viewDocument = editor.editing.view;
  30. const schema = document.schema;
  31. schema.registerItem( 'image' );
  32. schema.requireAttributes( 'image', [ 'src' ] );
  33. schema.allow( { name: 'image', attributes: [ 'alt', 'src' ], inside: '$root' } );
  34. schema.objects.add( 'image' );
  35. buildModelConverter().for( editor.editing.modelToView )
  36. .fromElement( 'image' )
  37. .toElement( () => toImageWidget( createImageViewElement() ) );
  38. createImageAttributeConverter( [ editor.editing.modelToView ], 'src' );
  39. createImageAttributeConverter( [ editor.editing.modelToView ], 'alt' );
  40. } );
  41. } );
  42. describe( 'viewFigureToModel', () => {
  43. function expectModel( model ) {
  44. expect( getModelData( document, { withoutSelection: true } ) ).to.equal( model );
  45. }
  46. let dispatcher, schema, imgConverterCalled;
  47. beforeEach( () => {
  48. imgConverterCalled = false;
  49. schema = document.schema;
  50. schema.allow( { name: '$text', inside: 'image' } );
  51. dispatcher = editor.data.viewToModel;
  52. dispatcher.on( 'element:figure', viewFigureToModel() );
  53. dispatcher.on( 'element:img', ( evt, data, consumable ) => {
  54. if ( consumable.consume( data.input, { name: true, attribute: 'src' } ) ) {
  55. data.output = new ModelElement( 'image', { src: data.input.getAttribute( 'src' ) } );
  56. imgConverterCalled = true;
  57. }
  58. } );
  59. } );
  60. it( 'should find img element among children and convert it using already defined converters', () => {
  61. editor.setData( '<figure class="image"><img src="foo.png" /></figure>' );
  62. expectModel( '<image src="foo.png"></image>' );
  63. expect( imgConverterCalled ).to.be.true;
  64. } );
  65. it( 'should convert non-img children in image context and append them to model image element', () => {
  66. buildViewConverter().for( editor.data.viewToModel ).fromElement( 'foo' ).toElement( 'foo' );
  67. buildViewConverter().for( editor.data.viewToModel ).fromElement( 'bar' ).toElement( 'bar' );
  68. schema.registerItem( 'foo' );
  69. schema.registerItem( 'bar' );
  70. schema.allow( { name: 'foo', inside: 'image' } );
  71. editor.setData( '<figure class="image">x<img src="foo.png" />y<foo></foo><bar></bar></figure>' );
  72. // Element bar not converted because schema does not allow it.
  73. expectModel( '<image src="foo.png">xy<foo></foo></image>' );
  74. } );
  75. it( 'should be possible to overwrite', () => {
  76. dispatcher.on( 'element:figure', ( evt, data, consumable ) => {
  77. consumable.consume( data.input, { name: true } );
  78. consumable.consume( data.input.getChild( 0 ), { name: true } );
  79. data.output = new ModelElement( 'myImage', { data: { src: data.input.getChild( 0 ).getAttribute( 'src' ) } } );
  80. }, { priority: 'high' } );
  81. editor.setData( '<figure class="image"><img src="foo.png" />xyz</figure>' );
  82. expectModel( '<myImage data="{"src":"foo.png"}"></myImage>' );
  83. } );
  84. // Test exactly what figure converter does, which is putting it's children element to image element.
  85. // If this has not been done, it means that figure converter was not used.
  86. it( 'should not convert if figure do not have class="image" attribute', () => {
  87. editor.setData( '<figure><img src="foo.png" />xyz</figure>' );
  88. // Default image converter will be fired.
  89. expectModel( '<image src="foo.png"></image>' );
  90. } );
  91. it( 'should not convert if there is no img element among children', () => {
  92. editor.setData( '<figure class="image">xyz</figure>' );
  93. // Figure converter outputs nothing and text is disallowed in root.
  94. expectModel( '' );
  95. } );
  96. it( 'should not convert if img element was not converted', () => {
  97. // Image element missing src attribute.
  98. editor.setData( '<figure class="image"><img alt="abc" />xyz</figure>' );
  99. // Figure converter outputs nothing and text is disallowed in root.
  100. expectModel( '' );
  101. } );
  102. } );
  103. describe( 'modelToViewAttributeConverter', () => {
  104. it( 'should convert adding attribute to image', () => {
  105. setModelData( document, '<image src=""></image>' );
  106. const image = document.getRoot().getChild( 0 );
  107. document.enqueueChanges( () => {
  108. const batch = document.batch();
  109. batch.setAttribute( image, 'alt', 'foo bar' );
  110. } );
  111. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  112. '<figure class="image ck-widget" contenteditable="false"><img alt="foo bar" src=""></img></figure>'
  113. );
  114. } );
  115. it( 'should convert removing attribute from image', () => {
  116. setModelData( document, '<image src="" alt="foo bar"></image>' );
  117. const image = document.getRoot().getChild( 0 );
  118. document.enqueueChanges( () => {
  119. const batch = document.batch();
  120. batch.removeAttribute( image, 'alt' );
  121. } );
  122. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  123. '<figure class="image ck-widget" contenteditable="false"><img src=""></img></figure>'
  124. );
  125. } );
  126. it( 'should convert change of attribute image', () => {
  127. setModelData( document, '<image src="" alt="foo bar"></image>' );
  128. const image = document.getRoot().getChild( 0 );
  129. document.enqueueChanges( () => {
  130. const batch = document.batch();
  131. batch.setAttribute( image, 'alt', 'baz quix' );
  132. } );
  133. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  134. '<figure class="image ck-widget" contenteditable="false"><img alt="baz quix" src=""></img></figure>'
  135. );
  136. } );
  137. it( 'should not change attribute if change is already consumed', () => {
  138. editor.editing.modelToView.on( 'changeAttribute:alt:image', ( evt, data, consumable ) => {
  139. consumable.consume( data.item, 'changeAttribute:alt' );
  140. }, { priority: 'high' } );
  141. setModelData( document, '<image src="" alt="foo bar"></image>' );
  142. const image = document.getRoot().getChild( 0 );
  143. document.enqueueChanges( () => {
  144. const batch = document.batch();
  145. batch.setAttribute( image, 'alt', 'baz quix' );
  146. } );
  147. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  148. '<figure class="image ck-widget" contenteditable="false"><img alt="foo bar" src=""></img></figure>'
  149. );
  150. } );
  151. } );
  152. // More integration tests are in imageengine.js.
  153. describe( 'autohoist converters', () => {
  154. let dispatcher, schema, imgConverterCalled, viewImg, modelDiv, modelParagraph, modelLimit;
  155. beforeEach( () => {
  156. imgConverterCalled = false;
  157. schema = document.schema;
  158. dispatcher = editor.data.viewToModel;
  159. dispatcher.on( 'element:img', ( evt, data, consumable ) => {
  160. if ( !schema.check( { name: 'image', attributes: [ 'src' ], inside: data.context } ) ) {
  161. return;
  162. }
  163. if ( consumable.consume( data.input, { name: true, attribute: 'src' } ) ) {
  164. data.output = new ModelElement( 'image', { src: data.input.getAttribute( 'src' ) } );
  165. imgConverterCalled = true;
  166. }
  167. } );
  168. // Make sure to pass document element to the converter and fire this callback after "normal" image callback.
  169. dispatcher.on( 'element:img', convertHoistableImage, { priority: 'low' } );
  170. viewImg = new ViewEmptyElement( 'img', { src: 'foo.jpg' } );
  171. modelDiv = new ModelElement( 'div' );
  172. modelParagraph = new ModelElement( 'paragraph' );
  173. modelLimit = new ModelElement( 'limit' );
  174. } );
  175. describe( 'convertHoistableImage', () => {
  176. it( 'should convert img element using already added converters if it is allowed in any point of given context #1', () => {
  177. const result = dispatcher.convert( viewImg, { context: [ '$root', 'div', 'paragraph' ] } );
  178. // `result` is a model document fragment.
  179. expect( result.childCount ).to.equal( 1 );
  180. expect( result.getChild( 0 ).is( 'image' ) ).to.be.true;
  181. expect( result.getChild( 0 ).getAttribute( 'src' ) ).to.equal( 'foo.jpg' );
  182. expect( imgConverterCalled ).to.be.true;
  183. } );
  184. it( 'should convert img element using already added converters if it is allowed in any point of given context #2', () => {
  185. const result = dispatcher.convert( viewImg, { context: [ '$root', modelDiv, modelParagraph ] } );
  186. // `result` is a model document fragment.
  187. expect( result.childCount ).to.equal( 1 );
  188. expect( result.getChild( 0 ).is( 'image' ) ).to.be.true;
  189. expect( result.getChild( 0 ).getAttribute( 'src' ) ).to.equal( 'foo.jpg' );
  190. expect( imgConverterCalled ).to.be.true;
  191. } );
  192. it( 'should not convert img element if there is no allowed context #1', () => {
  193. const result = dispatcher.convert( viewImg, { context: [ 'div', 'paragraph' ] } );
  194. // `result` is an empty model document fragment.
  195. expect( result.childCount ).to.equal( 0 );
  196. expect( imgConverterCalled ).to.be.false;
  197. } );
  198. it( 'should not convert img element if there is no allowed context #2', () => {
  199. const result = dispatcher.convert( viewImg, { context: [ modelDiv, modelParagraph ] } );
  200. // `result` is an empty model document fragment.
  201. expect( result.childCount ).to.equal( 0 );
  202. expect( imgConverterCalled ).to.be.false;
  203. } );
  204. it( 'should not convert img element if allowed context is "above" limiting element #1', () => {
  205. schema.limits.add( 'limit' );
  206. const result = dispatcher.convert( viewImg, { context: [ '$root', 'limit', 'div', 'paragraph' ] } );
  207. // `result` is an empty model document fragment.
  208. expect( result.childCount ).to.equal( 0 );
  209. expect( imgConverterCalled ).to.be.false;
  210. } );
  211. it( 'should not convert img element if allowed context is "above" limiting element #2', () => {
  212. schema.limits.add( 'limit' );
  213. const result = dispatcher.convert( viewImg, { context: [ '$root', modelLimit, modelDiv, modelParagraph ] } );
  214. // `result` is an empty model document fragment.
  215. expect( result.childCount ).to.equal( 0 );
  216. expect( imgConverterCalled ).to.be.false;
  217. } );
  218. } );
  219. describe( 'hoistImageThroughElement', () => {
  220. it( 'should hoist img element that was converted by convertHoistableImage', () => {
  221. schema.registerItem( 'div', '$block' );
  222. buildModelConverter().for( editor.data.modelToView, editor.editing.modelToView ).fromElement( 'div' ).toElement( 'div' );
  223. buildViewConverter().for( editor.data.viewToModel ).fromElement( 'div' ).toElement( 'div' );
  224. // Make sure to fire this callback after "normal" div callback.
  225. dispatcher.on( 'element:div', hoistImageThroughElement, { priority: 'low' } );
  226. // If img view element is converted it must have been converted thanks to convertHoistableImage,
  227. // because image is not allowed in div.
  228. const viewDiv = new ViewContainerElement( 'div', null, [ 'foo', viewImg, 'bar' ] );
  229. const result = dispatcher.convert( viewDiv, { context: [ '$root' ] } );
  230. // `result` is a model document fragment.
  231. expect( result.childCount ).to.equal( 3 );
  232. expect( result.getChild( 0 ).is( 'div' ) ).to.be.true;
  233. expect( result.getChild( 1 ).is( 'image' ) ).to.be.true;
  234. expect( result.getChild( 2 ).is( 'div' ) ).to.be.true;
  235. } );
  236. it( 'should work fine with elements that are converted to document fragments', () => {
  237. // The example here is <p> that contains some text and an <img> and is converted in $root > div context.
  238. // This way we enable autohoisting for <img> and test how it will work when <p> is converted to model document fragment.
  239. schema.registerItem( 'div', '$block' );
  240. dispatcher.on( 'element:p', convertToModelFragment() );
  241. dispatcher.on( 'element:p', hoistImageThroughElement, { priority: 'low' } );
  242. const viewDiv = new ViewContainerElement( 'p', null, [ 'foo', viewImg, 'bar' ] );
  243. const result = dispatcher.convert( viewDiv, { context: [ '$root', 'div' ] } );
  244. // `result` is a model document fragment.
  245. expect( result.childCount ).to.equal( 3 );
  246. expect( result.getChild( 0 ).is( 'text' ) ).to.be.true;
  247. expect( result.getChild( 1 ).is( 'image' ) ).to.be.true;
  248. expect( result.getChild( 2 ).is( 'text' ) ).to.be.true;
  249. } );
  250. } );
  251. } );
  252. } );