8
0

linkimageediting.js 13 KB

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