forked from firefox-devtools/debugger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.js
More file actions
301 lines (254 loc) · 7.06 KB
/
Copy pathstring.js
File metadata and controls
301 lines (254 loc) · 7.06 KB
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */
// Dependencies
const PropTypes = require("prop-types");
const {
containsURL,
isURL,
escapeString,
getGripType,
rawCropString,
sanitizeString,
wrapRender,
isGrip,
tokenSplitRegex,
ELLIPSIS
} = require("./rep-utils");
const dom = require("react-dom-factories");
const { a, span } = dom;
/**
* Renders a string. String value is enclosed within quotes.
*/
StringRep.propTypes = {
useQuotes: PropTypes.bool,
escapeWhitespace: PropTypes.bool,
style: PropTypes.object,
cropLimit: PropTypes.number.isRequired,
member: PropTypes.object,
object: PropTypes.object.isRequired,
openLink: PropTypes.func,
className: PropTypes.string
};
function StringRep(props) {
const {
className,
style,
cropLimit,
object,
useQuotes = true,
escapeWhitespace = true,
member,
openLink
} = props;
let text = object;
const isLong = isLongString(object);
const isOpen = member && member.open;
const shouldCrop = !isOpen && cropLimit && text.length > cropLimit;
if (isLong) {
text = maybeCropLongString(
{
shouldCrop,
cropLimit
},
text
);
const { fullText } = object;
if (isOpen && fullText) {
text = fullText;
}
}
text = formatText(
{
useQuotes,
escapeWhitespace
},
text
);
const config = getElementConfig({
className,
style,
actor: object.actor
});
if (!isLong) {
if (containsURL(text)) {
return span(
config,
...getLinkifiedElements(text, shouldCrop && cropLimit, openLink)
);
}
// Cropping of longString has been handled before formatting.
text = maybeCropString(
{
isLong,
shouldCrop,
cropLimit
},
text
);
}
return span(config, text);
}
function maybeCropLongString(opts, text) {
const { shouldCrop, cropLimit } = opts;
const { initial, length } = text;
text = shouldCrop ? initial.substring(0, cropLimit) : initial;
if (text.length < length) {
text += ELLIPSIS;
}
return text;
}
function formatText(opts, text) {
const { useQuotes, escapeWhitespace } = opts;
return useQuotes
? escapeString(text, escapeWhitespace)
: sanitizeString(text);
}
function getElementConfig(opts) {
const { className, style, actor } = opts;
const config = {};
if (actor) {
config["data-link-actor-id"] = actor;
}
const classNames = ["objectBox", "objectBox-string"];
if (className) {
classNames.push(className);
}
config.className = classNames.join(" ");
if (style) {
config.style = style;
}
return config;
}
function maybeCropString(opts, text) {
const { shouldCrop, cropLimit } = opts;
return shouldCrop ? rawCropString(text, cropLimit) : text;
}
/**
* Get an array of the elements representing the string, cropped if needed,
* with actual links.
*
* @param {String} text: The actual string to linkify.
* @param {Integer | null} cropLimit
* @param {Function} openLink: Function handling the link opening.
* @returns {Array<String|ReactElement>}
*/
function getLinkifiedElements(text, cropLimit, openLink) {
const halfLimit = Math.ceil((cropLimit - ELLIPSIS.length) / 2);
const startCropIndex = cropLimit ? halfLimit : null;
const endCropIndex = cropLimit ? text.length - halfLimit : null;
// As we walk through the tokens of the source string, we make sure to
// preserve the original whitespace that separated the tokens.
let currentIndex = 0;
const items = [];
for (const token of text.split(tokenSplitRegex)) {
if (isURL(token)) {
// Let's grab all the non-url strings before the link.
const tokenStart = text.indexOf(token, currentIndex);
let nonUrlText = text.slice(currentIndex, tokenStart);
nonUrlText = getCroppedString(
nonUrlText,
currentIndex,
startCropIndex,
endCropIndex
);
if (nonUrlText) {
items.push(nonUrlText);
}
// Update the index to match the beginning of the token.
currentIndex = tokenStart;
const linkText = getCroppedString(
token,
currentIndex,
startCropIndex,
endCropIndex
);
if (linkText) {
items.push(
a(
{
className: "url",
title: token,
draggable: false,
onClick: openLink
? e => {
e.preventDefault();
openLink(token, e);
}
: null
},
linkText
)
);
}
currentIndex = tokenStart + token.length;
}
}
// Clean up any non-URL text at the end of the source string,
// i.e. not handled in the loop.
if (currentIndex !== text.length) {
let nonUrlText = text.slice(currentIndex, text.length);
if (currentIndex < endCropIndex) {
const cutIndex = endCropIndex - currentIndex;
nonUrlText = nonUrlText.substring(cutIndex);
}
items.push(nonUrlText);
}
return items;
}
/**
* Returns a cropped substring given an offset, start and end crop indices in a
* parent string.
*
* @param {String} text: The substring to crop.
* @param {Integer} offset: The offset corresponding to the index at which
* the substring is in the parent string.
* @param {Integer|null} startCropIndex: the index where the start of the crop
* should happen in the parent string.
* @param {Integer|null} endCropIndex: the index where the end of the crop
* should happen in the parent string
* @returns {String|null} The cropped substring, or null if the text is
* completly cropped.
*/
function getCroppedString(text, offset = 0, startCropIndex, endCropIndex) {
if (!startCropIndex) {
return text;
}
const start = offset;
const end = offset + text.length;
const shouldBeVisible = !(start >= startCropIndex && end <= endCropIndex);
if (!shouldBeVisible) {
return null;
}
const shouldCropEnd = start < startCropIndex && end > startCropIndex;
const shouldCropStart = start < endCropIndex && end > endCropIndex;
if (shouldCropEnd) {
const cutIndex = startCropIndex - start;
return (
text.substring(0, cutIndex) +
ELLIPSIS +
(shouldCropStart ? text.substring(endCropIndex - start) : "")
);
}
if (shouldCropStart) {
// The string should be cropped at the beginning.
const cutIndex = endCropIndex - start;
return text.substring(cutIndex);
}
return text;
}
function isLongString(object) {
return object && object.type === "longString";
}
function supportsObject(object, noGrip = false) {
if (noGrip === false && isGrip(object)) {
return isLongString(object);
}
return getGripType(object, noGrip) == "string";
}
// Exports from this module
module.exports = {
rep: wrapRender(StringRep),
supportsObject,
isLongString
};