Class: Canon::Xml::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/canon/xml/node.rb

Overview

Base class for all XPath data model nodes

Constant Summary collapse

EMPTY_CHILDREN =

Shared by every childless node: leaves (text, attributes, namespaces, comments, PIs) and empty elements read children without materializing a private array. Mutating the returned array is a bug — writers go through add_child/children=, which install a private array first.

[].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeNode

Returns a new instance of Node.



16
17
18
19
20
# File 'lib/canon/xml/node.rb', line 16

def initialize
  @parent = nil
  @children = nil
  @in_node_set = true
end

Instance Attribute Details

#parentObject

Returns the value of attribute parent.



14
15
16
# File 'lib/canon/xml/node.rb', line 14

def parent
  @parent
end

Instance Method Details

#add_child(child) ⇒ Object



30
31
32
33
# File 'lib/canon/xml/node.rb', line 30

def add_child(child)
  child.parent = self
  (@children ||= []) << child
end

#childrenObject



22
23
24
# File 'lib/canon/xml/node.rb', line 22

def children
  @children || EMPTY_CHILDREN
end

#children=(new_children) ⇒ Object



26
27
28
# File 'lib/canon/xml/node.rb', line 26

def children=(new_children)
  @children = new_children
end

#in_node_set=(value) ⇒ Object



39
40
41
# File 'lib/canon/xml/node.rb', line 39

def in_node_set=(value)
  @in_node_set = value
end

#in_node_set?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/canon/xml/node.rb', line 35

def in_node_set?
  @in_node_set
end

#parse_errorsArray<String>

Parse-time errors carried alongside the node tree, captured at parse boundaries (Canon::Xml::DataModel.from_xml, etc.) so the diff report can surface libxml-level FATAL conditions that would otherwise be silently swallowed and produce misleading diffs against a partially-loaded tree. See lutaml/canon#130.

Returns:

  • (Array<String>)

    Parse errors as strings (empty by default)



50
51
52
# File 'lib/canon/xml/node.rb', line 50

def parse_errors
  @parse_errors || []
end

#parse_errors=(value) ⇒ Object



54
55
56
# File 'lib/canon/xml/node.rb', line 54

def parse_errors=(value)
  @parse_errors = Array(value)
end

#text_contentObject

Return the text content of this node and all descendants. ElementNode concatenates children's text_content; other nodes (TextNode, CommentNode, etc.) return their value.



61
62
63
# File 'lib/canon/xml/node.rb', line 61

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