Class: Moxml::Element
- Inherits:
-
Node
- Object
- Node
- Moxml::Element
show all
- Defined in:
- lib/moxml/element.rb
Instance Attribute Summary
Attributes inherited from Node
#native
Instance Method Summary
collapse
Methods inherited from Node
#add_next_sibling, #add_previous_sibling, #children, #column, #inner_html, #inner_html=, #line, #next_sibling, #outer_html, #parent, #path, #previous_sibling, #remove, #replace, #text, #text=, wrap
Constructor Details
#initialize(name_or_native = nil) ⇒ Element
Returns a new instance of Element.
3
4
5
6
7
8
9
10
|
# File 'lib/moxml/element.rb', line 3
def initialize(name_or_native = nil)
case name_or_native
when String
super(adapter.create_element(nil, name_or_native))
else
super(name_or_native)
end
end
|
Instance Method Details
#[](name) ⇒ Object
24
25
26
27
|
# File 'lib/moxml/element.rb', line 24
def [](name)
attr = adapter.get_attribute(native, name)
attr.nil? ? nil : Attribute.new(attr)
end
|
#[]=(name, value) ⇒ Object
20
21
22
|
# File 'lib/moxml/element.rb', line 20
def []=(name, value)
adapter.set_attribute(native, name, value)
end
|
#add_child(node) ⇒ Object
29
30
31
32
|
# File 'lib/moxml/element.rb', line 29
def add_child(node)
adapter.add_child(native, node.native)
self
end
|
#add_class(*names) ⇒ Object
117
118
119
120
|
# File 'lib/moxml/element.rb', line 117
def add_class(*names)
self["class"] = (classes + names).uniq.join(" ")
self
end
|
#ancestors ⇒ Object
82
83
84
|
# File 'lib/moxml/element.rb', line 82
def ancestors
NodeSet.new(adapter.ancestors(native))
end
|
#at_css(selector) ⇒ Object
56
57
58
59
|
# File 'lib/moxml/element.rb', line 56
def at_css(selector)
node = adapter.at_css(native, selector)
node.nil? ? nil : wrap_node(node)
end
|
#at_xpath(expression, namespaces = {}) ⇒ Object
61
62
63
64
|
# File 'lib/moxml/element.rb', line 61
def at_xpath(expression, namespaces = {})
node = adapter.at_xpath(native, expression, namespaces)
node.nil? ? nil : wrap_node(node)
end
|
#attributes ⇒ Object
16
17
18
|
# File 'lib/moxml/element.rb', line 16
def attributes
adapter.attributes(native).transform_values { |attr| Attribute.new(attr) }
end
|
#blank? ⇒ Boolean
66
67
68
|
# File 'lib/moxml/element.rb', line 66
def blank?
text.strip.empty? && children.empty?
end
|
#classes ⇒ Object
113
114
115
|
# File 'lib/moxml/element.rb', line 113
def classes
(self["class"] || "").split(/\s+/)
end
|
#css(selector) ⇒ Object
48
49
50
|
# File 'lib/moxml/element.rb', line 48
def css(selector)
NodeSet.new(adapter.css(native, selector))
end
|
#descendants ⇒ Object
86
87
88
|
# File 'lib/moxml/element.rb', line 86
def descendants
NodeSet.new(adapter.descendants(native))
end
|
#has_class?(name) ⇒ Boolean
135
136
137
|
# File 'lib/moxml/element.rb', line 135
def has_class?(name)
classes.include?(name)
end
|
#inner_text ⇒ Object
98
99
100
|
# File 'lib/moxml/element.rb', line 98
def inner_text
adapter.inner_text(native)
end
|
#inner_text=(text) ⇒ Object
102
103
104
105
|
# File 'lib/moxml/element.rb', line 102
def inner_text=(text)
adapter.set_inner_text(native, text)
self
end
|
#key?(name) ⇒ Boolean
Also known as:
has_attribute?
107
108
109
|
# File 'lib/moxml/element.rb', line 107
def key?(name)
adapter.has_attribute?(native, name)
end
|
#matches?(selector) ⇒ Boolean
78
79
80
|
# File 'lib/moxml/element.rb', line 78
def matches?(selector)
adapter.matches?(native, selector)
end
|
#name ⇒ Object
12
13
14
|
# File 'lib/moxml/element.rb', line 12
def name
adapter.node_name(native)
end
|
#namespace ⇒ Object
34
35
36
37
|
# File 'lib/moxml/element.rb', line 34
def namespace
ns = adapter.namespace(native)
ns.nil? ? nil : Namespace.new(ns)
end
|
#namespace=(ns) ⇒ Object
39
40
41
42
|
# File 'lib/moxml/element.rb', line 39
def namespace=(ns)
adapter.set_namespace(native, ns&.native)
self
end
|
#namespaces ⇒ Object
44
45
46
|
# File 'lib/moxml/element.rb', line 44
def namespaces
adapter.namespaces(native).transform_values { |ns| Namespace.new(ns) }
end
|
#next_elements ⇒ Object
94
95
96
|
# File 'lib/moxml/element.rb', line 94
def next_elements
NodeSet.new(adapter.next_elements(native))
end
|
#previous_elements ⇒ Object
90
91
92
|
# File 'lib/moxml/element.rb', line 90
def previous_elements
NodeSet.new(adapter.previous_elements(native))
end
|
#remove_class(*names) ⇒ Object
122
123
124
125
|
# File 'lib/moxml/element.rb', line 122
def remove_class(*names)
self["class"] = (classes - names).join(" ")
self
end
|
#toggle_class(name) ⇒ Object
127
128
129
130
131
132
133
|
# File 'lib/moxml/element.rb', line 127
def toggle_class(name)
if classes.include?(name)
remove_class(name)
else
add_class(name)
end
end
|
#value ⇒ Object
70
71
72
|
# File 'lib/moxml/element.rb', line 70
def value
text.strip
end
|
#value=(val) ⇒ Object
74
75
76
|
# File 'lib/moxml/element.rb', line 74
def value=(val)
self.text = val.to_s
end
|
#xpath(expression, namespaces = {}) ⇒ Object
52
53
54
|
# File 'lib/moxml/element.rb', line 52
def xpath(expression, namespaces = {})
NodeSet.new(adapter.xpath(native, expression, namespaces))
end
|