-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
utils.js
110 lines (94 loc) · 2.54 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/**
* External dependencies
*/
import { startsWith } from 'lodash';
/**
* WordPress dependencies
*/
import {
getProtocol,
isValidProtocol,
getAuthority,
isValidAuthority,
getPath,
isValidPath,
getQueryString,
isValidQueryString,
getFragment,
isValidFragment,
} from '@wordpress/url';
import { __, sprintf } from '@wordpress/i18n';
/**
* Check for issues with the provided href.
*
* @param {string} href The href.
*
* @return {boolean} Is the href invalid?
*/
export function isValidHref( href ) {
if ( ! href ) {
return false;
}
const trimmedHref = href.trim();
if ( ! trimmedHref ) {
return false;
}
// Does the href start with something that looks like a URL protocol?
if ( /^\S+:/.test( trimmedHref ) ) {
const protocol = getProtocol( trimmedHref );
if ( ! isValidProtocol( protocol ) ) {
return false;
}
// Add some extra checks for http(s) URIs, since these are the most common use-case.
// This ensures URIs with an http protocol have exactly two forward slashes following the protocol.
if ( startsWith( protocol, 'http' ) && ! /^https?:\/\/[^\/\s]/i.test( trimmedHref ) ) {
return false;
}
const authority = getAuthority( trimmedHref );
if ( ! isValidAuthority( authority ) ) {
return false;
}
const path = getPath( trimmedHref );
if ( path && ! isValidPath( path ) ) {
return false;
}
const queryString = getQueryString( trimmedHref );
if ( queryString && ! isValidQueryString( queryString ) ) {
return false;
}
const fragment = getFragment( trimmedHref );
if ( fragment && ! isValidFragment( fragment ) ) {
return false;
}
}
// Validate anchor links.
if ( startsWith( trimmedHref, '#' ) && ! isValidFragment( trimmedHref ) ) {
return false;
}
return true;
}
/**
* Generates the format object that will be applied to the link text.
*
* @param {string} url The href of the link.
* @param {boolean} opensInNewWindow Whether this link will open in a new window.
* @param {Object} text The text that is being hyperlinked.
*
* @return {Object} The final format object.
*/
export function createLinkFormat( { url, opensInNewWindow, text } ) {
const format = {
type: 'core/link',
attributes: {
url,
},
};
if ( opensInNewWindow ) {
// translators: accessibility label for external links, where the argument is the link text
const label = sprintf( __( '%s (opens in a new tab)' ), text );
format.attributes.target = '_blank';
format.attributes.rel = 'noreferrer noopener';
format.attributes[ 'aria-label' ] = label;
}
return format;
}