Class: Nokogiri::XML::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/ryoba/nokogiri/xml/node.rb

Constant Summary collapse

HTML_ELEMENT_URI_ATTRIBUTES =
{
  "a" => "href",
  "img" => "src",
  "form" => "action",
}

Instance Method Summary collapse

Instance Method Details

#content!String Also known as: inner_text!, text!

Equivalent to .content.strip, but raises an error if the result is an empty string.

Examples:

xml = Nokogiri::XML(<<-XML)
  <body>
    <h1>
      Headline 1
    </h1>
    <h2> Headline 2 </h2>
    <h3> </h3>
  </body>
XML

xml.at("h1").content!  # == "Headline 1"
xml.at("h2").content!  # == "Headline 2"
xml.at("h3").content!  # raise error

Returns:

  • (String)

Raises:



52
53
54
55
56
57
58
# File 'lib/ryoba/nokogiri/xml/node.rb', line 52

def content!
  result = self.content.strip
  if result.empty?
    raise Ryoba::Error.new("No text in:\n#{self.to_html}")
  end
  result
end

#content?String? Also known as: inner_text?

Equivalent to .content.strip, but returns nil if the result is an empty string.

Examples:

xml = Nokogiri::XML(<<-XML)
  <body>
    <h1>
      Headline 1
    </h1>
    <h2> Headline 2 </h2>
    <h3> </h3>
  </body>
XML

xml.at("h1").content?  # == "Headline 1"
xml.at("h2").content?  # == "Headline 2"
xml.at("h3").content?  # == nil

Returns:

  • (String, nil)


24
25
26
27
# File 'lib/ryoba/nokogiri/xml/node.rb', line 24

def content?
  result = self.content.strip
  result unless result.empty?
end

#matches!(selector) ⇒ self

Like Node#matches?, but returns the Node if selector matches, and raises an error otherwise.

Examples:

xml = Nokogiri::XML(<<-XML)
  <body>
    <div id="a" class="c" />
    <div id="b" />
  </body>
XML

xml.at("#a").matches(".c")!  # == Node div#a.c
xml.at("#b").matches(".c")!  # raise error

Parameters:

  • selector (String)

Returns:

  • (self)

Raises:



81
82
83
84
85
86
87
# File 'lib/ryoba/nokogiri/xml/node.rb', line 81

def matches!(selector)
  if !self.matches?(selector)
    abbreviated = self.to_html[/[^>]+>/]
    raise Ryoba::Error.new("Node #{abbreviated} does not match #{selector.inspect}")
  end
  self
end

#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