linkediting.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. /**
  2. * @license Copyright (c) 2003-2019, 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 VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  9. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  10. import Enter from '@ckeditor/ckeditor5-enter/src/enter';
  11. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  12. import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  13. import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  14. import { isLinkElement } from '../src/utils';
  15. import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
  16. /* global document */
  17. describe( 'LinkEditing', () => {
  18. let editor, model, view;
  19. beforeEach( () => {
  20. return VirtualTestEditor
  21. .create( {
  22. plugins: [ Paragraph, LinkEditing, Enter ]
  23. } )
  24. .then( newEditor => {
  25. editor = newEditor;
  26. model = editor.model;
  27. view = editor.editing.view;
  28. } );
  29. } );
  30. it( 'should be loaded', () => {
  31. expect( editor.plugins.get( LinkEditing ) ).to.be.instanceOf( LinkEditing );
  32. } );
  33. it( 'should set proper schema rules', () => {
  34. expect( model.schema.checkAttribute( [ '$block', '$text' ], 'linkHref' ) ).to.be.true;
  35. expect( model.schema.checkAttribute( [ '$clipboardHolder', '$text' ], 'linkHref' ) ).to.be.true;
  36. expect( model.schema.checkAttribute( [ '$block' ], 'linkHref' ) ).to.be.false;
  37. } );
  38. it( 'should bind two-step caret movement to `linkHref` attribute', () => {
  39. // Let's check only the minimum to not duplicated `bindTwoStepCaretToAttribute()` tests.
  40. // Testing minimum is better then testing using spies that might give false positive results.
  41. // Put selection before the link element.
  42. setModelData( editor.model, '<paragraph>foo[]<$text linkHref="url">b</$text>ar</paragraph>' );
  43. // The selection's gravity is not overridden because selection land here not as a result of `keydown`.
  44. expect( editor.model.document.selection.isGravityOverridden ).to.false;
  45. // So let's simulate `keydown` event.
  46. editor.editing.view.document.fire( 'keydown', {
  47. keyCode: keyCodes.arrowright,
  48. preventDefault: () => {},
  49. domTarget: document.body
  50. } );
  51. expect( editor.model.document.selection.isGravityOverridden ).to.true;
  52. } );
  53. describe( 'command', () => {
  54. it( 'should register link command', () => {
  55. const command = editor.commands.get( 'link' );
  56. expect( command ).to.be.instanceOf( LinkCommand );
  57. } );
  58. it( 'should register unlink command', () => {
  59. const command = editor.commands.get( 'unlink' );
  60. expect( command ).to.be.instanceOf( UnlinkCommand );
  61. } );
  62. } );
  63. describe( 'data pipeline conversions', () => {
  64. it( 'should convert `<a href="url">` to `linkHref="url"` attribute', () => {
  65. editor.setData( '<p><a href="url">foo</a>bar</p>' );
  66. expect( getModelData( model, { withoutSelection: true } ) )
  67. .to.equal( '<paragraph><$text linkHref="url">foo</$text>bar</paragraph>' );
  68. expect( editor.getData() ).to.equal( '<p><a href="url">foo</a>bar</p>' );
  69. } );
  70. it( 'should be integrated with autoparagraphing', () => {
  71. editor.setData( '<a href="url">foo</a>bar' );
  72. expect( getModelData( model, { withoutSelection: true } ) )
  73. .to.equal( '<paragraph><$text linkHref="url">foo</$text>bar</paragraph>' );
  74. expect( editor.getData() ).to.equal( '<p><a href="url">foo</a>bar</p>' );
  75. } );
  76. // https://github.com/ckeditor/ckeditor5/issues/500
  77. it( 'should not pick up `<a name="foo">`', () => {
  78. editor.setData( '<p><a name="foo">foo</a>bar</p>' );
  79. expect( getModelData( model, { withoutSelection: true } ) )
  80. .to.equal( '<paragraph>foobar</paragraph>' );
  81. } );
  82. // CKEditor 4 does. And CKEditor 5's balloon allows creating such links.
  83. it( 'should pick up `<a href="">`', () => {
  84. editor.setData( '<p><a href="">foo</a>bar</p>' );
  85. expect( getModelData( model, { withoutSelection: true } ) )
  86. .to.equal( '<paragraph><$text linkHref="">foo</$text>bar</paragraph>' );
  87. expect( editor.getData() ).to.equal( '<p><a href="">foo</a>bar</p>' );
  88. } );
  89. // The editor's role is not to filter out potentially malicious data.
  90. // Its job is to not let this code be executed inside the editor (see the test in "editing pipeline conversion").
  91. it( 'should output a link with a potential XSS code', () => {
  92. setModelData( model, '<paragraph>[]</paragraph>' );
  93. model.change( writer => {
  94. writer.insertText( 'foo', { linkHref: 'javascript:alert(1)' }, model.document.selection.getFirstPosition() );
  95. } );
  96. expect( editor.getData() ).to.equal( '<p><a href="javascript:alert(1)">foo</a></p>' );
  97. } );
  98. it( 'should load a link with a potential XSS code', () => {
  99. editor.setData( '<p><a href="javascript:alert(1)">foo</a></p>' );
  100. expect( getModelData( model, { withoutSelection: true } ) )
  101. .to.equal( '<paragraph><$text linkHref="javascript:alert(1)">foo</$text></paragraph>' );
  102. } );
  103. } );
  104. describe( 'editing pipeline conversion', () => {
  105. it( 'should convert attribute', () => {
  106. setModelData( model, '<paragraph><$text linkHref="url">foo</$text>bar</paragraph>' );
  107. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( '<p><a href="url">foo</a>bar</p>' );
  108. } );
  109. it( 'should convert to link element instance', () => {
  110. setModelData( model, '<paragraph><$text linkHref="url">foo</$text>bar</paragraph>' );
  111. const element = editor.editing.view.document.getRoot().getChild( 0 ).getChild( 0 );
  112. expect( isLinkElement( element ) ).to.be.true;
  113. } );
  114. // https://github.com/ckeditor/ckeditor5-link/issues/121
  115. it( 'should should set priority for `linkHref` higher than all other attribute elements', () => {
  116. model.schema.extend( '$text', { allowAttributes: 'foo' } );
  117. editor.conversion.for( 'downcast' ).attributeToElement( { model: 'foo', view: 'f' } );
  118. setModelData( model,
  119. '<paragraph>' +
  120. '<$text linkHref="url">a</$text><$text foo="true" linkHref="url">b</$text><$text linkHref="url">c</$text>' +
  121. '</paragraph>' );
  122. expect( editor.getData() ).to.equal( '<p><a href="url">a<f>b</f>c</a></p>' );
  123. } );
  124. it( 'must not render a link with a potential XSS code', () => {
  125. setModelData( model, '<paragraph><$text linkHref="javascript:alert(1)">[]foo</$text>bar[]</paragraph>' );
  126. expect( getViewData( editor.editing.view, { withoutSelection: true } ) )
  127. .to.equal( '<p><a href="#">foo</a>bar</p>' );
  128. } );
  129. } );
  130. describe( 'link highlighting', () => {
  131. it( 'should convert the highlight to a proper view classes', () => {
  132. setModelData( model,
  133. '<paragraph>foo <$text linkHref="url">b{}ar</$text> baz</paragraph>'
  134. );
  135. expect( model.document.selection.hasAttribute( 'linkHref' ) ).to.be.true;
  136. expect( getViewData( view ) ).to.equal(
  137. '<p>foo <a class="ck-link_selected" href="url">b{}ar</a> baz</p>'
  138. );
  139. } );
  140. it( 'should work whenever selection has linkHref attribute - link start', () => {
  141. setModelData( model,
  142. '<paragraph>foo {}<$text linkHref="url">bar</$text> baz</paragraph>'
  143. );
  144. expect( model.document.selection.hasAttribute( 'linkHref' ) ).to.be.false;
  145. model.change( writer => {
  146. writer.setSelectionAttribute( 'linkHref', 'url' );
  147. } );
  148. expect( model.document.selection.hasAttribute( 'linkHref' ) ).to.be.true;
  149. expect( getViewData( view ) ).to.equal(
  150. '<p>foo <a class="ck-link_selected" href="url">{}bar</a> baz</p>'
  151. );
  152. } );
  153. it( 'should work whenever selection has linkHref attribute - link end', () => {
  154. setModelData( model,
  155. '<paragraph>foo <$text linkHref="url">bar</$text>{} baz</paragraph>'
  156. );
  157. expect( model.document.selection.hasAttribute( 'linkHref' ) ).to.be.true;
  158. expect( getViewData( view ) ).to.equal(
  159. '<p>foo <a class="ck-link_selected" href="url">bar{}</a> baz</p>'
  160. );
  161. } );
  162. it( 'should render highlight correctly after splitting the link', () => {
  163. setModelData( model,
  164. '<paragraph>foo <$text linkHref="url">li{}nk</$text> baz</paragraph>'
  165. );
  166. editor.execute( 'enter' );
  167. expect( getModelData( model ) ).to.equal(
  168. '<paragraph>foo <$text linkHref="url">li</$text></paragraph>' +
  169. '<paragraph><$text linkHref="url">[]nk</$text> baz</paragraph>'
  170. );
  171. expect( model.document.selection.hasAttribute( 'linkHref' ) ).to.be.true;
  172. } );
  173. it( 'should remove classes when selection is moved out from the link', () => {
  174. setModelData( model,
  175. '<paragraph>foo <$text linkHref="url">li{}nk</$text> baz</paragraph>'
  176. );
  177. expect( getViewData( view ) ).to.equal(
  178. '<p>foo <a class="ck-link_selected" href="url">li{}nk</a> baz</p>'
  179. );
  180. model.change( writer => writer.setSelection( model.document.getRoot().getChild( 0 ), 0 ) );
  181. expect( getViewData( view ) ).to.equal(
  182. '<p>{}foo <a href="url">link</a> baz</p>'
  183. );
  184. } );
  185. it( 'should work correctly when selection is moved inside link', () => {
  186. setModelData( model,
  187. '<paragraph>foo <$text linkHref="url">li{}nk</$text> baz</paragraph>'
  188. );
  189. expect( getViewData( view ) ).to.equal(
  190. '<p>foo <a class="ck-link_selected" href="url">li{}nk</a> baz</p>'
  191. );
  192. model.change( writer => writer.setSelection( model.document.getRoot().getChild( 0 ), 5 ) );
  193. expect( getViewData( view ) ).to.equal(
  194. '<p>foo <a class="ck-link_selected" href="url">l{}ink</a> baz</p>'
  195. );
  196. } );
  197. describe( 'downcast conversion integration', () => {
  198. it( 'works for the #insert event', () => {
  199. setModelData( model,
  200. '<paragraph>foo <$text linkHref="url">li{}nk</$text> baz</paragraph>'
  201. );
  202. model.change( writer => {
  203. writer.insertText( 'FOO', { linkHref: 'url' }, model.document.selection.getFirstPosition() );
  204. } );
  205. expect( getViewData( view ) ).to.equal(
  206. '<p>foo <a class="ck-link_selected" href="url">liFOO{}nk</a> baz</p>'
  207. );
  208. } );
  209. it( 'works for the #remove event', () => {
  210. setModelData( model,
  211. '<paragraph>foo <$text linkHref="url">li{}nk</$text> baz</paragraph>'
  212. );
  213. model.change( writer => {
  214. writer.remove( writer.createRange(
  215. writer.createPositionAt( model.document.getRoot().getChild( 0 ), 0 ),
  216. writer.createPositionAt( model.document.getRoot().getChild( 0 ), 5 )
  217. ) );
  218. } );
  219. expect( getViewData( view ) ).to.equal(
  220. '<p><a class="ck-link_selected" href="url">i{}nk</a> baz</p>'
  221. );
  222. } );
  223. it( 'works for the #attribute event', () => {
  224. setModelData( model,
  225. '<paragraph>foo <$text linkHref="url">li{}nk</$text> baz</paragraph>'
  226. );
  227. model.change( writer => {
  228. writer.setAttribute( 'linkHref', 'new-url', writer.createRange(
  229. model.document.selection.getFirstPosition().getShiftedBy( -1 ),
  230. model.document.selection.getFirstPosition().getShiftedBy( 1 ) )
  231. );
  232. } );
  233. expect( getViewData( view ) ).to.equal(
  234. '<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>'
  235. );
  236. } );
  237. it( 'works for the #selection event', () => {
  238. setModelData( model,
  239. '<paragraph>foo <$text linkHref="url">li{}nk</$text> baz</paragraph>'
  240. );
  241. model.change( writer => {
  242. writer.setSelection( writer.createRange(
  243. model.document.selection.getFirstPosition().getShiftedBy( -1 ),
  244. model.document.selection.getFirstPosition().getShiftedBy( 1 ) )
  245. );
  246. } );
  247. expect( getViewData( view ) ).to.equal(
  248. '<p>foo <a class="ck-link_selected" href="url">l{in}k</a> baz</p>'
  249. );
  250. } );
  251. it( 'works for the addMarker and removeMarker events', () => {
  252. editor.conversion.for( 'editingDowncast' ).markerToHighlight( { model: 'fooMarker', view: {} } );
  253. setModelData( model,
  254. '<paragraph>foo <$text linkHref="url">li{}nk</$text> baz</paragraph>'
  255. );
  256. model.change( writer => {
  257. const range = writer.createRange(
  258. writer.createPositionAt( model.document.getRoot().getChild( 0 ), 0 ),
  259. writer.createPositionAt( model.document.getRoot().getChild( 0 ), 5 )
  260. );
  261. writer.addMarker( 'fooMarker', { range, usingOperation: true } );
  262. } );
  263. expect( getViewData( view ) ).to.equal(
  264. '<p><span>foo </span><a class="ck-link_selected" href="url"><span>l</span>i{}nk</a> baz</p>'
  265. );
  266. model.change( writer => writer.removeMarker( 'fooMarker' ) );
  267. expect( getViewData( view ) ).to.equal(
  268. '<p>foo <a class="ck-link_selected" href="url">li{}nk</a> baz</p>'
  269. );
  270. } );
  271. } );
  272. } );
  273. describe( 'link attributes decorator', () => {
  274. describe( 'default behavior', () => {
  275. const testLinks = [
  276. {
  277. external: true,
  278. url: 'http://example.com'
  279. }, {
  280. external: true,
  281. url: 'https://cksource.com'
  282. }, {
  283. external: false,
  284. url: 'ftp://server.io'
  285. }, {
  286. external: true,
  287. url: '//schemaless.org'
  288. }, {
  289. external: false,
  290. url: 'www.ckeditor.com'
  291. }, {
  292. external: false,
  293. url: '/relative/url.html'
  294. }, {
  295. external: false,
  296. url: 'another/relative/url.html'
  297. }, {
  298. external: false,
  299. url: '#anchor'
  300. }, {
  301. external: false,
  302. url: 'mailto:some@user.org'
  303. }, {
  304. external: false,
  305. url: 'tel:123456789'
  306. }
  307. ];
  308. it( 'link.targetDecorator is predefined as false value', () => {
  309. expect( editor.config.get( 'link.targetDecorator' ) ).to.be.false;
  310. } );
  311. describe( 'for link.targetDecorator = false', () => {
  312. beforeEach( () => {
  313. editor.destroy();
  314. return VirtualTestEditor
  315. .create( {
  316. plugins: [ Paragraph, LinkEditing, Enter ],
  317. link: {
  318. targetDecorator: true
  319. }
  320. } )
  321. .then( newEditor => {
  322. editor = newEditor;
  323. model = editor.model;
  324. view = editor.editing.view;
  325. } );
  326. } );
  327. it( 'link.targetDecorator is set as true value', () => {
  328. expect( editor.config.get( 'link.targetDecorator' ) ).to.be.true;
  329. } );
  330. testLinks.forEach( link => {
  331. it( `link: ${ link.url } should be treat as ${ link.external ? 'external' : 'non-external' } link`, () => {
  332. editor.setData( `<p><a href="${ link.url }">foo</a>bar</p>` );
  333. expect( getModelData( model, { withoutSelection: true } ) )
  334. .to.equal( `<paragraph><$text linkHref="${ link.url }">foo</$text>bar</paragraph>` );
  335. if ( link.external ) {
  336. expect( editor.getData() )
  337. .to.equal( `<p><a target="_blank" rel="noopener noreferrer" href="${ link.url }">foo</a>bar</p>` );
  338. } else {
  339. expect( editor.getData() ).to.equal( `<p><a href="${ link.url }">foo</a>bar</p>` );
  340. }
  341. } );
  342. } );
  343. } );
  344. testLinks.forEach( link => {
  345. it( `link: ${ link.url } should not get 'target' and 'rel' attributes`, () => {
  346. editor.setData( `<p><a href="${ link.url }">foo</a>bar</p>` );
  347. expect( getModelData( model, { withoutSelection: true } ) )
  348. .to.equal( `<paragraph><$text linkHref="${ link.url }">foo</$text>bar</paragraph>` );
  349. expect( editor.getData() ).to.equal( `<p><a href="${ link.url }">foo</a>bar</p>` );
  350. } );
  351. } );
  352. } );
  353. describe( 'custom config', () => {
  354. describe( 'mode: automatic', () => {
  355. const testLinks = [
  356. {
  357. url: 'relative/url.html',
  358. attributes: {}
  359. }, {
  360. url: 'http://exmaple.com',
  361. attributes: {
  362. target: '_blank'
  363. }
  364. }, {
  365. url: 'https://example.com/download/link.pdf',
  366. attributes: {
  367. target: '_blank',
  368. download: 'download'
  369. }
  370. }, {
  371. url: 'mailto:some@person.io',
  372. attributes: {
  373. class: 'mail-url'
  374. }
  375. }
  376. ];
  377. beforeEach( () => {
  378. editor.destroy();
  379. return VirtualTestEditor
  380. .create( {
  381. plugins: [ Paragraph, LinkEditing, Enter ],
  382. link: {
  383. targetDecorator: false,
  384. decorators: [
  385. {
  386. mode: 'automatic',
  387. callback: url => url.startsWith( 'http' ),
  388. attributes: {
  389. target: '_blank'
  390. }
  391. }, {
  392. mode: 'automatic',
  393. callback: url => url.includes( 'download' ),
  394. attributes: {
  395. download: 'download'
  396. }
  397. }, {
  398. mode: 'automatic',
  399. callback: url => url.startsWith( 'mailto:' ),
  400. attributes: {
  401. class: 'mail-url'
  402. }
  403. }
  404. ]
  405. }
  406. } )
  407. .then( newEditor => {
  408. editor = newEditor;
  409. model = editor.model;
  410. view = editor.editing.view;
  411. } );
  412. } );
  413. testLinks.forEach( link => {
  414. it( `Link: ${ link.url } should get attributes: ${ JSON.stringify( link.attributes ) }`, () => {
  415. const ORDER = [ 'target', 'download', 'class' ];
  416. const attr = Object.entries( link.attributes ).sort( ( a, b ) => {
  417. const aIndex = ORDER.indexOf( a[ 0 ] );
  418. const bIndex = ORDER.indexOf( b[ 0 ] );
  419. return aIndex - bIndex;
  420. } );
  421. const reducedAttr = attr.reduce( ( acc, cur ) => {
  422. return acc + `${ cur[ 0 ] }="${ cur[ 1 ] }" `;
  423. }, '' );
  424. editor.setData( `<p><a href="${ link.url }">foo</a>bar</p>` );
  425. expect( getModelData( model, { withoutSelection: true } ) )
  426. .to.equal( `<paragraph><$text linkHref="${ link.url }">foo</$text>bar</paragraph>` );
  427. // Order of attributes is important, that's why this is assert is construct in such way.
  428. expect( editor.getData() ).to.equal( `<p><a ${ reducedAttr }href="${ link.url }">foo</a>bar</p>` );
  429. } );
  430. } );
  431. } );
  432. } );
  433. describe( 'custom linkHref converter', () => {
  434. beforeEach( () => {
  435. class CustomLinks extends Plugin {
  436. init() {
  437. const editor = this.editor;
  438. editor.conversion.for( 'downcast' ).add( dispatcher => {
  439. dispatcher.on( 'attribute:linkHref', ( evt, data, conversionApi ) => {
  440. conversionApi.consumable.consume( data.item, 'attribute:linkHref' );
  441. // Very simplified downcast just for test assertion.
  442. const viewWriter = conversionApi.writer;
  443. const linkElement = viewWriter.createAttributeElement(
  444. 'a',
  445. {
  446. href: data.attributeNewValue
  447. }, {
  448. priority: 5
  449. }
  450. );
  451. viewWriter.setCustomProperty( 'link', true, linkElement );
  452. viewWriter.wrap( conversionApi.mapper.toViewRange( data.range ), linkElement );
  453. }, { priority: 'highest' } );
  454. } );
  455. }
  456. }
  457. editor.destroy();
  458. return VirtualTestEditor
  459. .create( {
  460. plugins: [ Paragraph, LinkEditing, Enter, CustomLinks ],
  461. link: {
  462. targetDecorator: true,
  463. }
  464. } )
  465. .then( newEditor => {
  466. editor = newEditor;
  467. model = editor.model;
  468. view = editor.editing.view;
  469. } );
  470. } );
  471. it( 'has possibility to override default one', () => {
  472. editor.setData( '<p><a href="http://example.com">foo</a>bar</p>' );
  473. expect( getModelData( model, { withoutSelection: true } ) )
  474. .to.equal( '<paragraph><$text linkHref="http://example.com">foo</$text>bar</paragraph>' );
  475. expect( editor.getData() ).to.equal( '<p><a href="http://example.com">foo</a>bar</p>' );
  476. } );
  477. } );
  478. } );
  479. } );