@ckeditor/ckeditor5-link 39.0.1 → 40.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +1 -1
- package/LICENSE.md +1 -1
- package/README.md +3 -3
- package/build/link.js.map +1 -0
- package/build/translations/de.js +1 -1
- package/build/translations/hy.js +1 -0
- package/build/translations/pt-br.js +1 -1
- package/lang/translations/ar.po +1 -0
- package/lang/translations/ast.po +1 -0
- package/lang/translations/az.po +1 -0
- package/lang/translations/bg.po +1 -0
- package/lang/translations/bn.po +1 -0
- package/lang/translations/ca.po +1 -0
- package/lang/translations/cs.po +1 -0
- package/lang/translations/da.po +1 -0
- package/lang/translations/de-ch.po +1 -0
- package/lang/translations/de.po +2 -1
- package/lang/translations/el.po +1 -0
- package/lang/translations/en-au.po +1 -0
- package/lang/translations/en-gb.po +1 -0
- package/lang/translations/en.po +1 -0
- package/lang/translations/eo.po +1 -0
- package/lang/translations/es.po +1 -0
- package/lang/translations/et.po +1 -0
- package/lang/translations/eu.po +1 -0
- package/lang/translations/fa.po +1 -0
- package/lang/translations/fi.po +1 -0
- package/lang/translations/fr.po +1 -0
- package/lang/translations/gl.po +1 -0
- package/lang/translations/he.po +1 -0
- package/lang/translations/hi.po +1 -0
- package/lang/translations/hr.po +1 -0
- package/lang/translations/hu.po +1 -0
- package/lang/translations/hy.po +54 -0
- package/lang/translations/id.po +1 -0
- package/lang/translations/it.po +1 -0
- package/lang/translations/ja.po +1 -0
- package/lang/translations/km.po +1 -0
- package/lang/translations/kn.po +1 -0
- package/lang/translations/ko.po +1 -0
- package/lang/translations/ku.po +1 -0
- package/lang/translations/lt.po +1 -0
- package/lang/translations/lv.po +1 -0
- package/lang/translations/ms.po +1 -0
- package/lang/translations/nb.po +1 -0
- package/lang/translations/ne.po +1 -0
- package/lang/translations/nl.po +1 -0
- package/lang/translations/no.po +1 -0
- package/lang/translations/pl.po +1 -0
- package/lang/translations/pt-br.po +3 -2
- package/lang/translations/pt.po +1 -0
- package/lang/translations/ro.po +1 -0
- package/lang/translations/ru.po +1 -0
- package/lang/translations/sk.po +1 -0
- package/lang/translations/sq.po +1 -0
- package/lang/translations/sr-latn.po +1 -0
- package/lang/translations/sr.po +1 -0
- package/lang/translations/sv.po +1 -0
- package/lang/translations/th.po +1 -0
- package/lang/translations/tk.po +1 -0
- package/lang/translations/tr.po +1 -0
- package/lang/translations/tt.po +1 -0
- package/lang/translations/ug.po +1 -0
- package/lang/translations/uk.po +1 -0
- package/lang/translations/ur.po +1 -0
- package/lang/translations/uz.po +1 -0
- package/lang/translations/vi.po +1 -0
- package/lang/translations/zh-cn.po +1 -0
- package/lang/translations/zh.po +1 -0
- package/package.json +3 -7
- package/src/augmentation.d.ts +30 -30
- package/src/augmentation.js +5 -5
- package/src/autolink.d.ts +60 -60
- package/src/autolink.js +216 -216
- package/src/index.d.ts +18 -18
- package/src/index.js +17 -17
- package/src/link.d.ts +27 -27
- package/src/link.js +31 -31
- package/src/linkcommand.d.ts +132 -132
- package/src/linkcommand.js +285 -285
- package/src/linkconfig.d.ts +251 -251
- package/src/linkconfig.js +5 -5
- package/src/linkediting.d.ts +106 -106
- package/src/linkediting.js +547 -547
- package/src/linkimage.d.ts +27 -27
- package/src/linkimage.js +31 -31
- package/src/linkimageediting.d.ts +39 -39
- package/src/linkimageediting.js +245 -245
- package/src/linkimageui.d.ts +40 -40
- package/src/linkimageui.js +96 -96
- package/src/linkui.d.ts +165 -165
- package/src/linkui.js +581 -581
- package/src/ui/linkactionsview.d.ts +101 -101
- package/src/ui/linkactionsview.js +156 -156
- package/src/ui/linkformview.d.ts +141 -141
- package/src/ui/linkformview.js +232 -232
- package/src/unlinkcommand.d.ts +31 -31
- package/src/unlinkcommand.js +66 -66
- package/src/utils/automaticdecorators.d.ts +45 -45
- package/src/utils/automaticdecorators.js +140 -140
- package/src/utils/manualdecorator.d.ts +72 -72
- package/src/utils/manualdecorator.js +47 -47
- package/src/utils.d.ts +80 -80
- package/src/utils.js +128 -128
package/src/utils.d.ts
CHANGED
|
@@ -1,80 +1,80 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. 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 type { DowncastConversionApi, Element, Schema, ViewAttributeElement, ViewNode, ViewDocumentFragment } from 'ckeditor5/src/engine';
|
|
9
|
-
import type { LocaleTranslate } from 'ckeditor5/src/utils';
|
|
10
|
-
import type { LinkDecoratorAutomaticDefinition, LinkDecoratorDefinition, LinkDecoratorManualDefinition } from './linkconfig';
|
|
11
|
-
/**
|
|
12
|
-
* A keystroke used by the {@link module:link/linkui~LinkUI link UI feature}.
|
|
13
|
-
*/
|
|
14
|
-
export declare const LINK_KEYSTROKE = "Ctrl+K";
|
|
15
|
-
/**
|
|
16
|
-
* Returns `true` if a given view node is the link element.
|
|
17
|
-
*/
|
|
18
|
-
export declare function isLinkElement(node: ViewNode | ViewDocumentFragment): boolean;
|
|
19
|
-
/**
|
|
20
|
-
* Creates a link {@link module:engine/view/attributeelement~AttributeElement} with the provided `href` attribute.
|
|
21
|
-
*/
|
|
22
|
-
export declare function createLinkElement(href: string, { writer }: DowncastConversionApi): ViewAttributeElement;
|
|
23
|
-
/**
|
|
24
|
-
* Returns a safe URL based on a given value.
|
|
25
|
-
*
|
|
26
|
-
* A URL is considered safe if it is safe for the user (does not contain any malicious code).
|
|
27
|
-
*
|
|
28
|
-
* If a URL is considered unsafe, a simple `"#"` is returned.
|
|
29
|
-
*
|
|
30
|
-
* @internal
|
|
31
|
-
*/
|
|
32
|
-
export declare function ensureSafeUrl(url: unknown): string;
|
|
33
|
-
/**
|
|
34
|
-
* Returns the {@link module:link/linkconfig~LinkConfig#decorators `config.link.decorators`} configuration processed
|
|
35
|
-
* to respect the locale of the editor, i.e. to display the {@link module:link/linkconfig~LinkDecoratorManualDefinition label}
|
|
36
|
-
* in the correct language.
|
|
37
|
-
*
|
|
38
|
-
* **Note**: Only the few most commonly used labels are translated automatically. Other labels should be manually
|
|
39
|
-
* translated in the {@link module:link/linkconfig~LinkConfig#decorators `config.link.decorators`} configuration.
|
|
40
|
-
*
|
|
41
|
-
* @param t Shorthand for {@link module:utils/locale~Locale#t Locale#t}.
|
|
42
|
-
* @param decorators The decorator reference where the label values should be localized.
|
|
43
|
-
*/
|
|
44
|
-
export declare function getLocalizedDecorators(t: LocaleTranslate, decorators: Array<NormalizedLinkDecoratorDefinition>): Array<NormalizedLinkDecoratorDefinition>;
|
|
45
|
-
/**
|
|
46
|
-
* Converts an object with defined decorators to a normalized array of decorators. The `id` key is added for each decorator and
|
|
47
|
-
* is used as the attribute's name in the model.
|
|
48
|
-
*/
|
|
49
|
-
export declare function normalizeDecorators(decorators?: Record<string, LinkDecoratorDefinition>): Array<NormalizedLinkDecoratorDefinition>;
|
|
50
|
-
/**
|
|
51
|
-
* Returns `true` if the specified `element` can be linked (the element allows the `linkHref` attribute).
|
|
52
|
-
*/
|
|
53
|
-
export declare function isLinkableElement(element: Element | null, schema: Schema): element is Element;
|
|
54
|
-
/**
|
|
55
|
-
* Returns `true` if the specified `value` is an email.
|
|
56
|
-
*/
|
|
57
|
-
export declare function isEmail(value: string): boolean;
|
|
58
|
-
/**
|
|
59
|
-
* Adds the protocol prefix to the specified `link` when:
|
|
60
|
-
*
|
|
61
|
-
* * it does not contain it already, and there is a {@link module:link/linkconfig~LinkConfig#defaultProtocol `defaultProtocol` }
|
|
62
|
-
* configuration value provided,
|
|
63
|
-
* * or the link is an email address.
|
|
64
|
-
*/
|
|
65
|
-
export declare function addLinkProtocolIfApplicable(link: string, defaultProtocol?: string): string;
|
|
66
|
-
/**
|
|
67
|
-
* Checks if protocol is already included in the link.
|
|
68
|
-
*/
|
|
69
|
-
export declare function linkHasProtocol(link: string): boolean;
|
|
70
|
-
/**
|
|
71
|
-
* Opens the link in a new browser tab.
|
|
72
|
-
*/
|
|
73
|
-
export declare function openLink(link: string): void;
|
|
74
|
-
export type NormalizedLinkDecoratorAutomaticDefinition = LinkDecoratorAutomaticDefinition & {
|
|
75
|
-
id: string;
|
|
76
|
-
};
|
|
77
|
-
export type NormalizedLinkDecoratorManualDefinition = LinkDecoratorManualDefinition & {
|
|
78
|
-
id: string;
|
|
79
|
-
};
|
|
80
|
-
export type NormalizedLinkDecoratorDefinition = NormalizedLinkDecoratorAutomaticDefinition | NormalizedLinkDecoratorManualDefinition;
|
|
1
|
+
/**
|
|
2
|
+
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. 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 type { DowncastConversionApi, Element, Schema, ViewAttributeElement, ViewNode, ViewDocumentFragment } from 'ckeditor5/src/engine';
|
|
9
|
+
import type { LocaleTranslate } from 'ckeditor5/src/utils';
|
|
10
|
+
import type { LinkDecoratorAutomaticDefinition, LinkDecoratorDefinition, LinkDecoratorManualDefinition } from './linkconfig';
|
|
11
|
+
/**
|
|
12
|
+
* A keystroke used by the {@link module:link/linkui~LinkUI link UI feature}.
|
|
13
|
+
*/
|
|
14
|
+
export declare const LINK_KEYSTROKE = "Ctrl+K";
|
|
15
|
+
/**
|
|
16
|
+
* Returns `true` if a given view node is the link element.
|
|
17
|
+
*/
|
|
18
|
+
export declare function isLinkElement(node: ViewNode | ViewDocumentFragment): boolean;
|
|
19
|
+
/**
|
|
20
|
+
* Creates a link {@link module:engine/view/attributeelement~AttributeElement} with the provided `href` attribute.
|
|
21
|
+
*/
|
|
22
|
+
export declare function createLinkElement(href: string, { writer }: DowncastConversionApi): ViewAttributeElement;
|
|
23
|
+
/**
|
|
24
|
+
* Returns a safe URL based on a given value.
|
|
25
|
+
*
|
|
26
|
+
* A URL is considered safe if it is safe for the user (does not contain any malicious code).
|
|
27
|
+
*
|
|
28
|
+
* If a URL is considered unsafe, a simple `"#"` is returned.
|
|
29
|
+
*
|
|
30
|
+
* @internal
|
|
31
|
+
*/
|
|
32
|
+
export declare function ensureSafeUrl(url: unknown): string;
|
|
33
|
+
/**
|
|
34
|
+
* Returns the {@link module:link/linkconfig~LinkConfig#decorators `config.link.decorators`} configuration processed
|
|
35
|
+
* to respect the locale of the editor, i.e. to display the {@link module:link/linkconfig~LinkDecoratorManualDefinition label}
|
|
36
|
+
* in the correct language.
|
|
37
|
+
*
|
|
38
|
+
* **Note**: Only the few most commonly used labels are translated automatically. Other labels should be manually
|
|
39
|
+
* translated in the {@link module:link/linkconfig~LinkConfig#decorators `config.link.decorators`} configuration.
|
|
40
|
+
*
|
|
41
|
+
* @param t Shorthand for {@link module:utils/locale~Locale#t Locale#t}.
|
|
42
|
+
* @param decorators The decorator reference where the label values should be localized.
|
|
43
|
+
*/
|
|
44
|
+
export declare function getLocalizedDecorators(t: LocaleTranslate, decorators: Array<NormalizedLinkDecoratorDefinition>): Array<NormalizedLinkDecoratorDefinition>;
|
|
45
|
+
/**
|
|
46
|
+
* Converts an object with defined decorators to a normalized array of decorators. The `id` key is added for each decorator and
|
|
47
|
+
* is used as the attribute's name in the model.
|
|
48
|
+
*/
|
|
49
|
+
export declare function normalizeDecorators(decorators?: Record<string, LinkDecoratorDefinition>): Array<NormalizedLinkDecoratorDefinition>;
|
|
50
|
+
/**
|
|
51
|
+
* Returns `true` if the specified `element` can be linked (the element allows the `linkHref` attribute).
|
|
52
|
+
*/
|
|
53
|
+
export declare function isLinkableElement(element: Element | null, schema: Schema): element is Element;
|
|
54
|
+
/**
|
|
55
|
+
* Returns `true` if the specified `value` is an email.
|
|
56
|
+
*/
|
|
57
|
+
export declare function isEmail(value: string): boolean;
|
|
58
|
+
/**
|
|
59
|
+
* Adds the protocol prefix to the specified `link` when:
|
|
60
|
+
*
|
|
61
|
+
* * it does not contain it already, and there is a {@link module:link/linkconfig~LinkConfig#defaultProtocol `defaultProtocol` }
|
|
62
|
+
* configuration value provided,
|
|
63
|
+
* * or the link is an email address.
|
|
64
|
+
*/
|
|
65
|
+
export declare function addLinkProtocolIfApplicable(link: string, defaultProtocol?: string): string;
|
|
66
|
+
/**
|
|
67
|
+
* Checks if protocol is already included in the link.
|
|
68
|
+
*/
|
|
69
|
+
export declare function linkHasProtocol(link: string): boolean;
|
|
70
|
+
/**
|
|
71
|
+
* Opens the link in a new browser tab.
|
|
72
|
+
*/
|
|
73
|
+
export declare function openLink(link: string): void;
|
|
74
|
+
export type NormalizedLinkDecoratorAutomaticDefinition = LinkDecoratorAutomaticDefinition & {
|
|
75
|
+
id: string;
|
|
76
|
+
};
|
|
77
|
+
export type NormalizedLinkDecoratorManualDefinition = LinkDecoratorManualDefinition & {
|
|
78
|
+
id: string;
|
|
79
|
+
};
|
|
80
|
+
export type NormalizedLinkDecoratorDefinition = NormalizedLinkDecoratorAutomaticDefinition | NormalizedLinkDecoratorManualDefinition;
|
package/src/utils.js
CHANGED
|
@@ -1,128 +1,128 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
|
-
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
|
-
*/
|
|
5
|
-
import { upperFirst } from 'lodash-es';
|
|
6
|
-
const ATTRIBUTE_WHITESPACES = /[\u0000-\u0020\u00A0\u1680\u180E\u2000-\u2029\u205f\u3000]/g; // eslint-disable-line no-control-regex
|
|
7
|
-
const SAFE_URL = /^(?:(?:https?|ftps?|mailto):|[^a-z]|[a-z+.-]+(?:[^a-z+.:-]|$))/i;
|
|
8
|
-
// Simplified email test - should be run over previously found URL.
|
|
9
|
-
const EMAIL_REG_EXP = /^[\S]+@((?![-_])(?:[-\w\u00a1-\uffff]{0,63}[^-_]\.))+(?:[a-z\u00a1-\uffff]{2,})$/i;
|
|
10
|
-
// The regex checks for the protocol syntax ('xxxx://' or 'xxxx:')
|
|
11
|
-
// or non-word characters at the beginning of the link ('/', '#' etc.).
|
|
12
|
-
const PROTOCOL_REG_EXP = /^((\w+:(\/{2,})?)|(\W))/i;
|
|
13
|
-
/**
|
|
14
|
-
* A keystroke used by the {@link module:link/linkui~LinkUI link UI feature}.
|
|
15
|
-
*/
|
|
16
|
-
export const LINK_KEYSTROKE = 'Ctrl+K';
|
|
17
|
-
/**
|
|
18
|
-
* Returns `true` if a given view node is the link element.
|
|
19
|
-
*/
|
|
20
|
-
export function isLinkElement(node) {
|
|
21
|
-
return node.is('attributeElement') && !!node.getCustomProperty('link');
|
|
22
|
-
}
|
|
23
|
-
/**
|
|
24
|
-
* Creates a link {@link module:engine/view/attributeelement~AttributeElement} with the provided `href` attribute.
|
|
25
|
-
*/
|
|
26
|
-
export function createLinkElement(href, { writer }) {
|
|
27
|
-
// Priority 5 - https://github.com/ckeditor/ckeditor5-link/issues/121.
|
|
28
|
-
const linkElement = writer.createAttributeElement('a', { href }, { priority: 5 });
|
|
29
|
-
writer.setCustomProperty('link', true, linkElement);
|
|
30
|
-
return linkElement;
|
|
31
|
-
}
|
|
32
|
-
/**
|
|
33
|
-
* Returns a safe URL based on a given value.
|
|
34
|
-
*
|
|
35
|
-
* A URL is considered safe if it is safe for the user (does not contain any malicious code).
|
|
36
|
-
*
|
|
37
|
-
* If a URL is considered unsafe, a simple `"#"` is returned.
|
|
38
|
-
*
|
|
39
|
-
* @internal
|
|
40
|
-
*/
|
|
41
|
-
export function ensureSafeUrl(url) {
|
|
42
|
-
const urlString = String(url);
|
|
43
|
-
return isSafeUrl(urlString) ? urlString : '#';
|
|
44
|
-
}
|
|
45
|
-
/**
|
|
46
|
-
* Checks whether the given URL is safe for the user (does not contain any malicious code).
|
|
47
|
-
*/
|
|
48
|
-
function isSafeUrl(url) {
|
|
49
|
-
const normalizedUrl = url.replace(ATTRIBUTE_WHITESPACES, '');
|
|
50
|
-
return !!normalizedUrl.match(SAFE_URL);
|
|
51
|
-
}
|
|
52
|
-
/**
|
|
53
|
-
* Returns the {@link module:link/linkconfig~LinkConfig#decorators `config.link.decorators`} configuration processed
|
|
54
|
-
* to respect the locale of the editor, i.e. to display the {@link module:link/linkconfig~LinkDecoratorManualDefinition label}
|
|
55
|
-
* in the correct language.
|
|
56
|
-
*
|
|
57
|
-
* **Note**: Only the few most commonly used labels are translated automatically. Other labels should be manually
|
|
58
|
-
* translated in the {@link module:link/linkconfig~LinkConfig#decorators `config.link.decorators`} configuration.
|
|
59
|
-
*
|
|
60
|
-
* @param t Shorthand for {@link module:utils/locale~Locale#t Locale#t}.
|
|
61
|
-
* @param decorators The decorator reference where the label values should be localized.
|
|
62
|
-
*/
|
|
63
|
-
export function getLocalizedDecorators(t, decorators) {
|
|
64
|
-
const localizedDecoratorsLabels = {
|
|
65
|
-
'Open in a new tab': t('Open in a new tab'),
|
|
66
|
-
'Downloadable': t('Downloadable')
|
|
67
|
-
};
|
|
68
|
-
decorators.forEach(decorator => {
|
|
69
|
-
if ('label' in decorator && localizedDecoratorsLabels[decorator.label]) {
|
|
70
|
-
decorator.label = localizedDecoratorsLabels[decorator.label];
|
|
71
|
-
}
|
|
72
|
-
return decorator;
|
|
73
|
-
});
|
|
74
|
-
return decorators;
|
|
75
|
-
}
|
|
76
|
-
/**
|
|
77
|
-
* Converts an object with defined decorators to a normalized array of decorators. The `id` key is added for each decorator and
|
|
78
|
-
* is used as the attribute's name in the model.
|
|
79
|
-
*/
|
|
80
|
-
export function normalizeDecorators(decorators) {
|
|
81
|
-
const retArray = [];
|
|
82
|
-
if (decorators) {
|
|
83
|
-
for (const [key, value] of Object.entries(decorators)) {
|
|
84
|
-
const decorator = Object.assign({}, value, { id: `link${upperFirst(key)}` });
|
|
85
|
-
retArray.push(decorator);
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
return retArray;
|
|
89
|
-
}
|
|
90
|
-
/**
|
|
91
|
-
* Returns `true` if the specified `element` can be linked (the element allows the `linkHref` attribute).
|
|
92
|
-
*/
|
|
93
|
-
export function isLinkableElement(element, schema) {
|
|
94
|
-
if (!element) {
|
|
95
|
-
return false;
|
|
96
|
-
}
|
|
97
|
-
return schema.checkAttribute(element.name, 'linkHref');
|
|
98
|
-
}
|
|
99
|
-
/**
|
|
100
|
-
* Returns `true` if the specified `value` is an email.
|
|
101
|
-
*/
|
|
102
|
-
export function isEmail(value) {
|
|
103
|
-
return EMAIL_REG_EXP.test(value);
|
|
104
|
-
}
|
|
105
|
-
/**
|
|
106
|
-
* Adds the protocol prefix to the specified `link` when:
|
|
107
|
-
*
|
|
108
|
-
* * it does not contain it already, and there is a {@link module:link/linkconfig~LinkConfig#defaultProtocol `defaultProtocol` }
|
|
109
|
-
* configuration value provided,
|
|
110
|
-
* * or the link is an email address.
|
|
111
|
-
*/
|
|
112
|
-
export function addLinkProtocolIfApplicable(link, defaultProtocol) {
|
|
113
|
-
const protocol = isEmail(link) ? 'mailto:' : defaultProtocol;
|
|
114
|
-
const isProtocolNeeded = !!protocol && !linkHasProtocol(link);
|
|
115
|
-
return link && isProtocolNeeded ? protocol + link : link;
|
|
116
|
-
}
|
|
117
|
-
/**
|
|
118
|
-
* Checks if protocol is already included in the link.
|
|
119
|
-
*/
|
|
120
|
-
export function linkHasProtocol(link) {
|
|
121
|
-
return PROTOCOL_REG_EXP.test(link);
|
|
122
|
-
}
|
|
123
|
-
/**
|
|
124
|
-
* Opens the link in a new browser tab.
|
|
125
|
-
*/
|
|
126
|
-
export function openLink(link) {
|
|
127
|
-
window.open(link, '_blank', 'noopener');
|
|
128
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
|
+
*/
|
|
5
|
+
import { upperFirst } from 'lodash-es';
|
|
6
|
+
const ATTRIBUTE_WHITESPACES = /[\u0000-\u0020\u00A0\u1680\u180E\u2000-\u2029\u205f\u3000]/g; // eslint-disable-line no-control-regex
|
|
7
|
+
const SAFE_URL = /^(?:(?:https?|ftps?|mailto):|[^a-z]|[a-z+.-]+(?:[^a-z+.:-]|$))/i;
|
|
8
|
+
// Simplified email test - should be run over previously found URL.
|
|
9
|
+
const EMAIL_REG_EXP = /^[\S]+@((?![-_])(?:[-\w\u00a1-\uffff]{0,63}[^-_]\.))+(?:[a-z\u00a1-\uffff]{2,})$/i;
|
|
10
|
+
// The regex checks for the protocol syntax ('xxxx://' or 'xxxx:')
|
|
11
|
+
// or non-word characters at the beginning of the link ('/', '#' etc.).
|
|
12
|
+
const PROTOCOL_REG_EXP = /^((\w+:(\/{2,})?)|(\W))/i;
|
|
13
|
+
/**
|
|
14
|
+
* A keystroke used by the {@link module:link/linkui~LinkUI link UI feature}.
|
|
15
|
+
*/
|
|
16
|
+
export const LINK_KEYSTROKE = 'Ctrl+K';
|
|
17
|
+
/**
|
|
18
|
+
* Returns `true` if a given view node is the link element.
|
|
19
|
+
*/
|
|
20
|
+
export function isLinkElement(node) {
|
|
21
|
+
return node.is('attributeElement') && !!node.getCustomProperty('link');
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Creates a link {@link module:engine/view/attributeelement~AttributeElement} with the provided `href` attribute.
|
|
25
|
+
*/
|
|
26
|
+
export function createLinkElement(href, { writer }) {
|
|
27
|
+
// Priority 5 - https://github.com/ckeditor/ckeditor5-link/issues/121.
|
|
28
|
+
const linkElement = writer.createAttributeElement('a', { href }, { priority: 5 });
|
|
29
|
+
writer.setCustomProperty('link', true, linkElement);
|
|
30
|
+
return linkElement;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Returns a safe URL based on a given value.
|
|
34
|
+
*
|
|
35
|
+
* A URL is considered safe if it is safe for the user (does not contain any malicious code).
|
|
36
|
+
*
|
|
37
|
+
* If a URL is considered unsafe, a simple `"#"` is returned.
|
|
38
|
+
*
|
|
39
|
+
* @internal
|
|
40
|
+
*/
|
|
41
|
+
export function ensureSafeUrl(url) {
|
|
42
|
+
const urlString = String(url);
|
|
43
|
+
return isSafeUrl(urlString) ? urlString : '#';
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Checks whether the given URL is safe for the user (does not contain any malicious code).
|
|
47
|
+
*/
|
|
48
|
+
function isSafeUrl(url) {
|
|
49
|
+
const normalizedUrl = url.replace(ATTRIBUTE_WHITESPACES, '');
|
|
50
|
+
return !!normalizedUrl.match(SAFE_URL);
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Returns the {@link module:link/linkconfig~LinkConfig#decorators `config.link.decorators`} configuration processed
|
|
54
|
+
* to respect the locale of the editor, i.e. to display the {@link module:link/linkconfig~LinkDecoratorManualDefinition label}
|
|
55
|
+
* in the correct language.
|
|
56
|
+
*
|
|
57
|
+
* **Note**: Only the few most commonly used labels are translated automatically. Other labels should be manually
|
|
58
|
+
* translated in the {@link module:link/linkconfig~LinkConfig#decorators `config.link.decorators`} configuration.
|
|
59
|
+
*
|
|
60
|
+
* @param t Shorthand for {@link module:utils/locale~Locale#t Locale#t}.
|
|
61
|
+
* @param decorators The decorator reference where the label values should be localized.
|
|
62
|
+
*/
|
|
63
|
+
export function getLocalizedDecorators(t, decorators) {
|
|
64
|
+
const localizedDecoratorsLabels = {
|
|
65
|
+
'Open in a new tab': t('Open in a new tab'),
|
|
66
|
+
'Downloadable': t('Downloadable')
|
|
67
|
+
};
|
|
68
|
+
decorators.forEach(decorator => {
|
|
69
|
+
if ('label' in decorator && localizedDecoratorsLabels[decorator.label]) {
|
|
70
|
+
decorator.label = localizedDecoratorsLabels[decorator.label];
|
|
71
|
+
}
|
|
72
|
+
return decorator;
|
|
73
|
+
});
|
|
74
|
+
return decorators;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Converts an object with defined decorators to a normalized array of decorators. The `id` key is added for each decorator and
|
|
78
|
+
* is used as the attribute's name in the model.
|
|
79
|
+
*/
|
|
80
|
+
export function normalizeDecorators(decorators) {
|
|
81
|
+
const retArray = [];
|
|
82
|
+
if (decorators) {
|
|
83
|
+
for (const [key, value] of Object.entries(decorators)) {
|
|
84
|
+
const decorator = Object.assign({}, value, { id: `link${upperFirst(key)}` });
|
|
85
|
+
retArray.push(decorator);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return retArray;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Returns `true` if the specified `element` can be linked (the element allows the `linkHref` attribute).
|
|
92
|
+
*/
|
|
93
|
+
export function isLinkableElement(element, schema) {
|
|
94
|
+
if (!element) {
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
97
|
+
return schema.checkAttribute(element.name, 'linkHref');
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Returns `true` if the specified `value` is an email.
|
|
101
|
+
*/
|
|
102
|
+
export function isEmail(value) {
|
|
103
|
+
return EMAIL_REG_EXP.test(value);
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Adds the protocol prefix to the specified `link` when:
|
|
107
|
+
*
|
|
108
|
+
* * it does not contain it already, and there is a {@link module:link/linkconfig~LinkConfig#defaultProtocol `defaultProtocol` }
|
|
109
|
+
* configuration value provided,
|
|
110
|
+
* * or the link is an email address.
|
|
111
|
+
*/
|
|
112
|
+
export function addLinkProtocolIfApplicable(link, defaultProtocol) {
|
|
113
|
+
const protocol = isEmail(link) ? 'mailto:' : defaultProtocol;
|
|
114
|
+
const isProtocolNeeded = !!protocol && !linkHasProtocol(link);
|
|
115
|
+
return link && isProtocolNeeded ? protocol + link : link;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Checks if protocol is already included in the link.
|
|
119
|
+
*/
|
|
120
|
+
export function linkHasProtocol(link) {
|
|
121
|
+
return PROTOCOL_REG_EXP.test(link);
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Opens the link in a new browser tab.
|
|
125
|
+
*/
|
|
126
|
+
export function openLink(link) {
|
|
127
|
+
window.open(link, '_blank', 'noopener');
|
|
128
|
+
}
|