Class: Moxml::Element
Constant Summary
Constants inherited from Node
Instance Attribute Summary
Attributes inherited from Node
#context, #native, #parent_node
Instance Method Summary collapse
- #[](name) ⇒ Object
- #[]=(name, value) ⇒ Object
- #add_namespace(prefix, uri) ⇒ Object (also: #add_namespace_definition)
- #attribute(name) ⇒ Object
- #attributes ⇒ Object
-
#expanded_name ⇒ Object
Returns the expanded name including namespace prefix.
- #find_all(xpath) ⇒ Object
-
#find_element(xpath) ⇒ Object
Convenience find methods.
-
#get(attr_name) ⇒ Object
Returns attribute value by name (used by XPath engine).
-
#identifier ⇒ String
Returns the primary identifier for this element (its tag name).
-
#in_scope_namespaces ⇒ Object
Returns all namespaces in scope for this element, including those inherited from ancestor elements.
- #inner_text ⇒ Object
- #inner_xml ⇒ Object
- #inner_xml=(xml) ⇒ Object
-
#invalidate_attribute_cache! ⇒ Object
Called by Attribute#remove to invalidate the cached attributes.
- #name ⇒ Object
- #name=(value) ⇒ Object
-
#namespace ⇒ Object
it’s NOT the same as namespaces.first.
-
#namespace=(ns_or_hash) ⇒ Object
add the prefix to the element name and add the namespace to the list of namespace definitions.
-
#namespace_name ⇒ Object
Returns the namespace URI of this element (alias for namespace_uri).
-
#namespace_prefix ⇒ Object
Returns the namespace prefix of this element.
-
#namespace_uri ⇒ Object
Returns the namespace URI of this element.
- #namespaces ⇒ Object (also: #namespace_definitions)
-
#nodes ⇒ Object
Alias for children (used by XPath engine).
-
#raw_inner_text ⇒ Object
Returns inner text without entity marker restoration.
- #remove_attribute(name) ⇒ Object
-
#set_attributes(attributes_hash) ⇒ Object
Bulk attribute setting.
- #text ⇒ Object
- #text=(content) ⇒ Object
-
#with_attribute(name, value) ⇒ Object
Fluent interface methods.
-
#with_child(child) ⇒ Object
Chainable child addition.
- #with_namespace(prefix, uri) ⇒ Object
- #with_text(content) ⇒ Object
Methods inherited from Node
#==, adapter, #add_child, #add_next_sibling, #add_previous_sibling, #at_xpath, #children, #clone, #document, #each_node, #find, #first_child, #has_children?, #initialize, #last_child, #next_sibling, #parent, #previous_sibling, #refresh_native!, #remove, #replace, #to_xml, wrap, #xpath
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
This class inherits a constructor from Moxml::Node
Instance Method Details
#[](name) ⇒ Object
48 49 50 51 |
# File 'lib/moxml/element.rb', line 48 def [](name) val = adapter.get_attribute_value(@native, name) val ? adapter.restore_entities(val) : val end |
#[]=(name, value) ⇒ Object
43 44 45 46 |
# File 'lib/moxml/element.rb', line 43 def []=(name, value) adapter.set_attribute(@native, name, normalize_xml_value(value)) @attributes = nil end |
#add_namespace(prefix, uri) ⇒ Object Also known as: add_namespace_definition
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/moxml/element.rb', line 77 def add_namespace(prefix, uri) adapter.create_namespace(@native, prefix, uri, namespace_validation_mode: context.config.namespace_validation_mode) @namespaces = nil self rescue ValidationError => e # Re-raise as NamespaceError, provide attributes for error context # but the to_s will only add details if provided raise Moxml::NamespaceError.new( e., prefix: prefix, uri: uri, element: self, ) end |
#attribute(name) ⇒ Object
53 54 55 56 |
# File 'lib/moxml/element.rb', line 53 def attribute(name) native_attr = adapter.get_attribute(@native, name) native_attr && Attribute.new(native_attr, context) end |
#attributes ⇒ Object
63 64 65 66 67 68 69 |
# File 'lib/moxml/element.rb', line 63 def attributes @attributes ||= adapter.attributes(@native).map do |attr| a = Attribute.new(attr, context) a.parent_node = self a end end |
#expanded_name ⇒ Object
Returns the expanded name including namespace prefix
23 24 25 26 27 28 29 |
# File 'lib/moxml/element.rb', line 23 def if namespace_prefix && !namespace_prefix.empty? "#{namespace_prefix}:#{name}" else name end end |
#find_all(xpath) ⇒ Object
199 200 201 |
# File 'lib/moxml/element.rb', line 199 def find_all(xpath) xpath(xpath).to_a end |
#find_element(xpath) ⇒ Object
Convenience find methods
195 196 197 |
# File 'lib/moxml/element.rb', line 195 def find_element(xpath) at_xpath(xpath) end |
#get(attr_name) ⇒ Object
Returns attribute value by name (used by XPath engine)
59 60 61 |
# File 'lib/moxml/element.rb', line 59 def get(attr_name) self[attr_name] end |
#identifier ⇒ String
Returns the primary identifier for this element (its tag name)
18 19 20 |
# File 'lib/moxml/element.rb', line 18 def identifier name end |
#in_scope_namespaces ⇒ Object
Returns all namespaces in scope for this element, including those inherited from ancestor elements.
124 125 126 127 128 |
# File 'lib/moxml/element.rb', line 124 def in_scope_namespaces adapter.in_scope_namespaces(@native).map do |ns| Namespace.new(ns, context) end end |
#inner_text ⇒ Object
145 146 147 148 |
# File 'lib/moxml/element.rb', line 145 def inner_text text = raw_inner_text adapter.restore_entities(text) end |
#inner_xml ⇒ Object
156 157 158 |
# File 'lib/moxml/element.rb', line 156 def inner_xml adapter.inner_xml(@native) end |
#inner_xml=(xml) ⇒ Object
160 161 162 163 164 |
# File 'lib/moxml/element.rb', line 160 def inner_xml=(xml) doc = context.parse("<root>#{xml}</root>") adapter.replace_children(@native, doc.root.children.map(&:native)) invalidate_children_cache! end |
#invalidate_attribute_cache! ⇒ Object
Called by Attribute#remove to invalidate the cached attributes
209 210 211 |
# File 'lib/moxml/element.rb', line 209 def invalidate_attribute_cache! @attributes = nil end |
#name ⇒ Object
8 9 10 |
# File 'lib/moxml/element.rb', line 8 def name adapter.node_name(@native) end |
#name=(value) ⇒ Object
12 13 14 |
# File 'lib/moxml/element.rb', line 12 def name=(value) adapter.set_node_name(@native, value) end |
#namespace ⇒ Object
it’s NOT the same as namespaces.first
95 96 97 98 |
# File 'lib/moxml/element.rb', line 95 def namespace ns = adapter.namespace(@native) ns && Namespace.new(ns, context) end |
#namespace=(ns_or_hash) ⇒ Object
add the prefix to the element name and add the namespace to the list of namespace definitions
102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/moxml/element.rb', line 102 def namespace=(ns_or_hash) if ns_or_hash.is_a?(Hash) adapter.set_namespace( @native, adapter.create_namespace(@native, *ns_or_hash.to_a.first, namespace_validation_mode: context.config.namespace_validation_mode), ) else adapter.set_namespace(@native, ns_or_hash&.native) end @namespaces = nil end |
#namespace_name ⇒ Object
Returns the namespace URI of this element (alias for namespace_uri)
131 132 133 |
# File 'lib/moxml/element.rb', line 131 def namespace_name namespace_uri end |
#namespace_prefix ⇒ Object
Returns the namespace prefix of this element
32 33 34 35 |
# File 'lib/moxml/element.rb', line 32 def namespace_prefix ns = namespace ns&.prefix end |
#namespace_uri ⇒ Object
Returns the namespace URI of this element
38 39 40 41 |
# File 'lib/moxml/element.rb', line 38 def namespace_uri ns = namespace ns&.uri end |
#namespaces ⇒ Object Also known as: namespace_definitions
115 116 117 118 119 |
# File 'lib/moxml/element.rb', line 115 def namespaces @namespaces ||= adapter.namespace_definitions(@native).map do |ns| Namespace.new(ns, context) end end |
#nodes ⇒ Object
Alias for children (used by XPath engine)
204 205 206 |
# File 'lib/moxml/element.rb', line 204 def nodes children end |
#raw_inner_text ⇒ Object
Returns inner text without entity marker restoration. Used internally when raw content with markers is needed (e.g., for DOM construction).
152 153 154 |
# File 'lib/moxml/element.rb', line 152 def raw_inner_text adapter.inner_text(@native) end |
#remove_attribute(name) ⇒ Object
71 72 73 74 75 |
# File 'lib/moxml/element.rb', line 71 def remove_attribute(name) adapter.remove_attribute(@native, name) @attributes = nil self end |
#set_attributes(attributes_hash) ⇒ Object
Bulk attribute setting
183 184 185 186 |
# File 'lib/moxml/element.rb', line 183 def set_attributes(attributes_hash) attributes_hash.each { |name, value| self[name] = value } self end |
#text ⇒ Object
135 136 137 138 |
# File 'lib/moxml/element.rb', line 135 def text val = adapter.text_content(@native) adapter.restore_entities(val) end |
#text=(content) ⇒ Object
140 141 142 143 |
# File 'lib/moxml/element.rb', line 140 def text=(content) adapter.set_text_content(@native, normalize_xml_value(content)) invalidate_children_cache! end |
#with_attribute(name, value) ⇒ Object
Fluent interface methods
167 168 169 170 |
# File 'lib/moxml/element.rb', line 167 def with_attribute(name, value) self[name] = value self end |
#with_child(child) ⇒ Object
Chainable child addition
189 190 191 192 |
# File 'lib/moxml/element.rb', line 189 def with_child(child) add_child(child) self end |
#with_namespace(prefix, uri) ⇒ Object
172 173 174 175 |
# File 'lib/moxml/element.rb', line 172 def with_namespace(prefix, uri) add_namespace(prefix, uri) self end |
#with_text(content) ⇒ Object
177 178 179 180 |
# File 'lib/moxml/element.rb', line 177 def with_text(content) self.text = content self end |