-
Notifications
You must be signed in to change notification settings - Fork 660
Expand file tree
/
Copy pathweb.js
More file actions
335 lines (260 loc) · 7.25 KB
/
web.js
File metadata and controls
335 lines (260 loc) · 7.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
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
// Event state
const BUBBLES = 0x1
const CANCELABLE = 0x2
const COMPOSED = 0x4
const CANCELED = 0x8
const DISPATCH = 0x10
const STOP = 0x20
// EventTarget state
const CAPTURE = 0x1
const PASSIVE = 0x2
const ONCE = 0x4
// https://dom.spec.whatwg.org/#event
class Event {
// https://dom.spec.whatwg.org/#dom-event-event
constructor(type, options = {}) {
const { bubbles = false, cancelable = false, composed = false } = options
this._type = type
this._target = null
this._state = 0
if (bubbles) this._state |= BUBBLES
if (cancelable) this._state |= CANCELABLE
if (composed) this._state |= COMPOSED
}
// https://dom.spec.whatwg.org/#dom-event-type
get type() {
return this._type
}
// https://dom.spec.whatwg.org/#dom-event-target
get target() {
return this._target
}
// https://dom.spec.whatwg.org/#dom-event-currenttarget
get currentTarget() {
return null
}
// https://dom.spec.whatwg.org/#dom-event-bubbles
get bubbles() {
return (this._state & BUBBLES) !== 0
}
// https://dom.spec.whatwg.org/#dom-event-cancelable
get cancelable() {
return (this._state & CANCELABLE) !== 0
}
// https://dom.spec.whatwg.org/#dom-event-composed
get composed() {
return (this._state & COMPOSED) !== 0
}
// https://dom.spec.whatwg.org/#dom-event-defaultprevented
get defaultPrevented() {
return (this._state & CANCELED) !== 0
}
// https://dom.spec.whatwg.org/#dom-event-istrusted
get isTrusted() {
return false
}
// https://dom.spec.whatwg.org/#dom-event-preventdefault
preventDefault() {
if (this._state & CANCELABLE) this._state |= CANCELED
}
// https://dom.spec.whatwg.org/#dom-event-stoppropagation
stopPropagation() {}
// https://dom.spec.whatwg.org/#dom-event-stopimmediatepropagation
stopImmediatePropagation() {
this._state |= STOP
}
toJSON() {
return {
type: this.type,
target: this.target,
bubbles: this.bubbles,
cancelable: this.cancelable,
composed: this.composed,
defaultPrevented: this.defaultPrevented,
isTrusted: this.isTrusted
}
}
[Symbol.for('bare.inspect')]() {
return {
__proto__: { constructor: Event },
type: this.type,
target: this.target,
bubbles: this.bubbles,
cancelable: this.cancelable,
composed: this.composed,
defaultPrevented: this.defaultPrevented,
isTrusted: this.isTrusted
}
}
}
exports.Event = Event
// https://dom.spec.whatwg.org/#customevent
exports.CustomEvent = class CustomEvent extends Event {
constructor(type, options = {}) {
super(type, options)
const { detail = null } = options
this._detail = detail
}
// https://dom.spec.whatwg.org/#dom-customevent-detail
get detail() {
return this._detail
}
}
// https://dom.spec.whatwg.org/#eventtarget
exports.EventTarget = class EventTarget {
// https://dom.spec.whatwg.org/#dom-eventtarget-eventtarget
constructor() {
this._listeners = new Map()
}
// https://dom.spec.whatwg.org/#dom-eventtarget-addeventlistener
addEventListener(type, callback = null, options = {}) {
if (typeof options === 'boolean') options = { capture: options }
const { capture = false, passive = false, once = false, signal = null } = options
if (signal !== null && signal.aborted) return
if (callback === null) return
const listener = new EventListener(type, callback, capture, passive, once, signal)
const listeners = this._listeners.get(type)
if (listeners === undefined) this._listeners.set(type, listener)
else {
for (const existing of listeners) {
if (callback === existing.callback && capture === existing.capture) {
return // Duplicate listener
}
}
listener.link(listeners)
if (signal !== null) {
signal.addEventListener('abort', onabort)
function onabort() {
listener.unlink()
}
}
}
}
// https://dom.spec.whatwg.org/#dom-eventtarget-removeeventlistener
removeEventListener(type, callback = null, options = {}) {
if (typeof options === 'boolean') options = { capture: options }
const { capture = false } = options
const listeners = this._listeners.get(type)
if (listeners === undefined) return
for (const existing of listeners) {
if (callback === existing.callback && capture === existing.capture) {
const next = existing.unlink()
if (listeners === existing) this._listeners.set(type, next)
return
}
}
}
// https://dom.spec.whatwg.org/#dom-eventtarget-dispatchevent
dispatchEvent(event) {
event._target = this
event._state |= DISPATCH
const listeners = this._listeners.get(event.type)
try {
if (listeners === undefined) return true
for (const listener of listeners) {
// https://dom.spec.whatwg.org/#concept-event-listener-inner-invoke
if (listener.once) listener.unlink()
let callback = listener.callback
let context = this
if (typeof callback === 'object') {
context = callback
callback = callback.handleEvent
}
Reflect.apply(callback, context, [event])
if (event._state & STOP) break
}
return (event._state & CANCELED) === 0
} finally {
event._state &= ~DISPATCH
event._state &= ~STOP
}
}
toJSON() {
return {}
}
[Symbol.for('bare.inspect')]() {
return {
__proto__: { constructor: EventTarget }
}
}
}
// https://dom.spec.whatwg.org/#concept-event-listener
class EventListener {
constructor(type, callback, capture, passive, once, signal) {
this._type = type
this._callback = callback
this._signal = signal
this._state = 0
if (capture) this._state |= CAPTURE
if (passive) this._state |= PASSIVE
if (once) this._state |= ONCE
this._previous = this
this._next = this
}
get type() {
return this._type
}
get callback() {
return this._callback
}
get capture() {
return (this._state & CAPTURE) !== 0
}
get passive() {
return (this._state & PASSIVE) !== 0
}
get once() {
return (this._state & ONCE) !== 0
}
get removed() {
return this._previous === this && this._next === this
}
link(listener) {
const next = this._next
const previous = listener._previous
this._next = listener
listener._previous = this
previous._next = next
next._previous = previous
return listener
}
unlink() {
if (this.removed) return this
const next = this._next
const previous = this._previous
this._next = this
this._previous = this
previous._next = next
next._previous = previous
return next
}
*[Symbol.iterator]() {
let current = this
while (true) {
const next = current._next
yield current
if (next === this) break
current = next
}
}
toJSON() {
return {
type: this.type,
capture: this.capture,
passive: this.passive,
once: this.once,
removed: this.removed
}
}
[Symbol.for('bare.inspect')]() {
return {
__proto__: { constructor: EventListener },
type: this.type,
callback: this.callback,
capture: this.capture,
passive: this.passive,
once: this.once,
removed: this.removed
}
}
}