Class: Itonoko::XML::Document

Inherits:
Node
  • Object
show all
Defined in:
lib/itonoko/xml/document.rb

Direct Known Subclasses

HTML::Document

Constant Summary

Constants inherited from Node

Node::ATTRIBUTE_NODE, Node::CDATA_SECTION_NODE, Node::COMMENT_NODE, Node::DOCUMENT_FRAGMENT_NODE, Node::DOCUMENT_NODE, Node::DOCUMENT_TYPE_NODE, Node::ELEMENT_NODE, Node::ESCAPE_ATTR, Node::ESCAPE_TEXT, Node::PROCESSING_INSTRUCTION_NODE, Node::TEXT_NODE

Instance Attribute Summary collapse

Attributes inherited from Node

#children, #document, #node_name, #node_type, #parent

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Node

#==, #[], #[]=, #add_child, #add_next_sibling, #add_previous_sibling, #ancestors, #at, #at_css, #at_xpath, #attribute, #attribute_nodes, #attributes, #cdata_node?, #child, #comment?, #description, #document?, #element?, #element_children, #fragment?, #get_attribute, #has_attribute?, #inner_html, #inner_html=, #inspect, #keys, #matches?, #name, #next_element, #next_sibling, #prepend_child, #previous_element, #previous_sibling, #remove, #remove_attribute, #replace, #search, #set_attribute, #text=, #text?

Constructor Details

#initializeDocument

Returns a new instance of Document.



8
9
10
11
12
13
14
# File 'lib/itonoko/xml/document.rb', line 8

def initialize
  super(DOCUMENT_NODE, "#document", nil)
  @document = self
  @encoding = "UTF-8"
  @version  = "1.0"
  @errors   = []
end

Instance Attribute Details

#encodingObject

Returns the value of attribute encoding.



6
7
8
# File 'lib/itonoko/xml/document.rb', line 6

def encoding
  @encoding
end

#errorsObject

Returns the value of attribute errors.



6
7
8
# File 'lib/itonoko/xml/document.rb', line 6

def errors
  @errors
end

#versionObject

Returns the value of attribute version.



6
7
8
# File 'lib/itonoko/xml/document.rb', line 6

def version
  @version
end

Class Method Details

.parse(string, url = nil, encoding = nil, options = nil, &block) ⇒ Object



16
17
18
19
# File 'lib/itonoko/xml/document.rb', line 16

def self.parse(string, url = nil, encoding = nil, options = nil, &block)
  require_relative "../parser/xml_parser"
  Parser::XmlParser.new.parse(string.to_s)
end

Instance Method Details

#collect_namespacesObject



76
77
78
# File 'lib/itonoko/xml/document.rb', line 76

def collect_namespaces
  {}
end

#create_cdata(content) ⇒ Object



45
46
47
# File 'lib/itonoko/xml/document.rb', line 45

def create_cdata(content)
  CDATA.new(content, self)
end

#create_comment(content) ⇒ Object



41
42
43
# File 'lib/itonoko/xml/document.rb', line 41

def create_comment(content)
  Comment.new(content, self)
end

#create_element(name, content = nil) ⇒ Object



31
32
33
34
35
# File 'lib/itonoko/xml/document.rb', line 31

def create_element(name, content = nil)
  node = Node.new(ELEMENT_NODE, name, self)
  node.add_child(Text.new(content, self)) if content
  node
end

#create_text_node(content) ⇒ Object



37
38
39
# File 'lib/itonoko/xml/document.rb', line 37

def create_text_node(content)
  Text.new(content, self)
end

#css(selector) ⇒ Object



71
72
73
74
# File 'lib/itonoko/xml/document.rb', line 71

def css(selector)
  require_relative "../css/matcher"
  CSS::Matcher.match(self, selector)
end

#docObject



80
81
82
# File 'lib/itonoko/xml/document.rb', line 80

def doc
  self
end

#rootObject



21
22
23
# File 'lib/itonoko/xml/document.rb', line 21

def root
  children.find { |c| c.node_type == ELEMENT_NODE }
end

#root=(node) ⇒ Object



25
26
27
28
29
# File 'lib/itonoko/xml/document.rb', line 25

def root=(node)
  old_root = root
  old_root&.remove
  add_child(node)
end

#textObject



49
50
51
# File 'lib/itonoko/xml/document.rb', line 49

def text
  root&.text.to_s
end

#to_htmlObject



58
59
60
# File 'lib/itonoko/xml/document.rb', line 58

def to_html
  children.map(&:to_html).join
end

#to_sObject



62
63
64
# File 'lib/itonoko/xml/document.rb', line 62

def to_s
  to_xml
end

#to_xml(options = {}) ⇒ Object



53
54
55
56
# File 'lib/itonoko/xml/document.rb', line 53

def to_xml(options = {})
  decl  = %(<?xml version="#{version}" encoding="#{encoding}"?>\n)
  decl + children.map { |c| c.to_xml(options) }.join
end

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



66
67
68
69
# File 'lib/itonoko/xml/document.rb', line 66

def xpath(expr, namespaces = {})
  require_relative "../xpath/evaluator"
  XPath::Evaluator.new(self, namespaces).evaluate(expr)
end