Class: JSON::LD::API::Nokogiri::NodeProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/json/ld/html/nokogiri.rb

Overview

Proxy class to implement uniform element accessors

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node, parent = nil) ⇒ NodeProxy

Returns a new instance of NodeProxy.



21
22
23
24
# File 'lib/json/ld/html/nokogiri.rb', line 21

def initialize(node, parent = nil)
  @node = node
  @parent = parent
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object

Proxy for everything else to @node



93
94
95
# File 'lib/json/ld/html/nokogiri.rb', line 93

def method_missing(method, *args)
  @node.send(method, *args)
end

Instance Attribute Details

#nodeObject (readonly)

Returns the value of attribute node.



18
19
20
# File 'lib/json/ld/html/nokogiri.rb', line 18

def node
  @node
end

#parentObject (readonly)

Returns the value of attribute parent.



19
20
21
# File 'lib/json/ld/html/nokogiri.rb', line 19

def parent
  @parent
end

Instance Method Details

#ancestorsObject

Ancestors of this element, in order



64
65
66
# File 'lib/json/ld/html/nokogiri.rb', line 64

def ancestors
  @ancestors ||= parent ? parent.ancestors + [parent] : []
end

#attribute_nodesString

Inner text of an element. Decode Entities

def inner_text

coder = HTMLEntities.new
coder.decode(@node.inner_text)

end

Returns:

  • (String)


77
78
79
# File 'lib/json/ld/html/nokogiri.rb', line 77

def attribute_nodes
  @attribute_nodes ||= NodeSetProxy.new(@node.attribute_nodes, self)
end

#baseString

Return xml:base on element, if defined

Returns:

  • (String)


30
31
32
# File 'lib/json/ld/html/nokogiri.rb', line 30

def base
  @node.attribute_with_ns("base", RDF::XML.to_s) || @node.attribute('xml:base')
end

#childrenNodeSetProxy

Children of this node

Returns:



59
60
61
# File 'lib/json/ld/html/nokogiri.rb', line 59

def children
  NodeSetProxy.new(@node.children, self)
end

#display_pathObject



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/json/ld/html/nokogiri.rb', line 34

def display_path
  @display_path ||= begin
    path = []
    path << parent.display_path if parent
    path << @node.name
    case @node
    when ::Nokogiri::XML::Element then path.join("/")
    when ::Nokogiri::XML::Attr    then path.join("@")
    else path.join("?")
    end
  end
end

#text_content?Array<:text, :element, :attribute>

Return true of all child elements are text

Returns:

  • (Array<:text, :element, :attribute>)


51
52
53
# File 'lib/json/ld/html/nokogiri.rb', line 51

def text_content?
  @node.children.all? {|c| c.text?}
end

#xpath(*args) ⇒ Object



81
82
83
84
85
86
87
88
89
# File 'lib/json/ld/html/nokogiri.rb', line 81

def xpath(*args)
  @node.xpath(*args).map do |n|
    # Get node ancestors
    parent = n.ancestors.reverse.inject(nil) do |p,node|
      NodeProxy.new(node, p)
    end
    NodeProxy.new(n, parent)
  end
end