class Oga::XML::Document

Class used for storing information about an entire XML document. This includes the doctype, XML declaration, child nodes and more.

Attributes

doctype[RW]

The doctype of the document.

When parsing a document this attribute will be set automatically if a doctype resides at the root of the document.

@return [Oga::XML::Doctype]

type[R]

The document type, either ‘:xml` or `:html`. @return [Symbol]

xml_declaration[RW]

@return [Oga::XML::XmlDeclaration]

Public Class Methods

new(options = {}) click to toggle source

@param [Hash] options

@option options [Oga::XML::NodeSet] :children @option options [Oga::XML::Doctype] :doctype @option options [Oga::XML::XmlDeclaration] :xml_declaration @option options [Symbol] :type

# File lib/oga/xml/document.rb, line 31
def initialize(options = {})
  @doctype         = options[:doctype]
  @xml_declaration = options[:xml_declaration]
  @type            = options[:type] || :xml

  self.children = options[:children] if options[:children]
end

Public Instance Methods

children() click to toggle source

@return [Oga::XML::NodeSet]

# File lib/oga/xml/document.rb, line 40
def children
  @children ||= NodeSet.new([], self)
end
children=(nodes) click to toggle source

Sets the child nodes of the document.

@param [Oga::XML::NodeSet|Array] nodes

# File lib/oga/xml/document.rb, line 47
def children=(nodes)
  if nodes.is_a?(NodeSet)
    nodes.owner = self
    nodes.take_ownership_on_nodes
    @children = nodes
  else
    @children = NodeSet.new(nodes, self)
  end
end
html?() click to toggle source

@return [TrueClass|FalseClass]

# File lib/oga/xml/document.rb, line 68
def html?
  type.equal?(:html)
end
inspect() click to toggle source

Inspects the document and its child nodes. Child nodes are indented for each nesting level.

@return [String]

# File lib/oga/xml/document.rb, line 76
      def inspect
        segments = []

        [:doctype, :xml_declaration, :children].each do |attr|
          value = send(attr)

          if value
            segments << "#{attr}: #{value.inspect}"
          end
        end

        <<-EOF.strip
Document(
  #{segments.join("\n  ")}
)
        EOF
      end
literal_html_name?() click to toggle source

@return [FalseClass]

# File lib/oga/xml/document.rb, line 95
def literal_html_name?
  false
end
root_node() click to toggle source

Returns self.

This method exists to make this class compatible with Element, which in turn makes it easier to use both in the XPath compiler.

@return [Oga::XML::Document]

# File lib/oga/xml/document.rb, line 63
def root_node
  self
end