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
|
# File 'lib/ruby2js/filter/jquery.rb', line 76
def on_send(node)
if [:call, :[]].include? node.children[1]
target = process(node.children.first)
if target.type == :gvar and target.children == ['$']
s(:send, nil, '$', *process_all(node.children[2..-1]))
else
super
end
elsif node.children[1] == :~
if node.children[0] and node.children[0].type == :op_asgn
asgn = node.children[0]
if asgn.children[0] and asgn.children[0].type == :send
inner = asgn.children[0]
return on_send s(:send, s(:send, inner.children[0],
(inner.children[1].to_s+'=').to_sym,
s(:send, s(:send, s(:send, inner.children[0], :~),
*inner.children[1..-1]), *asgn.children[1..-1])), :~)
else
return on_send asgn.updated nil, [s(:send, asgn.children[0], :~),
*asgn.children[1..-1]]
end
end
props = :context, :jquery, :browser, :fx, :support, :length, :selector
domprops = %w(checked disabled readonly readOnly required)
rewrite_tilda = proc do |node|
if node.type == :send and node.children[0]
if node.children[1] == :~ and node.children[0].children[1] == :~
if node.children[0].children[0].children[1] == :~
result = process(node.children[0].children[0].children[0])
else
result = s(:send, process(node.children[0].children[0]), :~)
end
s(:send, s(:send, result, :~), :~)
else
method = node.children[1]
method = method.to_s.chomp('=') if method =~ /=$/
rewrite = [rewrite_tilda[node.children[0]],
method, *process_all(node.children[2..-1])]
if props.include? node.children[1]
node.updated nil, rewrite
elsif domprops.include? method.to_s
method = :readOnly if method.to_s == 'readonly'
s(:send, rewrite[0], :prop, s(:sym, method), *rewrite[2..-1])
else
s(:send, *rewrite)
end
end
elsif node.type == :block
node.updated nil, [rewrite_tilda[node.children[0]],
*process_all(node.children[1..-1])]
else
s(:send, nil, '$', process(node))
end
end
rewrite_tilda[node].children[0]
else
super
end
end
|