Class: Itonoko::XML::Document
- Inherits:
-
Node
- Object
- Node
- Itonoko::XML::Document
show all
- Defined in:
- lib/itonoko/xml/document.rb
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
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
#encoding ⇒ Object
Returns the value of attribute encoding.
6
7
8
|
# File 'lib/itonoko/xml/document.rb', line 6
def encoding
@encoding
end
|
#errors ⇒ Object
Returns the value of attribute errors.
6
7
8
|
# File 'lib/itonoko/xml/document.rb', line 6
def errors
@errors
end
|
#version ⇒ Object
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_namespaces ⇒ Object
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
|
41
42
43
|
# File 'lib/itonoko/xml/document.rb', line 41
def (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
|
#doc ⇒ Object
80
81
82
|
# File 'lib/itonoko/xml/document.rb', line 80
def doc
self
end
|
#root ⇒ Object
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
|
#text ⇒ Object
49
50
51
|
# File 'lib/itonoko/xml/document.rb', line 49
def text
root&.text.to_s
end
|
#to_html ⇒ Object
58
59
60
|
# File 'lib/itonoko/xml/document.rb', line 58
def to_html
children.map(&:to_html).join
end
|
#to_s ⇒ Object
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
|