Method: Html2rss::AutoSource::Scraper::SemanticHtml.find_tag_in_ancestors

Defined in:
lib/html2rss/auto_source/scraper/semantic_html.rb

.find_tag_in_ancestors(current_tag, tag_name, stop_tag: 'html') ⇒ Nokogiri::XML::Node

Finds the closest ancestor tag matching the specified tag name

Parameters:

  • current_tag (Nokogiri::XML::Node)

    The current tag to start searching from

  • tag_name (String)

    The tag name to search for

  • stop_tag (String) (defaults to: 'html')

    The tag name to stop searching at

Returns:

  • (Nokogiri::XML::Node)

    The found ancestor tag or the current tag if matched


50
51
52
53
54
55
56
57
58
59
60
# File 'lib/html2rss/auto_source/scraper/semantic_html.rb', line 50

def self.find_tag_in_ancestors(current_tag, tag_name, stop_tag: 'html')
  return current_tag if current_tag.name == tag_name

  stop_tags = Set[tag_name, stop_tag]

  while current_tag.respond_to?(:parent) && !stop_tags.member?(current_tag.name)
    current_tag = current_tag.parent
  end

  current_tag
end