linkediting.js 40 KB

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