linkediting.js 17 KB

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