8
0

converters.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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, model, document, viewDocument;
  24. beforeEach( () => {
  25. return VirtualTestEditor.create()
  26. .then( newEditor => {
  27. editor = newEditor;
  28. model = editor.model;
  29. document = model.document;
  30. viewDocument = editor.editing.view;
  31. const schema = model.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( data ) {
  45. expect( getModelData( model, { withoutSelection: true } ) ).to.equal( data );
  46. }
  47. let dispatcher, schema, imgConverterCalled;
  48. beforeEach( () => {
  49. imgConverterCalled = false;
  50. schema = model.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( model, '<image src=""></image>' );
  107. const image = document.getRoot().getChild( 0 );
  108. model.change( writer => {
  109. writer.setAttribute( 'alt', 'foo bar', image );
  110. } );
  111. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  112. '<figure class="ck-widget image" contenteditable="false"><img alt="foo bar" src=""></img></figure>'
  113. );
  114. } );
  115. it( 'should convert removing attribute from image', () => {
  116. setModelData( model, '<image src="" alt="foo bar"></image>' );
  117. const image = document.getRoot().getChild( 0 );
  118. model.change( writer => {
  119. writer.removeAttribute( 'alt', image );
  120. } );
  121. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  122. '<figure class="ck-widget image" contenteditable="false"><img src=""></img></figure>'
  123. );
  124. } );
  125. it( 'should convert change of attribute image', () => {
  126. setModelData( model, '<image src="" alt="foo bar"></image>' );
  127. const image = document.getRoot().getChild( 0 );
  128. model.change( writer => {
  129. writer.setAttribute( 'alt', 'baz quix', image );
  130. } );
  131. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  132. '<figure class="ck-widget image" contenteditable="false"><img alt="baz quix" src=""></img></figure>'
  133. );
  134. } );
  135. it( 'should not set attribute if change was already consumed', () => {
  136. editor.editing.modelToView.on( 'attribute:alt:image', ( evt, data, consumable ) => {
  137. consumable.consume( data.item, 'attribute:alt' );
  138. }, { priority: 'high' } );
  139. setModelData( model, '<image src="" alt="foo bar"></image>' );
  140. expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
  141. '<figure class="ck-widget image" contenteditable="false"><img src=""></img></figure>'
  142. );
  143. } );
  144. } );
  145. // More integration tests are in imageengine.js.
  146. describe( 'autohoist converters', () => {
  147. let dispatcher, schema, imgConverterCalled, viewImg, modelDiv, modelParagraph, modelLimit;
  148. beforeEach( () => {
  149. imgConverterCalled = false;
  150. schema = model.schema;
  151. dispatcher = editor.data.viewToModel;
  152. dispatcher.on( 'element:img', ( evt, data, consumable ) => {
  153. if ( !schema.check( { name: 'image', attributes: [ 'src' ], inside: data.context } ) ) {
  154. return;
  155. }
  156. if ( consumable.consume( data.input, { name: true, attribute: 'src' } ) ) {
  157. data.output = new ModelElement( 'image', { src: data.input.getAttribute( 'src' ) } );
  158. imgConverterCalled = true;
  159. }
  160. } );
  161. // Make sure to pass document element to the converter and fire this callback after "normal" image callback.
  162. dispatcher.on( 'element:img', convertHoistableImage, { priority: 'low' } );
  163. viewImg = new ViewEmptyElement( 'img', { src: 'foo.jpg' } );
  164. modelDiv = new ModelElement( 'div' );
  165. modelParagraph = new ModelElement( 'paragraph' );
  166. modelLimit = new ModelElement( 'limit' );
  167. } );
  168. describe( 'convertHoistableImage', () => {
  169. it( 'should convert img element using already added converters if it is allowed in any point of given context #1', () => {
  170. const result = dispatcher.convert( viewImg, { context: [ '$root', 'div', 'paragraph' ] } );
  171. // `result` is a model document fragment.
  172. expect( result.childCount ).to.equal( 1 );
  173. expect( result.getChild( 0 ).is( 'image' ) ).to.be.true;
  174. expect( result.getChild( 0 ).getAttribute( 'src' ) ).to.equal( 'foo.jpg' );
  175. expect( imgConverterCalled ).to.be.true;
  176. } );
  177. it( 'should convert img element using already added converters if it is allowed in any point of given context #2', () => {
  178. const result = dispatcher.convert( viewImg, { context: [ '$root', modelDiv, modelParagraph ] } );
  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 not convert img element if there is no allowed context #1', () => {
  186. const result = dispatcher.convert( viewImg, { context: [ 'div', 'paragraph' ] } );
  187. // `result` is an empty model document fragment.
  188. expect( result.childCount ).to.equal( 0 );
  189. expect( imgConverterCalled ).to.be.false;
  190. } );
  191. it( 'should not convert img element if there is no allowed context #2', () => {
  192. const result = dispatcher.convert( viewImg, { context: [ modelDiv, modelParagraph ] } );
  193. // `result` is an empty model document fragment.
  194. expect( result.childCount ).to.equal( 0 );
  195. expect( imgConverterCalled ).to.be.false;
  196. } );
  197. it( 'should not convert img element if allowed context is "above" limiting element #1', () => {
  198. schema.limits.add( 'limit' );
  199. const result = dispatcher.convert( viewImg, { context: [ '$root', 'limit', 'div', 'paragraph' ] } );
  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 #2', () => {
  205. schema.limits.add( 'limit' );
  206. const result = dispatcher.convert( viewImg, { context: [ '$root', modelLimit, modelDiv, modelParagraph ] } );
  207. // `result` is an empty model document fragment.
  208. expect( result.childCount ).to.equal( 0 );
  209. expect( imgConverterCalled ).to.be.false;
  210. } );
  211. } );
  212. describe( 'hoistImageThroughElement', () => {
  213. it( 'should hoist img element that was converted by convertHoistableImage', () => {
  214. schema.registerItem( 'div', '$block' );
  215. buildModelConverter().for( editor.data.modelToView, editor.editing.modelToView ).fromElement( 'div' ).toElement( 'div' );
  216. buildViewConverter().for( editor.data.viewToModel ).fromElement( 'div' ).toElement( 'div' );
  217. // Make sure to fire this callback after "normal" div callback.
  218. dispatcher.on( 'element:div', hoistImageThroughElement, { priority: 'low' } );
  219. // If img view element is converted it must have been converted thanks to convertHoistableImage,
  220. // because image is not allowed in div.
  221. const viewDiv = new ViewContainerElement( 'div', null, [ 'foo', viewImg, 'bar' ] );
  222. const result = dispatcher.convert( viewDiv, { context: [ '$root' ] } );
  223. // `result` is a model document fragment.
  224. expect( result.childCount ).to.equal( 3 );
  225. expect( result.getChild( 0 ).is( 'div' ) ).to.be.true;
  226. expect( result.getChild( 1 ).is( 'image' ) ).to.be.true;
  227. expect( result.getChild( 2 ).is( 'div' ) ).to.be.true;
  228. } );
  229. it( 'should work fine with elements that are converted to document fragments', () => {
  230. // The example here is <p> that contains some text and an <img> and is converted in $root > div context.
  231. // This way we enable autohoisting for <img> and test how it will work when <p> is converted to model document fragment.
  232. schema.registerItem( 'div', '$block' );
  233. dispatcher.on( 'element:p', convertToModelFragment() );
  234. dispatcher.on( 'element:p', hoistImageThroughElement, { priority: 'low' } );
  235. const viewDiv = new ViewContainerElement( 'p', null, [ 'foo', viewImg, 'bar' ] );
  236. const result = dispatcher.convert( viewDiv, { context: [ '$root', 'div' ] } );
  237. // `result` is a model document fragment.
  238. expect( result.childCount ).to.equal( 3 );
  239. expect( result.getChild( 0 ).is( 'text' ) ).to.be.true;
  240. expect( result.getChild( 1 ).is( 'image' ) ).to.be.true;
  241. expect( result.getChild( 2 ).is( 'text' ) ).to.be.true;
  242. } );
  243. } );
  244. } );
  245. } );