8
0

linkimageediting.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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 VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  6. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  7. import LinkImageEditing from '../src/linkimageediting';
  8. import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  9. import normalizeHtml from '@ckeditor/ckeditor5-utils/tests/_utils/normalizehtml';
  10. import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  11. import ImageCaptionEditing from '@ckeditor/ckeditor5-image/src/imagecaption/imagecaptionediting';
  12. describe( 'LinkImageEditing', () => {
  13. let editor, model, view;
  14. beforeEach( () => {
  15. return VirtualTestEditor
  16. .create( {
  17. plugins: [ Paragraph, LinkImageEditing ]
  18. } )
  19. .then( newEditor => {
  20. editor = newEditor;
  21. model = editor.model;
  22. view = editor.editing.view;
  23. } );
  24. } );
  25. afterEach( () => {
  26. return editor.destroy();
  27. } );
  28. it( 'should have pluginName', () => {
  29. expect( LinkImageEditing.pluginName ).to.equal( 'LinkImageEditing' );
  30. } );
  31. it( 'should be loaded', () => {
  32. expect( editor.plugins.get( LinkImageEditing ) ).to.be.instanceOf( LinkImageEditing );
  33. } );
  34. it( 'should set proper schema rules', () => {
  35. expect( model.schema.checkAttribute( [ '$root', 'image' ], 'linkHref' ) ).to.be.true;
  36. } );
  37. describe( 'conversion in data pipeline', () => {
  38. describe( 'model to view', () => {
  39. it( 'should convert an image with a link', () => {
  40. setModelData( model, '<image src="/assets/sample.png" alt="alt text" linkHref="http://ckeditor.com"></image>' );
  41. expect( editor.getData() ).to.equal(
  42. '<figure class="image"><a href="http://ckeditor.com"><img alt="alt text" src="/assets/sample.png"></a></figure>'
  43. );
  44. } );
  45. it( 'should convert an image with a link and without alt attribute', () => {
  46. setModelData( model, '<image src="/assets/sample.png" linkHref="http://ckeditor.com"></image>' );
  47. expect( editor.getData() ).to.equal(
  48. '<figure class="image"><a href="http://ckeditor.com"><img src="/assets/sample.png"></a></figure>'
  49. );
  50. } );
  51. it( 'should convert srcset attribute to srcset and sizes attribute wrapped into a link', () => {
  52. setModelData( model,
  53. '<image src="/assets/sample.png" ' +
  54. 'linkHref="http://ckeditor.com" ' +
  55. 'srcset=\'{ "data": "small.png 148w, big.png 1024w" }\'>' +
  56. '</image>'
  57. );
  58. expect( normalizeHtml( editor.getData() ) ).to.equal(
  59. '<figure class="image">' +
  60. '<a href="http://ckeditor.com">' +
  61. '<img sizes="100vw" src="/assets/sample.png" srcset="small.png 148w, big.png 1024w"></img>' +
  62. '</a>' +
  63. '</figure>'
  64. );
  65. } );
  66. } );
  67. describe( 'view to model', () => {
  68. describe( 'figure > a > img', () => {
  69. it( 'should convert a link in an image figure', () => {
  70. editor.setData(
  71. '<figure class="image"><a href="http://ckeditor.com"><img src="/assets/sample.png" alt="alt text" /></a></figure>'
  72. );
  73. expect( getModelData( model, { withoutSelection: true } ) )
  74. .to.equal( '<image alt="alt text" linkHref="http://ckeditor.com" src="/assets/sample.png"></image>' );
  75. } );
  76. it( 'should convert an image with a link and without alt attribute', () => {
  77. editor.setData( '<figure class="image"><a href="http://ckeditor.com"><img src="/assets/sample.png" /></a></figure>' );
  78. expect( getModelData( model, { withoutSelection: true } ) )
  79. .to.equal( '<image linkHref="http://ckeditor.com" src="/assets/sample.png"></image>' );
  80. } );
  81. it( 'should not convert without src attribute', () => {
  82. editor.setData( '<figure class="image"><a href="http://ckeditor.com"><img alt="alt text" /></a></figure>' );
  83. expect( getModelData( model, { withoutSelection: true } ) )
  84. .to.equal( '<paragraph></paragraph>' );
  85. } );
  86. it( 'should not convert in wrong context', () => {
  87. model.schema.register( 'div', { inheritAllFrom: '$block' } );
  88. model.schema.addChildCheck( ( ctx, childDef ) => {
  89. if ( ctx.endsWith( '$root' ) && childDef.name == 'image' ) {
  90. return false;
  91. }
  92. } );
  93. editor.conversion.elementToElement( { model: 'div', view: 'div' } );
  94. editor.setData(
  95. '<div>' +
  96. '<figure class="image">' +
  97. '<a href="http://ckeditor.com">' +
  98. '<img src="/assets/sample.png" alt="alt text" />' +
  99. '</a>' +
  100. '</figure>' +
  101. '</div>' );
  102. expect( getModelData( model, { withoutSelection: true } ) )
  103. .to.equal( '<div></div>' );
  104. } );
  105. it( 'should not convert "a" element if is already consumed', () => {
  106. editor.data.upcastDispatcher.on( 'element:a', ( evt, data, conversionApi ) => {
  107. conversionApi.consumable.consume( data.viewItem, { attributes: [ 'href' ] } );
  108. }, { priority: 'highest' } );
  109. editor.setData(
  110. '<figure class="image"><a href="http://ckeditor.com"><img src="/assets/sample.png" alt="alt text" /></a></figure>'
  111. );
  112. expect( editor.getData() ).to.equal( '<figure class="image"><img src="/assets/sample.png" alt="alt text"></figure>' );
  113. } );
  114. it( 'should not convert if a link misses "href" attribute', () => {
  115. editor.setData(
  116. '<figure class="image"><a href=""><img src="/assets/sample.png" alt="alt text" /></a></figure>'
  117. );
  118. expect( getModelData( model, { withoutSelection: true } ) )
  119. .to.equal( '<image alt="alt text" src="/assets/sample.png"></image>' );
  120. } );
  121. it( 'should convert a link without an image to a paragraph with the link', () => {
  122. editor.setData(
  123. '<figure class="image"><a href="http://ckeditor.com">Foo</a></figure>'
  124. );
  125. expect( getModelData( model, { withoutSelection: true } ) )
  126. .to.equal( '<paragraph><$text linkHref="http://ckeditor.com">Foo</$text></paragraph>' );
  127. } );
  128. } );
  129. describe( 'a > img', () => {
  130. it( 'should convert a link in an image figure', () => {
  131. editor.setData(
  132. '<a href="http://ckeditor.com"><img src="/assets/sample.png" alt="alt text" /></a>'
  133. );
  134. expect( getModelData( model, { withoutSelection: true } ) )
  135. .to.equal( '<image alt="alt text" linkHref="http://ckeditor.com" src="/assets/sample.png"></image>' );
  136. } );
  137. it( 'should convert an image with a link and without alt attribute', () => {
  138. editor.setData( '<a href="http://ckeditor.com"><img src="/assets/sample.png" /></a>' );
  139. expect( getModelData( model, { withoutSelection: true } ) )
  140. .to.equal( '<image linkHref="http://ckeditor.com" src="/assets/sample.png"></image>' );
  141. } );
  142. it( 'should not convert without src attribute', () => {
  143. editor.setData( '<a href="http://ckeditor.com"><img alt="alt text" /></a>' );
  144. expect( getModelData( model, { withoutSelection: true } ) )
  145. .to.equal( '<paragraph></paragraph>' );
  146. } );
  147. it( 'should not convert in wrong context', () => {
  148. model.schema.register( 'div', { inheritAllFrom: '$block' } );
  149. model.schema.addChildCheck( ( ctx, childDef ) => {
  150. if ( ctx.endsWith( '$root' ) && childDef.name == 'image' ) {
  151. return false;
  152. }
  153. } );
  154. editor.conversion.elementToElement( { model: 'div', view: 'div' } );
  155. editor.setData(
  156. '<div>' +
  157. '<a href="http://ckeditor.com">' +
  158. '<img src="/assets/sample.png" alt="alt text" />' +
  159. '</a>' +
  160. '</div>' );
  161. expect( getModelData( model, { withoutSelection: true } ) )
  162. .to.equal( '<div></div>' );
  163. } );
  164. it( 'should not convert "a" element if is already consumed', () => {
  165. editor.data.upcastDispatcher.on( 'element:a', ( evt, data, conversionApi ) => {
  166. conversionApi.consumable.consume( data.viewItem, { attributes: [ 'href' ] } );
  167. }, { priority: 'highest' } );
  168. editor.setData(
  169. '<a href="http://ckeditor.com"><img src="/assets/sample.png" alt="alt text" /></a>'
  170. );
  171. expect( editor.getData() ).to.equal( '<figure class="image"><img src="/assets/sample.png" alt="alt text"></figure>' );
  172. } );
  173. it( 'should not convert if a link misses "href" attribute', () => {
  174. editor.setData(
  175. '<a href=""><img src="/assets/sample.png" alt="alt text" /></a>'
  176. );
  177. expect( getModelData( model, { withoutSelection: true } ) )
  178. .to.equal( '<image alt="alt text" src="/assets/sample.png"></image>' );
  179. } );
  180. } );
  181. describe( 'figure > a > img + figcaption', () => {
  182. it( 'should convert a link and the caption element', () => {
  183. return VirtualTestEditor
  184. .create( {
  185. plugins: [ Paragraph, LinkImageEditing, ImageCaptionEditing ]
  186. } )
  187. .then( editor => {
  188. editor.setData(
  189. '<figure class="image">' +
  190. '<a href="http://ckeditor.com">' +
  191. '<img src="/assets/sample.png" alt="alt text" />' +
  192. '</a>' +
  193. '<figcaption>' +
  194. 'Foo Bar.' +
  195. '</figcaption>' +
  196. '</figure>'
  197. );
  198. expect( getModelData( editor.model, { withoutSelection: true } ) ).to.equal(
  199. '<image alt="alt text" linkHref="http://ckeditor.com" src="/assets/sample.png">' +
  200. '<caption>Foo Bar.</caption>' +
  201. '</image>'
  202. );
  203. return editor.destroy();
  204. } );
  205. } );
  206. } );
  207. } );
  208. } );
  209. describe( 'conversion in editing pipeline', () => {
  210. describe( 'model to view', () => {
  211. it( 'should convert the image element', () => {
  212. setModelData( model, '<image linkHref="http://ckeditor.com" src="/assets/sample.png" alt="alt text"></image>' );
  213. expect( getViewData( view, { withoutSelection: true } ) ).to.equal(
  214. '<figure class="ck-widget image" contenteditable="false">' +
  215. '<a href="http://ckeditor.com">' +
  216. '<img alt="alt text" src="/assets/sample.png"></img>' +
  217. '</a>' +
  218. '</figure>'
  219. );
  220. } );
  221. it( 'should convert attribute change', () => {
  222. setModelData( model, '<image linkHref="http://ckeditor.com" src="/assets/sample.png" alt="alt text"></image>' );
  223. const image = model.document.getRoot().getChild( 0 );
  224. model.change( writer => {
  225. writer.setAttribute( 'linkHref', 'https://ckeditor.com/why-ckeditor/', image );
  226. } );
  227. expect( getViewData( view, { withoutSelection: true } ) ).to.equal(
  228. '<figure class="ck-widget image" contenteditable="false">' +
  229. '<a href="https://ckeditor.com/why-ckeditor/">' +
  230. '<img alt="alt text" src="/assets/sample.png"></img>' +
  231. '</a>' +
  232. '</figure>'
  233. );
  234. } );
  235. it( 'should convert attribute removal', () => {
  236. setModelData( model, '<image linkHref="http://ckeditor.com" src="/assets/sample.png" alt="alt text"></image>' );
  237. const image = model.document.getRoot().getChild( 0 );
  238. model.change( writer => {
  239. writer.removeAttribute( 'linkHref', image );
  240. } );
  241. expect( getViewData( view, { withoutSelection: true } ) ).to.equal(
  242. '<figure class="ck-widget image" contenteditable="false">' +
  243. '<img alt="alt text" src="/assets/sample.png"></img>' +
  244. '</figure>'
  245. );
  246. } );
  247. } );
  248. describe( 'figure > a > img + figcaption', () => {
  249. it( 'should convert a link and the caption element', () => {
  250. return VirtualTestEditor
  251. .create( {
  252. plugins: [ Paragraph, LinkImageEditing, ImageCaptionEditing ]
  253. } )
  254. .then( editor => {
  255. setModelData( editor.model,
  256. '<image linkHref="http://ckeditor.com" src="/assets/sample.png" alt="alt text">' +
  257. '<caption>Foo Bar.</caption>' +
  258. '</image>'
  259. );
  260. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal(
  261. '<figure class="ck-widget image" contenteditable="false">' +
  262. '<a href="http://ckeditor.com">' +
  263. '<img alt="alt text" src="/assets/sample.png"></img>' +
  264. '</a>' +
  265. '<figcaption class="ck-editor__editable ck-editor__nested-editable" ' +
  266. 'contenteditable="true" data-placeholder="Enter image caption">' +
  267. 'Foo Bar.' +
  268. '</figcaption>' +
  269. '</figure>'
  270. );
  271. return editor.destroy();
  272. } );
  273. } );
  274. } );
  275. } );
  276. } );