8
0

linkediting.js 42 KB

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