Class: Html2rss::HtmlNavigator
- Inherits:
-
Object
- Object
- Html2rss::HtmlNavigator
- Defined in:
- lib/html2rss/html_navigator.rb
Overview
HtmlNavigator provides methods to navigate through HTML nodes.
Class Method Summary collapse
-
.descendant_of?(child_node, parent_node) ⇒ Boolean
Returns true if child_node is a descendant of parent_node.
-
.find_closest_selector_upwards(current_tag, selector) ⇒ Nokogiri::XML::Node?
Think of it as
css_upwardsmethod. -
.find_tag_in_ancestors(current_tag, tag_name) ⇒ Nokogiri::XML::Node?
Searches for the closest parent that matches the given tag name.
-
.parent_until_condition(node, condition) ⇒ Nokogiri::XML::Node?
Returns the first parent that satisfies the condition.
Class Method Details
.descendant_of?(child_node, parent_node) ⇒ Boolean
Returns true if child_node is a descendant of parent_node. Walks up using parent pointers to avoid NodeSet allocations.
60 61 62 63 64 65 66 67 68 |
# File 'lib/html2rss/html_navigator.rb', line 60 def descendant_of?(child_node, parent_node) curr = child_node.respond_to?(:parent) ? child_node.parent : nil while curr return true if curr == parent_node curr = curr.respond_to?(:parent) ? curr.parent : nil end false end |
.find_closest_selector_upwards(current_tag, selector) ⇒ Nokogiri::XML::Node?
Think of it as css_upwards method.
It searches for the closest parent that matches the given selector.
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/html2rss/html_navigator.rb', line 30 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) ⇒ Nokogiri::XML::Node?
Searches for the closest parent that matches the given tag name.
47 48 49 50 51 |
# File 'lib/html2rss/html_navigator.rb', line 47 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.
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 |