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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
|
/* 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/. */
"use strict";
// React & Redux
const {
Component,
createFactory,
createElement,
} = require("devtools/client/shared/vendor/react");
const dom = require("devtools/client/shared/vendor/react-dom-factories");
const { l10n } = require("devtools/client/webconsole/utils/messages");
const actions = require("devtools/client/webconsole/actions/index");
const {
MESSAGE_LEVEL,
MESSAGE_SOURCE,
MESSAGE_TYPE,
} = require("devtools/client/webconsole/constants");
const {
MessageIndent,
} = require("devtools/client/webconsole/components/Output/MessageIndent");
const MessageIcon = require("devtools/client/webconsole/components/Output/MessageIcon");
const FrameView = createFactory(
require("devtools/client/shared/components/Frame")
);
loader.lazyRequireGetter(
this,
"CollapseButton",
"devtools/client/webconsole/components/Output/CollapseButton"
);
loader.lazyRequireGetter(
this,
"MessageRepeat",
"devtools/client/webconsole/components/Output/MessageRepeat"
);
loader.lazyRequireGetter(
this,
"PropTypes",
"devtools/client/shared/vendor/react-prop-types"
);
loader.lazyRequireGetter(
this,
"SmartTrace",
"devtools/client/shared/components/SmartTrace"
);
class Message extends Component {
static get propTypes() {
return {
open: PropTypes.bool,
collapsible: PropTypes.bool,
collapseTitle: PropTypes.string,
onToggle: PropTypes.func,
source: PropTypes.string.isRequired,
type: PropTypes.string.isRequired,
level: PropTypes.string.isRequired,
indent: PropTypes.number.isRequired,
inWarningGroup: PropTypes.bool,
isBlockedNetworkMessage: PropTypes.bool,
topLevelClasses: PropTypes.array.isRequired,
messageBody: PropTypes.any.isRequired,
repeat: PropTypes.any,
frame: PropTypes.any,
attachment: PropTypes.any,
stacktrace: PropTypes.any,
messageId: PropTypes.string,
scrollToMessage: PropTypes.bool,
exceptionDocURL: PropTypes.string,
request: PropTypes.object,
dispatch: PropTypes.func,
timeStamp: PropTypes.number,
timestampsVisible: PropTypes.bool.isRequired,
serviceContainer: PropTypes.shape({
emitForTests: PropTypes.func.isRequired,
onViewSource: PropTypes.func.isRequired,
onViewSourceInDebugger: PropTypes.func,
onViewSourceInStyleEditor: PropTypes.func,
openContextMenu: PropTypes.func.isRequired,
openLink: PropTypes.func.isRequired,
sourceMapURLService: PropTypes.any,
}),
notes: PropTypes.arrayOf(
PropTypes.shape({
messageBody: PropTypes.string.isRequired,
frame: PropTypes.any,
})
),
maybeScrollToBottom: PropTypes.func,
message: PropTypes.object.isRequired,
};
}
static get defaultProps() {
return {
indent: 0,
};
}
constructor(props) {
super(props);
this.onLearnMoreClick = this.onLearnMoreClick.bind(this);
this.toggleMessage = this.toggleMessage.bind(this);
this.onContextMenu = this.onContextMenu.bind(this);
this.renderIcon = this.renderIcon.bind(this);
}
componentDidMount() {
if (this.messageNode) {
if (this.props.scrollToMessage) {
this.messageNode.scrollIntoView();
}
this.emitNewMessage(this.messageNode);
}
}
componentDidCatch(e) {
this.setState({ error: e });
}
// Event used in tests. Some message types don't pass it in because existing tests
// did not emit for them.
emitNewMessage(node) {
const { serviceContainer, messageId, timeStamp } = this.props;
serviceContainer.emitForTests(
"new-messages",
new Set([{ node, messageId, timeStamp }])
);
}
onLearnMoreClick(e) {
const { exceptionDocURL } = this.props;
this.props.serviceContainer.openLink(exceptionDocURL, e);
e.preventDefault();
}
toggleMessage(e) {
// Don't bubble up to the main App component, which redirects focus to input,
// making difficult for screen reader users to review output
e.stopPropagation();
const { open, dispatch, messageId, onToggle } = this.props;
// Early exit the function to avoid the message to collapse if the user is
// selecting a range in the toggle message.
const window = e.target.ownerDocument.defaultView;
if (window.getSelection && window.getSelection().type === "Range") {
return;
}
// If defined on props, we let the onToggle() method handle the toggling,
// otherwise we toggle the message open/closed ourselves.
if (onToggle) {
onToggle(messageId, e);
} else if (open) {
dispatch(actions.messageClose(messageId));
} else {
dispatch(actions.messageOpen(messageId));
}
}
onContextMenu(e) {
const { serviceContainer, source, request, messageId } = this.props;
const messageInfo = {
source,
request,
messageId,
};
serviceContainer.openContextMenu(e, messageInfo);
e.stopPropagation();
e.preventDefault();
}
renderIcon() {
const { level, inWarningGroup, isBlockedNetworkMessage, type } = this.props;
if (inWarningGroup) {
return undefined;
}
if (isBlockedNetworkMessage) {
return MessageIcon({
level: MESSAGE_LEVEL.ERROR,
type: "blockedReason",
});
}
return MessageIcon({
level,
type,
});
}
renderTimestamp() {
if (!this.props.timestampsVisible) {
return null;
}
return dom.span(
{
className: "timestamp devtools-monospace",
},
l10n.timestampString(this.props.timeStamp || Date.now())
);
}
renderErrorState() {
const newBugUrl =
"https://bugzilla.mozilla.org/enter_bug.cgi?product=DevTools&component=Console";
const timestampEl = this.renderTimestamp();
return dom.div(
{
className: "message error message-did-catch",
},
timestampEl,
MessageIcon({ level: "error" }),
dom.span(
{ className: "message-body-wrapper" },
dom.span(
{
className: "message-flex-body",
},
// Add whitespaces for formatting when copying to the clipboard.
timestampEl ? " " : null,
dom.span(
{ className: "message-body devtools-monospace" },
l10n.getFormatStr("webconsole.message.componentDidCatch.label", [
newBugUrl,
]),
dom.button(
{
className: "devtools-button",
onClick: () =>
navigator.clipboard.writeText(
JSON.stringify(
this.props.message,
function(key, value) {
// The message can hold one or multiple fronts that we need to serialize
if (value?.getGrip) {
return value.getGrip();
}
return value;
},
2
)
),
},
l10n.getStr(
"webconsole.message.componentDidCatch.copyButton.label"
)
)
)
)
),
dom.br()
);
}
// eslint-disable-next-line complexity
render() {
if (this.state && this.state.error) {
return this.renderErrorState();
}
const {
open,
collapsible,
collapseTitle,
source,
type,
level,
indent,
inWarningGroup,
topLevelClasses,
messageBody,
frame,
stacktrace,
serviceContainer,
exceptionDocURL,
messageId,
notes,
} = this.props;
topLevelClasses.push("message", source, type, level);
if (open) {
topLevelClasses.push("open");
}
const timestampEl = this.renderTimestamp();
const icon = this.renderIcon();
// Figure out if there is an expandable part to the message.
let attachment = null;
if (this.props.attachment) {
attachment = this.props.attachment;
} else if (stacktrace && open) {
attachment = dom.div(
{
className: "stacktrace devtools-monospace",
},
createElement(SmartTrace, {
stacktrace,
onViewSourceInDebugger:
serviceContainer.onViewSourceInDebugger ||
serviceContainer.onViewSource,
onViewSource: serviceContainer.onViewSource,
onReady: this.props.maybeScrollToBottom,
sourceMapURLService: serviceContainer.sourceMapURLService,
})
);
}
// If there is an expandable part, make it collapsible.
let collapse = null;
if (collapsible) {
collapse = createElement(CollapseButton, {
open,
title: collapseTitle,
onClick: this.toggleMessage,
});
}
let notesNodes;
if (notes) {
notesNodes = notes.map(note =>
dom.span(
{ className: "message-flex-body error-note" },
dom.span(
{ className: "message-body devtools-monospace" },
"note: " + note.messageBody
),
dom.span(
{ className: "message-location devtools-monospace" },
note.frame
? FrameView({
frame: note.frame,
onClick: serviceContainer
? serviceContainer.onViewSourceInDebugger ||
serviceContainer.onViewSource
: undefined,
showEmptyPathAsHost: true,
sourceMapURLService: serviceContainer
? serviceContainer.sourceMapURLService
: undefined,
})
: null
)
)
);
} else {
notesNodes = [];
}
const repeat =
this.props.repeat && this.props.repeat > 1
? createElement(MessageRepeat, { repeat: this.props.repeat })
: null;
let onFrameClick;
if (serviceContainer && frame) {
if (source === MESSAGE_SOURCE.CSS) {
onFrameClick =
serviceContainer.onViewSourceInStyleEditor ||
serviceContainer.onViewSource;
} else {
// Point everything else to debugger, if source not available,
// it will fall back to view-source.
onFrameClick =
serviceContainer.onViewSourceInDebugger ||
serviceContainer.onViewSource;
}
}
// Configure the location.
const location = dom.span(
{ className: "message-location devtools-monospace" },
frame
? FrameView({
frame,
onClick: onFrameClick,
showEmptyPathAsHost: true,
sourceMapURLService: serviceContainer
? serviceContainer.sourceMapURLService
: undefined,
messageSource: source,
})
: null
);
let learnMore;
if (exceptionDocURL) {
learnMore = dom.a(
{
className: "learn-more-link webconsole-learn-more-link",
href: exceptionDocURL,
title: exceptionDocURL.split("?")[0],
onClick: this.onLearnMoreClick,
},
`[${l10n.getStr("webConsoleMoreInfoLabel")}]`
);
}
const bodyElements = Array.isArray(messageBody)
? messageBody
: [messageBody];
return dom.div(
{
className: topLevelClasses.join(" "),
onContextMenu: this.onContextMenu,
ref: node => {
this.messageNode = node;
},
"data-message-id": messageId,
"aria-live": type === MESSAGE_TYPE.COMMAND ? "off" : "polite",
},
timestampEl,
MessageIndent({
indent,
inWarningGroup,
}),
this.props.isBlockedNetworkMessage ? collapse : icon,
this.props.isBlockedNetworkMessage ? icon : collapse,
dom.span(
{ className: "message-body-wrapper" },
dom.span(
{
className: "message-flex-body",
onClick: collapsible ? this.toggleMessage : undefined,
},
// Add whitespaces for formatting when copying to the clipboard.
timestampEl ? " " : null,
dom.span(
{ className: "message-body devtools-monospace" },
...bodyElements,
learnMore
),
repeat ? " " : null,
repeat,
" ",
location
),
attachment,
...notesNodes
),
// If an attachment is displayed, the final newline is handled by the attachment.
attachment ? null : dom.br()
);
}
}
module.exports = Message;
|