/
element.rb
296 lines (221 loc) · 5.05 KB
/
element.rb
1
2
3
4
5
6
7
8
9
10
11
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
require 'browser/dom/element/position'
require 'browser/dom/element/offset'
require 'browser/dom/element/scroll'
require 'browser/dom/element/input'
module Browser; module DOM
class Element < Node
def self.create(*args)
$document.create_element(*args)
end
def self.new(node)
if self == Element
case `node.nodeName`.downcase
when :input
Input.new(node)
else super
end
else
super
end
end
include Event::Target
target {|value|
DOM(value) rescue nil
}
alias_native :id
def add_class(*names)
`#@native.className = #{(class_names + names).uniq.join ' '}`
self
end
def remove_class(*names)
`#@native.className = #{(class_names - names).join ' '}`
self
end
alias_native :class_name, :className
def class_names
`#@native.className`.split(/\s+/).reject(&:empty?)
end
alias attribute attr
def attribute_nodes
Native::Array.new(`#@native.attributes`, get: :item) { |e| DOM(e) }
end
def attributes(options = {})
Attributes.new(self, options)
end
def get(name, options = {})
if namespace = options[:namespace]
`#@native.getAttributeNS(#{namespace.to_s}, #{name.to_s}) || nil`
else
`#@native.getAttribute(#{name.to_s}) || nil`
end
end
def set(name, value, options = {})
if namespace = options[:namespace]
`#@native.setAttributeNS(#{namespace.to_s}, #{name.to_s}, #{value})`
else
`#@native.setAttribute(#{name.to_s}, #{value.to_s})`
end
end
alias [] get
alias []= set
alias attr get
alias attribute get
alias get_attribute get
alias set_attribute set
def key?(name)
!!self[name]
end
def keys
attributes_nodesmap(&:name)
end
def values
attribute_nodes.map(&:value)
end
def each(options = {}, &block)
return enum_for :each, options unless block
attributes(options).each(&block)
end
def remove_attribute(name)
`#@native.removeAttribute(name)`
end
def size(*inc)
Size.new(`#@native.offsetWidth`, `#@native.offsetHeight`)
end
def position
Position.new(self)
end
def offset(*values)
off = Offset.new(self)
unless values.empty?
off.set(*values)
end
off
end
def offset=(value)
offset.set(*value)
end
def scroll
Scroll.new(self)
end
def inner_dom(&block)
# FIXME: when block passing is fixed
doc = document
clear; Builder.new(doc, self, &block)
self
end
def inner_dom=(node)
clear; self << node
end
def /(*paths)
paths.map { |path| xpath(path) }.flatten.uniq
end
def at(path)
xpath(path).first || css(path).first
end
def at_css(*rules)
rules.each {|rule|
found = css(rule).first
return found if found
}
nil
end
def at_xpath(*paths)
paths.each {|path|
found = xpath(path).first
return found if found
}
nil
end
def search(*selectors)
NodeSet.new document, selectors.map {|selector|
xpath(selector).to_a.concat(css(selector).to_a)
}.flatten.uniq
end
def css(path)
NodeSet.new(document, Native::Array.new(`#@native.querySelectorAll(path)`))
end
def xpath(path)
result = []
begin
%x{
var tmp = (#@native.ownerDocument || #@native).evaluate(
path, #@native, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
result = #{Native::Array.new(`tmp`, get: :snapshotItem, length: :snapshotLength)};
}
rescue; end
NodeSet.new(document, result)
end
def style(data = nil, &block)
style = CSS::Declaration.new(`#@native.style`)
return style unless data || block
if data.is_a?(String)
style.replace(data)
elsif data.is_a?(Enumerable)
style.assign(data)
end
if block
style.apply(&block)
end
self
end
def style!
CSS::Declaration.new(`#{window.to_n}.getComputedStyle(#@native, null)`)
end
def data(what)
unless defined?(`#@native.$data`)
`#@native.$data = {}`
end
if Hash === what
what.each {|name, value|
`#@native.$data[name] = value`
}
else
%x{
var value = #@native.$data[what];
if (value === undefined) {
return nil;
}
else {
return value;
}
}
end
end
def matches?(selector)
`#@native.matches(#{selector})`
end
def window
document.window
end
def inspect
"#<DOM::Element: #{name}>"
end
class Attributes
include Enumerable
attr_reader :namespace
def initialize(element, options)
@element = element
@namespace = options[:namespace]
end
def each(&block)
return enum_for :each unless block_given?
@element.attribute_nodes.each {|attr|
yield attr.name, attr.value
}
self
end
def [](name)
@element.get_attribute(name, namespace: @namespace)
end
def []=(name, value)
@element.set_attribute(name, value, namespace: @namespace)
end
def merge!(hash)
hash.each {|name, value|
self[name] = value
}
self
end
end
end
end; end