# link/utils

module

<a id="constants">

## Constants

<a id="constant-LINK_KEYSTROKE">

### `LINK_KEYSTROKE: 'Ctrl+K'`

A keystroke used by the [link UI feature](module_link_linkui-LinkUI.md).

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-link/src/utils.ts#L50)

<a id="type-definitions">

## Type Definitions

<a id="typedef-NormalizedLinkDecoratorAutomaticDefinition">

### `NormalizedLinkDecoratorAutomaticDefinition`

<a id="typedef-NormalizedLinkDecoratorDefinition">

### `NormalizedLinkDecoratorDefinition`

<a id="typedef-NormalizedLinkDecoratorManualDefinition">

### `NormalizedLinkDecoratorManualDefinition`

<a id="functions">

## Functions

<a id="function-addLinkProtocolIfApplicable">

### `addLinkProtocolIfApplicable( link, defaultProtocol? ) → string`

Adds the protocol prefix to the specified `link` when:

* it does not contain it already, and there is a [`defaultProtocol`](module_link_linkconfig-LinkConfig.md#member-defaultProtocol) configuration value provided,
* or the link is an email address.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-link/src/utils.ts#L181)

#### Parameters

* `link: string`
* `defaultProtocol?: string`

#### Returns

* `string`

<a id="function-createLinkElement">

### `createLinkElement( href, __namedParameters ) → ViewAttributeElement`

Creates a link [`ViewAttributeElement`](module_engine_view_attributeelement-ViewAttributeElement.md) with the provided `href` attribute.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-link/src/utils.ts#L62)

#### Parameters

* `href: string`
* `__namedParameters: DowncastConversionApi`

#### Returns

* `ViewAttributeElement`

<a id="function-ensureSafeUrl">

### `ensureSafeUrl( url, allowedProtocols ) → string` _(internal)_

Returns a safe URL based on a given value.

A URL is considered safe if it is safe for the user (does not contain any malicious code).

If a URL is considered unsafe, a simple `"#"` is returned.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-link/src/utils.ts#L80)

#### Parameters

* `url: unknown`

* `allowedProtocols: Array<string>`

  Defaults to `DEFAULT_LINK_PROTOCOLS`

#### Returns

* `string`

<a id="function-extractTextFromLinkRange">

### `extractTextFromLinkRange( range ) → string | undefined`

Returns a text of a link range.

If the returned value is `undefined`, the range contains elements other than text nodes.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-link/src/utils.ts#L209)

#### Parameters

* `range: ModelRange`

#### Returns

* `string | undefined`

<a id="function-getLocalizedDecorators">

### `getLocalizedDecorators( t, decorators ) → Array<NormalizedLinkDecoratorDefinition>` _(internal)_

Returns the [`config.link.decorators`](module_link_linkconfig-LinkConfig.md#member-decorators) configuration processed to respect the locale of the editor, i.e. to display the [label](module_link_linkconfig-LinkDecoratorManualDefinition.md) in the correct language.

**Note**: Only the few most commonly used labels are translated automatically. Other labels should be manually translated in the [`config.link.decorators`](module_link_linkconfig-LinkConfig.md#member-decorators) configuration.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-link/src/utils.ts#L110)

#### Parameters

* `t: LocaleTranslate`

  Shorthand for [Locale#t](module_utils_locale-Locale.md#member-t).

* `decorators: Array<NormalizedLinkDecoratorDefinition>`

  The decorator reference where the label values should be localized.

#### Returns

* `Array<NormalizedLinkDecoratorDefinition>`

<a id="function-isEmail">

### `isEmail( value ) → boolean` _(internal)_

Returns `true` if the specified `value` is an email.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-link/src/utils.ts#L170)

#### Parameters

* `value: string`

#### Returns

* `boolean`

<a id="function-isLinkElement">

### `isLinkElement( node ) → boolean`

Returns `true` if a given view node is the link element.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-link/src/utils.ts#L55)

#### Parameters

* `node: ViewNode | ViewDocumentFragment`

#### Returns

* `boolean`

<a id="function-isLinkableElement">

### `isLinkableElement( element, schema ) → element is ModelElement`

Returns `true` if the specified `element` can be linked (the element allows the `linkHref` attribute).

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-link/src/utils.ts#L157)

#### Parameters

* `element: ModelElement | null`
* `schema: ModelSchema`

#### Returns

* `element is ModelElement`

<a id="function-linkHasProtocol">

### `linkHasProtocol( link ) → boolean` _(internal)_

Checks if protocol is already included in the link.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-link/src/utils.ts#L193)

#### Parameters

* `link: string`

#### Returns

* `boolean`

<a id="function-normalizeDecorators">

### `normalizeDecorators( decorators? ) → Array<NormalizedLinkDecoratorDefinition>` _(internal)_

Converts an object with defined decorators to a normalized array of decorators. The `id` key is added for each decorator and is used as the attribute's name in the model.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-link/src/utils.ts#L136)

#### Parameters

* `decorators?: Record<string, LinkDecoratorDefinition>`

#### Returns

* `Array<NormalizedLinkDecoratorDefinition>`

<a id="function-openLink">

### `openLink( link ) → void`

Opens the link in a new browser tab.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-link/src/utils.ts#L200)

#### Parameters

* `link: string`

#### Returns

* `void`

---

Full index of the CKEditor 5 API reference: [llms.txt](llms.txt)
