Module: OmniAI::Tools::Browser::Elements::ElementGrouper

Defined in:
lib/omniai/tools/browser/elements/element_grouper.rb

Overview

Groups HTML elements by their relevance for data entry

Class Method Summary collapse

Class Method Details

.categorize_element(element, groups, text) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/omniai/tools/browser/elements/element_grouper.rb', line 26

def categorize_element(element, groups, text)
  case element.name.downcase
  when "input"
    categorize_input_element(element, groups)
  when "textarea", "select"
    groups[:inputs] << element
  when "button"
    groups[:actions] << element if contains_text_match?(element, text)
  when "label", "span", "div", "th"
    groups[:labels] << element if contains_text_match?(element, text)
  end
end

.categorize_input_element(element, groups) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/omniai/tools/browser/elements/element_grouper.rb', line 39

def categorize_input_element(element, groups)
  if data_entry_input?(element)
    groups[:inputs] << element
  else
    group_form_control(element, groups[:form_controls])
  end
end

.contains_text_match?(element, text) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
68
# File 'lib/omniai/tools/browser/elements/element_grouper.rb', line 65

def contains_text_match?(element, text)
  element.text.downcase.include?(text.downcase) ||
    element["value"]&.downcase&.include?(text.downcase)
end

.data_entry_input?(element) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
50
51
52
53
54
# File 'lib/omniai/tools/browser/elements/element_grouper.rb', line 47

def data_entry_input?(element)
  # Handle missing type attribute - HTML default is "text"
  type = (element["type"] || "text").downcase

  # Include common data entry input types
  %w[text number email tel url date datetime-local time month week
     password search].include?(type)
end

.group_for_data_entry(elements, text) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/omniai/tools/browser/elements/element_grouper.rb', line 11

def group_for_data_entry(elements, text)
  groups = {
    inputs: [], # text, number, email inputs - primary targets
    form_controls: {}, # radio buttons, checkboxes grouped by name
    labels: [], # labels, headers, spans
    actions: [], # buttons and actionable elements
  }

  elements.each do |element|
    categorize_element(element, groups, text)
  end

  groups
end

.group_form_control(element, form_controls) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/omniai/tools/browser/elements/element_grouper.rb', line 56

def group_form_control(element, form_controls)
  type = (element["type"] || "text").downcase
  return unless %w[radio checkbox].include?(type)

  name = element["name"] || "unnamed"
  form_controls[name] ||= []
  form_controls[name] << element
end