linkediting.js 28 KB

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