8
0

converters.js 13 KB

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