linkimageediting.js 22 KB

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