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

Inherits:
Object
  • Object
show all
Defined in:
vendor/bundler/ruby/3.3.0/bundler/gems/rdf-rdfa-fb11c7e2d467/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

Returns a new instance of NodeProxy.



23
24
25
26
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-rdfa-fb11c7e2d467/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



167
168
169
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-rdfa-fb11c7e2d467/lib/rdf/rdfa/reader/rexml.rb', line 167

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 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-rdfa-fb11c7e2d467/lib/rdf/rdfa/reader/rexml.rb', line 20

def node
  @node
end

#parentObject (readonly)

Returns the value of attribute parent.



21
22
23
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-rdfa-fb11c7e2d467/lib/rdf/rdfa/reader/rexml.rb', line 21

def parent
  @parent
end

Instance Method Details

#ancestorsObject

Ancestors of this element, in order



100
101
102
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-rdfa-fb11c7e2d467/lib/rdf/rdfa/reader/rexml.rb', line 100

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

#at_xpath(*args) ⇒ Object



156
157
158
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-rdfa-fb11c7e2d467/lib/rdf/rdfa/reader/rexml.rb', line 156

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

#attribute_nodesObject



125
126
127
128
129
130
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-rdfa-fb11c7e2d467/lib/rdf/rdfa/reader/rexml.rb', line 125

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:



53
54
55
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-rdfa-fb11c7e2d467/lib/rdf/rdfa/reader/rexml.rb', line 53

def base
  @node.attribute("base", "http://www.w3.org/XML/1998/namespace") || @node.attribute('xml:base')
end

#blank?Boolean

Returns:

  • (Boolean)


144
145
146
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-rdfa-fb11c7e2d467/lib/rdf/rdfa/reader/rexml.rb', line 144

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

#childrenNodeSetProxy

Children of this node

Returns:



95
96
97
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-rdfa-fb11c7e2d467/lib/rdf/rdfa/reader/rexml.rb', line 95

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

#css(path) ⇒ Object

Simple case for <script>



161
162
163
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-rdfa-fb11c7e2d467/lib/rdf/rdfa/reader/rexml.rb', line 161

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

#display_pathObject



57
58
59
60
61
62
63
64
65
66
67
68
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-rdfa-fb11c7e2d467/lib/rdf/rdfa/reader/rexml.rb', line 57

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)


140
141
142
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-rdfa-fb11c7e2d467/lib/rdf/rdfa/reader/rexml.rb', line 140

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

#inner_htmlString

Inner text of an element



121
122
123
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-rdfa-fb11c7e2d467/lib/rdf/rdfa/reader/rexml.rb', line 121

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

#inner_textString

Inner text of an element



109
110
111
112
113
114
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-rdfa-fb11c7e2d467/lib/rdf/rdfa/reader/rexml.rb', line 109

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.

Returns:



38
39
40
41
42
43
44
45
46
47
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-rdfa-fb11c7e2d467/lib/rdf/rdfa/reader/rexml.rb', line 38

def language
  case
  when @node.attribute("lang", "http://www.w3.org/XML/1998/namespace")
    @node.attribute("lang", "http://www.w3.org/XML/1998/namespace")
  when @node.attribute("xml:lang")
    @node.attribute("xml:lang").to_s
  when @node.attribute("lang")
    @node.attribute("lang").to_s
  end
end

#namespacesHash{String => String}

Retrieve XMLNS definitions for this element

Returns:



82
83
84
85
86
87
88
89
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-rdfa-fb11c7e2d467/lib/rdf/rdfa/reader/rexml.rb', line 82

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

Returns:

  • (Boolean)


136
137
138
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-rdfa-fb11c7e2d467/lib/rdf/rdfa/reader/rexml.rb', line 136

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>)


74
75
76
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-rdfa-fb11c7e2d467/lib/rdf/rdfa/reader/rexml.rb', line 74

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

#to_sObject



148
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-rdfa-fb11c7e2d467/lib/rdf/rdfa/reader/rexml.rb', line 148

def to_s; @node.to_s; end

#xpath(*args) ⇒ Object



150
151
152
153
154
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-rdfa-fb11c7e2d467/lib/rdf/rdfa/reader/rexml.rb', line 150

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