linkediting.js 22 KB

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