Module: Arbre::Html::Querying
- Included in:
- Element
- Defined in:
- lib/arbre/html/querying.rb
Overview
Adds querying to the Arbre elements.
Instance Method Summary collapse
-
#child_tags ⇒ ElementCollection
Finds all child tags of this element.
-
#descendant_tags ⇒ ElementCollection
Finds all descendant tags of this element.
-
#find(query) ⇒ Object
Finds elements by running a query.
- #find_by_classes(classes) ⇒ Object
-
#find_by_id(id) ⇒ Object
Finds an element by an ID.
- #find_by_tag(tag) ⇒ Object
-
#find_by_tag_and_classes(tag = nil, classes = nil) ⇒ Object
Finds elements by a combination of tag and / or classes.
-
#find_first(query) ⇒ Object
Finds the first element from the given query.
Instance Method Details
#child_tags ⇒ ElementCollection
Finds all child tags of this element. This operation sees through all elements that are not a tag.
25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/arbre/html/querying.rb', line 25 def result = ElementCollection.new children.each do |child| if child.is_a?(Tag) result << child else result.concat child. end end result end |
#descendant_tags ⇒ ElementCollection
Finds all descendant tags of this element. This operation sees through all elements that are not a tag.
42 43 44 45 46 47 48 49 50 51 |
# File 'lib/arbre/html/querying.rb', line 42 def result = ElementCollection.new children.each do |child| result << child if child.is_a?(Tag) result.concat child. end result end |
#find(query) ⇒ Object
Finds elements by running a query.
13 14 15 |
# File 'lib/arbre/html/querying.rb', line 13 def find(query) Query.new(self).execute(query) end |
#find_by_classes(classes) ⇒ Object
74 75 76 |
# File 'lib/arbre/html/querying.rb', line 74 def find_by_classes(classes) find_by_tag_and_classes nil, classes end |
#find_by_id(id) ⇒ Object
Finds an element by an ID. Note that only the first element with the specified ID is retrieved.
80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/arbre/html/querying.rb', line 80 def find_by_id(id) children.each do |child| found = if child.is_a?(Tag) && child.id == id child else child.find_by_id(id) end return found if found end nil end |
#find_by_tag(tag) ⇒ Object
70 71 72 |
# File 'lib/arbre/html/querying.rb', line 70 def find_by_tag(tag) find_by_tag_and_classes tag end |
#find_by_tag_and_classes(tag = nil, classes = nil) ⇒ Object
Finds elements by a combination of tag and / or classes.
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/arbre/html/querying.rb', line 54 def find_by_tag_and_classes(tag = nil, classes = nil) tag_matches = ->(el) { tag.nil? || el.tag_name == tag } classes_match = ->(el) { classes.nil? || classes.all? { |cls| el.has_class?(cls) } } found = [] children.each do |child| if child.is_a?(Tag) found << child if tag_matches[child] && classes_match[child] end found += child.find_by_tag_and_classes(tag, classes) end ElementCollection.new(found) end |
#find_first(query) ⇒ Object
Finds the first element from the given query.
18 19 20 |
# File 'lib/arbre/html/querying.rb', line 18 def find_first(query) find(query).first end |