Module: Ruby2JS::Filter::JQuery
- Includes:
- SEXP
- Defined in:
- lib/ruby2js/filter/jquery.rb
Instance Method Summary collapse
- #initialize(*args) ⇒ Object
-
#on_block(node) ⇒ Object
Example conversion: before: $$.post …
-
#on_gvar(node) ⇒ Object
map $$ to $.
- #on_send(node) ⇒ Object
Methods included from SEXP
Instance Method Details
#initialize(*args) ⇒ Object
70 71 72 73 |
# File 'lib/ruby2js/filter/jquery.rb', line 70 def initialize(*args) @react = nil super end |
#on_block(node) ⇒ Object
Example conversion:
before:
$$.post ... do ... end
(block (send (gvar :$$) :post ...) (args) (...))
after:
$$.post ..., proc { ... }, :json
(send (gvar :$$) :post ...
(block (send nil :proc) (args) (...)) (:sym :json))
179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/ruby2js/filter/jquery.rb', line 179 def on_block(node) call = node.children.first return super unless call.children.first == s(:gvar, :$$) return super unless call.children[1] == :post return super unless call.children.length <= 4 children = call.children.dup children << s(:str, '') if children.length <= 2 children << s(:hash) if children.length <= 3 children << s(:block, s(:send, nil, :proc), *node.children[1..-1]) children << s(:sym, :json) process call.updated nil, children end |
#on_gvar(node) ⇒ Object
map $$ to $
76 77 78 79 80 81 82 |
# File 'lib/ruby2js/filter/jquery.rb', line 76 def on_gvar(node) if node.children[0] == :$$ node.updated nil, ['$'] else super end end |
#on_send(node) ⇒ Object
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 |
# File 'lib/ruby2js/filter/jquery.rb', line 84 def on_send(node) if [:call, :[]].include? node.children[1] and node.children.first # map $$.call(..), $$.(..), and $$[...] to $(...) 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] == :to_a process S(:call, node.children[0], :toArray, *node.children[2..-1]) elsif node.children[1] == :~ and not @react # map ~expression.method to $(expression).method 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 # See http://api.jquery.com/category/properties/ props = :context, :jquery, :browser, :fx, :support, :length, :selector domprops = %w(checked disabled readonly readOnly required) stopProps = false rewrite_tilda = proc do |tnode| # Example conversion: # before: # (send (send (send (send nil :a) :b) :c) :~) # after: # (send (send (send nil "$" (send nil :a)) :b) :c) if tnode.type == :send and tnode.children[0] stopProps = true if tnode.children[1] == :[] if tnode.children[1] == :~ and tnode.children[0].children[1] == :~ # consecutive tildes if tnode.children[0].children[0].children[1] == :~ result = tnode.children[0].children[0].children[0] else result = s(:attr, tnode.children[0].children[0], :~) end s(:attr, s(:attr, process(result), :~), :~) else # possible getter/setter method = tnode.children[1] method = method.to_s.chomp('=') if method =~ /=$/ method = :each! if method == :each rewrite = [rewrite_tilda[tnode.children[0]], method, *tnode.children[2..-1]] if stopProps or props.include? tnode.children[1] rewrite[1] = tnode.children[1] tnode.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 tnode.type == :block # method call with a block parameter tnode.updated nil, [rewrite_tilda[tnode.children[0]], *tnode.children[1..-1]] elsif tnode.type == :array # innermost expression is an array s(:send, nil, '$', *tnode) else # innermost expression is a scalar s(:send, nil, '$', tnode) end end process rewrite_tilda[node].children[0] else super end end |