Class: Moxml::Node

Inherits:
Object
  • Object
show all
Includes:
XmlUtils
Defined in:
lib/moxml/node.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#initialize(native, context) ⇒ Node

Returns a new instance of Node.



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

def initialize(native, context)
  @native = native
  @context = context
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



10
11
12
# File 'lib/moxml/node.rb', line 10

def context
  @context
end

#nativeObject (readonly)

Returns the value of attribute native.



10
11
12
# File 'lib/moxml/node.rb', line 10

def native
  @native
end

Class Method Details

.wrap(node, context) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/moxml/node.rb', line 82

def self.wrap(node, context)
  return nil if node.nil?

  klass = case adapter(context).node_type(node)
          when :element then Element
          when :text then Text
          when :cdata then Cdata
          when :comment then Comment
          when :processing_instruction then ProcessingInstruction
          when :document then Document
          when :declaration then Declaration
          when :doctype then Doctype
          else self
          end

  klass.new(node, context)
end

Instance Method Details

#==(other) ⇒ Object



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

def ==(other)
  self.class == other.class && @native == other.native
end

#add_child(node) ⇒ Object



37
38
39
40
41
# File 'lib/moxml/node.rb', line 37

def add_child(node)
  node = prepare_node(node)
  adapter.add_child(@native, node.native)
  self
end

#add_next_sibling(node) ⇒ Object



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

def add_next_sibling(node)
  node = prepare_node(node)
  adapter.add_next_sibling(@native, node.native)
  self
end

#add_previous_sibling(node) ⇒ Object



43
44
45
46
47
# File 'lib/moxml/node.rb', line 43

def add_previous_sibling(node)
  node = prepare_node(node)
  adapter.add_previous_sibling(@native, node.native)
  self
end

#at_xpath(expression, namespaces = {}) ⇒ Object



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

def at_xpath(expression, namespaces = {})
  Node.wrap(adapter.at_xpath(@native, expression, namespaces), context)
end

#childrenObject



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

def children
  NodeSet.new(adapter.children(@native), context)
end

#documentObject



17
18
19
# File 'lib/moxml/node.rb', line 17

def document
  Document.wrap(adapter.document(@native), context)
end

#next_siblingObject



29
30
31
# File 'lib/moxml/node.rb', line 29

def next_sibling
  Node.wrap(adapter.next_sibling(@native), context)
end

#parentObject



21
22
23
# File 'lib/moxml/node.rb', line 21

def parent
  Node.wrap(adapter.parent(@native), context)
end

#previous_siblingObject



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

def previous_sibling
  Node.wrap(adapter.previous_sibling(@native), context)
end

#removeObject



55
56
57
58
# File 'lib/moxml/node.rb', line 55

def remove
  adapter.remove(@native)
  self
end

#replace(node) ⇒ Object



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

def replace(node)
  node = prepare_node(node)
  adapter.replace(@native, node.native)
  self
end

#to_xml(options = {}) ⇒ Object



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

def to_xml(options = {})
  adapter.serialize(@native, default_options.merge(options))
end

#xpath(expression, namespaces = {}) ⇒ Object



70
71
72
# File 'lib/moxml/node.rb', line 70

def xpath(expression, namespaces = {})
  NodeSet.new(adapter.xpath(@native, expression, namespaces), context)
end