linkediting.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /**
  2. * @license Copyright (c) 2003-2019, 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 VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  9. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  10. import Enter from '@ckeditor/ckeditor5-enter/src/enter';
  11. import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  12. import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  13. import { isLinkElement } from '../src/utils';
  14. import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
  15. /* global document */
  16. describe( 'LinkEditing', () => {
  17. let editor, model, view;
  18. beforeEach( () => {
  19. return VirtualTestEditor
  20. .create( {
  21. plugins: [ Paragraph, LinkEditing, Enter ]
  22. } )
  23. .then( newEditor => {
  24. editor = newEditor;
  25. model = editor.model;
  26. view = editor.editing.view;
  27. } );
  28. } );
  29. it( 'should be loaded', () => {
  30. expect( editor.plugins.get( LinkEditing ) ).to.be.instanceOf( LinkEditing );
  31. } );
  32. it( 'should set proper schema rules', () => {
  33. expect( model.schema.checkAttribute( [ '$block', '$text' ], 'linkHref' ) ).to.be.true;
  34. expect( model.schema.checkAttribute( [ '$clipboardHolder', '$text' ], 'linkHref' ) ).to.be.true;
  35. expect( model.schema.checkAttribute( [ '$block' ], 'linkHref' ) ).to.be.false;
  36. } );
  37. it( 'should bind two-step caret movement to `linkHref` attribute', () => {
  38. // Let's check only the minimum to not duplicated `bindTwoStepCaretToAttribute()` tests.
  39. // Testing minimum is better then testing using spies that might give false positive results.
  40. // Put selection before the link element.
  41. setModelData( editor.model, '<paragraph>foo[]<$text linkHref="url">b</$text>ar</paragraph>' );
  42. // The selection's gravity is not overridden because selection land here not as a result of `keydown`.
  43. expect( editor.model.document.selection.isGravityOverridden ).to.false;
  44. // So let's simulate `keydown` event.
  45. editor.editing.view.document.fire( 'keydown', {
  46. keyCode: keyCodes.arrowright,
  47. preventDefault: () => {},
  48. domTarget: document.body
  49. } );
  50. expect( editor.model.document.selection.isGravityOverridden ).to.true;
  51. } );
  52. describe( 'command', () => {
  53. it( 'should register link command', () => {
  54. const command = editor.commands.get( 'link' );
  55. expect( command ).to.be.instanceOf( LinkCommand );
  56. } );
  57. it( 'should register unlink command', () => {
  58. const command = editor.commands.get( 'unlink' );
  59. expect( command ).to.be.instanceOf( UnlinkCommand );
  60. } );
  61. } );
  62. describe( 'data pipeline conversions', () => {
  63. it( 'should convert `<a href="url">` to `linkHref="url"` attribute', () => {
  64. editor.setData( '<p><a href="url">foo</a>bar</p>' );
  65. expect( getModelData( model, { withoutSelection: true } ) )
  66. .to.equal( '<paragraph><$text linkHref="url">foo</$text>bar</paragraph>' );
  67. expect( editor.getData() ).to.equal( '<p><a href="url">foo</a>bar</p>' );
  68. } );
  69. it( 'should be integrated with autoparagraphing', () => {
  70. editor.setData( '<a href="url">foo</a>bar' );
  71. expect( getModelData( model, { withoutSelection: true } ) )
  72. .to.equal( '<paragraph><$text linkHref="url">foo</$text>bar</paragraph>' );
  73. expect( editor.getData() ).to.equal( '<p><a href="url">foo</a>bar</p>' );
  74. } );
  75. // https://github.com/ckeditor/ckeditor5/issues/500
  76. it( 'should not pick up `<a name="foo">`', () => {
  77. editor.setData( '<p><a name="foo">foo</a>bar</p>' );
  78. expect( getModelData( model, { withoutSelection: true } ) )
  79. .to.equal( '<paragraph>foobar</paragraph>' );
  80. } );
  81. // CKEditor 4 does. And CKEditor 5's balloon allows creating such links.
  82. it( 'should pick up `<a href="">`', () => {
  83. editor.setData( '<p><a href="">foo</a>bar</p>' );
  84. expect( getModelData( model, { withoutSelection: true } ) )
  85. .to.equal( '<paragraph><$text linkHref="">foo</$text>bar</paragraph>' );
  86. expect( editor.getData() ).to.equal( '<p><a href="">foo</a>bar</p>' );
  87. } );
  88. // The editor's role is not to filter out potentially malicious data.
  89. // Its job is to not let this code be executed inside the editor (see the test in "editing pipeline conversion").
  90. it( 'should output a link with a potential XSS code', () => {
  91. setModelData( model, '<paragraph>[]</paragraph>' );
  92. model.change( writer => {
  93. writer.insertText( 'foo', { linkHref: 'javascript:alert(1)' }, model.document.selection.getFirstPosition() );
  94. } );
  95. expect( editor.getData() ).to.equal( '<p><a href="javascript:alert(1)">foo</a></p>' );
  96. } );
  97. it( 'should load a link with a potential XSS code', () => {
  98. editor.setData( '<p><a href="javascript:alert(1)">foo</a></p>' );
  99. expect( getModelData( model, { withoutSelection: true } ) )
  100. .to.equal( '<paragraph><$text linkHref="javascript:alert(1)">foo</$text></paragraph>' );
  101. } );
  102. } );
  103. describe( 'editing pipeline conversion', () => {
  104. it( 'should convert attribute', () => {
  105. setModelData( model, '<paragraph><$text linkHref="url">foo</$text>bar</paragraph>' );
  106. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( '<p><a href="url">foo</a>bar</p>' );
  107. } );
  108. it( 'should convert to link element instance', () => {
  109. setModelData( model, '<paragraph><$text linkHref="url">foo</$text>bar</paragraph>' );
  110. const element = editor.editing.view.document.getRoot().getChild( 0 ).getChild( 0 );
  111. expect( isLinkElement( element ) ).to.be.true;
  112. } );
  113. // https://github.com/ckeditor/ckeditor5-link/issues/121
  114. it( 'should should set priority for `linkHref` higher than all other attribute elements', () => {
  115. model.schema.extend( '$text', { allowAttributes: 'foo' } );
  116. editor.conversion.for( 'downcast' ).attributeToElement( { model: 'foo', view: 'f' } );
  117. setModelData( model,
  118. '<paragraph>' +
  119. '<$text linkHref="url">a</$text><$text foo="true" linkHref="url">b</$text><$text linkHref="url">c</$text>' +
  120. '</paragraph>' );
  121. expect( editor.getData() ).to.equal( '<p><a href="url">a<f>b</f>c</a></p>' );
  122. } );
  123. it( 'must not render a link with a potential XSS code', () => {
  124. setModelData( model, '<paragraph><$text linkHref="javascript:alert(1)">[]foo</$text>bar[]</paragraph>' );
  125. expect( getViewData( editor.editing.view, { withoutSelection: true } ) )
  126. .to.equal( '<p><a href="#">foo</a>bar</p>' );
  127. } );
  128. } );
  129. describe( 'link highlighting', () => {
  130. it( 'should convert the highlight to a proper view classes', () => {
  131. setModelData( model,
  132. '<paragraph>foo <$text linkHref="url">b{}ar</$text> baz</paragraph>'
  133. );
  134. expect( model.document.selection.hasAttribute( 'linkHref' ) ).to.be.true;
  135. expect( getViewData( view ) ).to.equal(
  136. '<p>foo <a class="ck-link_selected" href="url">b{}ar</a> baz</p>'
  137. );
  138. } );
  139. it( 'should work whenever selection has linkHref attribute - link start', () => {
  140. setModelData( model,
  141. '<paragraph>foo {}<$text linkHref="url">bar</$text> baz</paragraph>'
  142. );
  143. expect( model.document.selection.hasAttribute( 'linkHref' ) ).to.be.false;
  144. model.change( writer => {
  145. writer.setSelectionAttribute( 'linkHref', 'url' );
  146. } );
  147. expect( model.document.selection.hasAttribute( 'linkHref' ) ).to.be.true;
  148. expect( getViewData( view ) ).to.equal(
  149. '<p>foo <a class="ck-link_selected" href="url">{}bar</a> baz</p>'
  150. );
  151. } );
  152. it( 'should work whenever selection has linkHref attribute - link end', () => {
  153. setModelData( model,
  154. '<paragraph>foo <$text linkHref="url">bar</$text>{} baz</paragraph>'
  155. );
  156. expect( model.document.selection.hasAttribute( 'linkHref' ) ).to.be.true;
  157. expect( getViewData( view ) ).to.equal(
  158. '<p>foo <a class="ck-link_selected" href="url">bar{}</a> baz</p>'
  159. );
  160. } );
  161. it( 'should render highlight correctly after splitting the link', () => {
  162. setModelData( model,
  163. '<paragraph>foo <$text linkHref="url">li{}nk</$text> baz</paragraph>'
  164. );
  165. editor.execute( 'enter' );
  166. expect( getModelData( model ) ).to.equal(
  167. '<paragraph>foo <$text linkHref="url">li</$text></paragraph>' +
  168. '<paragraph><$text linkHref="url">[]nk</$text> baz</paragraph>'
  169. );
  170. expect( model.document.selection.hasAttribute( 'linkHref' ) ).to.be.true;
  171. } );
  172. it( 'should remove classes when selection is moved out from the link', () => {
  173. setModelData( model,
  174. '<paragraph>foo <$text linkHref="url">li{}nk</$text> baz</paragraph>'
  175. );
  176. expect( getViewData( view ) ).to.equal(
  177. '<p>foo <a class="ck-link_selected" href="url">li{}nk</a> baz</p>'
  178. );
  179. model.change( writer => writer.setSelection( model.document.getRoot().getChild( 0 ), 0 ) );
  180. expect( getViewData( view ) ).to.equal(
  181. '<p>{}foo <a href="url">link</a> baz</p>'
  182. );
  183. } );
  184. it( 'should work correctly when selection is moved inside link', () => {
  185. setModelData( model,
  186. '<paragraph>foo <$text linkHref="url">li{}nk</$text> baz</paragraph>'
  187. );
  188. expect( getViewData( view ) ).to.equal(
  189. '<p>foo <a class="ck-link_selected" href="url">li{}nk</a> baz</p>'
  190. );
  191. model.change( writer => writer.setSelection( model.document.getRoot().getChild( 0 ), 5 ) );
  192. expect( getViewData( view ) ).to.equal(
  193. '<p>foo <a class="ck-link_selected" href="url">l{}ink</a> baz</p>'
  194. );
  195. } );
  196. describe( 'downcast conversion integration', () => {
  197. it( 'works for the #insert event', () => {
  198. setModelData( model,
  199. '<paragraph>foo <$text linkHref="url">li{}nk</$text> baz</paragraph>'
  200. );
  201. model.change( writer => {
  202. writer.insertText( 'FOO', { linkHref: 'url' }, model.document.selection.getFirstPosition() );
  203. } );
  204. expect( getViewData( view ) ).to.equal(
  205. '<p>foo <a class="ck-link_selected" href="url">liFOO{}nk</a> baz</p>'
  206. );
  207. } );
  208. it( 'works for the #remove event', () => {
  209. setModelData( model,
  210. '<paragraph>foo <$text linkHref="url">li{}nk</$text> baz</paragraph>'
  211. );
  212. model.change( writer => {
  213. writer.remove( writer.createRange(
  214. writer.createPositionAt( model.document.getRoot().getChild( 0 ), 0 ),
  215. writer.createPositionAt( model.document.getRoot().getChild( 0 ), 5 )
  216. ) );
  217. } );
  218. expect( getViewData( view ) ).to.equal(
  219. '<p><a class="ck-link_selected" href="url">i{}nk</a> baz</p>'
  220. );
  221. } );
  222. it( 'works for the #attribute event', () => {
  223. setModelData( model,
  224. '<paragraph>foo <$text linkHref="url">li{}nk</$text> baz</paragraph>'
  225. );
  226. model.change( writer => {
  227. writer.setAttribute( 'linkHref', 'new-url', writer.createRange(
  228. model.document.selection.getFirstPosition().getShiftedBy( -1 ),
  229. model.document.selection.getFirstPosition().getShiftedBy( 1 ) )
  230. );
  231. } );
  232. expect( getViewData( view ) ).to.equal(
  233. '<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>'
  234. );
  235. } );
  236. it( 'works for the #selection event', () => {
  237. setModelData( model,
  238. '<paragraph>foo <$text linkHref="url">li{}nk</$text> baz</paragraph>'
  239. );
  240. model.change( writer => {
  241. writer.setSelection( writer.createRange(
  242. model.document.selection.getFirstPosition().getShiftedBy( -1 ),
  243. model.document.selection.getFirstPosition().getShiftedBy( 1 ) )
  244. );
  245. } );
  246. expect( getViewData( view ) ).to.equal(
  247. '<p>foo <a class="ck-link_selected" href="url">l{in}k</a> baz</p>'
  248. );
  249. } );
  250. it( 'works for the addMarker and removeMarker events', () => {
  251. editor.conversion.for( 'editingDowncast' ).markerToHighlight( { model: 'fooMarker', view: {} } );
  252. setModelData( model,
  253. '<paragraph>foo <$text linkHref="url">li{}nk</$text> baz</paragraph>'
  254. );
  255. model.change( writer => {
  256. const range = writer.createRange(
  257. writer.createPositionAt( model.document.getRoot().getChild( 0 ), 0 ),
  258. writer.createPositionAt( model.document.getRoot().getChild( 0 ), 5 )
  259. );
  260. writer.addMarker( 'fooMarker', { range, usingOperation: true } );
  261. } );
  262. expect( getViewData( view ) ).to.equal(
  263. '<p><span>foo </span><a class="ck-link_selected" href="url"><span>l</span>i{}nk</a> baz</p>'
  264. );
  265. model.change( writer => writer.removeMarker( 'fooMarker' ) );
  266. expect( getViewData( view ) ).to.equal(
  267. '<p>foo <a class="ck-link_selected" href="url">li{}nk</a> baz</p>'
  268. );
  269. } );
  270. } );
  271. } );
  272. } );