Class: Leptris::XML::DocumentFragment

Inherits:
Object
  • Object
show all
Defined in:
lib/leptris/xml/document_fragment.rb

Overview

Wraps the synthetic "#document-fragment" element returned by leptris_parse_fragment. Children of this fragment are the parsed nodes; the fragment itself isn't part of any document tree but borrows its document's lifetime.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document, c_ptr) ⇒ DocumentFragment

Returns a new instance of DocumentFragment.



10
11
12
13
# File 'lib/leptris/xml/document_fragment.rb', line 10

def initialize(document, c_ptr)
  @document = document
  @c_ptr = c_ptr
end

Instance Attribute Details

#c_ptrObject (readonly)

Returns the value of attribute c_ptr.



8
9
10
# File 'lib/leptris/xml/document_fragment.rb', line 8

def c_ptr
  @c_ptr
end

#documentObject (readonly)

Returns the value of attribute document.



8
9
10
# File 'lib/leptris/xml/document_fragment.rb', line 8

def document
  @document
end

Class Method Details

.parse(xml, document) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/leptris/xml/document_fragment.rb', line 15

def self.parse(xml, document)
  raw, status = Leptris::XML::FFI.parse_fragment_with_status(
    xml.to_s, document.c_ptr)
  if raw.null?
    raise Leptris::XML::ParseError,
      "leptris_parse_fragment failed (status=#{status})"
  end
  new(document, raw)
end

Instance Method Details

#childrenObject



25
26
27
28
29
30
# File 'lib/leptris/xml/document_fragment.rb', line 25

def children
  nodes = Leptris::XML::FFI.fetch_children(@c_ptr).map do |ptr|
    Leptris::XML::Node.wrap(ptr, @document)
  end
  Leptris::XML::NodeSet.new(@document, nodes)
end

#nameObject



32
33
34
# File 'lib/leptris/xml/document_fragment.rb', line 32

def name
  "#document-fragment"
end