Class: Oga::XML::Document

Inherits:
Object
  • Object
show all
Includes:
Querying, ToXML, Traversal
Defined in:
lib/oga/xml/document.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ToXML

#to_xml

Methods included from Traversal

#each_node

Methods included from Querying

#at_css, #at_xpath, #css, #xpath

Constructor Details

#initialize(options = {}) ⇒ Document

Returns a new instance of Document

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):



31
32
33
34
35
36
37
# 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

Instance Attribute Details

#doctypeOga::XML::Doctype

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.

Returns:



16
17
18
# File 'lib/oga/xml/document.rb', line 16

def doctype
  @doctype
end

#typeSymbol (readonly)

The document type, either :xml or :html.

Returns:

  • (Symbol)


23
24
25
# File 'lib/oga/xml/document.rb', line 23

def type
  @type
end

#xml_declarationOga::XML::XmlDeclaration



19
20
21
# File 'lib/oga/xml/document.rb', line 19

def xml_declaration
  @xml_declaration
end

Instance Method Details

#childrenOga::XML::NodeSet

Returns:



40
41
42
# File 'lib/oga/xml/document.rb', line 40

def children
  @children ||= NodeSet.new([], self)
end

#children=(nodes) ⇒ Object

Sets the child nodes of the document.

Parameters:



47
48
49
50
51
52
53
# File 'lib/oga/xml/document.rb', line 47

def children=(nodes)
  if nodes.is_a?(NodeSet)
    @children = nodes
  else
    @children = NodeSet.new(nodes, self)
  end
end

#html?TrueClass|FalseClass

Returns:

  • (TrueClass|FalseClass)


66
67
68
# File 'lib/oga/xml/document.rb', line 66

def html?
  type.equal?(:html)
end

#inspectString

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

Returns:

  • (String)


74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/oga/xml/document.rb', line 74

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?FalseClass

Returns:

  • (FalseClass)


93
94
95
# File 'lib/oga/xml/document.rb', line 93

def literal_html_name?
  false
end

#root_nodeOga::XML::Document

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.

Returns:



61
62
63
# File 'lib/oga/xml/document.rb', line 61

def root_node
  self
end