12
12
# JS.global[:document].write("Hello, world!")
13
13
# div = JS.global[:document].createElement("div")
14
14
# div[:innerText] = "click me"
15
- # JS.global[:document][:body].appendChild(div)
15
+ # body = JS.global[:document][:body]
16
+ # if body[:classList].contains?("main")
17
+ # body.appendChild(div)
18
+ # end
16
19
# div.addEventListener("click") do |event|
17
20
# puts event # => # [object MouseEvent]
18
21
# puts event[:detail] # => 1
30
33
# JS.global[:document].call(:write, "Hello, world!")
31
34
# div = JS.global[:document].call(:createElement, "div")
32
35
# div[:innerText] = "click me"
33
- # JS.global[:document][:body].call(:appendChild, div)
36
+ # if body[:classList].call(:contains, "main") == JS::True
37
+ # body.appendChild(div)
38
+ # end
34
39
# div.call(:addEventListener, "click") do |event|
35
40
# puts event # => # [object MouseEvent]
36
41
# puts event[:detail] # => 1
@@ -133,7 +138,12 @@ def new(*args)
133
138
end
134
139
135
140
def method_missing ( sym , *args , &block )
136
- if self [ sym ] . typeof == "function"
141
+ sym_str = sym . to_s
142
+ if sym_str . end_with? ( "?" )
143
+ # When a JS method is called with a ? suffix, it is treated as a predicate method,
144
+ # and the return value is converted to a Ruby boolean value automatically.
145
+ self . call ( sym_str [ 0 ..-2 ] . to_sym , *args , &block ) == JS ::True
146
+ elsif self [ sym ] . typeof == "function"
137
147
self . call ( sym , *args , &block )
138
148
else
139
149
super
@@ -142,6 +152,8 @@ def method_missing(sym, *args, &block)
142
152
143
153
def respond_to_missing? ( sym , include_private )
144
154
return true if super
155
+ sym_str = sym . to_s
156
+ sym = sym_str [ 0 ..-2 ] . to_sym if sym_str . end_with? ( "?" )
145
157
self [ sym ] . typeof == "function"
146
158
end
147
159
0 commit comments