linkediting.js 22 KB

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