8
0

converters.js 13 KB

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