Class: RDF::RDFa::Reader::REXML::NodeProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/rdf/rdfa/reader/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


23
24
25
26
# File 'lib/rdf/rdfa/reader/rexml.rb', line 23

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


165
166
167
# File 'lib/rdf/rdfa/reader/rexml.rb', line 165

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

Instance Attribute Details

#nodeObject (readonly)

Returns the value of attribute node


20
21
22
# File 'lib/rdf/rdfa/reader/rexml.rb', line 20

def node
  @node
end

#parentObject (readonly)

Returns the value of attribute parent


21
22
23
# File 'lib/rdf/rdfa/reader/rexml.rb', line 21

def parent
  @parent
end

Instance Method Details

#ancestorsObject

Ancestors of this element, in order


98
99
100
# File 'lib/rdf/rdfa/reader/rexml.rb', line 98

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

#at_xpath(*args) ⇒ Object


154
155
156
# File 'lib/rdf/rdfa/reader/rexml.rb', line 154

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

#attribute_nodesObject


123
124
125
126
127
128
# File 'lib/rdf/rdfa/reader/rexml.rb', line 123

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


51
52
53
# File 'lib/rdf/rdfa/reader/rexml.rb', line 51

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

#blank?Boolean


142
143
144
# File 'lib/rdf/rdfa/reader/rexml.rb', line 142

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

#childrenNodeSetProxy

Children of this node


93
94
95
# File 'lib/rdf/rdfa/reader/rexml.rb', line 93

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

#css(path) ⇒ Object

Simple case for <script>


159
160
161
# File 'lib/rdf/rdfa/reader/rexml.rb', line 159

def css(path)
  xpath("//script[@type]")
end

#display_pathObject


55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/rdf/rdfa/reader/rexml.rb', line 55

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


138
139
140
# File 'lib/rdf/rdfa/reader/rexml.rb', line 138

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

#inner_htmlString

Inner text of an element


119
120
121
# File 'lib/rdf/rdfa/reader/rexml.rb', line 119

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

#inner_textString

Inner text of an element


107
108
109
110
111
112
# File 'lib/rdf/rdfa/reader/rexml.rb', line 107

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

#languageString

Element language

From HTML5 3.2.3.3

If both the lang attribute in no namespace and the lang attribute in the XML namespace are set
on an element, user agents must use the lang attribute in the XML namespace, and the lang
attribute in no namespace must be ignored for the purposes of determining the element's
language.

38
39
40
41
42
43
44
45
# File 'lib/rdf/rdfa/reader/rexml.rb', line 38

def language
  language = case
  when @node.attribute("lang", RDF::XML.to_s)
    @node.attribute("lang", RDF::XML.to_s)
  when @node.attribute("lang")
    @node.attribute("lang").to_s
  end
end

#namespacesHash{String => String}

Retrieve XMLNS definitions for this element


80
81
82
83
84
85
86
87
# File 'lib/rdf/rdfa/reader/rexml.rb', line 80

def namespaces
  ns_decls = {}
  @node.attributes.each do |name, attr|
    next unless name =~ /^xmlns(?:\:(.+))?/
    ns_decls[$1] = attr
  end
  ns_decls
end

#text?Boolean

Node type accessors


134
135
136
# File 'lib/rdf/rdfa/reader/rexml.rb', line 134

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

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

Return true of all child elements are text


72
73
74
# File 'lib/rdf/rdfa/reader/rexml.rb', line 72

def text_content?
  @node.children.all? {|c| c.is_a?(::REXML::Text)}
end

#to_sObject


146
# File 'lib/rdf/rdfa/reader/rexml.rb', line 146

def to_s; @node.to_s; end

#xpath(*args) ⇒ Object


148
149
150
151
152
# File 'lib/rdf/rdfa/reader/rexml.rb', line 148

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