8
0

linkediting.js 45 KB

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