utils.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. /**
  6. * @module link/utils
  7. */
  8. import { upperFirst } from 'lodash-es';
  9. const ATTRIBUTE_WHITESPACES = /[\u0000-\u0020\u00A0\u1680\u180E\u2000-\u2029\u205f\u3000]/g; // eslint-disable-line no-control-regex
  10. const SAFE_URL = /^(?:(?:https?|ftps?|mailto):|[^a-z]|[a-z+.-]+(?:[^a-z+.:-]|$))/i;
  11. /**
  12. * A keystroke used by the {@link module:link/linkui~LinkUI link UI feature}.
  13. */
  14. export const LINK_KEYSTROKE = 'Ctrl+K';
  15. /**
  16. * Returns `true` if a given view node is the link element.
  17. *
  18. * @param {module:engine/view/node~Node} node
  19. * @returns {Boolean}
  20. */
  21. export function isLinkElement( node ) {
  22. return node.is( 'attributeElement' ) && !!node.getCustomProperty( 'link' );
  23. }
  24. /**
  25. * Creates link {@link module:engine/view/attributeelement~AttributeElement} with the provided `href` attribute.
  26. *
  27. * @param {String} href
  28. * @returns {module:engine/view/attributeelement~AttributeElement}
  29. */
  30. export function createLinkElement( href, writer ) {
  31. // Priority 5 - https://github.com/ckeditor/ckeditor5-link/issues/121.
  32. const linkElement = writer.createAttributeElement( 'a', { href }, { priority: 5 } );
  33. writer.setCustomProperty( 'link', true, linkElement );
  34. return linkElement;
  35. }
  36. /**
  37. * Returns a safe URL based on a given value.
  38. *
  39. * A URL is considered safe if it is safe for the user (does not contain any malicious code).
  40. *
  41. * If a URL is considered unsafe, a simple `"#"` is returned.
  42. *
  43. * @protected
  44. * @param {*} url
  45. * @returns {String} Safe URL.
  46. */
  47. export function ensureSafeUrl( url ) {
  48. url = String( url );
  49. return isSafeUrl( url ) ? url : '#';
  50. }
  51. // Checks whether the given URL is safe for the user (does not contain any malicious code).
  52. //
  53. // @param {String} url URL to check.
  54. function isSafeUrl( url ) {
  55. const normalizedUrl = url.replace( ATTRIBUTE_WHITESPACES, '' );
  56. return normalizedUrl.match( SAFE_URL );
  57. }
  58. /**
  59. * Returns the {@link module:link/link~LinkConfig#decorators `config.link.decorators`} configuration processed
  60. * to respect the locale of the editor, i.e. to display the {@link module:link/link~LinkDecoratorManualDefinition label}
  61. * in the correct language.
  62. *
  63. * **Note**: Only the few most commonly used labels are translated automatically. Other labels should be manually
  64. * translated in the {@link module:link/link~LinkConfig#decorators `config.link.decorators`} configuration.
  65. *
  66. * @param {module:utils/locale~Locale#t} t shorthand for {@link module:utils/locale~Locale#t Locale#t}
  67. * @param {Array.<module:link/link~LinkDecoratorDefinition>} The decorator reference
  68. * where the label values should be localized.
  69. * @returns {Array.<module:link/link~LinkDecoratorDefinition>}
  70. */
  71. export function getLocalizedDecorators( t, decorators ) {
  72. const localizedDecoratorsLabels = {
  73. 'Open in a new tab': t( 'Open in a new tab' ),
  74. 'Downloadable': t( 'Downloadable' )
  75. };
  76. decorators.forEach( decorator => {
  77. if ( decorator.label && localizedDecoratorsLabels[ decorator.label ] ) {
  78. decorator.label = localizedDecoratorsLabels[ decorator.label ];
  79. }
  80. return decorator;
  81. } );
  82. return decorators;
  83. }
  84. /**
  85. * Converts an object with defined decorators to a normalized array of decorators. The `id` key is added for each decorator and
  86. * is used as the attribute's name in the model.
  87. *
  88. * @param {Object.<String, module:link/link~LinkDecoratorDefinition>} decorators
  89. * @returns {Array.<module:link/link~LinkDecoratorDefinition>}
  90. */
  91. export function normalizeDecorators( decorators ) {
  92. const retArray = [];
  93. if ( decorators ) {
  94. for ( const [ key, value ] of Object.entries( decorators ) ) {
  95. const decorator = Object.assign(
  96. {},
  97. value,
  98. { id: `link${ upperFirst( key ) }` }
  99. );
  100. retArray.push( decorator );
  101. }
  102. }
  103. return retArray;
  104. }
  105. /**
  106. * Returns `true` if the specified `element` is an image and it can be linked (the element allows having the `linkHref` attribute).
  107. *
  108. * @params {module:engine/model/element~Element|null} element
  109. * @params {module:engine/model/schema~Schema} schema
  110. * @returns {Boolean}
  111. */
  112. export function isImageAllowed( element, schema ) {
  113. if ( !element ) {
  114. return false;
  115. }
  116. return element.is( 'image' ) && schema.checkAttribute( 'image', 'linkHref' );
  117. }