linkediting.js 34 KB

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