linkediting.js 12 KB

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