Class: JSON::LD::API::REXML::NodeProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/json/ld/html/rexml.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.



25
26
27
28
# File 'lib/json/ld/html/rexml.rb', line 25

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



132
133
134
# File 'lib/json/ld/html/rexml.rb', line 132

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

Instance Attribute Details

#nodeObject (readonly)

Returns the value of attribute node.



23
24
25
# File 'lib/json/ld/html/rexml.rb', line 23

def node
  @node
end

#parentObject (readonly)

Returns the value of attribute parent.



23
24
25
# File 'lib/json/ld/html/rexml.rb', line 23

def parent
  @parent
end

Instance Method Details

#ancestorsObject

Ancestors of this element, in order



68
69
70
# File 'lib/json/ld/html/rexml.rb', line 68

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

#at_xpath(*args) ⇒ Object



126
127
128
# File 'lib/json/ld/html/rexml.rb', line 126

def at_xpath(*args)
  xpath(*args).first
end

#attribute_nodesObject



93
94
95
96
97
98
# File 'lib/json/ld/html/rexml.rb', line 93

def attribute_nodes
  attrs = @node.attributes.dup.keep_if do |name, _attr|
    !name.start_with?('xmlns')
  end
  @attribute_nodes ||= (attrs.empty? ? attrs : NodeSetProxy.new(attrs, self))
end

#baseString

Return xml:base on element, if defined

Returns:

  • (String)


34
35
36
# File 'lib/json/ld/html/rexml.rb', line 34

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

#blank?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/json/ld/html/rexml.rb', line 112

def blank?
  @node.is_a?(::REXML::Text) && @node.empty?
end

#childrenNodeSetProxy

Children of this node

Returns:



63
64
65
# File 'lib/json/ld/html/rexml.rb', line 63

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

#display_pathObject



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/json/ld/html/rexml.rb', line 38

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

#element?Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/json/ld/html/rexml.rb', line 108

def element?
  @node.is_a?(::REXML::Element)
end

#inner_htmlString

Inner text of an element



89
90
91
# File 'lib/json/ld/html/rexml.rb', line 89

def inner_html
  @node.children.map(&:to_s).join
end

#inner_textString

Inner text of an element



77
78
79
80
81
82
# File 'lib/json/ld/html/rexml.rb', line 77

def inner_text
  coder = HTMLEntities.new
  ::REXML::XPath.match(@node, './/text()').map do |e|
    coder.decode(e)
  end.join
end

#text?Boolean

Node type accessors

Returns:

  • (Boolean)


104
105
106
# File 'lib/json/ld/html/rexml.rb', line 104

def text?
  @node.is_a?(::REXML::Text)
end

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

Return true of all child elements are text

Returns:

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


55
56
57
# File 'lib/json/ld/html/rexml.rb', line 55

def text_content?
  @node.children.all?(::REXML::Text)
end

#to_sObject



116
117
118
# File 'lib/json/ld/html/rexml.rb', line 116

def to_s
  @node.to_s
end

#xpath(*args) ⇒ Object



120
121
122
123
124
# File 'lib/json/ld/html/rexml.rb', line 120

def xpath(*args)
  ::REXML::XPath.match(@node, *args).map do |n|
    NodeProxy.new(n, parent)
  end
end