linkediting.js 35 KB

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