Class: Canon::Xml::Nodes::ElementNode
- Inherits:
-
Canon::Xml::Node
- Object
- Canon::Xml::Node
- Canon::Xml::Nodes::ElementNode
- Defined in:
- lib/canon/xml/nodes/element_node.rb
Constant Summary collapse
- EMPTY_ATTRIBUTE_NODES =
Lazy: attribute-free elements (common in real documents) share one frozen empty array on read instead of materializing one per element; writers install a private array first. Inherited namespace nodes arrive as one shared frozen array from TreeBuilder#attach_namespace_scope.
[].freeze
- EMPTY_NAMESPACE_NODES =
[].freeze
Constants inherited from Canon::Xml::Node
Canon::Xml::Node::EMPTY_CHILDREN
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#namespace_uri ⇒ Object
readonly
Returns the value of attribute namespace_uri.
-
#prefix ⇒ Object
readonly
Returns the value of attribute prefix.
Attributes inherited from Canon::Xml::Node
Instance Method Summary collapse
- #add_attribute(attribute_node) ⇒ Object
- #add_namespace(namespace_node) ⇒ Object
- #attribute_nodes ⇒ Object
-
#initialize(name:, namespace_uri: nil, prefix: nil) ⇒ ElementNode
constructor
A new instance of ElementNode.
- #namespace_nodes ⇒ Object
- #namespace_nodes=(nodes) ⇒ Object
- #node_info ⇒ Object
- #node_type ⇒ Object
- #qname ⇒ Object
-
#sorted_attribute_nodes ⇒ Object
Get attribute nodes in sorted order (by namespace URI then local name).
-
#sorted_namespace_nodes ⇒ Object
Get namespace nodes in sorted order (lexicographically by local name).
- #to_s ⇒ Object
Methods inherited from Canon::Xml::Node
#add_child, #children, #children=, #in_node_set=, #in_node_set?, #parse_errors, #parse_errors=, #text_content
Constructor Details
#initialize(name:, namespace_uri: nil, prefix: nil) ⇒ ElementNode
Returns a new instance of ElementNode.
9 10 11 12 13 14 15 16 |
# File 'lib/canon/xml/nodes/element_node.rb', line 9 def initialize(name:, namespace_uri: nil, prefix: nil) super() @name = name @namespace_uri = namespace_uri @prefix = prefix @namespace_nodes = nil @attribute_nodes = nil end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
7 8 9 |
# File 'lib/canon/xml/nodes/element_node.rb', line 7 def name @name end |
#namespace_uri ⇒ Object (readonly)
Returns the value of attribute namespace_uri.
7 8 9 |
# File 'lib/canon/xml/nodes/element_node.rb', line 7 def namespace_uri @namespace_uri end |
#prefix ⇒ Object (readonly)
Returns the value of attribute prefix.
7 8 9 |
# File 'lib/canon/xml/nodes/element_node.rb', line 7 def prefix @prefix end |
Instance Method Details
#add_attribute(attribute_node) ⇒ Object
51 52 53 54 |
# File 'lib/canon/xml/nodes/element_node.rb', line 51 def add_attribute(attribute_node) attribute_node.parent = self (@attribute_nodes ||= []) << attribute_node end |
#add_namespace(namespace_node) ⇒ Object
46 47 48 49 |
# File 'lib/canon/xml/nodes/element_node.rb', line 46 def add_namespace(namespace_node) namespace_node.parent = self (@namespace_nodes ||= []) << namespace_node end |
#attribute_nodes ⇒ Object
34 35 36 |
# File 'lib/canon/xml/nodes/element_node.rb', line 34 def attribute_nodes @attribute_nodes || EMPTY_ATTRIBUTE_NODES end |
#namespace_nodes ⇒ Object
26 27 28 |
# File 'lib/canon/xml/nodes/element_node.rb', line 26 def namespace_nodes @namespace_nodes || EMPTY_NAMESPACE_NODES end |
#namespace_nodes=(nodes) ⇒ Object
30 31 32 |
# File 'lib/canon/xml/nodes/element_node.rb', line 30 def namespace_nodes=(nodes) @namespace_nodes = nodes end |
#node_info ⇒ Object
79 80 81 |
# File 'lib/canon/xml/nodes/element_node.rb', line 79 def node_info "name: #{name} namespace_uri: #{namespace_uri} prefix: #{prefix}" end |
#node_type ⇒ Object
38 39 40 |
# File 'lib/canon/xml/nodes/element_node.rb', line 38 def node_type :element end |
#qname ⇒ Object
42 43 44 |
# File 'lib/canon/xml/nodes/element_node.rb', line 42 def qname prefix.nil? || prefix.empty? ? name : "#{prefix}:#{name}" end |
#sorted_attribute_nodes ⇒ Object
Get attribute nodes in sorted order (by namespace URI then local name). A comparator block instead of sort_by keys allocates no per-attribute key arrays; (uri, name) pairs are unique per element (duplicates are resolved at build), so the order is identical.
69 70 71 72 73 74 75 76 77 |
# File 'lib/canon/xml/nodes/element_node.rb', line 69 def sorted_attribute_nodes attrs = attribute_nodes return attrs if attrs.empty? attrs.sort do |a, b| (a.namespace_uri.to_s <=> b.namespace_uri.to_s).nonzero? || (a.local_name <=> b.local_name) end end |
#sorted_namespace_nodes ⇒ Object
Get namespace nodes in sorted order (lexicographically by local name).
57 58 59 60 61 62 |
# File 'lib/canon/xml/nodes/element_node.rb', line 57 def sorted_namespace_nodes nodes = namespace_nodes return nodes if nodes.empty? nodes.sort_by(&:local_name) end |
#to_s ⇒ Object
83 84 85 |
# File 'lib/canon/xml/nodes/element_node.rb', line 83 def to_s "<#{qname}>" end |