utils.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 ViewDocument from '@ckeditor/ckeditor5-engine/src/view/document';
  6. import ViewDowncastWriter from '@ckeditor/ckeditor5-engine/src/view/downcastwriter';
  7. import AttributeElement from '@ckeditor/ckeditor5-engine/src/view/attributeelement';
  8. import ContainerElement from '@ckeditor/ckeditor5-engine/src/view/containerelement';
  9. import Text from '@ckeditor/ckeditor5-engine/src/view/text';
  10. import Schema from '@ckeditor/ckeditor5-engine/src/model/schema';
  11. import ModelElement from '@ckeditor/ckeditor5-engine/src/model/element';
  12. import {
  13. createLinkElement, isLinkElement, ensureSafeUrl, normalizeDecorators, isImageAllowed, isEmail, addLinkProtocolIfApplicable
  14. } from '../src/utils';
  15. describe( 'utils', () => {
  16. describe( 'isLinkElement()', () => {
  17. it( 'should return true for elements created by createLinkElement', () => {
  18. const writer = new ViewDowncastWriter( new ViewDocument() );
  19. const element = createLinkElement( 'http://ckeditor.com', { writer } );
  20. expect( isLinkElement( element ) ).to.be.true;
  21. } );
  22. it( 'should return false for other AttributeElements', () => {
  23. expect( isLinkElement( new AttributeElement( 'a' ) ) ).to.be.false;
  24. } );
  25. it( 'should return false for ContainerElements', () => {
  26. expect( isLinkElement( new ContainerElement( 'p' ) ) ).to.be.false;
  27. } );
  28. it( 'should return false for text nodes', () => {
  29. expect( isLinkElement( new Text( 'foo' ) ) ).to.be.false;
  30. } );
  31. } );
  32. describe( 'createLinkElement()', () => {
  33. it( 'should create link AttributeElement', () => {
  34. const writer = new ViewDowncastWriter( new ViewDocument() );
  35. const element = createLinkElement( 'http://cksource.com', { writer } );
  36. expect( isLinkElement( element ) ).to.be.true;
  37. expect( element.priority ).to.equal( 5 );
  38. expect( element.getAttribute( 'href' ) ).to.equal( 'http://cksource.com' );
  39. expect( element.name ).to.equal( 'a' );
  40. } );
  41. } );
  42. describe( 'ensureSafeUrl()', () => {
  43. it( 'returns the same absolute http URL', () => {
  44. const url = 'http://xx.yy/zz#foo';
  45. expect( ensureSafeUrl( url ) ).to.equal( url );
  46. } );
  47. it( 'returns the same absolute https URL', () => {
  48. const url = 'https://xx.yy/zz';
  49. expect( ensureSafeUrl( url ) ).to.equal( url );
  50. } );
  51. it( 'returns the same absolute ftp URL', () => {
  52. const url = 'ftp://xx.yy/zz';
  53. expect( ensureSafeUrl( url ) ).to.equal( url );
  54. } );
  55. it( 'returns the same absolute ftps URL', () => {
  56. const url = 'ftps://xx.yy/zz';
  57. expect( ensureSafeUrl( url ) ).to.equal( url );
  58. } );
  59. it( 'returns the same absolute mailto URL', () => {
  60. const url = 'mailto://foo@bar.com';
  61. expect( ensureSafeUrl( url ) ).to.equal( url );
  62. } );
  63. it( 'returns the same relative URL (starting with a dot)', () => {
  64. const url = './xx/yyy';
  65. expect( ensureSafeUrl( url ) ).to.equal( url );
  66. } );
  67. it( 'returns the same relative URL (starting with two dots)', () => {
  68. const url = '../../xx/yyy';
  69. expect( ensureSafeUrl( url ) ).to.equal( url );
  70. } );
  71. it( 'returns the same relative URL (starting with a slash)', () => {
  72. const url = '/xx/yyy';
  73. expect( ensureSafeUrl( url ) ).to.equal( url );
  74. } );
  75. it( 'returns the same relative URL (starting with a backslash)', () => {
  76. const url = '\\xx\\yyy';
  77. expect( ensureSafeUrl( url ) ).to.equal( url );
  78. } );
  79. it( 'returns the same relative URL (starting with a letter)', () => {
  80. const url = 'xx/yyy';
  81. expect( ensureSafeUrl( url ) ).to.equal( url );
  82. } );
  83. it( 'returns the same URL even if it contains whitespaces', () => {
  84. const url = ' ./xx/ yyy\t';
  85. expect( ensureSafeUrl( url ) ).to.equal( url );
  86. } );
  87. it( 'returns the same URL even if it contains non ASCII characters', () => {
  88. const url = 'https://kłącze.yy/źdźbło';
  89. expect( ensureSafeUrl( url ) ).to.equal( url );
  90. } );
  91. it( 'accepts non string values', () => {
  92. expect( ensureSafeUrl( undefined ) ).to.equal( 'undefined' );
  93. expect( ensureSafeUrl( null ) ).to.equal( 'null' );
  94. } );
  95. it( 'returns safe URL when a malicious URL starts with javascript:', () => {
  96. const url = 'javascript:alert(1)';
  97. expect( ensureSafeUrl( url ) ).to.equal( '#' );
  98. } );
  99. it( 'returns safe URL when a malicious URL starts with an unknown protocol', () => {
  100. const url = 'foo:alert(1)';
  101. expect( ensureSafeUrl( url ) ).to.equal( '#' );
  102. } );
  103. it( 'returns safe URL when a malicious URL contains spaces', () => {
  104. const url = 'java\u0000script:\talert(1)';
  105. expect( ensureSafeUrl( url ) ).to.equal( '#' );
  106. } );
  107. it( 'returns safe URL when a malicious URL contains spaces (2)', () => {
  108. const url = '\u0000 javascript:alert(1)';
  109. expect( ensureSafeUrl( url ) ).to.equal( '#' );
  110. } );
  111. it( 'returns safe URL when a malicious URL contains a safe part', () => {
  112. const url = 'javascript:alert(1)\nhttp://xxx';
  113. expect( ensureSafeUrl( url ) ).to.equal( '#' );
  114. } );
  115. it( 'returns safe URL when a malicious URL contains a safe part (2)', () => {
  116. const url = 'javascript:alert(1);http://xxx';
  117. expect( ensureSafeUrl( url ) ).to.equal( '#' );
  118. } );
  119. } );
  120. describe( 'normalizeDecorators()', () => {
  121. it( 'should transform an entry object to a normalized array', () => {
  122. const callback = () => {};
  123. const entryObject = {
  124. foo: {
  125. mode: 'manual',
  126. label: 'Foo',
  127. attributes: {
  128. foo: 'foo'
  129. }
  130. },
  131. bar: {
  132. mode: 'automatic',
  133. callback,
  134. attributes: {
  135. bar: 'bar'
  136. }
  137. },
  138. baz: {
  139. mode: 'manual',
  140. label: 'Baz label',
  141. attributes: {
  142. target: '_blank',
  143. rel: 'noopener noreferrer'
  144. }
  145. }
  146. };
  147. expect( normalizeDecorators( entryObject ) ).to.deep.equal( [
  148. {
  149. id: 'linkFoo',
  150. mode: 'manual',
  151. label: 'Foo',
  152. attributes: {
  153. foo: 'foo'
  154. }
  155. },
  156. {
  157. id: 'linkBar',
  158. mode: 'automatic',
  159. callback,
  160. attributes: {
  161. bar: 'bar'
  162. }
  163. },
  164. {
  165. id: 'linkBaz',
  166. mode: 'manual',
  167. label: 'Baz label',
  168. attributes: {
  169. target: '_blank',
  170. rel: 'noopener noreferrer'
  171. }
  172. }
  173. ] );
  174. } );
  175. } );
  176. describe( 'isImageAllowed()', () => {
  177. it( 'returns false when passed "null" as element', () => {
  178. expect( isImageAllowed( null, new Schema() ) ).to.equal( false );
  179. } );
  180. it( 'returns false when passed an element that is not the image element', () => {
  181. const element = new ModelElement( 'paragraph' );
  182. expect( isImageAllowed( element, new Schema() ) ).to.equal( false );
  183. } );
  184. it( 'returns false when schema does not allow linking images', () => {
  185. const element = new ModelElement( 'image' );
  186. expect( isImageAllowed( element, new Schema() ) ).to.equal( false );
  187. } );
  188. it( 'returns true when passed an image element and it can be linked', () => {
  189. const element = new ModelElement( 'image' );
  190. const schema = new Schema();
  191. schema.register( 'image', {
  192. allowIn: '$root',
  193. allowAttributes: [ 'linkHref' ]
  194. } );
  195. expect( isImageAllowed( element, schema ) ).to.equal( true );
  196. } );
  197. } );
  198. describe( 'isEmail()', () => {
  199. it( 'should return true for email string', () => {
  200. expect( isEmail( 'newsletter@cksource.com' ) ).to.be.true;
  201. } );
  202. it( 'should return false for not email string', () => {
  203. expect( isEmail( 'test' ) ).to.be.false;
  204. expect( isEmail( 'test.test' ) ).to.be.false;
  205. expect( isEmail( 'test@test' ) ).to.be.false;
  206. } );
  207. } );
  208. describe( 'addLinkProtocolIfApplicable()', () => {
  209. it( 'should return link with email protocol for email string', () => {
  210. expect( addLinkProtocolIfApplicable( 'foo@bar.com' ) ).to.equal( 'mailto:foo@bar.com' );
  211. expect( addLinkProtocolIfApplicable( 'foo@bar.com', 'http://' ) ).to.equal( 'mailto:foo@bar.com' );
  212. } );
  213. it( 'should return link with http protocol for url string if defaultProtocol is provided', () => {
  214. expect( addLinkProtocolIfApplicable( 'www.ckeditor.com', 'http://' ) ).to.equal( 'http://www.ckeditor.com' );
  215. } );
  216. it( 'should return unmodified link if not applicable', () => {
  217. expect( addLinkProtocolIfApplicable( 'test' ) ).to.equal( 'test' );
  218. expect( addLinkProtocolIfApplicable( 'www.ckeditor.com' ) ).to.equal( 'www.ckeditor.com' );
  219. expect( addLinkProtocolIfApplicable( 'http://www.ckeditor.com' ) ).to.equal( 'http://www.ckeditor.com' );
  220. expect( addLinkProtocolIfApplicable( 'http://www.ckeditor.com', 'http://' ) ).to.equal( 'http://www.ckeditor.com' );
  221. expect( addLinkProtocolIfApplicable( 'mailto:foo@bar.com' ) ).to.equal( 'mailto:foo@bar.com' );
  222. expect( addLinkProtocolIfApplicable( 'mailto:foo@bar.com', 'http://' ) ).to.equal( 'mailto:foo@bar.com' );
  223. } );
  224. } );
  225. } );