linkediting.js 35 KB

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