Class: Moxml::Element

Inherits:
Node
  • Object
show all
Defined in:
lib/moxml/element.rb

Constant Summary

Constants inherited from Node

Node::TYPES

Instance Attribute Summary

Attributes inherited from Node

#context, #native

Instance Method Summary collapse

Methods inherited from Node

#==, #add_child, #add_next_sibling, #add_previous_sibling, #at_xpath, #children, #clone, #document, #each_node, #find, #first_child, #has_children?, #initialize, #last_child, #next_sibling, #parent, #previous_sibling, #remove, #replace, #to_xml, wrap, #xpath

Methods included from XmlUtils

#encode_entities, #normalize_xml_value, #validate_comment_content, #validate_declaration_encoding, #validate_declaration_standalone, #validate_declaration_version, #validate_element_name, #validate_pi_target, #validate_prefix, #validate_uri

Constructor Details

This class inherits a constructor from Moxml::Node

Instance Method Details

#[](name) ⇒ Object



41
42
43
# File 'lib/moxml/element.rb', line 41

def [](name)
  adapter.get_attribute_value(@native, name)
end

#[]=(name, value) ⇒ Object



37
38
39
# File 'lib/moxml/element.rb', line 37

def []=(name, value)
  adapter.set_attribute(@native, name, normalize_xml_value(value))
end

#add_namespace(prefix, uri) ⇒ Object Also known as: add_namespace_definition



71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/moxml/element.rb', line 71

def add_namespace(prefix, uri)
  validate_uri(uri)
  adapter.create_native_namespace(@native, prefix, uri)
  self
rescue ValidationError => e
  # Re-raise as NamespaceError, provide attributes for error context
  # but the to_s will only add details if provided
  raise Moxml::NamespaceError.new(
    e.message,
    prefix: prefix,
    uri: uri,
    element: self,
  )
end

#attribute(name) ⇒ Object



45
46
47
48
# File 'lib/moxml/element.rb', line 45

def attribute(name)
  native_attr = adapter.get_attribute(@native, name)
  native_attr && Attribute.new(native_attr, context)
end

#attributesObject



60
61
62
63
64
# File 'lib/moxml/element.rb', line 60

def attributes
  adapter.attributes(@native).map do |attr|
    Attribute.new(attr, context)
  end
end

#expanded_nameObject

Returns the expanded name including namespace prefix



17
18
19
20
21
22
23
# File 'lib/moxml/element.rb', line 17

def expanded_name
  if namespace_prefix && !namespace_prefix.empty?
    "#{namespace_prefix}:#{name}"
  else
    name
  end
end

#find_all(xpath) ⇒ Object



172
173
174
# File 'lib/moxml/element.rb', line 172

def find_all(xpath)
  xpath(xpath).to_a
end

#find_element(xpath) ⇒ Object

Convenience find methods



168
169
170
# File 'lib/moxml/element.rb', line 168

def find_element(xpath)
  at_xpath(xpath)
end

#get(attr_name) ⇒ Object

Alias for getting attribute value (used by XPath engine)



51
52
53
# File 'lib/moxml/element.rb', line 51

def get(attr_name)
  attribute(attr_name)
end

#inner_textObject



126
127
128
# File 'lib/moxml/element.rb', line 126

def inner_text
  adapter.inner_text(@native)
end

#inner_xmlObject



130
131
132
# File 'lib/moxml/element.rb', line 130

def inner_xml
  adapter.inner_xml(@native)
end

#inner_xml=(xml) ⇒ Object



134
135
136
137
# File 'lib/moxml/element.rb', line 134

def inner_xml=(xml)
  doc = context.parse("<root>#{xml}</root>")
  adapter.replace_children(@native, doc.root.children.map(&:native))
end

#nameObject



8
9
10
# File 'lib/moxml/element.rb', line 8

def name
  adapter.node_name(@native)
end

#name=(value) ⇒ Object



12
13
14
# File 'lib/moxml/element.rb', line 12

def name=(value)
  adapter.set_node_name(@native, value)
end

#namespaceObject

it’s NOT the same as namespaces.first



88
89
90
91
# File 'lib/moxml/element.rb', line 88

def namespace
  ns = adapter.namespace(@native)
  ns && Namespace.new(ns, context)
end

#namespace=(ns_or_hash) ⇒ Object

add the prefix to the element name and add the namespace to the list of namespace definitions



95
96
97
98
99
100
101
102
103
104
# File 'lib/moxml/element.rb', line 95

def namespace=(ns_or_hash)
  if ns_or_hash.is_a?(Hash)
    adapter.set_namespace(
      @native,
      adapter.create_namespace(@native, *ns_or_hash.to_a.first),
    )
  else
    adapter.set_namespace(@native, ns_or_hash&.native)
  end
end

#namespace_nameObject

Returns the namespace URI of this element (alias for namespace_uri)



114
115
116
# File 'lib/moxml/element.rb', line 114

def namespace_name
  namespace_uri
end

#namespace_prefixObject

Returns the namespace prefix of this element



26
27
28
29
# File 'lib/moxml/element.rb', line 26

def namespace_prefix
  ns = namespace
  ns&.prefix
end

#namespace_uriObject

Returns the namespace URI of this element



32
33
34
35
# File 'lib/moxml/element.rb', line 32

def namespace_uri
  ns = namespace
  ns&.uri
end

#namespacesObject Also known as: namespace_definitions



106
107
108
109
110
# File 'lib/moxml/element.rb', line 106

def namespaces
  adapter.namespace_definitions(@native).map do |ns|
    Namespace.new(ns, context)
  end
end

#nodesObject

Alias for children (used by XPath engine)



177
178
179
# File 'lib/moxml/element.rb', line 177

def nodes
  children
end

#remove_attribute(name) ⇒ Object



66
67
68
69
# File 'lib/moxml/element.rb', line 66

def remove_attribute(name)
  adapter.remove_attribute(@native, name)
  self
end

#set_attributes(attributes_hash) ⇒ Object

Bulk attribute setting



156
157
158
159
# File 'lib/moxml/element.rb', line 156

def set_attributes(attributes_hash)
  attributes_hash.each { |name, value| self[name] = value }
  self
end

#textObject



118
119
120
# File 'lib/moxml/element.rb', line 118

def text
  adapter.text_content(@native)
end

#text=(content) ⇒ Object



122
123
124
# File 'lib/moxml/element.rb', line 122

def text=(content)
  adapter.set_text_content(@native, normalize_xml_value(content))
end

#with_attribute(name, value) ⇒ Object

Fluent interface methods



140
141
142
143
# File 'lib/moxml/element.rb', line 140

def with_attribute(name, value)
  self[name] = value
  self
end

#with_child(child) ⇒ Object

Chainable child addition



162
163
164
165
# File 'lib/moxml/element.rb', line 162

def with_child(child)
  add_child(child)
  self
end

#with_namespace(prefix, uri) ⇒ Object



145
146
147
148
# File 'lib/moxml/element.rb', line 145

def with_namespace(prefix, uri)
  add_namespace(prefix, uri)
  self
end

#with_text(content) ⇒ Object



150
151
152
153
# File 'lib/moxml/element.rb', line 150

def with_text(content)
  self.text = content
  self
end