linkediting.js 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545
  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 Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  10. import BoldEditing from '@ckeditor/ckeditor5-basic-styles/src/bold/boldediting';
  11. import ItalicEditing from '@ckeditor/ckeditor5-basic-styles/src/italic/italicediting';
  12. import Clipboard from '@ckeditor/ckeditor5-clipboard/src/clipboard';
  13. import Enter from '@ckeditor/ckeditor5-enter/src/enter';
  14. import DomEventData from '@ckeditor/ckeditor5-engine/src/view/observer/domeventdata';
  15. import ImageEditing from '@ckeditor/ckeditor5-image/src/image/imageediting';
  16. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  17. import Input from '@ckeditor/ckeditor5-typing/src/input';
  18. import Delete from '@ckeditor/ckeditor5-typing/src/delete';
  19. import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  20. import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  21. import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
  22. import { isLinkElement } from '../src/utils';
  23. import '@ckeditor/ckeditor5-core/tests/_utils/assertions/attribute';
  24. /* global document */
  25. describe( 'LinkEditing', () => {
  26. let element, editor, model, view;
  27. beforeEach( async () => {
  28. element = document.createElement( 'div' );
  29. document.body.appendChild( element );
  30. editor = await ClassicTestEditor.create( element, {
  31. plugins: [ Paragraph, LinkEditing, Enter ],
  32. link: {
  33. decorators: {
  34. isExternal: {
  35. mode: 'manual',
  36. label: 'Open in a new window',
  37. attributes: {
  38. target: '_blank',
  39. rel: 'noopener noreferrer'
  40. }
  41. }
  42. }
  43. }
  44. } );
  45. editor.model.schema.extend( '$text', { allowAttributes: 'bold' } );
  46. editor.conversion.attributeToElement( {
  47. model: 'bold',
  48. view: 'b'
  49. } );
  50. model = editor.model;
  51. view = editor.editing.view;
  52. } );
  53. afterEach( async () => {
  54. element.remove();
  55. await editor.destroy();
  56. } );
  57. it( 'should have pluginName', () => {
  58. expect( LinkEditing.pluginName ).to.equal( 'LinkEditing' );
  59. } );
  60. it( 'should be loaded', () => {
  61. expect( editor.plugins.get( LinkEditing ) ).to.be.instanceOf( LinkEditing );
  62. } );
  63. it( 'should set proper schema rules', () => {
  64. expect( model.schema.checkAttribute( [ '$block', '$text' ], 'linkHref' ) ).to.be.true;
  65. expect( model.schema.checkAttribute( [ '$clipboardHolder', '$text' ], 'linkHref' ) ).to.be.true;
  66. expect( model.schema.checkAttribute( [ '$block' ], 'linkHref' ) ).to.be.false;
  67. } );
  68. // Let's check only the minimum to not duplicate `TwoStepCaretMovement` tests.
  69. // Testing minimum is better than testing using spies that might give false positive results.
  70. describe( 'two-step caret movement', () => {
  71. it( 'should be bound to the `linkHref` attribute (LTR)', () => {
  72. const selection = editor.model.document.selection;
  73. // Put selection before the link element.
  74. setModelData( editor.model, '<paragraph>foo[]<$text linkHref="url">b</$text>ar</paragraph>' );
  75. // The selection's gravity should read attributes from the left.
  76. expect( selection ).not.to.have.attribute( 'linkHref' );
  77. // So let's simulate the `keydown` event.
  78. editor.editing.view.document.fire( 'keydown', {
  79. keyCode: keyCodes.arrowright,
  80. preventDefault: () => {},
  81. domTarget: document.body
  82. } );
  83. expect( getModelData( model ) ).to.equal( '<paragraph>foo<$text linkHref="url">[]b</$text>ar</paragraph>' );
  84. // Selection should get the attributes from the right.
  85. expect( selection ).to.have.attribute( 'linkHref' );
  86. expect( selection ).to.have.attribute( 'linkHref', 'url' );
  87. } );
  88. it( 'should be bound to the `linkHref` attribute (RTL)', async () => {
  89. const editor = await ClassicTestEditor.create( element, {
  90. plugins: [ Paragraph, LinkEditing, Enter ],
  91. language: {
  92. content: 'ar'
  93. }
  94. } );
  95. model = editor.model;
  96. view = editor.editing.view;
  97. const selection = editor.model.document.selection;
  98. // Put selection before the link element.
  99. setModelData( editor.model, '<paragraph>foo[]<$text linkHref="url">b</$text>ar</paragraph>' );
  100. // The selection's gravity should read attributes from the left.
  101. expect( selection ).not.to.have.attribute( 'linkHref' );
  102. // So let's simulate the `keydown` event.
  103. editor.editing.view.document.fire( 'keydown', {
  104. keyCode: keyCodes.arrowleft,
  105. preventDefault: () => {},
  106. domTarget: document.body
  107. } );
  108. expect( getModelData( model ) ).to.equal( '<paragraph>foo<$text linkHref="url">[]b</$text>ar</paragraph>' );
  109. // Selection should get the attributes from the right.
  110. expect( selection ).to.have.attribute( 'linkHref' );
  111. expect( selection ).to.have.attribute( 'linkHref', 'url' );
  112. await editor.destroy();
  113. } );
  114. } );
  115. // https://github.com/ckeditor/ckeditor5/issues/6053
  116. describe( 'selection attribute management on paste', () => {
  117. it( 'should remove link atttributes when pasting a link', () => {
  118. setModelData( model, '<paragraph>foo[]</paragraph>' );
  119. model.change( writer => {
  120. model.insertContent( writer.createText( 'INSERTED', { linkHref: 'ckeditor.com' } ) );
  121. } );
  122. expect( getModelData( model ) ).to.equal( '<paragraph>foo<$text linkHref="ckeditor.com">INSERTED</$text>[]</paragraph>' );
  123. expect( [ ...model.document.selection.getAttributeKeys() ] ).to.be.empty;
  124. } );
  125. it( 'should remove all atttributes starting with "link" (e.g. decorator attributes) when pasting a link', () => {
  126. setModelData( model, '<paragraph>foo[]</paragraph>' );
  127. model.change( writer => {
  128. model.insertContent( writer.createText( 'INSERTED', { linkHref: 'ckeditor.com', linkIsExternal: true } ) );
  129. } );
  130. expect( getModelData( model ) ).to.equal(
  131. '<paragraph>' +
  132. 'foo<$text linkHref="ckeditor.com" linkIsExternal="true">INSERTED</$text>[]' +
  133. '</paragraph>'
  134. );
  135. expect( [ ...model.document.selection.getAttributeKeys() ] ).to.be.empty;
  136. } );
  137. it( 'should not remove link atttributes when pasting a non-link content', () => {
  138. setModelData( model, '<paragraph><$text linkHref="ckeditor.com">foo[]</$text></paragraph>' );
  139. model.change( writer => {
  140. model.insertContent( writer.createText( 'INSERTED', { bold: 'true' } ) );
  141. } );
  142. expect( getModelData( model ) ).to.equal(
  143. '<paragraph>' +
  144. '<$text linkHref="ckeditor.com">foo</$text>' +
  145. '<$text bold="true">INSERTED[]</$text>' +
  146. '</paragraph>'
  147. );
  148. expect( model.document.selection ).to.have.attribute( 'bold' );
  149. } );
  150. it( 'should not remove link atttributes when pasting in the middle of a link with the same URL', () => {
  151. setModelData( model, '<paragraph><$text linkHref="ckeditor.com">fo[]o</$text></paragraph>' );
  152. model.change( writer => {
  153. model.insertContent( writer.createText( 'INSERTED', { linkHref: 'ckeditor.com' } ) );
  154. } );
  155. expect( getModelData( model ) ).to.equal( '<paragraph><$text linkHref="ckeditor.com">foINSERTED[]o</$text></paragraph>' );
  156. expect( model.document.selection ).to.have.attribute( 'linkHref' );
  157. } );
  158. it( 'should not remove link atttributes from the selection when pasting before a link when the gravity is overridden', () => {
  159. setModelData( model, '<paragraph>foo[]<$text linkHref="ckeditor.com">bar</$text></paragraph>' );
  160. view.document.fire( 'keydown', {
  161. keyCode: keyCodes.arrowright,
  162. preventDefault: () => {},
  163. domTarget: document.body
  164. } );
  165. expect( model.document.selection ).to.have.property( 'isGravityOverridden', true );
  166. model.change( writer => {
  167. model.insertContent( writer.createText( 'INSERTED', { bold: true } ) );
  168. } );
  169. expect( getModelData( model ) ).to.equal(
  170. '<paragraph>' +
  171. 'foo' +
  172. '<$text bold="true">INSERTED</$text>' +
  173. '<$text linkHref="ckeditor.com">[]bar</$text>' +
  174. '</paragraph>'
  175. );
  176. expect( model.document.selection ).to.have.property( 'isGravityOverridden', true );
  177. expect( model.document.selection ).to.have.attribute( 'linkHref' );
  178. } );
  179. it( 'should not remove link atttributes when pasting a link into another link (different URLs, no merge)', () => {
  180. setModelData( model, '<paragraph><$text linkHref="ckeditor.com">f[]oo</$text></paragraph>' );
  181. model.change( writer => {
  182. model.insertContent( writer.createText( 'INSERTED', { linkHref: 'http://INSERTED' } ) );
  183. } );
  184. expect( getModelData( model ) ).to.equal(
  185. '<paragraph>' +
  186. '<$text linkHref="ckeditor.com">f</$text>' +
  187. '<$text linkHref="http://INSERTED">INSERTED[]</$text>' +
  188. '<$text linkHref="ckeditor.com">oo</$text>' +
  189. '</paragraph>'
  190. );
  191. expect( model.document.selection ).to.have.attribute( 'linkHref' );
  192. } );
  193. it( 'should not remove link atttributes when pasting before another link (different URLs, no merge)', () => {
  194. setModelData( model, '<paragraph>[]<$text linkHref="ckeditor.com">foo</$text></paragraph>' );
  195. expect( model.document.selection ).to.have.property( 'isGravityOverridden', false );
  196. model.change( writer => {
  197. model.insertContent( writer.createText( 'INSERTED', { linkHref: 'http://INSERTED' } ) );
  198. } );
  199. expect( getModelData( model ) ).to.equal(
  200. '<paragraph>' +
  201. '<$text linkHref="http://INSERTED">INSERTED[]</$text>' +
  202. '<$text linkHref="ckeditor.com">foo</$text>' +
  203. '</paragraph>'
  204. );
  205. expect( model.document.selection ).to.have.attribute( 'linkHref' );
  206. expect( model.document.selection ).to.have.attribute( 'linkHref', 'http://INSERTED' );
  207. } );
  208. } );
  209. describe( 'command', () => {
  210. it( 'should register link command', () => {
  211. const command = editor.commands.get( 'link' );
  212. expect( command ).to.be.instanceOf( LinkCommand );
  213. } );
  214. it( 'should register unlink command', () => {
  215. const command = editor.commands.get( 'unlink' );
  216. expect( command ).to.be.instanceOf( UnlinkCommand );
  217. } );
  218. } );
  219. describe( 'data pipeline conversions', () => {
  220. it( 'should convert `<a href="url">` to `linkHref="url"` attribute', () => {
  221. editor.setData( '<p><a href="url">foo</a>bar</p>' );
  222. expect( getModelData( model, { withoutSelection: true } ) )
  223. .to.equal( '<paragraph><$text linkHref="url">foo</$text>bar</paragraph>' );
  224. expect( editor.getData() ).to.equal( '<p><a href="url">foo</a>bar</p>' );
  225. } );
  226. it( 'should be integrated with autoparagraphing', () => {
  227. editor.setData( '<a href="url">foo</a>bar' );
  228. expect( getModelData( model, { withoutSelection: true } ) )
  229. .to.equal( '<paragraph><$text linkHref="url">foo</$text>bar</paragraph>' );
  230. expect( editor.getData() ).to.equal( '<p><a href="url">foo</a>bar</p>' );
  231. } );
  232. // https://github.com/ckeditor/ckeditor5/issues/500
  233. it( 'should not pick up `<a name="foo">`', () => {
  234. editor.setData( '<p><a name="foo">foo</a>bar</p>' );
  235. expect( getModelData( model, { withoutSelection: true } ) )
  236. .to.equal( '<paragraph>foobar</paragraph>' );
  237. } );
  238. // CKEditor 4 does. And CKEditor 5's balloon allows creating such links.
  239. it( 'should pick up `<a href="">`', () => {
  240. editor.setData( '<p><a href="">foo</a>bar</p>' );
  241. expect( getModelData( model, { withoutSelection: true } ) )
  242. .to.equal( '<paragraph><$text linkHref="">foo</$text>bar</paragraph>' );
  243. expect( editor.getData() ).to.equal( '<p><a href="">foo</a>bar</p>' );
  244. } );
  245. // The editor's role is not to filter out potentially malicious data.
  246. // Its job is to not let this code be executed inside the editor (see the test in "editing pipeline conversion").
  247. it( 'should output a link with a potential XSS code', () => {
  248. setModelData( model, '<paragraph>[]</paragraph>' );
  249. model.change( writer => {
  250. writer.insertText( 'foo', { linkHref: 'javascript:alert(1)' }, model.document.selection.getFirstPosition() );
  251. } );
  252. expect( editor.getData() ).to.equal( '<p><a href="javascript:alert(1)">foo</a></p>' );
  253. } );
  254. it( 'should load a link with a potential XSS code', () => {
  255. editor.setData( '<p><a href="javascript:alert(1)">foo</a></p>' );
  256. expect( getModelData( model, { withoutSelection: true } ) )
  257. .to.equal( '<paragraph><$text linkHref="javascript:alert(1)">foo</$text></paragraph>' );
  258. } );
  259. } );
  260. describe( 'editing pipeline conversion', () => {
  261. it( 'should convert attribute', () => {
  262. setModelData( model, '<paragraph><$text linkHref="url">foo</$text>bar</paragraph>' );
  263. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( '<p><a href="url">foo</a>bar</p>' );
  264. } );
  265. it( 'should convert to link element instance', () => {
  266. setModelData( model, '<paragraph><$text linkHref="url">foo</$text>bar</paragraph>' );
  267. const element = editor.editing.view.document.getRoot().getChild( 0 ).getChild( 0 );
  268. expect( isLinkElement( element ) ).to.be.true;
  269. } );
  270. // https://github.com/ckeditor/ckeditor5-link/issues/121
  271. it( 'should should set priority for `linkHref` higher than all other attribute elements', () => {
  272. model.schema.extend( '$text', { allowAttributes: 'foo' } );
  273. editor.conversion.for( 'downcast' ).attributeToElement( { model: 'foo', view: 'f' } );
  274. setModelData( model,
  275. '<paragraph>' +
  276. '<$text linkHref="url">a</$text><$text foo="true" linkHref="url">b</$text><$text linkHref="url">c</$text>' +
  277. '</paragraph>' );
  278. expect( editor.getData() ).to.equal( '<p><a href="url">a<f>b</f>c</a></p>' );
  279. } );
  280. it( 'must not render a link with a potential XSS code', () => {
  281. setModelData( model, '<paragraph><$text linkHref="javascript:alert(1)">[]foo</$text>bar[]</paragraph>' );
  282. expect( getViewData( editor.editing.view, { withoutSelection: true } ) )
  283. .to.equal( '<p><a href="#">foo</a>bar</p>' );
  284. } );
  285. } );
  286. describe( 'link highlighting', () => {
  287. it( 'should convert the highlight to a proper view classes', () => {
  288. setModelData( model,
  289. '<paragraph>foo <$text linkHref="url">b{}ar</$text> baz</paragraph>'
  290. );
  291. expect( model.document.selection ).to.have.attribute( 'linkHref' );
  292. expect( getViewData( view ) ).to.equal(
  293. '<p>foo <a class="ck-link_selected" href="url">b{}ar</a> baz</p>'
  294. );
  295. } );
  296. it( 'should work whenever selection has linkHref attribute - link start', () => {
  297. setModelData( model,
  298. '<paragraph>foo {}<$text linkHref="url">bar</$text> baz</paragraph>'
  299. );
  300. expect( model.document.selection ).to.not.have.attribute( 'linkHref' );
  301. model.change( writer => {
  302. writer.setSelectionAttribute( 'linkHref', 'url' );
  303. } );
  304. expect( model.document.selection ).to.have.attribute( 'linkHref' );
  305. expect( getViewData( view ) ).to.equal(
  306. '<p>foo <a class="ck-link_selected" href="url">{}bar</a> baz</p>'
  307. );
  308. } );
  309. it( 'should work whenever selection has linkHref attribute - link end', () => {
  310. setModelData( model,
  311. '<paragraph>foo <$text linkHref="url">bar</$text>{} baz</paragraph>'
  312. );
  313. expect( model.document.selection ).to.have.attribute( 'linkHref' );
  314. expect( getViewData( view ) ).to.equal(
  315. '<p>foo <a class="ck-link_selected" href="url">bar{}</a> baz</p>'
  316. );
  317. } );
  318. it( 'should render highlight correctly after splitting the link', () => {
  319. setModelData( model,
  320. '<paragraph>foo <$text linkHref="url">li{}nk</$text> baz</paragraph>'
  321. );
  322. editor.execute( 'enter' );
  323. expect( getModelData( model ) ).to.equal(
  324. '<paragraph>foo <$text linkHref="url">li</$text></paragraph>' +
  325. '<paragraph><$text linkHref="url">[]nk</$text> baz</paragraph>'
  326. );
  327. expect( model.document.selection ).to.have.attribute( 'linkHref' );
  328. expect( getViewData( view ) ).to.equal(
  329. '<p>foo <a href="url">li</a></p>' +
  330. '<p><a class="ck-link_selected" href="url">{}nk</a> baz</p>'
  331. );
  332. } );
  333. it( 'should remove classes when selection is moved out from the link', () => {
  334. setModelData( model,
  335. '<paragraph>foo <$text linkHref="url">li{}nk</$text> baz</paragraph>'
  336. );
  337. expect( getViewData( view ) ).to.equal(
  338. '<p>foo <a class="ck-link_selected" href="url">li{}nk</a> baz</p>'
  339. );
  340. model.change( writer => writer.setSelection( model.document.getRoot().getChild( 0 ), 0 ) );
  341. expect( getViewData( view ) ).to.equal(
  342. '<p>{}foo <a href="url">link</a> baz</p>'
  343. );
  344. } );
  345. it( 'should work correctly when selection is moved inside link', () => {
  346. setModelData( model,
  347. '<paragraph>foo <$text linkHref="url">li{}nk</$text> baz</paragraph>'
  348. );
  349. expect( getViewData( view ) ).to.equal(
  350. '<p>foo <a class="ck-link_selected" href="url">li{}nk</a> baz</p>'
  351. );
  352. model.change( writer => writer.setSelection( model.document.getRoot().getChild( 0 ), 5 ) );
  353. expect( getViewData( view ) ).to.equal(
  354. '<p>foo <a class="ck-link_selected" href="url">l{}ink</a> baz</p>'
  355. );
  356. } );
  357. describe( 'downcast conversion integration', () => {
  358. it( 'works for the #insert event', () => {
  359. setModelData( model,
  360. '<paragraph>foo <$text linkHref="url">li{}nk</$text> baz</paragraph>'
  361. );
  362. model.change( writer => {
  363. writer.insertText( 'FOO', { linkHref: 'url' }, model.document.selection.getFirstPosition() );
  364. } );
  365. expect( getViewData( view ) ).to.equal(
  366. '<p>foo <a class="ck-link_selected" href="url">liFOO{}nk</a> baz</p>'
  367. );
  368. } );
  369. it( 'works for the #remove event', () => {
  370. setModelData( model,
  371. '<paragraph>foo <$text linkHref="url">li{}nk</$text> baz</paragraph>'
  372. );
  373. model.change( writer => {
  374. writer.remove( writer.createRange(
  375. writer.createPositionAt( model.document.getRoot().getChild( 0 ), 0 ),
  376. writer.createPositionAt( model.document.getRoot().getChild( 0 ), 5 )
  377. ) );
  378. } );
  379. expect( getViewData( view ) ).to.equal(
  380. '<p><a class="ck-link_selected" href="url">i{}nk</a> baz</p>'
  381. );
  382. } );
  383. it( 'works for the #attribute event', () => {
  384. setModelData( model,
  385. '<paragraph>foo <$text linkHref="url">li{}nk</$text> baz</paragraph>'
  386. );
  387. model.change( writer => {
  388. writer.setAttribute( 'linkHref', 'new-url', writer.createRange(
  389. model.document.selection.getFirstPosition().getShiftedBy( -1 ),
  390. model.document.selection.getFirstPosition().getShiftedBy( 1 ) )
  391. );
  392. } );
  393. expect( getViewData( view ) ).to.equal(
  394. '<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>'
  395. );
  396. } );
  397. it( 'works for the #selection event', () => {
  398. setModelData( model,
  399. '<paragraph>foo <$text linkHref="url">li{}nk</$text> baz</paragraph>'
  400. );
  401. model.change( writer => {
  402. writer.setSelection( writer.createRange(
  403. model.document.selection.getFirstPosition().getShiftedBy( -1 ),
  404. model.document.selection.getFirstPosition().getShiftedBy( 1 ) )
  405. );
  406. } );
  407. expect( getViewData( view ) ).to.equal(
  408. '<p>foo <a class="ck-link_selected" href="url">l{in}k</a> baz</p>'
  409. );
  410. } );
  411. it( 'works for the addMarker and removeMarker events', () => {
  412. editor.conversion.for( 'editingDowncast' ).markerToHighlight( { model: 'fooMarker', view: {} } );
  413. setModelData( model,
  414. '<paragraph>foo <$text linkHref="url">li{}nk</$text> baz</paragraph>'
  415. );
  416. model.change( writer => {
  417. const range = writer.createRange(
  418. writer.createPositionAt( model.document.getRoot().getChild( 0 ), 0 ),
  419. writer.createPositionAt( model.document.getRoot().getChild( 0 ), 5 )
  420. );
  421. writer.addMarker( 'fooMarker', { range, usingOperation: true } );
  422. } );
  423. expect( getViewData( view ) ).to.equal(
  424. '<p><span>foo </span><a class="ck-link_selected" href="url"><span>l</span>i{}nk</a> baz</p>'
  425. );
  426. model.change( writer => writer.removeMarker( 'fooMarker' ) );
  427. expect( getViewData( view ) ).to.equal(
  428. '<p>foo <a class="ck-link_selected" href="url">li{}nk</a> baz</p>'
  429. );
  430. } );
  431. } );
  432. } );
  433. describe( 'link attributes decorator', () => {
  434. describe( 'default behavior', () => {
  435. const testLinks = [
  436. {
  437. external: true,
  438. url: 'http://example.com'
  439. }, {
  440. external: true,
  441. url: 'https://cksource.com'
  442. }, {
  443. external: false,
  444. url: 'ftp://server.io'
  445. }, {
  446. external: true,
  447. url: '//schemaless.org'
  448. }, {
  449. external: false,
  450. url: 'www.ckeditor.com'
  451. }, {
  452. external: false,
  453. url: '/relative/url.html'
  454. }, {
  455. external: false,
  456. url: 'another/relative/url.html'
  457. }, {
  458. external: false,
  459. url: '#anchor'
  460. }, {
  461. external: false,
  462. url: 'mailto:some@user.org'
  463. }, {
  464. external: false,
  465. url: 'tel:123456789'
  466. }
  467. ];
  468. it( 'link.addTargetToExternalLinks is predefined as false value', () => {
  469. expect( editor.config.get( 'link.addTargetToExternalLinks' ) ).to.be.false;
  470. } );
  471. describe( 'for link.addTargetToExternalLinks = false', () => {
  472. let editor, model;
  473. beforeEach( async () => {
  474. editor = await ClassicTestEditor.create( element, {
  475. plugins: [ Paragraph, LinkEditing, Enter ],
  476. link: {
  477. addTargetToExternalLinks: true
  478. }
  479. } );
  480. model = editor.model;
  481. view = editor.editing.view;
  482. } );
  483. afterEach( async () => {
  484. await editor.destroy();
  485. } );
  486. it( 'link.addTargetToExternalLinks is set as true value', () => {
  487. expect( editor.config.get( 'link.addTargetToExternalLinks' ) ).to.be.true;
  488. } );
  489. testLinks.forEach( link => {
  490. it( `link: ${ link.url } should be treat as ${ link.external ? 'external' : 'non-external' } link`, () => {
  491. editor.setData( `<p><a href="${ link.url }">foo</a>bar</p>` );
  492. expect( getModelData( model, { withoutSelection: true } ) )
  493. .to.equal( `<paragraph><$text linkHref="${ link.url }">foo</$text>bar</paragraph>` );
  494. if ( link.external ) {
  495. expect( editor.getData() )
  496. .to.equal( `<p><a target="_blank" rel="noopener noreferrer" href="${ link.url }">foo</a>bar</p>` );
  497. } else {
  498. expect( editor.getData() ).to.equal( `<p><a href="${ link.url }">foo</a>bar</p>` );
  499. }
  500. } );
  501. } );
  502. } );
  503. testLinks.forEach( link => {
  504. it( `link: ${ link.url } should not get 'target' and 'rel' attributes`, () => {
  505. editor.setData( `<p><a href="${ link.url }">foo</a>bar</p>` );
  506. expect( getModelData( model, { withoutSelection: true } ) )
  507. .to.equal( `<paragraph><$text linkHref="${ link.url }">foo</$text>bar</paragraph>` );
  508. expect( editor.getData() ).to.equal( `<p><a href="${ link.url }">foo</a>bar</p>` );
  509. } );
  510. } );
  511. } );
  512. describe( 'custom config', () => {
  513. describe( 'mode: automatic', () => {
  514. const testLinks = [
  515. {
  516. url: 'relative/url.html',
  517. attributes: {}
  518. }, {
  519. url: 'http://exmaple.com',
  520. attributes: {
  521. target: '_blank'
  522. }
  523. }, {
  524. url: 'https://example.com/download/link.pdf',
  525. attributes: {
  526. target: '_blank',
  527. download: 'download'
  528. }
  529. }, {
  530. url: 'mailto:some@person.io',
  531. attributes: {
  532. class: 'mail-url'
  533. }
  534. }
  535. ];
  536. beforeEach( async () => {
  537. await editor.destroy();
  538. editor = await ClassicTestEditor.create( element, {
  539. plugins: [ Paragraph, LinkEditing, Enter ],
  540. link: {
  541. addTargetToExternalLinks: false,
  542. decorators: {
  543. isExternal: {
  544. mode: 'automatic',
  545. callback: url => url.startsWith( 'http' ),
  546. attributes: {
  547. target: '_blank'
  548. }
  549. },
  550. isDownloadable: {
  551. mode: 'automatic',
  552. callback: url => url.includes( 'download' ),
  553. attributes: {
  554. download: 'download'
  555. }
  556. },
  557. isMail: {
  558. mode: 'automatic',
  559. callback: url => url.startsWith( 'mailto:' ),
  560. attributes: {
  561. class: 'mail-url'
  562. }
  563. }
  564. }
  565. }
  566. } );
  567. model = editor.model;
  568. view = editor.editing.view;
  569. } );
  570. testLinks.forEach( link => {
  571. it( `Link: ${ link.url } should get attributes: ${ JSON.stringify( link.attributes ) }`, () => {
  572. const ORDER = [ 'target', 'download', 'class' ];
  573. const attr = Object.entries( link.attributes ).sort( ( a, b ) => {
  574. const aIndex = ORDER.indexOf( a[ 0 ] );
  575. const bIndex = ORDER.indexOf( b[ 0 ] );
  576. return aIndex - bIndex;
  577. } );
  578. const reducedAttr = attr.reduce( ( acc, cur ) => {
  579. return acc + `${ cur[ 0 ] }="${ cur[ 1 ] }" `;
  580. }, '' );
  581. editor.setData( `<p><a href="${ link.url }">foo</a>bar</p>` );
  582. expect( getModelData( model, { withoutSelection: true } ) )
  583. .to.equal( `<paragraph><$text linkHref="${ link.url }">foo</$text>bar</paragraph>` );
  584. // Order of attributes is important, that's why this is assert is construct in such way.
  585. expect( editor.getData() ).to.equal( `<p><a ${ reducedAttr }href="${ link.url }">foo</a>bar</p>` );
  586. } );
  587. } );
  588. it( 'stores decorators in LinkCommand#automaticDecorators collection', () => {
  589. expect( editor.commands.get( 'link' ).automaticDecorators.length ).to.equal( 3 );
  590. } );
  591. } );
  592. } );
  593. describe( 'custom linkHref converter', () => {
  594. beforeEach( async () => {
  595. class CustomLinks extends Plugin {
  596. init() {
  597. const editor = this.editor;
  598. editor.conversion.for( 'downcast' ).add( dispatcher => {
  599. dispatcher.on( 'attribute:linkHref', ( evt, data, conversionApi ) => {
  600. conversionApi.consumable.consume( data.item, 'attribute:linkHref' );
  601. // Very simplified downcast just for test assertion.
  602. const viewWriter = conversionApi.writer;
  603. const linkElement = viewWriter.createAttributeElement(
  604. 'a',
  605. {
  606. href: data.attributeNewValue
  607. }, {
  608. priority: 5
  609. }
  610. );
  611. viewWriter.setCustomProperty( 'link', true, linkElement );
  612. viewWriter.wrap( conversionApi.mapper.toViewRange( data.range ), linkElement );
  613. }, { priority: 'highest' } );
  614. } );
  615. }
  616. }
  617. await editor.destroy();
  618. editor = await ClassicTestEditor.create( element, {
  619. plugins: [ Paragraph, LinkEditing, Enter, CustomLinks ],
  620. link: {
  621. addTargetToExternalLinks: true
  622. }
  623. } );
  624. model = editor.model;
  625. view = editor.editing.view;
  626. } );
  627. it( 'has possibility to override default one', () => {
  628. editor.setData( '<p><a href="http://example.com">foo</a>bar</p>' );
  629. expect( getModelData( model, { withoutSelection: true } ) )
  630. .to.equal( '<paragraph><$text linkHref="http://example.com">foo</$text>bar</paragraph>' );
  631. expect( editor.getData() ).to.equal( '<p><a href="http://example.com">foo</a>bar</p>' );
  632. } );
  633. } );
  634. describe( 'upcast converter', () => {
  635. let element, editor;
  636. beforeEach( () => {
  637. element = document.createElement( 'div' );
  638. document.body.appendChild( element );
  639. } );
  640. afterEach( () => {
  641. element.remove();
  642. } );
  643. it( 'should upcast attributes from initial data', async () => {
  644. editor = await ClassicTestEditor.create( element, {
  645. initialData: '<p><a href="url" target="_blank" rel="noopener noreferrer" download="file">Foo</a>' +
  646. '<a href="example.com" download="file">Bar</a></p>',
  647. plugins: [ Paragraph, LinkEditing, Enter ],
  648. link: {
  649. decorators: {
  650. isExternal: {
  651. mode: 'manual',
  652. label: 'Open in a new window',
  653. attributes: {
  654. target: '_blank',
  655. rel: 'noopener noreferrer'
  656. }
  657. },
  658. isDownloadable: {
  659. mode: 'manual',
  660. label: 'Downloadable',
  661. attributes: {
  662. download: 'file'
  663. }
  664. }
  665. }
  666. }
  667. } );
  668. model = editor.model;
  669. expect( getModelData( model, { withoutSelection: true } ) ).to.equal(
  670. '<paragraph>' +
  671. '<$text linkHref="url" linkIsDownloadable="true" linkIsExternal="true">Foo</$text>' +
  672. '<$text linkHref="example.com" linkIsDownloadable="true">Bar</$text>' +
  673. '</paragraph>'
  674. );
  675. await editor.destroy();
  676. } );
  677. it( 'should not upcast partial and incorrect attributes', async () => {
  678. editor = await ClassicTestEditor.create( element, {
  679. initialData: '<p><a href="url" target="_blank" download="something">Foo</a>' +
  680. '<a href="example.com" download="test">Bar</a></p>',
  681. plugins: [ Paragraph, LinkEditing, Enter ],
  682. link: {
  683. decorators: {
  684. isExternal: {
  685. mode: 'manual',
  686. label: 'Open in a new window',
  687. attributes: {
  688. target: '_blank',
  689. rel: 'noopener noreferrer'
  690. }
  691. },
  692. isDownloadable: {
  693. mode: 'manual',
  694. label: 'Downloadable',
  695. attributes: {
  696. download: 'file'
  697. }
  698. }
  699. }
  700. }
  701. } );
  702. model = editor.model;
  703. expect( getModelData( model, { withoutSelection: true } ) ).to.equal(
  704. '<paragraph>' +
  705. '<$text linkHref="url">Foo</$text>' +
  706. '<$text linkHref="example.com">Bar</$text>' +
  707. '</paragraph>'
  708. );
  709. await editor.destroy();
  710. } );
  711. } );
  712. } );
  713. // https://github.com/ckeditor/ckeditor5/issues/1016
  714. describe( 'typing around the link after a click', () => {
  715. let editor;
  716. beforeEach( async () => {
  717. editor = await ClassicTestEditor.create( element, {
  718. plugins: [ Paragraph, LinkEditing, Enter, Input, BoldEditing ],
  719. link: {
  720. decorators: {
  721. isFoo: {
  722. mode: 'manual',
  723. label: 'Foo',
  724. attributes: {
  725. class: 'foo'
  726. }
  727. },
  728. isBar: {
  729. mode: 'manual',
  730. label: 'Bar',
  731. attributes: {
  732. target: '_blank'
  733. }
  734. }
  735. }
  736. }
  737. } );
  738. model = editor.model;
  739. view = editor.editing.view;
  740. model.schema.extend( '$text', {
  741. allowIn: '$root',
  742. allowAttributes: [ 'linkIsFoo', 'linkIsBar' ]
  743. } );
  744. } );
  745. afterEach( async () => {
  746. await editor.destroy();
  747. } );
  748. it( 'should insert content after the link', () => {
  749. setModelData( model, '<paragraph><$text linkHref="url">Bar[]</$text></paragraph>' );
  750. editor.editing.view.document.fire( 'mousedown' );
  751. editor.editing.view.document.fire( 'selectionChange', {
  752. newSelection: view.document.selection
  753. } );
  754. expect( getModelData( model ) ).to.equal( '<paragraph><$text linkHref="url">Bar</$text>[]</paragraph>' );
  755. editor.execute( 'input', { text: 'Foo' } );
  756. expect( getModelData( model ) ).to.equal( '<paragraph><$text linkHref="url">Bar</$text>Foo[]</paragraph>' );
  757. } );
  758. it( 'should insert content before the link', () => {
  759. setModelData( model, '<paragraph><$text linkHref="url">[]Bar</$text></paragraph>' );
  760. editor.editing.view.document.fire( 'mousedown' );
  761. editor.editing.view.document.fire( 'selectionChange', {
  762. newSelection: view.document.selection
  763. } );
  764. expect( getModelData( model ) ).to.equal( '<paragraph>[]<$text linkHref="url">Bar</$text></paragraph>' );
  765. editor.execute( 'input', { text: 'Foo' } );
  766. expect( getModelData( model ) ).to.equal( '<paragraph>Foo[]<$text linkHref="url">Bar</$text></paragraph>' );
  767. } );
  768. it( 'should insert content to the link if clicked inside it', () => {
  769. setModelData( model, '<paragraph><$text linkHref="url">B[]ar</$text></paragraph>' );
  770. editor.editing.view.document.fire( 'mousedown' );
  771. editor.editing.view.document.fire( 'selectionChange', {
  772. newSelection: view.document.selection
  773. } );
  774. expect( getModelData( model ) ).to.equal( '<paragraph><$text linkHref="url">B[]ar</$text></paragraph>' );
  775. editor.execute( 'input', { text: 'ar. B' } );
  776. expect( getModelData( model ) ).to.equal( '<paragraph><$text linkHref="url">Bar. B[]ar</$text></paragraph>' );
  777. } );
  778. it( 'should insert content between two links (selection at the end of the first link)', () => {
  779. setModelData( model, '<paragraph><$text linkHref="foo">Foo[]</$text><$text linkHref="bar">Bar</$text></paragraph>' );
  780. editor.editing.view.document.fire( 'mousedown' );
  781. editor.editing.view.document.fire( 'selectionChange', {
  782. newSelection: view.document.selection
  783. } );
  784. expect( getModelData( model ) ).to.equal(
  785. '<paragraph><$text linkHref="foo">Foo</$text>[]<$text linkHref="bar">Bar</$text></paragraph>'
  786. );
  787. editor.execute( 'input', { text: 'Foo' } );
  788. expect( getModelData( model ) ).to.equal(
  789. '<paragraph><$text linkHref="foo">Foo</$text>Foo[]<$text linkHref="bar">Bar</$text></paragraph>'
  790. );
  791. } );
  792. it( 'should insert content between two links (selection at the beginning of the second link)', () => {
  793. setModelData( model, '<paragraph><$text linkHref="foo">Foo</$text><$text linkHref="bar">[]Bar</$text></paragraph>' );
  794. editor.editing.view.document.fire( 'mousedown' );
  795. editor.editing.view.document.fire( 'selectionChange', {
  796. newSelection: view.document.selection
  797. } );
  798. expect( getModelData( model ) ).to.equal(
  799. '<paragraph><$text linkHref="foo">Foo</$text>[]<$text linkHref="bar">Bar</$text></paragraph>'
  800. );
  801. editor.execute( 'input', { text: 'Foo' } );
  802. expect( getModelData( model ) ).to.equal(
  803. '<paragraph><$text linkHref="foo">Foo</$text>Foo[]<$text linkHref="bar">Bar</$text></paragraph>'
  804. );
  805. } );
  806. it( 'should not touch other attributes than `linkHref`', () => {
  807. setModelData( model, '<paragraph><$text bold="true" linkHref="url">Bar[]</$text></paragraph>' );
  808. editor.editing.view.document.fire( 'mousedown' );
  809. editor.editing.view.document.fire( 'selectionChange', {
  810. newSelection: view.document.selection
  811. } );
  812. expect( getModelData( model ) ).to.equal(
  813. '<paragraph><$text bold="true" linkHref="url">Bar</$text><$text bold="true">[]</$text></paragraph>'
  814. );
  815. editor.execute( 'input', { text: 'Foo' } );
  816. expect( getModelData( model ) ).to.equal(
  817. '<paragraph><$text bold="true" linkHref="url">Bar</$text><$text bold="true">Foo[]</$text></paragraph>'
  818. );
  819. } );
  820. it( 'should do nothing if the text was not clicked', () => {
  821. setModelData( model, '<paragraph><$text linkHref="url">Bar[]</$text></paragraph>' );
  822. editor.editing.view.document.fire( 'selectionChange', {
  823. newSelection: view.document.selection
  824. } );
  825. expect( getModelData( model ) ).to.equal( '<paragraph><$text linkHref="url">Bar[]</$text></paragraph>' );
  826. } );
  827. it( 'should do nothing if the selection is not collapsed after the click', () => {
  828. setModelData( model, '<paragraph>[<$text linkHref="url">Bar</$text>]</paragraph>' );
  829. editor.editing.view.document.fire( 'mousedown' );
  830. editor.editing.view.document.fire( 'selectionChange', {
  831. newSelection: view.document.selection
  832. } );
  833. expect( getModelData( model ) ).to.equal( '<paragraph>[<$text linkHref="url">Bar</$text>]</paragraph>' );
  834. } );
  835. it( 'should do nothing if the text is not a link', () => {
  836. setModelData( model, '<paragraph><$text bold="true">Bar[]</$text></paragraph>' );
  837. editor.editing.view.document.fire( 'mousedown' );
  838. editor.editing.view.document.fire( 'selectionChange', {
  839. newSelection: view.document.selection
  840. } );
  841. expect( getModelData( model ) ).to.equal( '<paragraph><$text bold="true">Bar[]</$text></paragraph>' );
  842. } );
  843. it( 'should remove manual decorators', () => {
  844. setModelData( model, '<paragraph><$text linkIsFoo="true" linkIsBar="true" linkHref="url">Bar[]</$text></paragraph>' );
  845. editor.editing.view.document.fire( 'mousedown' );
  846. editor.editing.view.document.fire( 'selectionChange', {
  847. newSelection: view.document.selection
  848. } );
  849. expect( getModelData( model ) ).to.equal(
  850. '<paragraph><$text linkHref="url" linkIsBar="true" linkIsFoo="true">Bar</$text>[]</paragraph>'
  851. );
  852. editor.execute( 'input', { text: 'Foo' } );
  853. expect( getModelData( model ) ).to.equal(
  854. '<paragraph><$text linkHref="url" linkIsBar="true" linkIsFoo="true">Bar</$text>Foo[]</paragraph>'
  855. );
  856. } );
  857. } );
  858. // https://github.com/ckeditor/ckeditor5/issues/4762
  859. describe( 'typing over the link', () => {
  860. let editor;
  861. beforeEach( async () => {
  862. editor = await ClassicTestEditor.create( element, {
  863. plugins: [ Paragraph, LinkEditing, Enter, BoldEditing, ItalicEditing, ImageEditing ],
  864. link: {
  865. decorators: {
  866. isFoo: {
  867. mode: 'manual',
  868. label: 'Foo',
  869. attributes: {
  870. class: 'foo'
  871. }
  872. },
  873. isBar: {
  874. mode: 'manual',
  875. label: 'Bar',
  876. attributes: {
  877. target: '_blank'
  878. }
  879. }
  880. }
  881. }
  882. } );
  883. model = editor.model;
  884. view = editor.editing.view;
  885. model.schema.extend( '$text', {
  886. allowIn: '$root',
  887. allowAttributes: [ 'linkIsFoo', 'linkIsBar' ]
  888. } );
  889. } );
  890. afterEach( async () => {
  891. await editor.destroy();
  892. } );
  893. it( 'should require Clipboard plugin', () => {
  894. expect( LinkEditing.requires.includes( Clipboard ) ).to.equal( true );
  895. } );
  896. it( 'should require Input plugin', () => {
  897. expect( LinkEditing.requires.includes( Input ) ).to.equal( true );
  898. } );
  899. it( 'should preserve selection attributes when the entire link is selected', () => {
  900. setModelData( model,
  901. '<paragraph>This is [<$text linkHref="foo">Foo</$text>] from <$text linkHref="bar">Bar</$text>.</paragraph>'
  902. );
  903. editor.execute( 'input', {
  904. text: 'Abcde'
  905. } );
  906. expect( getModelData( model ) ).to.equal(
  907. '<paragraph>This is <$text linkHref="foo">Abcde</$text>[] from <$text linkHref="bar">Bar</$text>.</paragraph>'
  908. );
  909. } );
  910. it( 'should preserve selection attributes when the entire link is selected (mixed attributes in the link)', () => {
  911. setModelData( model,
  912. '<paragraph>' +
  913. 'This is [' +
  914. '<$text linkHref="foo" italic="true">F</$text>' +
  915. '<$text linkHref="foo" bold="true">o</$text>' +
  916. '<$text linkHref="foo" bold="true" italic="true">o</$text>' +
  917. '<$text linkHref="foo" bold="true">B</$text>' +
  918. '<$text linkHref="foo" bold="true" italic="true">a</$text>' +
  919. '<$text linkHref="foo">r</$text>]' +
  920. ' from ' +
  921. '<$text linkHref="bar">Bar</$text>' +
  922. '.' +
  923. '</paragraph>'
  924. );
  925. editor.execute( 'input', {
  926. text: 'Abcde'
  927. } );
  928. expect( getModelData( model ) ).to.equal(
  929. '<paragraph>' +
  930. 'This is ' +
  931. '<$text italic="true" linkHref="foo">Abcde</$text>' +
  932. '<$text italic="true">[]</$text>' +
  933. ' from ' +
  934. '<$text linkHref="bar">Bar</$text>' +
  935. '.' +
  936. '</paragraph>'
  937. );
  938. } );
  939. it( 'should preserve selection attributes when the selection starts at the beginning of the link', () => {
  940. setModelData( model,
  941. '<paragraph>This is [<$text linkHref="foo">Fo]o</$text> from <$text linkHref="bar">Bar</$text>.</paragraph>'
  942. );
  943. editor.execute( 'input', {
  944. text: 'Abcde'
  945. } );
  946. expect( getModelData( model ) ).to.equal(
  947. '<paragraph>This is <$text linkHref="foo">Abcde[]o</$text> from <$text linkHref="bar">Bar</$text>.</paragraph>'
  948. );
  949. } );
  950. it( 'should preserve selection attributes when it starts at the beginning of the link (mixed attributes in the link)', () => {
  951. setModelData( model,
  952. '<paragraph>' +
  953. 'This is [' +
  954. '<$text linkHref="foo" italic="true">F</$text>' +
  955. '<$text linkHref="foo" bold="true">o</$text>' +
  956. '<$text linkHref="foo" bold="true" italic="true">o</$text>' +
  957. '<$text linkHref="foo" bold="true">B</$text>' +
  958. '<$text linkHref="foo" bold="true" italic="true">a]</$text>' +
  959. '<$text linkHref="foo">r</$text>' +
  960. ' from ' +
  961. '<$text linkHref="bar">Bar</$text>' +
  962. '.' +
  963. '</paragraph>'
  964. );
  965. editor.execute( 'input', {
  966. text: 'Abcde'
  967. } );
  968. expect( getModelData( model ) ).to.equal(
  969. '<paragraph>' +
  970. 'This is ' +
  971. '<$text italic="true" linkHref="foo">Abcde[]</$text>' +
  972. '<$text linkHref="foo">r</$text>' +
  973. ' from ' +
  974. '<$text linkHref="bar">Bar</$text>' +
  975. '.' +
  976. '</paragraph>'
  977. );
  978. } );
  979. it( 'should preserve all attributes of the link node (decorators check)', () => {
  980. setModelData( model,
  981. '<paragraph>' +
  982. 'This is ' +
  983. '<$text linkIsFoo="true" linkIsBar="true" linkHref="foo">[Foo]</$text>' +
  984. ' from ' +
  985. '<$text linkHref="bar">Bar</$text>' +
  986. '.' +
  987. '</paragraph>'
  988. );
  989. editor.execute( 'input', {
  990. text: 'Abcde'
  991. } );
  992. expect( getModelData( model ) ).to.equal(
  993. '<paragraph>' +
  994. 'This is ' +
  995. '<$text linkHref="foo" linkIsBar="true" linkIsFoo="true">Abcde</$text>[]' +
  996. ' from ' +
  997. '<$text linkHref="bar">Bar</$text>' +
  998. '.' +
  999. '</paragraph>'
  1000. );
  1001. } );
  1002. it( 'should not preserve selection attributes when the changes are not caused by typing', () => {
  1003. setModelData( model,
  1004. '<paragraph>This is [<$text linkHref="foo">Foo</$text>] from <$text linkHref="bar">Bar</$text>.</paragraph>'
  1005. );
  1006. model.change( writer => {
  1007. model.deleteContent( model.document.selection );
  1008. model.insertContent( writer.createText( 'Abcde' ) );
  1009. } );
  1010. expect( getModelData( model ) ).to.equal(
  1011. '<paragraph>This is Abcde[] from <$text linkHref="bar">Bar</$text>.</paragraph>'
  1012. );
  1013. } );
  1014. it( 'should not preserve selection attributes when the changes are not caused by typing (pasting check)', () => {
  1015. setModelData( model,
  1016. '<paragraph>This is [<$text linkHref="foo">Foo</$text>] from <$text linkHref="bar">Bar</$text>.</paragraph>'
  1017. );
  1018. view.document.fire( 'paste', {
  1019. dataTransfer: createDataTransfer( {
  1020. 'text/html': '<p>Abcde</p>',
  1021. 'text/plain': 'Abcde'
  1022. } ),
  1023. preventDefault: sinon.spy(),
  1024. stopPropagation: sinon.spy()
  1025. } );
  1026. expect( getModelData( model ) ).to.equal(
  1027. '<paragraph>This is Abcde[] from <$text linkHref="bar">Bar</$text>.</paragraph>'
  1028. );
  1029. } );
  1030. it( 'should not preserve selection attributes when typed after cutting the content', () => {
  1031. setModelData( model,
  1032. '<paragraph>This is [<$text linkHref="foo">Foo</$text>] from <$text linkHref="bar">Bar</$text>.</paragraph>'
  1033. );
  1034. view.document.fire( 'cut', {
  1035. dataTransfer: createDataTransfer(),
  1036. preventDefault: sinon.spy(),
  1037. stopPropagation: sinon.spy()
  1038. } );
  1039. editor.execute( 'input', {
  1040. text: 'Abcde'
  1041. } );
  1042. expect( getModelData( model ) ).to.equal(
  1043. '<paragraph>This is Abcde[] from <$text linkHref="bar">Bar</$text>.</paragraph>'
  1044. );
  1045. } );
  1046. it( 'should not preserve anything if selected an element instead of text', () => {
  1047. setModelData( model,
  1048. '[<image src="/assets/sample.png"></image>]'
  1049. );
  1050. editor.execute( 'input', {
  1051. text: 'Abcde'
  1052. } );
  1053. expect( getModelData( model ) ).to.equal(
  1054. 'Abcde[]'
  1055. );
  1056. } );
  1057. it( 'should not preserve anything if selected text does not have the `linkHref` attribute`', () => {
  1058. setModelData( model,
  1059. '<paragraph>This is [<$text bold="foo">Foo</$text>] from <$text linkHref="bar">Bar</$text>.</paragraph>'
  1060. );
  1061. editor.execute( 'input', {
  1062. text: 'Abcde'
  1063. } );
  1064. expect( getModelData( model ) ).to.equal(
  1065. '<paragraph>This is Abcde[] from <$text linkHref="bar">Bar</$text>.</paragraph>'
  1066. );
  1067. } );
  1068. it( 'should not preserve selection attributes when the entire link is selected and pressed "Backspace"', () => {
  1069. setModelData( model,
  1070. '<paragraph>This is [<$text linkHref="foo">Foo</$text>] from <$text linkHref="bar">Bar</$text>.</paragraph>'
  1071. );
  1072. view.document.fire( 'delete', new DomEventData( view.document, {
  1073. keyCode: keyCodes.backspace,
  1074. preventDefault: () => {}
  1075. } ) );
  1076. editor.execute( 'input', {
  1077. text: 'Abcde'
  1078. } );
  1079. expect( getModelData( model ) ).to.equal(
  1080. '<paragraph>This is Abcde[] from <$text linkHref="bar">Bar</$text>.</paragraph>'
  1081. );
  1082. } );
  1083. it( 'should not preserve selection attributes when the entire link is selected and pressed "Delete"', () => {
  1084. setModelData( model,
  1085. '<paragraph>This is [<$text linkHref="foo">Foo</$text>] from <$text linkHref="bar">Bar</$text>.</paragraph>'
  1086. );
  1087. view.document.fire( 'delete', new DomEventData( view.document, {
  1088. keyCode: keyCodes.delete,
  1089. preventDefault: () => {}
  1090. } ) );
  1091. editor.execute( 'input', {
  1092. text: 'Abcde'
  1093. } );
  1094. expect( getModelData( model ) ).to.equal(
  1095. '<paragraph>This is Abcde[] from <$text linkHref="bar">Bar</$text>.</paragraph>'
  1096. );
  1097. } );
  1098. it( 'should not preserve selection attributes when selected different links', () => {
  1099. setModelData( model,
  1100. '<paragraph>This is <$text linkHref="foo">[Foo</$text> from <$text linkHref="bar">Bar]</$text>.</paragraph>'
  1101. );
  1102. editor.execute( 'input', {
  1103. text: 'Abcde'
  1104. } );
  1105. expect( getModelData( model ) ).to.equal( '<paragraph>This is Abcde[].</paragraph>' );
  1106. } );
  1107. it( 'should not preserve selection attributes when selected more than single link (start of the selection)', () => {
  1108. setModelData( model,
  1109. '<paragraph>This is[ <$text linkHref="foo">Foo]</$text> from <$text linkHref="bar">Bar</$text>.</paragraph>'
  1110. );
  1111. editor.execute( 'input', {
  1112. text: 'Abcde'
  1113. } );
  1114. expect( getModelData( model ) ).to.equal(
  1115. '<paragraph>This isAbcde[] from <$text linkHref="bar">Bar</$text>.</paragraph>'
  1116. );
  1117. } );
  1118. it( 'should not preserve selection attributes when selected more than single link (end of the selection)', () => {
  1119. setModelData( model,
  1120. '<paragraph>This is <$text linkHref="foo">[Foo</$text> ]from <$text linkHref="bar">Bar</$text>.</paragraph>'
  1121. );
  1122. editor.execute( 'input', {
  1123. text: 'Abcde'
  1124. } );
  1125. expect( getModelData( model ) ).to.equal(
  1126. '<paragraph>This is Abcde[]from <$text linkHref="bar">Bar</$text>.</paragraph>'
  1127. );
  1128. } );
  1129. function createDataTransfer( data ) {
  1130. return {
  1131. getData( type ) {
  1132. return data[ type ];
  1133. },
  1134. setData() {}
  1135. };
  1136. }
  1137. } );
  1138. // https://github.com/ckeditor/ckeditor5/issues/7521
  1139. describe( 'removing a character before the link element', () => {
  1140. let editor;
  1141. beforeEach( async () => {
  1142. editor = await ClassicTestEditor.create( element, {
  1143. plugins: [ Paragraph, LinkEditing, Delete, BoldEditing ],
  1144. link: {
  1145. decorators: {
  1146. isFoo: {
  1147. mode: 'manual',
  1148. label: 'Foo',
  1149. attributes: {
  1150. class: 'foo'
  1151. }
  1152. },
  1153. isBar: {
  1154. mode: 'manual',
  1155. label: 'Bar',
  1156. attributes: {
  1157. target: '_blank'
  1158. }
  1159. }
  1160. }
  1161. }
  1162. } );
  1163. model = editor.model;
  1164. view = editor.editing.view;
  1165. model.schema.extend( '$text', {
  1166. allowIn: '$root',
  1167. allowAttributes: [ 'linkIsFoo', 'linkIsBar' ]
  1168. } );
  1169. } );
  1170. afterEach( async () => {
  1171. await editor.destroy();
  1172. } );
  1173. it( 'should not preserve the `linkHref` attribute when deleting content after the link', () => {
  1174. setModelData( model, '<paragraph>Foo <$text linkHref="url">Bar</$text> []</paragraph>' );
  1175. expect( model.document.selection.hasAttribute( 'linkHref' ), 'initial state' ).to.equal( false );
  1176. view.document.fire( 'delete', new DomEventData( view.document, {
  1177. keyCode: keyCodes.backspace,
  1178. preventDefault: () => {}
  1179. } ) );
  1180. expect( model.document.selection.hasAttribute( 'linkHref' ), 'removing space after the link' ).to.equal( false );
  1181. view.document.fire( 'delete', new DomEventData( view.document, {
  1182. keyCode: keyCodes.backspace,
  1183. preventDefault: () => {}
  1184. } ) );
  1185. expect( model.document.selection.hasAttribute( 'linkHref' ), 'removing a character in the link' ).to.equal( false );
  1186. expect( getModelData( model ) ).to.equal( '<paragraph>Foo <$text linkHref="url">Ba</$text>[]</paragraph>' );
  1187. } );
  1188. it( 'should preserve the `linkHref` attribute when deleting content while the selection is at the end of the link', () => {
  1189. setModelData( model, '<paragraph>Foo <$text linkHref="url">Bar []</$text></paragraph>' );
  1190. expect( model.document.selection.hasAttribute( 'linkHref' ), 'initial state' ).to.equal( true );
  1191. view.document.fire( 'delete', new DomEventData( view.document, {
  1192. keyCode: keyCodes.backspace,
  1193. preventDefault: () => {}
  1194. } ) );
  1195. expect( model.document.selection.hasAttribute( 'linkHref' ), 'removing space after the link' ).to.equal( true );
  1196. view.document.fire( 'delete', new DomEventData( view.document, {
  1197. keyCode: keyCodes.backspace,
  1198. preventDefault: () => {}
  1199. } ) );
  1200. expect( model.document.selection.hasAttribute( 'linkHref' ), 'removing a character in the link' ).to.equal( true );
  1201. expect( getModelData( model ) ).to.equal( '<paragraph>Foo <$text linkHref="url">Ba[]</$text></paragraph>' );
  1202. } );
  1203. it( 'should preserve the `linkHref` attribute when deleting content while the selection is inside the link', () => {
  1204. setModelData( model, '<paragraph>Foo <$text linkHref="url">A long URLLs[] description</$text></paragraph>' );
  1205. expect( model.document.selection.hasAttribute( 'linkHref' ), 'initial state' ).to.equal( true );
  1206. view.document.fire( 'delete', new DomEventData( view.document, {
  1207. keyCode: keyCodes.backspace,
  1208. preventDefault: () => {}
  1209. } ) );
  1210. expect( model.document.selection.hasAttribute( 'linkHref' ), 'removing space after the link' ).to.equal( true );
  1211. view.document.fire( 'delete', new DomEventData( view.document, {
  1212. keyCode: keyCodes.backspace,
  1213. preventDefault: () => {}
  1214. } ) );
  1215. expect( model.document.selection.hasAttribute( 'linkHref' ), 'removing a character in the link' ).to.equal( true );
  1216. expect( getModelData( model ) ).to.equal( '<paragraph>Foo <$text linkHref="url">A long URL[] description</$text></paragraph>' );
  1217. } );
  1218. it( 'should do nothing if there is no `linkHref` attribute', () => {
  1219. setModelData( model, '<paragraph>Foo <$text bold="true">Bolded.</$text> []Bar</paragraph>' );
  1220. view.document.fire( 'delete', new DomEventData( view.document, {
  1221. keyCode: keyCodes.backspace,
  1222. preventDefault: () => {}
  1223. } ) );
  1224. view.document.fire( 'delete', new DomEventData( view.document, {
  1225. keyCode: keyCodes.backspace,
  1226. preventDefault: () => {}
  1227. } ) );
  1228. expect( getModelData( model ) ).to.equal( '<paragraph>Foo <$text bold="true">Bolded[]</$text>Bar</paragraph>' );
  1229. } );
  1230. it( 'should preserve the `linkHref` attribute when deleting content using "Delete" key', () => {
  1231. setModelData( model, '<paragraph>Foo <$text linkHref="url">Bar</$text>[ ]</paragraph>' );
  1232. expect( model.document.selection.hasAttribute( 'linkHref' ), 'initial state' ).to.equal( false );
  1233. view.document.fire( 'delete', new DomEventData( view.document, {
  1234. keyCode: keyCodes.delete,
  1235. preventDefault: () => {}
  1236. }, { direction: 'forward' } ) );
  1237. expect( getModelData( model ) ).to.equal( '<paragraph>Foo <$text linkHref="url">Bar[]</$text></paragraph>' );
  1238. expect( model.document.selection.hasAttribute( 'linkHref' ), 'removing space after the link' ).to.equal( true );
  1239. } );
  1240. } );
  1241. } );