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
|
/* 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";
const EventEmitter = require("resource://devtools/shared/event-emitter.js");
loader.lazyRequireGetter(
this,
"nodeConstants",
"resource://devtools/shared/dom-node-constants.js"
);
/**
* Selection is a singleton belonging to the Toolbox that manages the current selected
* NodeFront. In addition, it provides some helpers about the context of the selected
* node.
*
* API
*
* new Selection()
* destroy()
* nodeFront (readonly)
* setNodeFront(node, origin="unknown")
*
* Helpers:
*
* window
* document
* isRoot()
* isNode()
* isHTMLNode()
*
* Check the nature of the node:
*
* isElementNode()
* isAttributeNode()
* isTextNode()
* isCDATANode()
* isEntityRefNode()
* isEntityNode()
* isProcessingInstructionNode()
* isCommentNode()
* isDocumentNode()
* isDocumentTypeNode()
* isDocumentFragmentNode()
* isNotationNode()
*
* Events:
* "new-node-front" when the inner node changed
* "attribute-changed" when an attribute is changed
* "detached-front" when the node (or one of its parents) is removed from
* the document
* "reparented" when the node (or one of its parents) is moved under
* a different node
*/
function Selection() {
EventEmitter.decorate(this);
// The WalkerFront is dynamic and is always set to the selected NodeFront's WalkerFront.
this._walker = null;
// A single node front can be represented twice on the client when the node is a slotted
// element. It will be displayed once as a direct child of the host element, and once as
// a child of a slot in the "shadow DOM". The latter is called the slotted version.
this._isSlotted = false;
this._onMutations = this._onMutations.bind(this);
this.setNodeFront = this.setNodeFront.bind(this);
}
Selection.prototype = {
_onMutations(mutations) {
let attributeChange = false;
let pseudoChange = false;
let detached = false;
let parentNode = null;
for (const m of mutations) {
if (!attributeChange && m.type == "attributes") {
attributeChange = true;
}
if (m.type == "childList") {
if (!detached && !this.isConnected()) {
if (this.isNode()) {
parentNode = m.target;
}
detached = true;
}
}
if (m.type == "pseudoClassLock") {
pseudoChange = true;
}
}
// Fire our events depending on what changed in the mutations array
if (attributeChange) {
this.emit("attribute-changed");
}
if (pseudoChange) {
this.emit("pseudoclass");
}
if (detached) {
this.emit("detached-front", parentNode);
}
},
destroy() {
this.setWalker();
this._nodeFront = null;
},
/**
* @param {WalkerFront|null} walker
*/
setWalker(walker = null) {
if (this._walker) {
this._removeWalkerFrontEventListeners(this._walker);
}
this._walker = walker;
if (this._walker) {
this._setWalkerFrontEventListeners(this._walker);
}
},
/**
* Set event listeners on the passed walker front
*
* @param {WalkerFront} walker
*/
_setWalkerFrontEventListeners(walker) {
walker.on("mutations", this._onMutations);
},
/**
* Remove event listeners we previously set on walker front
*
* @param {WalkerFront} walker
*/
_removeWalkerFrontEventListeners(walker) {
walker.off("mutations", this._onMutations);
},
/**
* Called when a target front is destroyed.
*
* @param {TargetFront} front
* @emits detached-front
*/
onTargetDestroyed(targetFront) {
// if the current walker belongs to the target that is destroyed, emit a `detached-front`
// event so consumers can act accordingly (e.g. in the inspector, another node will be
// selected)
if (
this._walker &&
!targetFront.isTopLevel &&
this._walker.targetFront == targetFront
) {
this._removeWalkerFrontEventListeners(this._walker);
this.emit("detached-front");
}
},
/**
* Update the currently selected node-front.
*
* @param {NodeFront} nodeFront
* The NodeFront being selected.
* @param {Object} (optional)
* - {String} reason: Reason that triggered the selection, will be fired with
* the "new-node-front" event.
* - {Boolean} isSlotted: Is the selection representing the slotted version of
* the node.
*/
setNodeFront(nodeFront, { reason = "unknown", isSlotted = false } = {}) {
this.reason = reason;
// If an inlineTextChild text node is being set, then set it's parent instead.
const parentNode = nodeFront && nodeFront.parentNode();
if (nodeFront && parentNode && parentNode.inlineTextChild === nodeFront) {
nodeFront = parentNode;
}
if (this._nodeFront == null && nodeFront == null) {
// Avoid to notify multiple "unselected" events with a null/undefined nodeFront
// (e.g. once when the webpage start to navigate away from the current webpage,
// and then again while the new page is being loaded).
return;
}
this.emit("node-front-will-unset");
this._isSlotted = isSlotted;
this._nodeFront = nodeFront;
if (nodeFront) {
this.setWalker(nodeFront.walkerFront);
} else {
this.setWalker();
}
this.emit("new-node-front", nodeFront, this.reason);
},
get nodeFront() {
return this._nodeFront;
},
isRoot() {
return (
this.isNode() && this.isConnected() && this._nodeFront.isDocumentElement
);
},
isNode() {
return !!this._nodeFront;
},
isConnected() {
let node = this._nodeFront;
if (!node || node.isDestroyed()) {
return false;
}
while (node) {
if (node === this._walker.rootNode) {
return true;
}
node = node.parentOrHost();
}
return false;
},
isHTMLNode() {
const xhtmlNs = "http://www.w3.org/1999/xhtml";
return this.isNode() && this.nodeFront.namespaceURI == xhtmlNs;
},
isSVGNode() {
const svgNs = "http://www.w3.org/2000/svg";
return this.isNode() && this.nodeFront.namespaceURI == svgNs;
},
isMathMLNode() {
const mathmlNs = "http://www.w3.org/1998/Math/MathML";
return this.isNode() && this.nodeFront.namespaceURI == mathmlNs;
},
// Node type
isElementNode() {
return (
this.isNode() && this.nodeFront.nodeType == nodeConstants.ELEMENT_NODE
);
},
isPseudoElementNode() {
return this.isNode() && this.nodeFront.isPseudoElement;
},
isAnonymousNode() {
return this.isNode() && this.nodeFront.isAnonymous;
},
isAttributeNode() {
return (
this.isNode() && this.nodeFront.nodeType == nodeConstants.ATTRIBUTE_NODE
);
},
isTextNode() {
return this.isNode() && this.nodeFront.nodeType == nodeConstants.TEXT_NODE;
},
isCDATANode() {
return (
this.isNode() &&
this.nodeFront.nodeType == nodeConstants.CDATA_SECTION_NODE
);
},
isEntityRefNode() {
return (
this.isNode() &&
this.nodeFront.nodeType == nodeConstants.ENTITY_REFERENCE_NODE
);
},
isEntityNode() {
return (
this.isNode() && this.nodeFront.nodeType == nodeConstants.ENTITY_NODE
);
},
isProcessingInstructionNode() {
return (
this.isNode() &&
this.nodeFront.nodeType == nodeConstants.PROCESSING_INSTRUCTION_NODE
);
},
isCommentNode() {
return (
this.isNode() &&
this.nodeFront.nodeType == nodeConstants.PROCESSING_INSTRUCTION_NODE
);
},
isDocumentNode() {
return (
this.isNode() && this.nodeFront.nodeType == nodeConstants.DOCUMENT_NODE
);
},
/**
* @returns true if the selection is the <body> HTML element.
*/
isBodyNode() {
return (
this.isHTMLNode() &&
this.isConnected() &&
this.nodeFront.nodeName === "BODY"
);
},
/**
* @returns true if the selection is the <head> HTML element.
*/
isHeadNode() {
return (
this.isHTMLNode() &&
this.isConnected() &&
this.nodeFront.nodeName === "HEAD"
);
},
isDocumentTypeNode() {
return (
this.isNode() &&
this.nodeFront.nodeType == nodeConstants.DOCUMENT_TYPE_NODE
);
},
isDocumentFragmentNode() {
return (
this.isNode() &&
this.nodeFront.nodeType == nodeConstants.DOCUMENT_FRAGMENT_NODE
);
},
isNotationNode() {
return (
this.isNode() && this.nodeFront.nodeType == nodeConstants.NOTATION_NODE
);
},
isSlotted() {
return this._isSlotted;
},
isShadowRootNode() {
return this.isNode() && this.nodeFront.isShadowRoot;
},
};
module.exports = Selection;
|