8
0

linkediting.js 36 KB

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