Class: Dommy::Event

Inherits:
Object
  • Object
show all
Defined in:
lib/dommy/event.rb

Direct Known Subclasses

CustomEvent, KeyboardEvent, MouseEvent

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, init = nil) ⇒ Event

Returns a new instance of Event.



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/dommy/event.rb', line 200

def initialize(type, init = nil)
  @type = type.to_s
  @bubbles = !!read_init(init, "bubbles")
  @cancelable = !!read_init(init, "cancelable")
  @composed = !!read_init(init, "composed")
  @default_prevented = false
  @propagation_stopped = false
  @immediate_propagation_stopped = false
  @target = nil
  @current_target = nil
  @composed_path = []
  # `timeStamp` is the high-resolution timestamp at construction
  # in ms (browser uses performance.now). We use monotonic time
  # for determinism across spec runs.
  @time_stamp = (Process.clock_gettime(Process::CLOCK_MONOTONIC) * 1000.0)
end

Instance Attribute Details

#typeObject (readonly)

Returns the value of attribute type.



217
218
219
# File 'lib/dommy/event.rb', line 217

def type
  @type
end

Instance Method Details

#__js_call__(method, args) ⇒ Object



279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/dommy/event.rb', line 279

def __js_call__(method, args)
  case method
  when "preventDefault"
    @default_prevented = true if @cancelable
    nil
  when "stopPropagation"
    @propagation_stopped = true
    nil
  when "stopImmediatePropagation"
    @propagation_stopped = true
    @immediate_propagation_stopped = true
    nil
  when "composedPath"
    @composed_path.dup
  when "initEvent"
    init_event(args[0], args[1], args[2])
  end
end

#__js_get__(key) ⇒ Object



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/dommy/event.rb', line 243

def __js_get__(key)
  case key
  when "type"
    @type
  when "bubbles"
    @bubbles
  when "cancelable"
    @cancelable
  when "composed"
    @composed
  when "defaultPrevented"
    @default_prevented
  when "target"
    @target
  when "currentTarget"
    @current_target
  when "timeStamp"
    @time_stamp
  when "cancelBubble"
    @propagation_stopped
  when "eventPhase"
    event_phase
  end
end

#__js_set__(key, value) ⇒ Object



268
269
270
271
272
273
274
275
276
277
# File 'lib/dommy/event.rb', line 268

def __js_set__(key, value)
  case key
  when "cancelBubble"
    # Setting to truthy stops propagation; spec quirk that
    # `cancelBubble = false` does NOT un-stop (browser observation).
    @propagation_stopped = true if value
  end

  nil
end

#__prepare_for_dispatch__(target) ⇒ Object



235
236
237
# File 'lib/dommy/event.rb', line 235

def __prepare_for_dispatch__(target)
  @target ||= target
end

#__record_path__(targets) ⇒ Object

Filled in by EventTarget#dispatch_event as the event walks the bubble path so ‘composedPath()` returns the right list.

Per spec, ‘load` events do not propagate to the Window when composed paths are computed (resource-finished signal stays at the target).



317
318
319
320
321
322
323
# File 'lib/dommy/event.rb', line 317

def __record_path__(targets)
  @composed_path = if @type == "load"
    targets.reject { |t| t.is_a?(Window) }
  else
    targets
  end
end

#__set_current_target__(target) ⇒ Object



239
240
241
# File 'lib/dommy/event.rb', line 239

def __set_current_target__(target)
  @current_target = target
end

#bubbles?Boolean

Returns:

  • (Boolean)


219
220
221
# File 'lib/dommy/event.rb', line 219

def bubbles?
  @bubbles
end

#default_prevented?Boolean

Returns:

  • (Boolean)


223
224
225
# File 'lib/dommy/event.rb', line 223

def default_prevented?
  @default_prevented
end

#immediate_propagation_stopped?Boolean

Returns:

  • (Boolean)


231
232
233
# File 'lib/dommy/event.rb', line 231

def immediate_propagation_stopped?
  @immediate_propagation_stopped
end

#init_event(type, bubbles = false, cancelable = false) ⇒ Object

Deprecated ‘Event#initEvent(type, bubbles, cancelable)` — older browsers used `document.createEvent(“Event”).initEvent(…)`. Resets internal flags as a side effect.



301
302
303
304
305
306
307
308
309
# File 'lib/dommy/event.rb', line 301

def init_event(type, bubbles = false, cancelable = false)
  @type = type.to_s
  @bubbles = !!bubbles
  @cancelable = !!cancelable
  @default_prevented = false
  @propagation_stopped = false
  @immediate_propagation_stopped = false
  nil
end

#propagation_stopped?Boolean

Returns:

  • (Boolean)


227
228
229
# File 'lib/dommy/event.rb', line 227

def propagation_stopped?
  @propagation_stopped
end