Class: Moxml::Document
- Inherits:
-
Node
- Object
- Node
- Moxml::Document
show all
- Defined in:
- lib/moxml/document.rb
Constant Summary
Constants inherited
from Node
Node::TYPES
Instance Attribute Summary collapse
Attributes inherited from Node
#context, #native
Instance Method Summary
collapse
-
#add_child(node) ⇒ Object
-
#add_element(name, attributes = {}, &block) ⇒ Object
Quick element creation and addition.
-
#at_xpath(expression, namespaces = nil) ⇒ Object
-
#create_cdata(content) ⇒ Object
-
#create_comment(content) ⇒ Object
-
#create_declaration(version = "1.0", encoding = "UTF-8", standalone = nil) ⇒ Object
-
#create_doctype(name, external_id, system_id) ⇒ Object
-
#create_element(name) ⇒ Object
-
#create_entity_reference(name) ⇒ Object
-
#create_processing_instruction(target, content) ⇒ Object
-
#create_text(content) ⇒ Object
-
#document ⇒ Object
-
#find(xpath) ⇒ Object
Convenience find methods.
-
#find_all(xpath) ⇒ Object
-
#initialize(native, context) ⇒ Document
constructor
A new instance of Document.
-
#root ⇒ Object
-
#root=(element) ⇒ Object
-
#xpath(expression, namespaces = nil) ⇒ Object
Methods inherited from Node
#==, #[], adapter, #add_next_sibling, #add_previous_sibling, #children, #clone, #each_node, #first_child, #has_children?, #identifier, #last_child, #namespace, #namespaces, #next_sibling, #parent, #previous_sibling, #remove, #replace, #text, #to_xml, wrap
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_entity_reference_name, #validate_pi_target, #validate_prefix, #validate_uri
Constructor Details
#initialize(native, context) ⇒ Document
Returns a new instance of Document.
18
19
20
21
|
# File 'lib/moxml/document.rb', line 18
def initialize(native, context)
super
@has_xml_declaration = false
end
|
Instance Attribute Details
#has_xml_declaration ⇒ Object
Returns the value of attribute has_xml_declaration.
16
17
18
|
# File 'lib/moxml/document.rb', line 16
def has_xml_declaration
@has_xml_declaration
end
|
Instance Method Details
#add_child(node) ⇒ Object
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
# File 'lib/moxml/document.rb', line 78
def add_child(node)
node = prepare_node(node)
if node.is_a?(Declaration)
@has_xml_declaration = true
if children.empty?
adapter.add_child(@native, node.native)
else
adapter.add_previous_sibling(adapter.children(@native).first,
node.native)
end
elsif root && !node.is_a?(ProcessingInstruction) && !node.is_a?(Comment)
raise Error, "Document already has a root element"
else
adapter.add_child(@native, node.native)
end
node.instance_variable_set(:@parent_node, self)
invalidate_children_cache!
self
end
|
#add_element(name, attributes = {}, &block) ⇒ Object
Quick element creation and addition
126
127
128
129
130
131
132
|
# File 'lib/moxml/document.rb', line 126
def add_element(name, attributes = {}, &block)
elem = create_element(name)
attributes.each { |k, v| elem[k] = v }
add_child(elem)
block&.call(elem)
elem
end
|
#at_xpath(expression, namespaces = nil) ⇒ Object
119
120
121
122
123
|
# File 'lib/moxml/document.rb', line 119
def at_xpath(expression, namespaces = nil)
if (native_node = adapter.at_xpath(@native, expression, namespaces))
Moxml::Node.wrap(native_node, context)
end
end
|
#create_cdata(content) ⇒ Object
46
47
48
|
# File 'lib/moxml/document.rb', line 46
def create_cdata(content)
Cdata.new(adapter.create_cdata(content, owner_doc: @native), context)
end
|
50
51
52
|
# File 'lib/moxml/document.rb', line 50
def (content)
Comment.new(adapter.(content, owner_doc: @native), context)
end
|
#create_declaration(version = "1.0", encoding = "UTF-8", standalone = nil) ⇒ Object
68
69
70
71
72
|
# File 'lib/moxml/document.rb', line 68
def create_declaration(version = "1.0", encoding = "UTF-8",
standalone = nil)
decl = adapter.create_declaration(version, encoding, standalone)
Declaration.new(decl, context)
end
|
#create_doctype(name, external_id, system_id) ⇒ Object
54
55
56
57
58
59
|
# File 'lib/moxml/document.rb', line 54
def create_doctype(name, external_id, system_id)
Doctype.new(
adapter.create_doctype(name, external_id, system_id),
context,
)
end
|
#create_element(name) ⇒ Object
38
39
40
|
# File 'lib/moxml/document.rb', line 38
def create_element(name)
Element.new(adapter.create_element(name, owner_doc: @native), context)
end
|
#create_entity_reference(name) ⇒ Object
74
75
76
|
# File 'lib/moxml/document.rb', line 74
def create_entity_reference(name)
EntityReference.new(adapter.create_entity_reference(name), context)
end
|
#create_processing_instruction(target, content) ⇒ Object
61
62
63
64
65
66
|
# File 'lib/moxml/document.rb', line 61
def create_processing_instruction(target, content)
ProcessingInstruction.new(
adapter.create_processing_instruction(target, content),
context,
)
end
|
#create_text(content) ⇒ Object
42
43
44
|
# File 'lib/moxml/document.rb', line 42
def create_text(content)
Text.new(adapter.create_text(content, owner_doc: @native), context)
end
|
#document ⇒ Object
23
24
25
|
# File 'lib/moxml/document.rb', line 23
def document
self
end
|
#find(xpath) ⇒ Object
135
136
137
|
# File 'lib/moxml/document.rb', line 135
def find(xpath)
at_xpath(xpath)
end
|
#find_all(xpath) ⇒ Object
139
140
141
|
# File 'lib/moxml/document.rb', line 139
def find_all(xpath)
xpath(xpath).to_a
end
|
#root ⇒ Object
33
34
35
36
|
# File 'lib/moxml/document.rb', line 33
def root
root_element = adapter.root(@native)
root_element ? Element.new(root_element, context) : nil
end
|
#root=(element) ⇒ Object
27
28
29
30
31
|
# File 'lib/moxml/document.rb', line 27
def root=(element)
adapter.set_root(@native, element.native)
element.instance_variable_set(:@parent_node, self)
invalidate_children_cache!
end
|
#xpath(expression, namespaces = nil) ⇒ Object
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
# File 'lib/moxml/document.rb', line 101
def xpath(expression, namespaces = nil)
result = adapter.xpath(@native, expression, namespaces)
case result
when NodeSet, Float, String, TrueClass, FalseClass, NilClass
result
when Array
NodeSet.new(result, context)
else
NodeSet.new(result, context)
end
end
|