Method: Nokogiri::XML::Node#uri

Defined in:
lib/ryoba/nokogiri/xml/node.rb

#uri(attribute_name = nil) ⇒ URI?

Builds a URI from a specified attribute. If no attribute is specified, an element-appropriate attribute will be chosen from HTML_ELEMENT_URI_ATTRIBUTES, if possible. Relative URIs will be converted to absolute URIs using the Node document’s url, if possible.

Examples:

xml = Nokogiri::XML(<<-XML, "http://localhost/qux/")
  <body>
    <a href="https://www.example.com/foo">FOO</a>
    <img src="/bar" />
    <div data-src="baz" />
    <p>blah</p>
  </body>
XML

xml.at("a").uri                # == URI("https://www.example.com/foo")
xml.at("img").uri              # == URI("http://localhost/bar")
xml.at("div").uri("data-src")  # == URI("http://localhost/qux/baz")
xml.at("p").uri                # == nil

Parameters:

  • attribute_name (String) (defaults to: nil)

Returns:

  • (URI, nil)


118
119
120
121
122
# File 'lib/ryoba/nokogiri/xml/node.rb', line 118

def uri(attribute_name = nil)
  attribute_name ||= HTML_ELEMENT_URI_ATTRIBUTES[self.name]
  url = self[attribute_name]
  URI.join(*self.document.url, url) if url
end