linkimageediting.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  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 attach a link indicator to the image element', () => {
  40. setModelData( model, '<image src="/assets/sample.png" alt="alt text" linkHref="foo"></image>' );
  41. expect( getViewData( view, { withoutSelection: true, renderUIElements: true } ) ).to.match( new RegExp(
  42. '<figure class="ck-widget image" contenteditable="false">' +
  43. '<a href="foo">' +
  44. '<img alt="alt text" src="/assets/sample.png"></img>' +
  45. '<span class="ck ck-link-image_icon">' +
  46. '<svg[^>]+>.*<\\/svg>' +
  47. '</span>' +
  48. '</a>' +
  49. '</figure>'
  50. ) );
  51. } );
  52. } );
  53. describe( 'model to data', () => {
  54. it( 'should convert an image with a link', () => {
  55. setModelData( model, '<image src="/assets/sample.png" alt="alt text" linkHref="http://ckeditor.com"></image>' );
  56. expect( editor.getData() ).to.equal(
  57. '<figure class="image"><a href="http://ckeditor.com"><img alt="alt text" src="/assets/sample.png"></a></figure>'
  58. );
  59. } );
  60. it( 'should convert an image with a link and without alt attribute', () => {
  61. setModelData( model, '<image src="/assets/sample.png" linkHref="http://ckeditor.com"></image>' );
  62. expect( editor.getData() ).to.equal(
  63. '<figure class="image"><a href="http://ckeditor.com"><img src="/assets/sample.png"></a></figure>'
  64. );
  65. } );
  66. it( 'should convert srcset attribute to srcset and sizes attribute wrapped into a link', () => {
  67. setModelData( model,
  68. '<image src="/assets/sample.png" ' +
  69. 'linkHref="http://ckeditor.com" ' +
  70. 'srcset=\'{ "data": "small.png 148w, big.png 1024w" }\'>' +
  71. '</image>'
  72. );
  73. expect( normalizeHtml( editor.getData() ) ).to.equal(
  74. '<figure class="image">' +
  75. '<a href="http://ckeditor.com">' +
  76. '<img sizes="100vw" src="/assets/sample.png" srcset="small.png 148w, big.png 1024w"></img>' +
  77. '</a>' +
  78. '</figure>'
  79. );
  80. } );
  81. } );
  82. describe( 'view to model', () => {
  83. describe( 'figure > a > img', () => {
  84. it( 'should convert a link in an image figure', () => {
  85. editor.setData(
  86. '<figure class="image"><a href="http://ckeditor.com"><img src="/assets/sample.png" alt="alt text" /></a></figure>'
  87. );
  88. expect( getModelData( model, { withoutSelection: true } ) )
  89. .to.equal( '<image alt="alt text" linkHref="http://ckeditor.com" src="/assets/sample.png"></image>' );
  90. } );
  91. it( 'should convert an image with a link and without alt attribute', () => {
  92. editor.setData( '<figure class="image"><a href="http://ckeditor.com"><img src="/assets/sample.png" /></a></figure>' );
  93. expect( getModelData( model, { withoutSelection: true } ) )
  94. .to.equal( '<image linkHref="http://ckeditor.com" src="/assets/sample.png"></image>' );
  95. } );
  96. it( 'should not convert without src attribute', () => {
  97. editor.setData( '<figure class="image"><a href="http://ckeditor.com"><img alt="alt text" /></a></figure>' );
  98. expect( getModelData( model, { withoutSelection: true } ) )
  99. .to.equal( '<paragraph></paragraph>' );
  100. } );
  101. it( 'should not convert in wrong context', () => {
  102. model.schema.register( 'div', { inheritAllFrom: '$block' } );
  103. model.schema.addChildCheck( ( ctx, childDef ) => {
  104. if ( ctx.endsWith( '$root' ) && childDef.name == 'image' ) {
  105. return false;
  106. }
  107. } );
  108. editor.conversion.elementToElement( { model: 'div', view: 'div' } );
  109. editor.setData(
  110. '<div>' +
  111. '<figure class="image">' +
  112. '<a href="http://ckeditor.com">' +
  113. '<img src="/assets/sample.png" alt="alt text" />' +
  114. '</a>' +
  115. '</figure>' +
  116. '</div>' );
  117. expect( getModelData( model, { withoutSelection: true } ) )
  118. .to.equal( '<div></div>' );
  119. } );
  120. it( 'should not convert "a" element if is already consumed', () => {
  121. editor.data.upcastDispatcher.on( 'element:a', ( evt, data, conversionApi ) => {
  122. conversionApi.consumable.consume( data.viewItem, { attributes: [ 'href' ] } );
  123. }, { priority: 'highest' } );
  124. editor.setData(
  125. '<figure class="image"><a href="http://ckeditor.com"><img src="/assets/sample.png" alt="alt text" /></a></figure>'
  126. );
  127. expect( editor.getData() ).to.equal( '<figure class="image"><img src="/assets/sample.png" alt="alt text"></figure>' );
  128. } );
  129. it( 'should not convert if a link misses "href" attribute', () => {
  130. editor.setData(
  131. '<figure class="image"><a href=""><img src="/assets/sample.png" alt="alt text" /></a></figure>'
  132. );
  133. expect( getModelData( model, { withoutSelection: true } ) )
  134. .to.equal( '<image alt="alt text" src="/assets/sample.png"></image>' );
  135. } );
  136. it( 'should convert a link without an image to a paragraph with the link', () => {
  137. editor.setData(
  138. '<figure class="image"><a href="http://ckeditor.com">Foo</a></figure>'
  139. );
  140. expect( getModelData( model, { withoutSelection: true } ) )
  141. .to.equal( '<paragraph><$text linkHref="http://ckeditor.com">Foo</$text></paragraph>' );
  142. } );
  143. } );
  144. describe( 'a > img', () => {
  145. it( 'should convert a link in an image figure', () => {
  146. editor.setData(
  147. '<a href="http://ckeditor.com"><img src="/assets/sample.png" alt="alt text" /></a>'
  148. );
  149. expect( getModelData( model, { withoutSelection: true } ) )
  150. .to.equal( '<image alt="alt text" linkHref="http://ckeditor.com" src="/assets/sample.png"></image>' );
  151. } );
  152. it( 'should convert an image with a link and without alt attribute', () => {
  153. editor.setData( '<a href="http://ckeditor.com"><img src="/assets/sample.png" /></a>' );
  154. expect( getModelData( model, { withoutSelection: true } ) )
  155. .to.equal( '<image linkHref="http://ckeditor.com" src="/assets/sample.png"></image>' );
  156. } );
  157. it( 'should not convert without src attribute', () => {
  158. editor.setData( '<a href="http://ckeditor.com"><img alt="alt text" /></a>' );
  159. expect( getModelData( model, { withoutSelection: true } ) )
  160. .to.equal( '<paragraph></paragraph>' );
  161. } );
  162. it( 'should not convert in wrong context', () => {
  163. model.schema.register( 'div', { inheritAllFrom: '$block' } );
  164. model.schema.addChildCheck( ( ctx, childDef ) => {
  165. if ( ctx.endsWith( '$root' ) && childDef.name == 'image' ) {
  166. return false;
  167. }
  168. } );
  169. editor.conversion.elementToElement( { model: 'div', view: 'div' } );
  170. editor.setData(
  171. '<div>' +
  172. '<a href="http://ckeditor.com">' +
  173. '<img src="/assets/sample.png" alt="alt text" />' +
  174. '</a>' +
  175. '</div>' );
  176. expect( getModelData( model, { withoutSelection: true } ) )
  177. .to.equal( '<div></div>' );
  178. } );
  179. it( 'should not convert "a" element if is already consumed', () => {
  180. editor.data.upcastDispatcher.on( 'element:a', ( evt, data, conversionApi ) => {
  181. conversionApi.consumable.consume( data.viewItem, { attributes: [ 'href' ] } );
  182. }, { priority: 'highest' } );
  183. editor.setData(
  184. '<a href="http://ckeditor.com"><img src="/assets/sample.png" alt="alt text" /></a>'
  185. );
  186. expect( editor.getData() ).to.equal( '<figure class="image"><img src="/assets/sample.png" alt="alt text"></figure>' );
  187. } );
  188. it( 'should not convert if a link misses "href" attribute', () => {
  189. editor.setData(
  190. '<a href=""><img src="/assets/sample.png" alt="alt text" /></a>'
  191. );
  192. expect( getModelData( model, { withoutSelection: true } ) )
  193. .to.equal( '<image alt="alt text" src="/assets/sample.png"></image>' );
  194. } );
  195. } );
  196. describe( 'figure > a > img + figcaption', () => {
  197. it( 'should convert a link and the caption element', () => {
  198. return VirtualTestEditor
  199. .create( {
  200. plugins: [ Paragraph, LinkImageEditing, ImageCaptionEditing ]
  201. } )
  202. .then( editor => {
  203. editor.setData(
  204. '<figure class="image">' +
  205. '<a href="http://ckeditor.com">' +
  206. '<img src="/assets/sample.png" alt="alt text" />' +
  207. '</a>' +
  208. '<figcaption>' +
  209. 'Foo Bar.' +
  210. '</figcaption>' +
  211. '</figure>'
  212. );
  213. expect( getModelData( editor.model, { withoutSelection: true } ) ).to.equal(
  214. '<image alt="alt text" linkHref="http://ckeditor.com" src="/assets/sample.png">' +
  215. '<caption>Foo Bar.</caption>' +
  216. '</image>'
  217. );
  218. return editor.destroy();
  219. } );
  220. } );
  221. } );
  222. } );
  223. } );
  224. describe( 'conversion in editing pipeline', () => {
  225. describe( 'model to view', () => {
  226. it( 'should convert the image element', () => {
  227. setModelData( model, '<image linkHref="http://ckeditor.com" src="/assets/sample.png" alt="alt text"></image>' );
  228. expect( getViewData( view, { withoutSelection: true } ) ).to.equal(
  229. '<figure class="ck-widget image" contenteditable="false">' +
  230. '<a href="http://ckeditor.com">' +
  231. '<img alt="alt text" src="/assets/sample.png"></img>' +
  232. // Content of the UIElement is skipped here.
  233. '<span class="ck ck-link-image_icon"></span>' +
  234. '</a>' +
  235. '</figure>'
  236. );
  237. } );
  238. it( 'should convert attribute change', () => {
  239. setModelData( model, '<image linkHref="http://ckeditor.com" src="/assets/sample.png" alt="alt text"></image>' );
  240. const image = model.document.getRoot().getChild( 0 );
  241. model.change( writer => {
  242. writer.setAttribute( 'linkHref', 'https://ckeditor.com/why-ckeditor/', image );
  243. } );
  244. expect( getViewData( view, { withoutSelection: true } ) ).to.equal(
  245. '<figure class="ck-widget image" contenteditable="false">' +
  246. '<a href="https://ckeditor.com/why-ckeditor/">' +
  247. '<img alt="alt text" src="/assets/sample.png"></img>' +
  248. // Content of the UIElement is skipped here.
  249. '<span class="ck ck-link-image_icon"></span>' +
  250. '</a>' +
  251. '</figure>'
  252. );
  253. } );
  254. it( 'should convert attribute removal', () => {
  255. setModelData( model, '<image linkHref="http://ckeditor.com" src="/assets/sample.png" alt="alt text"></image>' );
  256. const image = model.document.getRoot().getChild( 0 );
  257. model.change( writer => {
  258. writer.removeAttribute( 'linkHref', image );
  259. } );
  260. expect( getViewData( view, { withoutSelection: true } ) ).to.equal(
  261. '<figure class="ck-widget image" contenteditable="false">' +
  262. '<img alt="alt text" src="/assets/sample.png"></img>' +
  263. '</figure>'
  264. );
  265. } );
  266. } );
  267. describe( 'figure > a > img + span + figcaption', () => {
  268. it( 'should convert a link and the caption element', () => {
  269. return VirtualTestEditor
  270. .create( {
  271. plugins: [ Paragraph, LinkImageEditing, ImageCaptionEditing ]
  272. } )
  273. .then( editor => {
  274. setModelData( editor.model,
  275. '<image linkHref="http://ckeditor.com" src="/assets/sample.png" alt="alt text">' +
  276. '<caption>Foo Bar.</caption>' +
  277. '</image>'
  278. );
  279. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal(
  280. '<figure class="ck-widget image" contenteditable="false">' +
  281. '<a href="http://ckeditor.com">' +
  282. '<img alt="alt text" src="/assets/sample.png"></img>' +
  283. // Content of the UIElement is skipped here.
  284. '<span class="ck ck-link-image_icon"></span>' +
  285. '</a>' +
  286. '<figcaption class="ck-editor__editable ck-editor__nested-editable" ' +
  287. 'contenteditable="true" data-placeholder="Enter image caption">' +
  288. 'Foo Bar.' +
  289. '</figcaption>' +
  290. '</figure>'
  291. );
  292. return editor.destroy();
  293. } );
  294. } );
  295. } );
  296. } );
  297. describe( 'link attributes decorator', () => {
  298. describe( 'default behavior', () => {
  299. const testLinks = [
  300. {
  301. external: true,
  302. url: 'http://example.com'
  303. }, {
  304. external: true,
  305. url: 'https://cksource.com'
  306. }, {
  307. external: false,
  308. url: 'ftp://server.io'
  309. }, {
  310. external: true,
  311. url: '//schemaless.org'
  312. }, {
  313. external: false,
  314. url: 'www.ckeditor.com'
  315. }, {
  316. external: false,
  317. url: '/relative/url.html'
  318. }, {
  319. external: false,
  320. url: 'another/relative/url.html'
  321. }, {
  322. external: false,
  323. url: '#anchor'
  324. }, {
  325. external: false,
  326. url: 'mailto:some@user.org'
  327. }, {
  328. external: false,
  329. url: 'tel:123456789'
  330. }
  331. ];
  332. describe( 'for link.addTargetToExternalLinks=false', () => {
  333. let editor, model;
  334. beforeEach( async () => {
  335. editor = await VirtualTestEditor.create( {
  336. plugins: [ Paragraph, LinkImageEditing ],
  337. link: {
  338. addTargetToExternalLinks: false
  339. }
  340. } );
  341. model = editor.model;
  342. view = editor.editing.view;
  343. } );
  344. afterEach( async () => {
  345. await editor.destroy();
  346. } );
  347. testLinks.forEach( link => {
  348. it( `link: ${ link.url } should not get 'target' and 'rel' attributes`, () => {
  349. // Upcast check.
  350. editor.setData(
  351. '<figure class="image">' +
  352. `<a href="${ link.url }" target="_blank" rel="noopener noreferrer">` +
  353. '<img src="/assets/sample.png">' +
  354. '</a>' +
  355. '</figure>'
  356. );
  357. expect( getModelData( model, { withoutSelection: true } ) )
  358. .to.equal( `<image linkHref="${ link.url }" src="/assets/sample.png"></image>` );
  359. // Downcast check.
  360. expect( editor.getData() ).to.equal(
  361. '<figure class="image">' +
  362. `<a href="${ link.url }">` +
  363. '<img src="/assets/sample.png">' +
  364. '</a>' +
  365. '</figure>'
  366. );
  367. } );
  368. } );
  369. } );
  370. describe( 'for link.addTargetToExternalLinks=true', () => {
  371. let editor, model;
  372. beforeEach( async () => {
  373. editor = await VirtualTestEditor.create( {
  374. plugins: [ Paragraph, LinkImageEditing ],
  375. link: {
  376. addTargetToExternalLinks: true
  377. }
  378. } );
  379. model = editor.model;
  380. view = editor.editing.view;
  381. } );
  382. afterEach( async () => {
  383. await editor.destroy();
  384. } );
  385. testLinks.forEach( link => {
  386. it( `link: ${ link.url } should be treat as ${ link.external ? 'external' : 'non-external' } link`, () => {
  387. // Upcast check.
  388. editor.setData(
  389. `<a href="${ link.url }" target="_blank" rel="noopener noreferrer"><img src="/assets/sample.png"></a>`
  390. );
  391. expect( getModelData( model, { withoutSelection: true } ) )
  392. .to.equal( `<image linkHref="${ link.url }" src="/assets/sample.png"></image>` );
  393. // Downcast check.
  394. if ( link.external ) {
  395. expect( editor.getData() ).to.equal(
  396. '<figure class="image">' +
  397. `<a href="${ link.url }" target="_blank" rel="noopener noreferrer">` +
  398. '<img src="/assets/sample.png">' +
  399. '</a>' +
  400. '</figure>'
  401. );
  402. } else {
  403. expect( editor.getData() ).to.equal(
  404. '<figure class="image">' +
  405. `<a href="${ link.url }">` +
  406. '<img src="/assets/sample.png">' +
  407. '</a>' +
  408. '</figure>'
  409. );
  410. }
  411. } );
  412. } );
  413. } );
  414. } );
  415. describe( 'custom config', () => {
  416. describe( 'mode: automatic', () => {
  417. let editor;
  418. const testLinks = [
  419. {
  420. url: 'relative/url.html',
  421. attributes: {}
  422. }, {
  423. url: 'http://exmaple.com',
  424. attributes: {
  425. target: '_blank'
  426. }
  427. }, {
  428. url: 'https://example.com/download/link.pdf',
  429. attributes: {
  430. target: '_blank',
  431. download: 'download'
  432. }
  433. }, {
  434. url: 'mailto:some@person.io',
  435. attributes: {
  436. class: 'mail-url'
  437. }
  438. }
  439. ];
  440. beforeEach( async () => {
  441. editor = await VirtualTestEditor.create( {
  442. plugins: [ Paragraph, LinkImageEditing ],
  443. link: {
  444. addTargetToExternalLinks: false,
  445. decorators: {
  446. isExternal: {
  447. mode: 'automatic',
  448. callback: url => url.startsWith( 'http' ),
  449. attributes: {
  450. target: '_blank'
  451. }
  452. },
  453. isDownloadable: {
  454. mode: 'automatic',
  455. callback: url => url.includes( 'download' ),
  456. attributes: {
  457. download: 'download'
  458. }
  459. },
  460. isMail: {
  461. mode: 'automatic',
  462. callback: url => url.startsWith( 'mailto:' ),
  463. attributes: {
  464. class: 'mail-url'
  465. }
  466. }
  467. }
  468. }
  469. } );
  470. model = editor.model;
  471. } );
  472. afterEach( () => {
  473. return editor.destroy();
  474. } );
  475. testLinks.forEach( link => {
  476. it( `Link: ${ link.url } should get attributes: ${ JSON.stringify( link.attributes ) }`, () => {
  477. const ORDER = [ 'class', 'href', 'target', 'download' ];
  478. const attributes = Object.assign( {}, link.attributes, {
  479. href: link.url
  480. } );
  481. const attr = Object.entries( attributes ).sort( ( a, b ) => {
  482. const aIndex = ORDER.indexOf( a[ 0 ] );
  483. const bIndex = ORDER.indexOf( b[ 0 ] );
  484. return aIndex - bIndex;
  485. } );
  486. const reducedAttr = attr.reduce( ( acc, cur ) => {
  487. return acc + `${ cur[ 0 ] }="${ cur[ 1 ] }" `;
  488. }, '' ).trim();
  489. editor.setData( `<a href="${ link.url }"><img src="/assets/sample.png"></a>` );
  490. expect( getModelData( model, { withoutSelection: true } ) )
  491. .to.equal( `<image linkHref="${ link.url }" src="/assets/sample.png"></image>` );
  492. // Order of attributes is important, that's why this is assert is construct in such way.
  493. expect( editor.getData() ).to.equal(
  494. '<figure class="image">' +
  495. `<a ${ reducedAttr }>` +
  496. '<img src="/assets/sample.png">' +
  497. '</a>' +
  498. '</figure>'
  499. );
  500. } );
  501. } );
  502. } );
  503. } );
  504. describe( 'upcast converter', () => {
  505. let editor, model;
  506. beforeEach( () => {
  507. return VirtualTestEditor
  508. .create( {
  509. plugins: [ Paragraph, LinkImageEditing ],
  510. link: {
  511. decorators: {
  512. isExternal: {
  513. mode: 'manual',
  514. label: 'Open in a new tab',
  515. attributes: {
  516. target: '_blank',
  517. rel: 'noopener noreferrer'
  518. }
  519. },
  520. isDownloadable: {
  521. mode: 'manual',
  522. label: 'Downloadable',
  523. attributes: {
  524. download: 'download'
  525. }
  526. },
  527. isGallery: {
  528. mode: 'manual',
  529. label: 'Gallery link',
  530. attributes: {
  531. class: 'gallery'
  532. }
  533. }
  534. }
  535. }
  536. } )
  537. .then( newEditor => {
  538. editor = newEditor;
  539. model = editor.model;
  540. } );
  541. } );
  542. afterEach( () => {
  543. return editor.destroy();
  544. } );
  545. it( 'should upcast attributes', async () => {
  546. editor.setData(
  547. '<figure class="image">' +
  548. '<a href="url" target="_blank" rel="noopener noreferrer" download="download">' +
  549. '<img src="/assets/sample.png">' +
  550. '</a>' +
  551. '</figure>'
  552. );
  553. expect( getModelData( model, { withoutSelection: true } ) ).to.equal(
  554. '<image linkHref="url" linkIsDownloadable="true" linkIsExternal="true" src="/assets/sample.png"></image>'
  555. );
  556. await editor.destroy();
  557. } );
  558. it( 'should not upcast partial and incorrect attributes', async () => {
  559. editor.setData(
  560. '<figure class="image">' +
  561. '<a href="url" target="_blank" rel="noopener noreferrer" download="something">' +
  562. '<img src="/assets/sample.png">' +
  563. '</a>' +
  564. '</figure>'
  565. );
  566. expect( getModelData( model, { withoutSelection: true } ) ).to.equal(
  567. '<image linkHref="url" linkIsExternal="true" src="/assets/sample.png"></image>'
  568. );
  569. } );
  570. it( 'allows overwriting conversion process using highest priority', () => {
  571. // Block manual decorator converter. Consume all attributes and do nothing with them.
  572. editor.conversion.for( 'upcast' ).add( dispatcher => {
  573. dispatcher.on( 'element:a', ( evt, data, conversionApi ) => {
  574. const consumableAttributes = {
  575. attributes: [ 'target', 'rel', 'download' ]
  576. };
  577. conversionApi.consumable.consume( data.viewItem, consumableAttributes );
  578. }, { priority: 'highest' } );
  579. } );
  580. editor.setData(
  581. '<figure class="image">' +
  582. '<a href="url" target="_blank" rel="noopener noreferrer" download="something">' +
  583. '<img src="/assets/sample.png">' +
  584. '</a>' +
  585. '</figure>'
  586. );
  587. expect( editor.getData() ).to.equal( '<figure class="image"><a href="url"><img src="/assets/sample.png"></a></figure>' );
  588. } );
  589. it( 'should upcast the decorators when linked image (figure > a > img)', () => {
  590. // (#7975)
  591. editor.setData(
  592. '<figure class="image">' +
  593. '<a class="gallery" href="https://cksource.com" target="_blank" rel="noopener noreferrer" download="download">' +
  594. '<img src="sample.jpg" alt="bar">' +
  595. '</a>' +
  596. '<figcaption>Caption</figcaption>' +
  597. '</figure>' +
  598. '<p>' +
  599. '<a href="https://cksource.com" target="_blank" rel="noopener noreferrer" download="download">' +
  600. 'https://cksource.com' +
  601. '</a>' +
  602. '</p>'
  603. );
  604. expect( getModelData( model, { withoutSelection: true } ) ).to.equal(
  605. '<image alt="bar" ' +
  606. 'linkHref="https://cksource.com" ' +
  607. 'linkIsDownloadable="true" ' +
  608. 'linkIsExternal="true" ' +
  609. 'linkIsGallery="true" ' +
  610. 'src="sample.jpg">' +
  611. '</image>' +
  612. '<paragraph>' +
  613. '<$text linkHref="https://cksource.com" linkIsDownloadable="true" linkIsExternal="true">' +
  614. 'https://cksource.com' +
  615. '</$text>' +
  616. '</paragraph>'
  617. );
  618. } );
  619. it( 'should upcast the decorators when linked image (a > img)', () => {
  620. // (#7975)
  621. editor.setData(
  622. '<a class="gallery" href="https://cksource.com" target="_blank" rel="noopener noreferrer" download="download">' +
  623. '<img src="sample.jpg" alt="bar">' +
  624. '</a>' +
  625. '<p>' +
  626. '<a href="https://cksource.com" target="_blank" rel="noopener noreferrer" download="download">' +
  627. 'https://cksource.com' +
  628. '</a>' +
  629. '</p>'
  630. );
  631. expect( getModelData( model, { withoutSelection: true } ) ).to.equal(
  632. '<image alt="bar" ' +
  633. 'linkHref="https://cksource.com" ' +
  634. 'linkIsDownloadable="true" ' +
  635. 'linkIsExternal="true" ' +
  636. 'linkIsGallery="true" ' +
  637. 'src="sample.jpg">' +
  638. '</image>' +
  639. '<paragraph>' +
  640. '<$text linkHref="https://cksource.com" linkIsDownloadable="true" linkIsExternal="true">' +
  641. 'https://cksource.com' +
  642. '</$text>' +
  643. '</paragraph>'
  644. );
  645. } );
  646. } );
  647. describe( 'downcast converter', () => {
  648. let editor, model;
  649. beforeEach( () => {
  650. return VirtualTestEditor
  651. .create( {
  652. plugins: [ Paragraph, LinkImageEditing ],
  653. link: {
  654. decorators: {
  655. isExternal: {
  656. mode: 'manual',
  657. label: 'Open in a new tab',
  658. attributes: {
  659. target: '_blank',
  660. rel: 'noopener noreferrer'
  661. }
  662. },
  663. isDownloadable: {
  664. mode: 'manual',
  665. label: 'Downloadable',
  666. attributes: {
  667. download: 'download'
  668. }
  669. },
  670. isGallery: {
  671. mode: 'manual',
  672. label: 'Gallery link',
  673. attributes: {
  674. class: 'gallery'
  675. }
  676. }
  677. }
  678. }
  679. } )
  680. .then( newEditor => {
  681. editor = newEditor;
  682. model = editor.model;
  683. } );
  684. } );
  685. afterEach( () => {
  686. return editor.destroy();
  687. } );
  688. it( 'should downcast the decorators after applying a change', () => {
  689. setModelData( model,
  690. '[<image alt="bar" src="sample.jpg"></image>]' +
  691. '<paragraph>' +
  692. '<$text>https://cksource.com</$text>' +
  693. '</paragraph>'
  694. );
  695. editor.execute( 'link', 'https://cksource.com', {
  696. linkIsDownloadable: true,
  697. linkIsExternal: true,
  698. linkIsGallery: true
  699. } );
  700. model.change( writer => {
  701. writer.setSelection( model.document.getRoot().getChild( 1 ).getChild( 0 ), 'on' );
  702. } );
  703. editor.execute( 'link', 'https://cksource.com', {
  704. linkIsDownloadable: true,
  705. linkIsExternal: true,
  706. linkIsGallery: true
  707. } );
  708. expect( editor.getData() ).to.equal(
  709. '<figure class="image">' +
  710. '<a class="gallery" href="https://cksource.com" download="download" target="_blank" rel="noopener noreferrer">' +
  711. '<img src="sample.jpg" alt="bar">' +
  712. '</a>' +
  713. '</figure>' +
  714. '<p>' +
  715. '<a class="gallery" href="https://cksource.com" download="download" target="_blank" rel="noopener noreferrer">' +
  716. 'https://cksource.com' +
  717. '</a>' +
  718. '</p>'
  719. );
  720. } );
  721. } );
  722. } );
  723. } );