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
|
/* 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/. */
// This file contains event parsers that are then used by developer tools in
// order to find information about events affecting an HTML element.
"use strict";
const {Cc, Ci, Cu} = require("chrome");
loader.lazyGetter(this, "eventListenerService", () => {
return Cc["@mozilla.org/eventlistenerservice;1"]
.getService(Ci.nsIEventListenerService);
});
var parsers = [
{
id: "jQuery events",
getListeners: function (node) {
let global = node.ownerGlobal.wrappedJSObject;
let hasJQuery = global.jQuery && global.jQuery.fn && global.jQuery.fn.jquery;
if (!hasJQuery) {
return;
}
let jQuery = global.jQuery;
let handlers = [];
// jQuery 1.2+
let data = jQuery._data || jQuery.data;
if (data) {
let eventsObj = data(node, "events");
for (let type in eventsObj) {
let events = eventsObj[type];
for (let key in events) {
let event = events[key];
if (typeof event === "object" || typeof event === "function") {
let eventInfo = {
type: type,
handler: event.handler || event,
tags: "jQuery",
hide: {
capturing: true,
dom0: true
}
};
handlers.push(eventInfo);
}
}
}
}
// JQuery 1.0 & 1.1
let entry = jQuery(node)[0];
if (!entry) {
return handlers;
}
for (let type in entry.events) {
let events = entry.events[type];
for (let key in events) {
if (typeof events[key] === "function") {
let eventInfo = {
type: type,
handler: events[key],
tags: "jQuery",
hide: {
capturing: true,
dom0: true
}
};
handlers.push(eventInfo);
}
}
}
return handlers;
}
},
{
id: "jQuery live events",
hasListeners: function (node) {
return jQueryLiveGetListeners(node, true);
},
getListeners: function (node) {
return jQueryLiveGetListeners(node, false);
},
normalizeHandler: function (handlerDO) {
function isFunctionInProxy(funcDO) {
// If the anonymous function is inside the |proxy| function and the
// function only has guessed atom, the guessed atom should starts with
// "proxy/".
let displayName = funcDO.displayName;
if (displayName && displayName.startsWith("proxy/")) {
return true;
}
// If the anonymous function is inside the |proxy| function and the
// function gets name at compile time by SetFunctionName, its guessed
// atom doesn't contain "proxy/". In that case, check if the caller is
// "proxy" function, as a fallback.
let calleeDO = funcDO.environment.callee;
if (!calleeDO) {
return false;
}
let calleeName = calleeDO.displayName;
return calleeName == "proxy";
}
function getFirstFunctionVariable(funcDO) {
// The handler function inside the |proxy| function should point the
// unwrapped function via environment variable.
let names = funcDO.environment.names();
for (let varName of names) {
let varDO = handlerDO.environment.getVariable(varName);
if (!varDO) {
continue;
}
if (varDO.class == "Function") {
return varDO;
}
}
return null;
}
if (!isFunctionInProxy(handlerDO)) {
return handlerDO;
}
const MAX_NESTED_HANDLER_COUNT = 2;
for (let i = 0; i < MAX_NESTED_HANDLER_COUNT; i++) {
let funcDO = getFirstFunctionVariable(handlerDO);
if (!funcDO)
return handlerDO;
handlerDO = funcDO;
if (isFunctionInProxy(handlerDO)) {
continue;
}
break;
}
return handlerDO;
}
},
{
id: "DOM events",
hasListeners: function (node) {
let listeners;
if (node.nodeName.toLowerCase() === "html") {
listeners = eventListenerService.getListenerInfoFor(node.ownerGlobal) || [];
} else {
listeners = eventListenerService.getListenerInfoFor(node) || [];
}
for (let listener of listeners) {
if (listener.listenerObject && listener.type) {
return true;
}
}
return false;
},
getListeners: function (node) {
let handlers = [];
let listeners = eventListenerService.getListenerInfoFor(node);
// The Node actor's getEventListenerInfo knows that when an html tag has
// been passed we need the window object so we don't need to account for
// event hoisting here as we did in hasListeners.
for (let listenerObj of listeners) {
let listener = listenerObj.listenerObject;
// If there is no JS event listener skip this.
if (!listener) {
continue;
}
let eventInfo = {
capturing: listenerObj.capturing,
type: listenerObj.type,
handler: listener
};
handlers.push(eventInfo);
}
return handlers;
}
}
];
function jQueryLiveGetListeners(node, boolOnEventFound) {
let global = node.ownerGlobal.wrappedJSObject;
let hasJQuery = global.jQuery && global.jQuery.fn && global.jQuery.fn.jquery;
if (!hasJQuery) {
return;
}
let jQuery = global.jQuery;
let handlers = [];
let data = jQuery._data || jQuery.data;
if (data) {
// Live events are added to the document and bubble up to all elements.
// Any element matching the specified selector will trigger the live
// event.
let events = data(global.document, "events");
for (let type in events) {
let eventHolder = events[type];
for (let idx in eventHolder) {
if (typeof idx !== "string" || isNaN(parseInt(idx, 10))) {
continue;
}
let event = eventHolder[idx];
let selector = event.selector;
if (!selector && event.data) {
selector = event.data.selector || event.data || event.selector;
}
if (!selector || !node.ownerDocument) {
continue;
}
let matches;
try {
matches = node.matches && node.matches(selector);
} catch (e) {
// Invalid selector, do nothing.
}
if (boolOnEventFound && matches) {
return true;
}
if (!matches) {
continue;
}
if (!boolOnEventFound && (typeof event === "object" || typeof event === "function")) {
let eventInfo = {
type: event.origType || event.type.substr(selector.length + 1),
handler: event.handler || event,
tags: "jQuery,Live",
hide: {
dom0: true,
capturing: true
}
};
if (!eventInfo.type && event.data && event.data.live) {
eventInfo.type = event.data.live;
}
handlers.push(eventInfo);
}
}
}
}
if (boolOnEventFound) {
return false;
}
return handlers;
}
this.EventParsers = function EventParsers() {
if (this._eventParsers.size === 0) {
for (let parserObj of parsers) {
this.registerEventParser(parserObj);
}
}
};
exports.EventParsers = EventParsers;
EventParsers.prototype = {
_eventParsers: new Map(), // NOTE: This is shared amongst all instances.
get parsers() {
return this._eventParsers;
},
/**
* Register a new event parser to be used in the processing of event info.
*
* @param {Object} parserObj
* Each parser must contain the following properties:
* - parser, which must take the following form:
* {
* id {String}: "jQuery events", // Unique id.
* getListeners: function(node) { }, // Function that takes a node and
* // returns an array of eventInfo
* // objects (see below).
*
* hasListeners: function(node) { }, // Optional function that takes a
* // node and returns a boolean
* // indicating whether a node has
* // listeners attached.
*
* normalizeHandler: function(fnDO) { }, // Optional function that takes a
* // Debugger.Object instance and
* // climbs the scope chain to get
* // the function that should be
* // displayed in the event bubble
* // see the following url for
* // details:
* // https://developer.mozilla.org/
* // docs/Tools/Debugger-API/
* // Debugger.Object
* }
*
* An eventInfo object should take the following form:
* {
* type {String}: "click",
* handler {Function}: event handler,
* tags {String}: "jQuery,Live", // These tags will be displayed as
* // attributes in the events popup.
* hide: { // Hide or show fields:
* debugger: false, // Debugger icon
* type: false, // Event type e.g. click
* filename: false, // Filename
* capturing: false, // Capturing
* dom0: false // DOM 0
* },
*
* override: { // The following can be overridden:
* type: "click",
* origin: "http://www.mozilla.com",
* searchString: 'onclick="doSomething()"',
* DOM0: true,
* capturing: true
* }
* }
*/
registerEventParser: function (parserObj) {
let parserId = parserObj.id;
if (!parserId) {
throw new Error("Cannot register new event parser with id " + parserId);
}
if (this._eventParsers.has(parserId)) {
throw new Error("Duplicate event parser id " + parserId);
}
this._eventParsers.set(parserId, {
getListeners: parserObj.getListeners,
hasListeners: parserObj.hasListeners,
normalizeHandler: parserObj.normalizeHandler
});
},
/**
* Removes parser that matches a given parserId.
*
* @param {String} parserId
* id of the event parser to unregister.
*/
unregisterEventParser: function (parserId) {
this._eventParsers.delete(parserId);
},
/**
* Tidy up parsers.
*/
destroy: function () {
for (let [id] of this._eventParsers) {
this.unregisterEventParser(id, true);
}
}
};
|