Class: Leptris::XML::ProcessingInstruction

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

Instance Attribute Summary

Attributes inherited from Node

#c_ptr, #document

Instance Method Summary collapse

Methods inherited from Node

#<=>, #==, #cdata?, #child, #children, #comment?, #css_path, #dup, #element?, #element_children, #ensure_alive!, #ensure_writable!, #first_element_child, #initialize, #inner_text, #inspect, #last_element_child, #line, #next_element, #next_sibling, #parent, #path, #previous_element, #previous_sibling, #processing_instruction?, #readonly_document?, #text, #text?, #traverse, #type, #visit, wrap

Methods included from Searchable

#at, #at_css, #at_xpath, #css, #search, wrap_xpath_first_result, wrap_xpath_result, #xpath

Constructor Details

This class inherits a constructor from Leptris::XML::Node

Instance Method Details

#contentObject



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/leptris/xml/processing_instruction.rb', line 16

def content
  return @content if memo_hit?(@content_version)
  ensure_alive!
  result = Leptris::XML::FFI.read_pi_data(
    Leptris::XML::FFI.leptris_pi_node_get_data(@c_ptr)).to_s
  if @document
    @content = result
    @content_version = @document.version
  end
  result
end

#data=(new_data) ⇒ Object



38
39
40
41
42
43
# File 'lib/leptris/xml/processing_instruction.rb', line 38

def data=(new_data)
  ensure_writable!
  Leptris::XML::FFI.check_status(
    Leptris::XML::FFI.leptris_pi_node_set_data(@c_ptr, new_data.to_s))
  new_data
end

#nameObject Also known as: target



4
5
6
7
8
9
10
11
12
13
# File 'lib/leptris/xml/processing_instruction.rb', line 4

def name
  return @name if memo_hit?(@name_version)
  ensure_alive!
  result = Leptris::XML::FFI.leptris_pi_node_get_target(@c_ptr)
  if @document
    @name = result
    @name_version = @document.version
  end
  result
end

#target=(new_target) ⇒ Object

Since libleptris 1.9.9 (#612) parse-created document-level PIs carry document linkage — the setters work on them like on any tree-level PI.



31
32
33
34
35
36
# File 'lib/leptris/xml/processing_instruction.rb', line 31

def target=(new_target)
  ensure_writable!
  Leptris::XML::FFI.check_status(
    Leptris::XML::FFI.leptris_pi_node_set_target(@c_ptr, new_target.to_s))
  new_target
end

Document-level PIs have no tree parent for leptris_node_unlink — route through the document-level removal (libleptris 1.9.9, #612), identity-matched by index so the right same-target PI comes out.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/leptris/xml/processing_instruction.rb', line 49

def unlink
  ensure_writable!
  rc = Leptris::XML::FFI.leptris_node_unlink(@c_ptr)
  if rc == Leptris::XML::FFI::LEPTRIS_ERROR_NOT_FOUND
    index = document_pi_index
    if index.negative?
      raise Leptris::XML::Error,
        "PI not found in its document — already removed?"
    end
    removed = @document.remove_pi(index)
    unless removed && removed.c_ptr == @c_ptr
      raise Leptris::XML::Error, "document-level PI removal missed"
    end
    @parent = nil
    return self
  end
  Leptris::XML::FFI.check_status(rc)
  @parent = nil
  self
end