Class: Moxml::Element

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

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, #document, #initialize, #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



20
21
22
# File 'lib/moxml/element.rb', line 20

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

#[]=(name, value) ⇒ Object



16
17
18
# File 'lib/moxml/element.rb', line 16

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

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



40
41
42
43
44
45
46
# File 'lib/moxml/element.rb', line 40

def add_namespace(prefix, uri)
  validate_uri(uri)
  adapter.create_native_namespace(@native, prefix, uri)
  self
rescue ValidationError => e
  raise Moxml::NamespaceError, e.message
end

#attribute(name) ⇒ Object



24
25
26
27
# File 'lib/moxml/element.rb', line 24

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

#attributesObject



29
30
31
32
33
# File 'lib/moxml/element.rb', line 29

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

#inner_htmlObject



82
83
84
# File 'lib/moxml/element.rb', line 82

def inner_html
  adapter.inner_html(@native)
end

#inner_html=(html) ⇒ Object



86
87
88
89
# File 'lib/moxml/element.rb', line 86

def inner_html=(html)
  doc = context.parse("<root>#{html}</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



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

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

#namespace=(ns_or_hash) ⇒ Object

it does NOT change the list of namespace definitions



56
57
58
59
60
61
62
63
64
65
# File 'lib/moxml/element.rb', line 56

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

#namespacesObject Also known as: namespace_definitions



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

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

#remove_attribute(name) ⇒ Object



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

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

#textObject



74
75
76
# File 'lib/moxml/element.rb', line 74

def text
  adapter.text_content(@native)
end

#text=(content) ⇒ Object



78
79
80
# File 'lib/moxml/element.rb', line 78

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

#with_attribute(name, value) ⇒ Object

Fluent interface methods



92
93
94
95
# File 'lib/moxml/element.rb', line 92

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

#with_namespace(prefix, uri) ⇒ Object



97
98
99
100
# File 'lib/moxml/element.rb', line 97

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

#with_text(content) ⇒ Object



102
103
104
105
# File 'lib/moxml/element.rb', line 102

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