8
0

linkediting.js 27 KB

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