Class: Wedge::DOM
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Methods
#client?, included, #server?
Constructor Details
#initialize(html) ⇒ DOM
Returns a new instance of DOM.
16
17
18
19
20
21
22
23
24
25
26
27
|
# File 'lib/wedge/dom.rb', line 16
def initialize html
html = '' if html.nil?
html = html.to_html if html.is_a? HTML::DSL
@raw_html = html
if server?
@dom = raw_html.is_a?(String) ? HTML[raw_html.dup] : raw_html
else
@dom = raw_html.is_a?(String) ? Element[raw_html.dup] : raw_html
end
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
This allows you to use all the nokogiri or opal jquery methods if a global one isn’t set
133
134
135
136
137
138
139
140
|
# File 'lib/wedge/dom.rb', line 133
def method_missing method, *args, &block
if dom.respond_to? method, true
dom.send method, *args, &block
else
super
end
end
|
Instance Attribute Details
Returns the value of attribute dom.
5
6
7
|
# File 'lib/wedge/dom.rb', line 5
def dom
@dom
end
|
Returns the value of attribute raw_html.
5
6
7
|
# File 'lib/wedge/dom.rb', line 5
def raw_html
@raw_html
end
|
Class Method Details
Shortcut for creating dom
11
12
13
|
# File 'lib/wedge/dom.rb', line 11
def [] html
new html
end
|
Instance Method Details
#add_class(classes) ⇒ Object
66
67
68
69
70
|
# File 'lib/wedge/dom.rb', line 66
def add_class classes
classes = (classes || '').split ' ' unless classes.is_a? Array
new_classes = ((node.attr('class') || '').split(' ') << classes).uniq.join(' ')
node['class'] = new_classes
end
|
#attr(key, value = false) ⇒ Object
77
78
79
80
81
82
83
84
|
# File 'lib/wedge/dom.rb', line 77
def attr key, value = false
if value
value = value.join ' ' if value.is_a? Array
node[key] = value
else
super key
end
end
|
#data(key = false, value = false) ⇒ Object
50
51
52
53
54
55
56
57
58
59
60
|
# File 'lib/wedge/dom.rb', line 50
def data key = false, value = false
d = Hash[node.xpath("@*[starts-with(name(), 'data-')]").map{|a| [a.name, a.value]}]
if !key
d
elsif key && !value
d[key]
else
node["data-#{key}"] = value
end
end
|
#find(string, &block) ⇒ Object
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
# File 'lib/wedge/dom.rb', line 29
def find string, &block
if client?
node = Wedge::DOM.new dom.find(string)
elsif server?
if block_given?
node = Wedge::DOM.new dom.css(string)
else
node = Wedge::DOM.new dom.at(string)
end
end
if block_given?
node.each_with_index do |n, i|
block.call Wedge::DOM.new(n), i
end
end
node
end
|
#html(content = false) ⇒ Object
115
116
117
118
119
120
121
122
123
124
125
|
# File 'lib/wedge/dom.rb', line 115
def html content = false
if !content
if server?
node.inner_html
else
node ? node.html : dom.html
end
else
self.html = content
end
end
|
#html=(content) ⇒ Object
87
88
89
90
91
92
93
94
95
96
|
# File 'lib/wedge/dom.rb', line 87
def html= content
if server?
node.inner_html = content
else
content = content.dom if content.is_a? Wedge::DOM
node.html content
end
node
end
|
127
128
129
|
# File 'lib/wedge/dom.rb', line 127
def node
@node || dom
end
|
#remove_class(classes) ⇒ Object
72
73
74
75
|
# File 'lib/wedge/dom.rb', line 72
def remove_class classes
classes = (classes || '').split ' ' unless classes.is_a? Array
(node.attr('class') || '').split(' ').reject { |n| n =~ /active|asc|desc/i }.join(' ')
end
|
108
109
110
111
112
|
# File 'lib/wedge/dom.rb', line 108
def to_html
@dom ||= Wedge::DOM.new '<div>'
el = dom.first
Wedge::DOM.new('<div>').append(el).html
end
|
#val(value) ⇒ Object
62
63
64
|
# File 'lib/wedge/dom.rb', line 62
def val value
node.content = value
end
|