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
|
/* 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";
// Make this available to both AMD and CJS environments
define(function (require, exports, module) {
// ReactJS
const {
button,
span,
} = require("devtools/client/shared/vendor/react-dom-factories");
const PropTypes = require("devtools/client/shared/vendor/react-prop-types");
// Utils
const {
appendRTLClassNameIfNeeded,
wrapRender,
} = require("devtools/client/shared/components/reps/reps/rep-utils");
const {
rep: StringRep,
isLongString,
} = require("devtools/client/shared/components/reps/reps/string");
const {
MODE,
} = require("devtools/client/shared/components/reps/reps/constants");
const nodeConstants = require("devtools/client/shared/components/reps/shared/dom-node-constants");
const MAX_ATTRIBUTE_LENGTH = 50;
/**
* Renders DOM element node.
*/
ElementNode.propTypes = {
object: PropTypes.object.isRequired,
// The class should be in reps.css
inspectIconTitle: PropTypes.oneOf(["open-inspector", "highlight-node"]),
inspectIconClassName: PropTypes.string,
mode: PropTypes.oneOf(Object.values(MODE)),
onDOMNodeClick: PropTypes.func,
onDOMNodeMouseOver: PropTypes.func,
onDOMNodeMouseOut: PropTypes.func,
onInspectIconClick: PropTypes.func,
shouldRenderTooltip: PropTypes.bool,
};
function ElementNode(props) {
const { object, mode, shouldRenderTooltip } = props;
const {
isAfterPseudoElement,
isBeforePseudoElement,
isMarkerPseudoElement,
} = object.preview;
let renderElements = [];
const isInTree = object.preview && object.preview.isConnected === true;
let config = getElementConfig({ ...props, isInTree });
const inspectIcon = getInspectIcon({ ...props, isInTree });
// Elements Case 1: Pseudo Element
if (
isAfterPseudoElement ||
isBeforePseudoElement ||
isMarkerPseudoElement
) {
const pseudoNodeElement = getPseudoNodeElement(object);
// Regenerate config if shouldRenderTooltip
if (shouldRenderTooltip) {
const tooltipString = pseudoNodeElement.content;
config = getElementConfig({ ...props, tooltipString, isInTree });
}
// Return ONLY pseudo node element as array[0]
renderElements = [
span(pseudoNodeElement.config, pseudoNodeElement.content),
];
} else if (mode === MODE.TINY) {
// Elements Case 2: MODE.TINY
const tinyElements = getTinyElements(object);
// Regenerate config to include tooltip title
if (shouldRenderTooltip) {
// Reduce for plaintext
const tooltipString = tinyElements.reduce(function (acc, cur) {
return acc.concat(cur.content);
}, "");
config = getElementConfig({ ...props, tooltipString, isInTree });
}
// Reduce for React elements
const tinyElementsRender = tinyElements.reduce(function (acc, cur) {
acc.push(span(cur.config, cur.content));
return acc;
}, []);
// Render array of React spans
renderElements = tinyElementsRender;
} else {
// Elements Default case
renderElements = getElements(props);
}
return span(config, ...renderElements, inspectIcon ? inspectIcon : null);
}
function getElementConfig(opts) {
const {
object,
isInTree,
onDOMNodeClick,
onDOMNodeMouseOver,
onDOMNodeMouseOut,
shouldRenderTooltip,
tooltipString,
} = opts;
// Initiate config
const config = {
"data-link-actor-id": object.actor,
"data-link-content-dom-reference": JSON.stringify(
object.contentDomReference
),
className: "objectBox objectBox-node",
};
// Triage event handlers
if (isInTree) {
if (onDOMNodeClick) {
Object.assign(config, {
onClick: _ => onDOMNodeClick(object),
className: `${config.className} clickable`,
});
}
if (onDOMNodeMouseOver) {
Object.assign(config, {
onMouseOver: _ => onDOMNodeMouseOver(object),
});
}
if (onDOMNodeMouseOut) {
Object.assign(config, {
onMouseOut: _ => onDOMNodeMouseOut(object),
});
}
}
// If tooltip, build tooltip
if (tooltipString && shouldRenderTooltip) {
config.title = tooltipString;
}
// Return config obj
return config;
}
function getElements(opts) {
const { object: grip } = opts;
const { attributes, nodeName } = grip.preview;
const nodeNameElement = span(
{
className: appendRTLClassNameIfNeeded("tag-name", nodeName),
},
nodeName
);
const attributeKeys = Object.keys(attributes);
if (attributeKeys.includes("class")) {
attributeKeys.splice(attributeKeys.indexOf("class"), 1);
attributeKeys.unshift("class");
}
if (attributeKeys.includes("id")) {
attributeKeys.splice(attributeKeys.indexOf("id"), 1);
attributeKeys.unshift("id");
}
const attributeElements = attributeKeys.reduce((arr, name) => {
const value = attributes[name];
let title = isLongString(value) ? value.initial : value;
if (title.length < MAX_ATTRIBUTE_LENGTH) {
title = null;
}
const attribute = span(
{},
span(
{
className: appendRTLClassNameIfNeeded("attrName", name),
},
name
),
span({ className: "attrEqual" }, "="),
StringRep({
className: "attrValue",
object: value,
cropLimit: MAX_ATTRIBUTE_LENGTH,
title,
})
);
return arr.concat([" ", attribute]);
}, []);
return [
span({ className: "angleBracket" }, "<"),
nodeNameElement,
...attributeElements,
span({ className: "angleBracket" }, ">"),
];
}
function getTinyElements(grip) {
const { attributes, nodeName } = grip.preview;
// Initialize elements array
const elements = [
{
config: {
className: appendRTLClassNameIfNeeded("tag-name", nodeName),
},
content: nodeName,
},
];
// Push ID element
if (attributes.id) {
elements.push({
config: {
className: appendRTLClassNameIfNeeded("attrName", attributes.id),
},
content: `#${attributes.id}`,
});
}
// Push Classes
if (attributes.class) {
const elementClasses = attributes.class
.trim()
.split(/\s+/)
.map(cls => `.${cls}`)
.join("");
elements.push({
config: {
className: appendRTLClassNameIfNeeded("attrName", elementClasses),
},
content: elementClasses,
});
}
return elements;
}
function getPseudoNodeElement(grip) {
const {
isAfterPseudoElement,
isBeforePseudoElement,
isMarkerPseudoElement,
} = grip.preview;
let pseudoNodeName;
if (isAfterPseudoElement) {
pseudoNodeName = "after";
} else if (isBeforePseudoElement) {
pseudoNodeName = "before";
} else if (isMarkerPseudoElement) {
pseudoNodeName = "marker";
}
return {
config: { className: "attrName" },
content: `::${pseudoNodeName}`,
};
}
function getInspectIcon(opts) {
const {
object,
isInTree,
onInspectIconClick,
inspectIconTitle,
inspectIconClassName,
onDOMNodeClick,
} = opts;
if (!isInTree || !onInspectIconClick) {
return null;
}
return button({
className: inspectIconClassName || "open-inspector",
// TODO: Localize this with "openNodeInInspector" when Bug 1317038 lands
title: inspectIconTitle || "Click to select the node in the inspector",
onClick: e => {
if (onDOMNodeClick) {
e.stopPropagation();
}
onInspectIconClick(object, e);
},
});
}
// Registration
function supportsObject(object) {
return object?.preview?.nodeType === nodeConstants.ELEMENT_NODE;
}
// Exports from this module
module.exports = {
rep: wrapRender(ElementNode),
supportsObject,
MAX_ATTRIBUTE_LENGTH,
};
});
|