Class: Html2rss::HtmlNavigator

Inherits:
Object
  • Object
show all
Defined in:
lib/html2rss/html_navigator.rb

Overview

HtmlNavigator provides methods to navigate through HTML nodes.

Class Method Summary collapse

Class Method Details

.find_closest_selector_upwards(current_tag, selector) ⇒ Object

Think of it as css_upwards method. It searches for the closest parent that matches the given selector.



26
27
28
29
30
31
32
33
34
35
# File 'lib/html2rss/html_navigator.rb', line 26

def find_closest_selector_upwards(current_tag, selector)
  while current_tag
    found = current_tag.at_css(selector)
    return found if found

    return nil unless current_tag.respond_to?(:parent)

    current_tag = current_tag.parent
  end
end

.find_tag_in_ancestors(current_tag, tag_name) ⇒ Object

Searches for the closest parent that matches the given tag name.



39
40
41
42
43
# File 'lib/html2rss/html_navigator.rb', line 39

def find_tag_in_ancestors(current_tag, tag_name)
  return current_tag if current_tag.name == tag_name

  current_tag.ancestors(tag_name).first
end

.parent_until_condition(node, condition) ⇒ Nokogiri::XML::Node?

Returns the first parent that satisfies the condition. If the condition is met, it returns the node itself.

Parameters:

  • node (Nokogiri::XML::Node)

    The node to start the search from.

  • condition (Proc)

    The condition to be met.

Returns:

  • (Nokogiri::XML::Node, nil)

    The first parent that satisfies the condition.



15
16
17
18
19
20
21
# File 'lib/html2rss/html_navigator.rb', line 15

def parent_until_condition(node, condition)
  while node && !node.document? && node.name != 'html'
    return node if condition.call(node)

    node = node.parent
  end
end