Class: Moxml::Node

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

Constant Summary collapse

TYPES =
i[
  element text cdata comment processing_instruction document
  declaration doctype namespace attribute unknown
].freeze

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.



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

def initialize(native, context)
  @context = context
  # @native = adapter.patch_node(native)
  @native = native
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



15
16
17
# File 'lib/moxml/node.rb', line 15

def context
  @context
end

#nativeObject (readonly)

Returns the value of attribute native.



15
16
17
# File 'lib/moxml/node.rb', line 15

def native
  @native
end

Class Method Details

.wrap(node, context) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/moxml/node.rb', line 97

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



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

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

#add_child(node) ⇒ Object



46
47
48
49
50
# File 'lib/moxml/node.rb', line 46

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

#add_next_sibling(node) ⇒ Object



58
59
60
61
62
# File 'lib/moxml/node.rb', line 58

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

#add_previous_sibling(node) ⇒ Object



52
53
54
55
56
# File 'lib/moxml/node.rb', line 52

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

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



83
84
85
# File 'lib/moxml/node.rb', line 83

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

#childrenObject



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

def children
  NodeSet.new(
    adapter.children(@native).map { adapter.patch_node(_1, @native) },
    context
  )
end

#documentObject



23
24
25
# File 'lib/moxml/node.rb', line 23

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

#next_siblingObject



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

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

#parentObject



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

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

#previous_siblingObject



42
43
44
# File 'lib/moxml/node.rb', line 42

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

#removeObject



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

def remove
  adapter.remove(@native)
  self
end

#replace(node) ⇒ Object



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

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

#to_xml(options = {}) ⇒ Object



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

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

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



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

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