Class: Nokogiri::XML::Node
- Inherits:
-
Object
- Object
- Nokogiri::XML::Node
- 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
-
#content! ⇒ String
(also: #inner_text!, #text!)
Equivalent to
.content.strip
, but raises an error if the result is an empty string. -
#content? ⇒ String?
(also: #inner_text?)
Equivalent to
.content.strip
, but returns nil if the result is an empty string. -
#matches!(selector) ⇒ self
Like Node#matches?, but returns the Node if
selector
matches, and raises an error otherwise. -
#uri(attribute_name = nil) ⇒ URI?
Builds a URI from a specified attribute.
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.
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.
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.
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.
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 |