Module: OmniAI::Tools::Browser::InspectUtils

Included in:
InspectTool, PageInspectTool, SelectorInspectTool
Defined in:
lib/omniai/tools/browser/inspect_utils.rb

Overview

Utility methods for browser inspection tools that handle HTML document cleaning and various element searching functionalities.

Instance Method Summary collapse

Instance Method Details

#add_elements_from_matching_labels(doc, text_downcase, elements) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/omniai/tools/browser/inspect_utils.rb', line 34

def add_elements_from_matching_labels(doc, text_downcase, elements)
  label_condition = ci_contains(".//text()", text_downcase)
  matching_labels = doc.xpath("//label[#{label_condition}]")

  matching_labels.each do |label|
    for_attr = label["for"]
    next unless for_attr && !for_attr.empty?

    associated_input = doc.css("[id='#{for_attr}']")
    elements += associated_input if associated_input.any?
  end

  elements
end

#ci_contains(attribute, value) ⇒ Object



18
19
20
21
# File 'lib/omniai/tools/browser/inspect_utils.rb', line 18

def ci_contains(attribute, value)
  "contains(translate(#{attribute}, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', " \
    "'abcdefghijklmnopqrstuvwxyz'), '#{value}')"
end

#clean_document(doc) ⇒ Object



13
14
15
16
# File 'lib/omniai/tools/browser/inspect_utils.rb', line 13

def clean_document(doc)
  doc.css("link, style, script").each(&:remove)
  doc
end

#cleaned_document(html:) ⇒ Object



9
10
11
# File 'lib/omniai/tools/browser/inspect_utils.rb', line 9

def cleaned_document(html:)
  clean_document(Nokogiri::HTML(html))
end

#find_elements_with_matching_text(doc, text_downcase) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/omniai/tools/browser/inspect_utils.rb', line 23

def find_elements_with_matching_text(doc, text_downcase)
  xpath_conditions = [
    ci_contains("text()", text_downcase),
    ci_contains("@value", text_downcase),
    ci_contains("@placeholder", text_downcase),
    ci_contains("@type", text_downcase),
  ].join(" or ")

  doc.xpath("//*[#{xpath_conditions}]")
end