-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
utils.js
262 lines (223 loc) · 6.73 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/**
* WordPress dependencies
*/
import {
getProtocol,
isValidProtocol,
getAuthority,
isValidAuthority,
getPath,
isValidPath,
getQueryString,
isValidQueryString,
getFragment,
isValidFragment,
} from '@wordpress/url';
/**
* 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 (
protocol.startsWith( '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 ( trimmedHref.startsWith( '#' ) && ! isValidFragment( trimmedHref ) ) {
return false;
}
return true;
}
/**
* Generates the format object that will be applied to the link text.
*
* @param {Object} options
* @param {string} options.url The href of the link.
* @param {string} options.type The type of the link.
* @param {string} options.id The ID of the link.
* @param {boolean} options.opensInNewWindow Whether this link will open in a new window.
* @param {boolean} options.nofollow Whether this link is marked as no follow relationship.
* @return {Object} The final format object.
*/
export function createLinkFormat( {
url,
type,
id,
opensInNewWindow,
nofollow,
} ) {
const format = {
type: 'core/link',
attributes: {
url,
},
};
if ( type ) {
format.attributes.type = type;
}
if ( id ) {
format.attributes.id = id;
}
if ( opensInNewWindow ) {
format.attributes.target = '_blank';
format.attributes.rel = format.attributes.rel
? format.attributes.rel + ' noreferrer noopener'
: 'noreferrer noopener';
}
if ( nofollow ) {
format.attributes.rel = format.attributes.rel
? format.attributes.rel + ' nofollow'
: 'nofollow';
}
return format;
}
/* eslint-disable jsdoc/no-undefined-types */
/**
* Get the start and end boundaries of a given format from a rich text value.
*
*
* @param {RichTextValue} value the rich text value to interrogate.
* @param {string} format the identifier for the target format (e.g. `core/link`, `core/bold`).
* @param {number?} startIndex optional startIndex to seek from.
* @param {number?} endIndex optional endIndex to seek from.
* @return {Object} object containing start and end values for the given format.
*/
/* eslint-enable jsdoc/no-undefined-types */
export function getFormatBoundary(
value,
format,
startIndex = value.start,
endIndex = value.end
) {
const EMPTY_BOUNDARIES = {
start: null,
end: null,
};
const { formats } = value;
let targetFormat;
let initialIndex;
if ( ! formats?.length ) {
return EMPTY_BOUNDARIES;
}
// Clone formats to avoid modifying source formats.
const newFormats = formats.slice();
const formatAtStart = newFormats[ startIndex ]?.find(
( { type } ) => type === format.type
);
const formatAtEnd = newFormats[ endIndex ]?.find(
( { type } ) => type === format.type
);
const formatAtEndMinusOne = newFormats[ endIndex - 1 ]?.find(
( { type } ) => type === format.type
);
if ( !! formatAtStart ) {
// Set values to conform to "start"
targetFormat = formatAtStart;
initialIndex = startIndex;
} else if ( !! formatAtEnd ) {
// Set values to conform to "end"
targetFormat = formatAtEnd;
initialIndex = endIndex;
} else if ( !! formatAtEndMinusOne ) {
// This is an edge case which will occur if you create a format, then place
// the caret just before the format and hit the back ARROW key. The resulting
// value object will have start and end +1 beyond the edge of the format boundary.
targetFormat = formatAtEndMinusOne;
initialIndex = endIndex - 1;
} else {
return EMPTY_BOUNDARIES;
}
const index = newFormats[ initialIndex ].indexOf( targetFormat );
const walkingArgs = [ newFormats, initialIndex, targetFormat, index ];
// Walk the startIndex "backwards" to the leading "edge" of the matching format.
startIndex = walkToStart( ...walkingArgs );
// Walk the endIndex "forwards" until the trailing "edge" of the matching format.
endIndex = walkToEnd( ...walkingArgs );
// Safe guard: start index cannot be less than 0.
startIndex = startIndex < 0 ? 0 : startIndex;
// // Return the indicies of the "edges" as the boundaries.
return {
start: startIndex,
end: endIndex,
};
}
/**
* Walks forwards/backwards towards the boundary of a given format within an
* array of format objects. Returns the index of the boundary.
*
* @param {Array} formats the formats to search for the given format type.
* @param {number} initialIndex the starting index from which to walk.
* @param {Object} targetFormatRef a reference to the format type object being sought.
* @param {number} formatIndex the index at which we expect the target format object to be.
* @param {string} direction either 'forwards' or 'backwards' to indicate the direction.
* @return {number} the index of the boundary of the given format.
*/
function walkToBoundary(
formats,
initialIndex,
targetFormatRef,
formatIndex,
direction
) {
let index = initialIndex;
const directions = {
forwards: 1,
backwards: -1,
};
const directionIncrement = directions[ direction ] || 1; // invalid direction arg default to forwards
const inverseDirectionIncrement = directionIncrement * -1;
while (
formats[ index ] &&
formats[ index ][ formatIndex ] === targetFormatRef
) {
// Increment/decrement in the direction of operation.
index = index + directionIncrement;
}
// Restore by one in inverse direction of operation
// to avoid out of bounds.
index = index + inverseDirectionIncrement;
return index;
}
const partialRight =
( fn, ...partialArgs ) =>
( ...args ) =>
fn( ...args, ...partialArgs );
const walkToStart = partialRight( walkToBoundary, 'backwards' );
const walkToEnd = partialRight( walkToBoundary, 'forwards' );