Method: Nokogiri::XML::Node#text!

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

#text!String

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:



61
62
63
64
65
66
67
# File 'lib/ryoba/nokogiri/xml/node.rb', line 61

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