linkediting.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import LinkEditing from '../src/linkediting';
  6. import LinkCommand from '../src/linkcommand';
  7. import UnlinkCommand from '../src/unlinkcommand';
  8. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  9. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  10. import Enter from '@ckeditor/ckeditor5-enter/src/enter';
  11. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  12. import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  13. import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  14. import { isLinkElement } from '../src/utils';
  15. import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
  16. /* global document */
  17. describe( 'LinkEditing', () => {
  18. let editor, model, view;
  19. beforeEach( () => {
  20. return VirtualTestEditor
  21. .create( {
  22. plugins: [ Paragraph, LinkEditing, Enter ]
  23. } )
  24. .then( newEditor => {
  25. editor = newEditor;
  26. model = editor.model;
  27. view = editor.editing.view;
  28. } );
  29. } );
  30. afterEach( () => {
  31. editor.destroy();
  32. } );
  33. it( 'should be loaded', () => {
  34. expect( editor.plugins.get( LinkEditing ) ).to.be.instanceOf( LinkEditing );
  35. } );
  36. it( 'should set proper schema rules', () => {
  37. expect( model.schema.checkAttribute( [ '$block', '$text' ], 'linkHref' ) ).to.be.true;
  38. expect( model.schema.checkAttribute( [ '$clipboardHolder', '$text' ], 'linkHref' ) ).to.be.true;
  39. expect( model.schema.checkAttribute( [ '$block' ], 'linkHref' ) ).to.be.false;
  40. } );
  41. it( 'should bind two-step caret movement to `linkHref` attribute', () => {
  42. // Let's check only the minimum to not duplicated `bindTwoStepCaretToAttribute()` tests.
  43. // Testing minimum is better then testing using spies that might give false positive results.
  44. // Put selection before the link element.
  45. setModelData( editor.model, '<paragraph>foo[]<$text linkHref="url">b</$text>ar</paragraph>' );
  46. // The selection's gravity is not overridden because selection land here not as a result of `keydown`.
  47. expect( editor.model.document.selection.isGravityOverridden ).to.false;
  48. // So let's simulate `keydown` event.
  49. editor.editing.view.document.fire( 'keydown', {
  50. keyCode: keyCodes.arrowright,
  51. preventDefault: () => {},
  52. domTarget: document.body
  53. } );
  54. expect( editor.model.document.selection.isGravityOverridden ).to.true;
  55. } );
  56. describe( 'command', () => {
  57. it( 'should register link command', () => {
  58. const command = editor.commands.get( 'link' );
  59. expect( command ).to.be.instanceOf( LinkCommand );
  60. } );
  61. it( 'should register unlink command', () => {
  62. const command = editor.commands.get( 'unlink' );
  63. expect( command ).to.be.instanceOf( UnlinkCommand );
  64. } );
  65. } );
  66. describe( 'data pipeline conversions', () => {
  67. it( 'should convert `<a href="url">` to `linkHref="url"` attribute', () => {
  68. editor.setData( '<p><a href="url">foo</a>bar</p>' );
  69. expect( getModelData( model, { withoutSelection: true } ) )
  70. .to.equal( '<paragraph><$text linkHref="url">foo</$text>bar</paragraph>' );
  71. expect( editor.getData() ).to.equal( '<p><a href="url">foo</a>bar</p>' );
  72. } );
  73. it( 'should be integrated with autoparagraphing', () => {
  74. editor.setData( '<a href="url">foo</a>bar' );
  75. expect( getModelData( model, { withoutSelection: true } ) )
  76. .to.equal( '<paragraph><$text linkHref="url">foo</$text>bar</paragraph>' );
  77. expect( editor.getData() ).to.equal( '<p><a href="url">foo</a>bar</p>' );
  78. } );
  79. // https://github.com/ckeditor/ckeditor5/issues/500
  80. it( 'should not pick up `<a name="foo">`', () => {
  81. editor.setData( '<p><a name="foo">foo</a>bar</p>' );
  82. expect( getModelData( model, { withoutSelection: true } ) )
  83. .to.equal( '<paragraph>foobar</paragraph>' );
  84. } );
  85. // CKEditor 4 does. And CKEditor 5's balloon allows creating such links.
  86. it( 'should pick up `<a href="">`', () => {
  87. editor.setData( '<p><a href="">foo</a>bar</p>' );
  88. expect( getModelData( model, { withoutSelection: true } ) )
  89. .to.equal( '<paragraph><$text linkHref="">foo</$text>bar</paragraph>' );
  90. expect( editor.getData() ).to.equal( '<p><a href="">foo</a>bar</p>' );
  91. } );
  92. // The editor's role is not to filter out potentially malicious data.
  93. // Its job is to not let this code be executed inside the editor (see the test in "editing pipeline conversion").
  94. it( 'should output a link with a potential XSS code', () => {
  95. setModelData( model, '<paragraph>[]</paragraph>' );
  96. model.change( writer => {
  97. writer.insertText( 'foo', { linkHref: 'javascript:alert(1)' }, model.document.selection.getFirstPosition() );
  98. } );
  99. expect( editor.getData() ).to.equal( '<p><a href="javascript:alert(1)">foo</a></p>' );
  100. } );
  101. it( 'should load a link with a potential XSS code', () => {
  102. editor.setData( '<p><a href="javascript:alert(1)">foo</a></p>' );
  103. expect( getModelData( model, { withoutSelection: true } ) )
  104. .to.equal( '<paragraph><$text linkHref="javascript:alert(1)">foo</$text></paragraph>' );
  105. } );
  106. } );
  107. describe( 'editing pipeline conversion', () => {
  108. it( 'should convert attribute', () => {
  109. setModelData( model, '<paragraph><$text linkHref="url">foo</$text>bar</paragraph>' );
  110. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( '<p><a href="url">foo</a>bar</p>' );
  111. } );
  112. it( 'should convert to link element instance', () => {
  113. setModelData( model, '<paragraph><$text linkHref="url">foo</$text>bar</paragraph>' );
  114. const element = editor.editing.view.document.getRoot().getChild( 0 ).getChild( 0 );
  115. expect( isLinkElement( element ) ).to.be.true;
  116. } );
  117. // https://github.com/ckeditor/ckeditor5-link/issues/121
  118. it( 'should should set priority for `linkHref` higher than all other attribute elements', () => {
  119. model.schema.extend( '$text', { allowAttributes: 'foo' } );
  120. editor.conversion.for( 'downcast' ).attributeToElement( { model: 'foo', view: 'f' } );
  121. setModelData( model,
  122. '<paragraph>' +
  123. '<$text linkHref="url">a</$text><$text foo="true" linkHref="url">b</$text><$text linkHref="url">c</$text>' +
  124. '</paragraph>' );
  125. expect( editor.getData() ).to.equal( '<p><a href="url">a<f>b</f>c</a></p>' );
  126. } );
  127. it( 'must not render a link with a potential XSS code', () => {
  128. setModelData( model, '<paragraph><$text linkHref="javascript:alert(1)">[]foo</$text>bar[]</paragraph>' );
  129. expect( getViewData( editor.editing.view, { withoutSelection: true } ) )
  130. .to.equal( '<p><a href="#">foo</a>bar</p>' );
  131. } );
  132. } );
  133. describe( 'link highlighting', () => {
  134. it( 'should convert the highlight to a proper view classes', () => {
  135. setModelData( model,
  136. '<paragraph>foo <$text linkHref="url">b{}ar</$text> baz</paragraph>'
  137. );
  138. expect( model.document.selection.hasAttribute( 'linkHref' ) ).to.be.true;
  139. expect( getViewData( view ) ).to.equal(
  140. '<p>foo <a class="ck-link_selected" href="url">b{}ar</a> baz</p>'
  141. );
  142. } );
  143. it( 'should work whenever selection has linkHref attribute - link start', () => {
  144. setModelData( model,
  145. '<paragraph>foo {}<$text linkHref="url">bar</$text> baz</paragraph>'
  146. );
  147. expect( model.document.selection.hasAttribute( 'linkHref' ) ).to.be.false;
  148. model.change( writer => {
  149. writer.setSelectionAttribute( 'linkHref', 'url' );
  150. } );
  151. expect( model.document.selection.hasAttribute( 'linkHref' ) ).to.be.true;
  152. expect( getViewData( view ) ).to.equal(
  153. '<p>foo <a class="ck-link_selected" href="url">{}bar</a> baz</p>'
  154. );
  155. } );
  156. it( 'should work whenever selection has linkHref attribute - link end', () => {
  157. setModelData( model,
  158. '<paragraph>foo <$text linkHref="url">bar</$text>{} baz</paragraph>'
  159. );
  160. expect( model.document.selection.hasAttribute( 'linkHref' ) ).to.be.true;
  161. expect( getViewData( view ) ).to.equal(
  162. '<p>foo <a class="ck-link_selected" href="url">bar{}</a> baz</p>'
  163. );
  164. } );
  165. it( 'should render highlight correctly after splitting the link', () => {
  166. setModelData( model,
  167. '<paragraph>foo <$text linkHref="url">li{}nk</$text> baz</paragraph>'
  168. );
  169. editor.execute( 'enter' );
  170. expect( getModelData( model ) ).to.equal(
  171. '<paragraph>foo <$text linkHref="url">li</$text></paragraph>' +
  172. '<paragraph><$text linkHref="url">[]nk</$text> baz</paragraph>'
  173. );
  174. expect( model.document.selection.hasAttribute( 'linkHref' ) ).to.be.true;
  175. } );
  176. it( 'should remove classes when selection is moved out from the link', () => {
  177. setModelData( model,
  178. '<paragraph>foo <$text linkHref="url">li{}nk</$text> baz</paragraph>'
  179. );
  180. expect( getViewData( view ) ).to.equal(
  181. '<p>foo <a class="ck-link_selected" href="url">li{}nk</a> baz</p>'
  182. );
  183. model.change( writer => writer.setSelection( model.document.getRoot().getChild( 0 ), 0 ) );
  184. expect( getViewData( view ) ).to.equal(
  185. '<p>{}foo <a href="url">link</a> baz</p>'
  186. );
  187. } );
  188. it( 'should work correctly when selection is moved inside link', () => {
  189. setModelData( model,
  190. '<paragraph>foo <$text linkHref="url">li{}nk</$text> baz</paragraph>'
  191. );
  192. expect( getViewData( view ) ).to.equal(
  193. '<p>foo <a class="ck-link_selected" href="url">li{}nk</a> baz</p>'
  194. );
  195. model.change( writer => writer.setSelection( model.document.getRoot().getChild( 0 ), 5 ) );
  196. expect( getViewData( view ) ).to.equal(
  197. '<p>foo <a class="ck-link_selected" href="url">l{}ink</a> baz</p>'
  198. );
  199. } );
  200. describe( 'downcast conversion integration', () => {
  201. it( 'works for the #insert event', () => {
  202. setModelData( model,
  203. '<paragraph>foo <$text linkHref="url">li{}nk</$text> baz</paragraph>'
  204. );
  205. model.change( writer => {
  206. writer.insertText( 'FOO', { linkHref: 'url' }, model.document.selection.getFirstPosition() );
  207. } );
  208. expect( getViewData( view ) ).to.equal(
  209. '<p>foo <a class="ck-link_selected" href="url">liFOO{}nk</a> baz</p>'
  210. );
  211. } );
  212. it( 'works for the #remove event', () => {
  213. setModelData( model,
  214. '<paragraph>foo <$text linkHref="url">li{}nk</$text> baz</paragraph>'
  215. );
  216. model.change( writer => {
  217. writer.remove( writer.createRange(
  218. writer.createPositionAt( model.document.getRoot().getChild( 0 ), 0 ),
  219. writer.createPositionAt( model.document.getRoot().getChild( 0 ), 5 )
  220. ) );
  221. } );
  222. expect( getViewData( view ) ).to.equal(
  223. '<p><a class="ck-link_selected" href="url">i{}nk</a> baz</p>'
  224. );
  225. } );
  226. it( 'works for the #attribute event', () => {
  227. setModelData( model,
  228. '<paragraph>foo <$text linkHref="url">li{}nk</$text> baz</paragraph>'
  229. );
  230. model.change( writer => {
  231. writer.setAttribute( 'linkHref', 'new-url', writer.createRange(
  232. model.document.selection.getFirstPosition().getShiftedBy( -1 ),
  233. model.document.selection.getFirstPosition().getShiftedBy( 1 ) )
  234. );
  235. } );
  236. expect( getViewData( view ) ).to.equal(
  237. '<p>foo <a href="url">l</a><a class="ck-link_selected" href="new-url">i{}n</a><a href="url">k</a> baz</p>'
  238. );
  239. } );
  240. it( 'works for the #selection event', () => {
  241. setModelData( model,
  242. '<paragraph>foo <$text linkHref="url">li{}nk</$text> baz</paragraph>'
  243. );
  244. model.change( writer => {
  245. writer.setSelection( writer.createRange(
  246. model.document.selection.getFirstPosition().getShiftedBy( -1 ),
  247. model.document.selection.getFirstPosition().getShiftedBy( 1 ) )
  248. );
  249. } );
  250. expect( getViewData( view ) ).to.equal(
  251. '<p>foo <a class="ck-link_selected" href="url">l{in}k</a> baz</p>'
  252. );
  253. } );
  254. it( 'works for the addMarker and removeMarker events', () => {
  255. editor.conversion.for( 'editingDowncast' ).markerToHighlight( { model: 'fooMarker', view: {} } );
  256. setModelData( model,
  257. '<paragraph>foo <$text linkHref="url">li{}nk</$text> baz</paragraph>'
  258. );
  259. model.change( writer => {
  260. const range = writer.createRange(
  261. writer.createPositionAt( model.document.getRoot().getChild( 0 ), 0 ),
  262. writer.createPositionAt( model.document.getRoot().getChild( 0 ), 5 )
  263. );
  264. writer.addMarker( 'fooMarker', { range, usingOperation: true } );
  265. } );
  266. expect( getViewData( view ) ).to.equal(
  267. '<p><span>foo </span><a class="ck-link_selected" href="url"><span>l</span>i{}nk</a> baz</p>'
  268. );
  269. model.change( writer => writer.removeMarker( 'fooMarker' ) );
  270. expect( getViewData( view ) ).to.equal(
  271. '<p>foo <a class="ck-link_selected" href="url">li{}nk</a> baz</p>'
  272. );
  273. } );
  274. } );
  275. } );
  276. describe( 'link attributes decorator', () => {
  277. describe( 'default behavior', () => {
  278. const testLinks = [
  279. {
  280. external: true,
  281. url: 'http://example.com'
  282. }, {
  283. external: true,
  284. url: 'https://cksource.com'
  285. }, {
  286. external: false,
  287. url: 'ftp://server.io'
  288. }, {
  289. external: true,
  290. url: '//schemaless.org'
  291. }, {
  292. external: false,
  293. url: 'www.ckeditor.com'
  294. }, {
  295. external: false,
  296. url: '/relative/url.html'
  297. }, {
  298. external: false,
  299. url: 'another/relative/url.html'
  300. }, {
  301. external: false,
  302. url: '#anchor'
  303. }, {
  304. external: false,
  305. url: 'mailto:some@user.org'
  306. }, {
  307. external: false,
  308. url: 'tel:123456789'
  309. }
  310. ];
  311. it( 'link.addTargetToExternalLinks is predefined as false value', () => {
  312. expect( editor.config.get( 'link.addTargetToExternalLinks' ) ).to.be.false;
  313. } );
  314. describe( 'for link.addTargetToExternalLinks = false', () => {
  315. let editor, model;
  316. beforeEach( () => {
  317. return VirtualTestEditor
  318. .create( {
  319. plugins: [ Paragraph, LinkEditing, Enter ],
  320. link: {
  321. addTargetToExternalLinks: true
  322. }
  323. } )
  324. .then( newEditor => {
  325. editor = newEditor;
  326. model = editor.model;
  327. view = editor.editing.view;
  328. } );
  329. } );
  330. afterEach( () => {
  331. editor.destroy();
  332. } );
  333. it( 'link.addTargetToExternalLinks is set as true value', () => {
  334. expect( editor.config.get( 'link.addTargetToExternalLinks' ) ).to.be.true;
  335. } );
  336. testLinks.forEach( link => {
  337. it( `link: ${ link.url } should be treat as ${ link.external ? 'external' : 'non-external' } link`, () => {
  338. editor.setData( `<p><a href="${ link.url }">foo</a>bar</p>` );
  339. expect( getModelData( model, { withoutSelection: true } ) )
  340. .to.equal( `<paragraph><$text linkHref="${ link.url }">foo</$text>bar</paragraph>` );
  341. if ( link.external ) {
  342. expect( editor.getData() )
  343. .to.equal( `<p><a target="_blank" rel="noopener noreferrer" href="${ link.url }">foo</a>bar</p>` );
  344. } else {
  345. expect( editor.getData() ).to.equal( `<p><a href="${ link.url }">foo</a>bar</p>` );
  346. }
  347. } );
  348. } );
  349. } );
  350. testLinks.forEach( link => {
  351. it( `link: ${ link.url } should not get 'target' and 'rel' attributes`, () => {
  352. editor.setData( `<p><a href="${ link.url }">foo</a>bar</p>` );
  353. expect( getModelData( model, { withoutSelection: true } ) )
  354. .to.equal( `<paragraph><$text linkHref="${ link.url }">foo</$text>bar</paragraph>` );
  355. expect( editor.getData() ).to.equal( `<p><a href="${ link.url }">foo</a>bar</p>` );
  356. } );
  357. } );
  358. } );
  359. describe( 'custom config', () => {
  360. describe( 'mode: automatic', () => {
  361. const testLinks = [
  362. {
  363. url: 'relative/url.html',
  364. attributes: {}
  365. }, {
  366. url: 'http://exmaple.com',
  367. attributes: {
  368. target: '_blank'
  369. }
  370. }, {
  371. url: 'https://example.com/download/link.pdf',
  372. attributes: {
  373. target: '_blank',
  374. download: 'download'
  375. }
  376. }, {
  377. url: 'mailto:some@person.io',
  378. attributes: {
  379. class: 'mail-url'
  380. }
  381. }
  382. ];
  383. beforeEach( () => {
  384. editor.destroy();
  385. return VirtualTestEditor
  386. .create( {
  387. plugins: [ Paragraph, LinkEditing, Enter ],
  388. link: {
  389. addTargetToExternalLinks: false,
  390. decorators: {
  391. isExternal: {
  392. mode: 'automatic',
  393. callback: url => url.startsWith( 'http' ),
  394. attributes: {
  395. target: '_blank'
  396. }
  397. },
  398. isDownloadable: {
  399. mode: 'automatic',
  400. callback: url => url.includes( 'download' ),
  401. attributes: {
  402. download: 'download'
  403. }
  404. },
  405. isMail: {
  406. mode: 'automatic',
  407. callback: url => url.startsWith( 'mailto:' ),
  408. attributes: {
  409. class: 'mail-url'
  410. }
  411. }
  412. }
  413. }
  414. } )
  415. .then( newEditor => {
  416. editor = newEditor;
  417. model = editor.model;
  418. view = editor.editing.view;
  419. } );
  420. } );
  421. testLinks.forEach( link => {
  422. it( `Link: ${ link.url } should get attributes: ${ JSON.stringify( link.attributes ) }`, () => {
  423. const ORDER = [ 'target', 'download', 'class' ];
  424. const attr = Object.entries( link.attributes ).sort( ( a, b ) => {
  425. const aIndex = ORDER.indexOf( a[ 0 ] );
  426. const bIndex = ORDER.indexOf( b[ 0 ] );
  427. return aIndex - bIndex;
  428. } );
  429. const reducedAttr = attr.reduce( ( acc, cur ) => {
  430. return acc + `${ cur[ 0 ] }="${ cur[ 1 ] }" `;
  431. }, '' );
  432. editor.setData( `<p><a href="${ link.url }">foo</a>bar</p>` );
  433. expect( getModelData( model, { withoutSelection: true } ) )
  434. .to.equal( `<paragraph><$text linkHref="${ link.url }">foo</$text>bar</paragraph>` );
  435. // Order of attributes is important, that's why this is assert is construct in such way.
  436. expect( editor.getData() ).to.equal( `<p><a ${ reducedAttr }href="${ link.url }">foo</a>bar</p>` );
  437. } );
  438. } );
  439. } );
  440. } );
  441. describe( 'custom linkHref converter', () => {
  442. beforeEach( () => {
  443. class CustomLinks extends Plugin {
  444. init() {
  445. const editor = this.editor;
  446. editor.conversion.for( 'downcast' ).add( dispatcher => {
  447. dispatcher.on( 'attribute:linkHref', ( evt, data, conversionApi ) => {
  448. conversionApi.consumable.consume( data.item, 'attribute:linkHref' );
  449. // Very simplified downcast just for test assertion.
  450. const viewWriter = conversionApi.writer;
  451. const linkElement = viewWriter.createAttributeElement(
  452. 'a',
  453. {
  454. href: data.attributeNewValue
  455. }, {
  456. priority: 5
  457. }
  458. );
  459. viewWriter.setCustomProperty( 'link', true, linkElement );
  460. viewWriter.wrap( conversionApi.mapper.toViewRange( data.range ), linkElement );
  461. }, { priority: 'highest' } );
  462. } );
  463. }
  464. }
  465. editor.destroy();
  466. return VirtualTestEditor
  467. .create( {
  468. plugins: [ Paragraph, LinkEditing, Enter, CustomLinks ],
  469. link: {
  470. addTargetToExternalLinks: true,
  471. }
  472. } )
  473. .then( newEditor => {
  474. editor = newEditor;
  475. model = editor.model;
  476. view = editor.editing.view;
  477. } );
  478. } );
  479. it( 'has possibility to override default one', () => {
  480. editor.setData( '<p><a href="http://example.com">foo</a>bar</p>' );
  481. expect( getModelData( model, { withoutSelection: true } ) )
  482. .to.equal( '<paragraph><$text linkHref="http://example.com">foo</$text>bar</paragraph>' );
  483. expect( editor.getData() ).to.equal( '<p><a href="http://example.com">foo</a>bar</p>' );
  484. } );
  485. } );
  486. describe( 'upcast converter', () => {
  487. it( 'should upcast attributes from initial data', done => {
  488. VirtualTestEditor
  489. .create( {
  490. initialData: '<p><a href="url" target="_blank" rel="noopener noreferrer" download="file">Foo</a>' +
  491. '<a href="example.com" download="file">Bar</a></p>',
  492. plugins: [ Paragraph, LinkEditing, Enter ],
  493. link: {
  494. decorators: {
  495. isExternal: {
  496. mode: 'manual',
  497. label: 'Open in a new window',
  498. attributes: {
  499. target: '_blank',
  500. rel: 'noopener noreferrer'
  501. }
  502. },
  503. isDownloadable: {
  504. mode: 'manual',
  505. label: 'Downloadable',
  506. attributes: {
  507. download: 'file'
  508. }
  509. }
  510. }
  511. }
  512. } )
  513. .then( newEditor => {
  514. editor = newEditor;
  515. model = editor.model;
  516. expect( getModelData( model, { withoutSelection: true } ) ).to.equal(
  517. '<paragraph>' +
  518. '<$text linkHref="url" linkIsDownloadable="true" linkIsExternal="true">Foo</$text>' +
  519. '<$text linkHref="example.com" linkIsDownloadable="true">Bar</$text>' +
  520. '</paragraph>'
  521. );
  522. } )
  523. .then( done )
  524. .catch( done );
  525. } );
  526. it( 'should not upcast partial and incorrect attributes', done => {
  527. VirtualTestEditor
  528. .create( {
  529. initialData: '<p><a href="url" target="_blank" download="something">Foo</a>' +
  530. '<a href="example.com" download="test">Bar</a></p>',
  531. plugins: [ Paragraph, LinkEditing, Enter ],
  532. link: {
  533. decorators: {
  534. isExternal: {
  535. mode: 'manual',
  536. label: 'Open in a new window',
  537. attributes: {
  538. target: '_blank',
  539. rel: 'noopener noreferrer'
  540. }
  541. },
  542. isDownloadable: {
  543. mode: 'manual',
  544. label: 'Downloadable',
  545. attributes: {
  546. download: 'file'
  547. }
  548. }
  549. }
  550. }
  551. } )
  552. .then( newEditor => {
  553. editor = newEditor;
  554. model = editor.model;
  555. expect( getModelData( model, { withoutSelection: true } ) ).to.equal(
  556. '<paragraph>' +
  557. '<$text linkHref="url">Foo</$text>' +
  558. '<$text linkHref="example.com">Bar</$text>' +
  559. '</paragraph>'
  560. );
  561. } )
  562. .then( done )
  563. .catch( done );
  564. } );
  565. } );
  566. } );
  567. } );