linkediting.js 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589
  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 not preserve the `linkHref` attribute when deleting content after the link (decorators check)', () => {
  1189. setModelData( model,
  1190. '<paragraph>' +
  1191. 'This is ' +
  1192. '<$text linkIsFoo="true" linkIsBar="true" linkHref="foo">Foo</$text>' +
  1193. ' []from ' +
  1194. '<$text linkHref="bar">Bar</$text>' +
  1195. '.' +
  1196. '</paragraph>'
  1197. );
  1198. expect( model.document.selection.hasAttribute( 'linkHref' ), 'initial "linkHref" state' ).to.equal( false );
  1199. expect( model.document.selection.hasAttribute( 'linkIsFoo' ), 'initial "linkIsFoo" state' ).to.equal( false );
  1200. expect( model.document.selection.hasAttribute( 'linkHref' ), 'initial "linkHref" state' ).to.equal( false );
  1201. view.document.fire( 'delete', new DomEventData( view.document, {
  1202. keyCode: keyCodes.backspace,
  1203. preventDefault: () => {}
  1204. } ) );
  1205. expect( model.document.selection.hasAttribute( 'linkHref' ), 'removing space after the link ("linkHref")' ).to.equal( false );
  1206. expect( model.document.selection.hasAttribute( 'linkIsFoo' ), 'removing space after the link ("linkIsFoo")' ).to.equal( false );
  1207. expect( model.document.selection.hasAttribute( 'linkHref' ), 'removing space after the link ("linkHref")' ).to.equal( false );
  1208. view.document.fire( 'delete', new DomEventData( view.document, {
  1209. keyCode: keyCodes.backspace,
  1210. preventDefault: () => {}
  1211. } ) );
  1212. expect( model.document.selection.hasAttribute( 'linkHref' ), 'removing a character the link ("linkHref")' ).to.equal( false );
  1213. expect( model.document.selection.hasAttribute( 'linkIsFoo' ), 'removing a character the link ("linkIsFoo")' ).to.equal( false );
  1214. expect( model.document.selection.hasAttribute( 'linkHref' ), 'removing a character the link ("linkHref")' ).to.equal( false );
  1215. expect( getModelData( model ) ).to.equal(
  1216. '<paragraph>' +
  1217. 'This is ' +
  1218. '<$text linkHref="foo" linkIsBar="true" linkIsFoo="true">Fo</$text>' +
  1219. '[]from ' +
  1220. '<$text linkHref="bar">Bar</$text>' +
  1221. '.' +
  1222. '</paragraph>'
  1223. );
  1224. } );
  1225. it( 'should preserve the `linkHref` attribute when deleting content while the selection is at the end of the link', () => {
  1226. setModelData( model, '<paragraph>Foo <$text linkHref="url">Bar []</$text></paragraph>' );
  1227. expect( model.document.selection.hasAttribute( 'linkHref' ), 'initial state' ).to.equal( true );
  1228. view.document.fire( 'delete', new DomEventData( view.document, {
  1229. keyCode: keyCodes.backspace,
  1230. preventDefault: () => {}
  1231. } ) );
  1232. expect( model.document.selection.hasAttribute( 'linkHref' ), 'removing space after the link' ).to.equal( true );
  1233. view.document.fire( 'delete', new DomEventData( view.document, {
  1234. keyCode: keyCodes.backspace,
  1235. preventDefault: () => {}
  1236. } ) );
  1237. expect( model.document.selection.hasAttribute( 'linkHref' ), 'removing a character in the link' ).to.equal( true );
  1238. expect( getModelData( model ) ).to.equal( '<paragraph>Foo <$text linkHref="url">Ba[]</$text></paragraph>' );
  1239. } );
  1240. it( 'should preserve the `linkHref` attribute when deleting content while the selection is inside the link', () => {
  1241. setModelData( model, '<paragraph>Foo <$text linkHref="url">A long URLLs[] description</$text></paragraph>' );
  1242. expect( model.document.selection.hasAttribute( 'linkHref' ), 'initial state' ).to.equal( true );
  1243. view.document.fire( 'delete', new DomEventData( view.document, {
  1244. keyCode: keyCodes.backspace,
  1245. preventDefault: () => {}
  1246. } ) );
  1247. expect( model.document.selection.hasAttribute( 'linkHref' ), 'removing space after the link' ).to.equal( true );
  1248. view.document.fire( 'delete', new DomEventData( view.document, {
  1249. keyCode: keyCodes.backspace,
  1250. preventDefault: () => {}
  1251. } ) );
  1252. expect( model.document.selection.hasAttribute( 'linkHref' ), 'removing a character in the link' ).to.equal( true );
  1253. expect( getModelData( model ) ).to.equal( '<paragraph>Foo <$text linkHref="url">A long URL[] description</$text></paragraph>' );
  1254. } );
  1255. it( 'should do nothing if there is no `linkHref` attribute', () => {
  1256. setModelData( model, '<paragraph>Foo <$text bold="true">Bolded.</$text> []Bar</paragraph>' );
  1257. view.document.fire( 'delete', new DomEventData( view.document, {
  1258. keyCode: keyCodes.backspace,
  1259. preventDefault: () => {}
  1260. } ) );
  1261. view.document.fire( 'delete', new DomEventData( view.document, {
  1262. keyCode: keyCodes.backspace,
  1263. preventDefault: () => {}
  1264. } ) );
  1265. expect( getModelData( model ) ).to.equal( '<paragraph>Foo <$text bold="true">Bolded[]</$text>Bar</paragraph>' );
  1266. } );
  1267. it( 'should preserve the `linkHref` attribute when deleting content using "Delete" key', () => {
  1268. setModelData( model, '<paragraph>Foo <$text linkHref="url">Bar</$text>[ ]</paragraph>' );
  1269. expect( model.document.selection.hasAttribute( 'linkHref' ), 'initial state' ).to.equal( false );
  1270. view.document.fire( 'delete', new DomEventData( view.document, {
  1271. keyCode: keyCodes.delete,
  1272. preventDefault: () => {}
  1273. }, { direction: 'forward' } ) );
  1274. expect( getModelData( model ) ).to.equal( '<paragraph>Foo <$text linkHref="url">Bar[]</$text></paragraph>' );
  1275. expect( model.document.selection.hasAttribute( 'linkHref' ), 'removing space after the link' ).to.equal( true );
  1276. } );
  1277. } );
  1278. } );