Module: Ruby2JS::Filter::Nokogiri

Extended by:
SEXP
Includes:
SEXP
Defined in:
lib/ruby2js/filter/nokogiri.rb

Constant Summary collapse

IMPORT_JSDOM =
s(:import, ["jsdom"], [s(:attr, nil, :JSDOM)])

Instance Method Summary collapse

Methods included from SEXP

S, s

Instance Method Details

#on_send(node) ⇒ Object



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
# File 'lib/ruby2js/filter/nokogiri.rb', line 12

def on_send(node)
  target, method, *args = node.children
  return super if excluded?(method)

  if target == nil
    if \
      method == :require and args.length == 1 and 
      args.first.type == :str and 
      %w(nokogiri nokogumbo).include? args.first.children.first
    then
      s(:begin)

    else
      super
    end

  elsif \
    [:HTML, :HTML5].include? method and
    target == s(:const, nil, :Nokogiri)
  then
    prepend_list << IMPORT_JSDOM
    S(:attr, s(:attr, s(:send, s(:const, nil, :JSDOM), :new,
      *process_all(args)), :window), :document)

  elsif \
    method == :parse and
    target.type == :const and
    target.children.first == s(:const, nil, :Nokogiri) and
    [:HTML, :HTML5].include? target.children.last
  then
    prepend_list << IMPORT_JSDOM
    S(:attr, s(:attr, s(:send, s(:const, nil, :JSDOM), :new,
      *process_all(args)), :window), :document)

  elsif \
    method == :at and 
    args.length == 1 and args.first.type == :str
  then
    S(:send, process(target), :querySelector, process(args.first))

  elsif \
    method == :search and 
    args.length == 1 and args.first.type == :str
  then
    S(:send, process(target), :querySelectorAll, process(args.first))

  elsif method === :parent and args.length == 0
    S(:attr, process(target), :parentNode)

  elsif method === :name and args.length == 0
    S(:attr, process(target), :nodeName)

  elsif [:text, :content].include? method and args.length == 0
    S(:attr, process(target), :textContent)

  elsif method == :content= and args.length == 1
    S(:send, process(target), :textContent=, *process_all(args))

  elsif method === :inner_html and args.length == 0
    S(:attr, process(target), :innerHTML)

  elsif method == :inner_html= and args.length == 1
    S(:send, process(target), :innerHTML=, *process_all(args))

  elsif method === :to_html and args.length == 0
    S(:attr, process(target), :outerHTML)

  elsif \
    [:attr, :get_attribute].include? method and 
    args.length == 1 and args.first.type == :str
  then
    S(:send, process(target), :getAttribute, process(args.first))

  elsif \
    [:key?, :has_attribute].include? method and 
    args.length == 1 and args.first.type == :str
  then
    S(:send, target, :hasAttribute, process(args.first))

  elsif \
    method == :set_attribute and 
    args.length == 2 and args.first.type == :str
  then
    S(:send, target, :setAttribute, *process_all(args))

  elsif \
    method == :attribute and 
    args.length == 1 and args.first.type == :str
  then
    S(:send, target, :getAttributeNode, *process_all(args))

  elsif method == :remove_attribute and args.length == 1
    S(:send, target, :removeAttribute, process(args.first))

  elsif method == :attribute_nodes and args.length == 0
    S(:attr, target, :attributes)

  elsif \
    method == :new and args.length == 2 and
    target == s(:const, s(:const, s(:const, nil, :Nokogiri), :XML), :Node)
  then
    S(:send, process(args.last), :createElement, process(args.first))

  elsif method == :create_element
    create = S(:send, target, :createElement, process(args.first))
    if args.length == 1
      create
    elsif true
      init = []
      args[1..-1].each do |arg|
        if arg.type == :hash
          init += arg.children.map do |pair|
            s(:send, s(:gvar, :$_), :setAttribute,
              *process_all(pair.children))
          end
        elsif arg.type == :str
          init << s(:send, s(:gvar, :$_), :textContent=, process(arg))
        else
          return super
        end
      end

      S(:send, s(:block, s(:send, nil, :proc), s(:args),
        s(:begin, s(:gvasgn, :$_, create), *init,
        s(:return, s(:gvar, :$_)))), :[])
    else
      super
    end

  elsif method == :create_text and args.length == 1
    create = S(:send, target, :createTextNode, process(args.first))

  elsif method == :create_comment and args.length == 1
    create = S(:send, target, :createComment, process(args.first))

  elsif method == :create_cdata and args.length == 1
    create = S(:send, target, :createCDATASection, process(args.first))

  elsif method == :add_child and args.length == 1
    S(:send, target, :appendChild, process(args.first))

  elsif \
    [:add_next_sibling, :next=, :after].include? method and
    args.length == 1
  then
    S(:send, s(:attr, process(target), :parentNode), :insertBefore,
      process(args.first), s(:attr, target, :nextSibling))

  elsif \
    [:add_previous_sibling, :previous=, :before].include? method and
    args.length == 1
  then
    S(:send, s(:attr, process(target), :parentNode), :insertBefore,
      process(args.first), target)

  elsif method == :prepend_child and args.length == 1
    S(:send, target, :insertBefore,
      process(args.first), s(:attr, target, :firstChild))

  elsif method == :next_element and args.length == 0
    S(:attr, target, :nextElement)

  elsif [:next, :next_sibling].include? method and args.length == 0
    S(:attr, target, :nextSibling)

  elsif method == :previous_element and args.length == 0
    S(:attr, target, :previousElement)

  elsif \
    [:previous, :previous_sibling].include? method and args.length == 0
  then
    S(:attr, target, :previousSibling)

  elsif method == :cdata? and args.length == 0
    S(:send, s(:attr, target, :nodeType), :===,
      s(:attr, s(:const, nil, :Node), :CDATA_SECTION_NODE))

  elsif method == :comment? and args.length == 0
    S(:send, s(:attr, target, :nodeType), :===,
      s(:attr, s(:const, nil, :Node), :COMMENT_NODE))

  elsif method == :element? and args.length == 0
    S(:send, s(:attr, target, :nodeType), :===,
      s(:attr, s(:const, nil, :Node), :ELEMENT_NODE))

  elsif method == :fragment? and args.length == 0
    S(:send, s(:attr, target, :nodeType), :===,
      s(:attr, s(:const, nil, :Node), :DOCUMENT_FRAGMENT_NODE))

  elsif method == :processing_instruction? and args.length == 0
    S(:send, s(:attr, target, :nodeType), :===,
      s(:attr, s(:const, nil, :Node), :PROCESSING_INSTRUCTION_NODE))

  elsif method == :text? and args.length == 0
    S(:send, s(:attr, target, :nodeType), :===,
      s(:attr, s(:const, nil, :Node), :TEXT_NODE))

  elsif method == :children and args.length == 0
    S(:attr, target, :childNodes)

  elsif method == :first_element_child and args.length == 0
    S(:attr, target, :firstElementChild)

  elsif method == :last_element_child and args.length == 0
    S(:attr, target, :lastElementChild)

  elsif method == :replace and args.length == 1
    S(:send, target, :replaceWith, process(args.first))

  elsif [:remove, :unlink].include? method and args.length == 0
    S(:send, target, :remove)

  elsif method == :root and args.length == 0
    S(:attr, target, :documentElement)

  elsif method == :document and args.length == 0
    S(:attr, target, :ownerDocument)

  else
    return super

  end
end