forked from firefox-devtools/debugger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathelement-node.js
More file actions
174 lines (154 loc) · 4.25 KB
/
Copy pathelement-node.js
File metadata and controls
174 lines (154 loc) · 4.25 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
/* 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/>. */
// ReactJS
const PropTypes = require("prop-types");
// Utils
const { isGrip, wrapRender } = require("./rep-utils");
const { rep: StringRep } = require("./string");
const { MODE } = require("./constants");
const nodeConstants = require("../shared/dom-node-constants");
const dom = require("react-dom-factories");
const { span } = dom;
/**
* Renders DOM element node.
*/
ElementNode.propTypes = {
object: PropTypes.object.isRequired,
inspectIconTitle: PropTypes.string,
// @TODO Change this to Object.values when supported in Node's version of V8
mode: PropTypes.oneOf(Object.keys(MODE).map(key => MODE[key])),
onDOMNodeClick: PropTypes.func,
onDOMNodeMouseOver: PropTypes.func,
onDOMNodeMouseOut: PropTypes.func,
onInspectIconClick: PropTypes.func
};
function ElementNode(props) {
const {
object,
inspectIconTitle,
mode,
onDOMNodeClick,
onDOMNodeMouseOver,
onDOMNodeMouseOut,
onInspectIconClick
} = props;
const elements = getElements(object, mode);
const isInTree = object.preview && object.preview.isConnected === true;
const baseConfig = {
"data-link-actor-id": object.actor,
className: "objectBox objectBox-node"
};
let inspectIcon;
if (isInTree) {
if (onDOMNodeClick) {
Object.assign(baseConfig, {
onClick: _ => onDOMNodeClick(object)
});
}
if (onDOMNodeMouseOver) {
Object.assign(baseConfig, {
onMouseOver: _ => onDOMNodeMouseOver(object)
});
}
if (onDOMNodeMouseOut) {
Object.assign(baseConfig, {
onMouseOut: onDOMNodeMouseOut
});
}
if (onInspectIconClick) {
inspectIcon = dom.button({
className: "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);
}
});
}
}
return span(baseConfig, ...elements, inspectIcon);
}
function getElements(grip, mode) {
const {
attributes,
nodeName,
isAfterPseudoElement,
isBeforePseudoElement
} = grip.preview;
const nodeNameElement = span(
{
className: "tag-name"
},
nodeName
);
if (isAfterPseudoElement || isBeforePseudoElement) {
return [
span(
{ className: "attrName" },
`::${isAfterPseudoElement ? "after" : "before"}`
)
];
}
if (mode === MODE.TINY) {
const elements = [nodeNameElement];
if (attributes.id) {
elements.push(span({ className: "attrName" }, `#${attributes.id}`));
}
if (attributes.class) {
elements.push(
span(
{ className: "attrName" },
attributes.class
.trim()
.split(/\s+/)
.map(cls => `.${cls}`)
.join("")
)
);
}
return elements;
}
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, i, keys) => {
const value = attributes[name];
const attribute = span(
{},
span({ className: "attrName" }, name),
span({ className: "attrEqual" }, "="),
StringRep({ className: "attrValue", object: value })
);
return arr.concat([" ", attribute]);
}, []);
return [
span({ className: "angleBracket" }, "<"),
nodeNameElement,
...attributeElements,
span({ className: "angleBracket" }, ">")
];
}
// Registration
function supportsObject(object, noGrip = false) {
if (noGrip === true || !isGrip(object)) {
return false;
}
return (
object.preview && object.preview.nodeType === nodeConstants.ELEMENT_NODE
);
}
// Exports from this module
module.exports = {
rep: wrapRender(ElementNode),
supportsObject
};