8
0

linkediting.js 12 KB

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