linkediting.js 35 KB

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