Module: Stratagem::Crawler::HtmlUtils

Included in:
Session, SiteModel::Page, SiteModel::PageSet
Defined in:
lib/stratagem/crawler/html_utils.rb

Constant Summary collapse

INPUT_TEXT =
['text', 'password', 'hidden']
INPUT_BUTTON =
['button', 'submit', 'reset', 'image', 'src']
INPUT_TOGGLE =
['checkbox']
INPUT_RADIO =
['radio']

Instance Method Summary collapse

Instance Method Details

#find_login_form(document) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/stratagem/crawler/html_utils.rb', line 11

def (document)
  possibilities = parse_forms(document).select {|form|
    # this maps to the form action, not the controller action
    form.action =~ /log[-]*in/ ||
    form.action =~ /sign[-]*in/ ||
    begin
      password_field = form.inputs.find {|input| input.type == 'password' }
      if (password_field)
        confirmation_field = form.inputs.find {|input| input.name.include?('_confirmation') && (input.name.to_s.sub('_confirmation','') == password_field.name) }
        confirmation_field.nil?
      else
        false
      end
    end
  }.sort {|a,b| a.inputs.size <=> b.inputs.size }
  possibilities.first
end

#parse_forms(document) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/stratagem/crawler/html_utils.rb', line 29

def parse_forms(document)
  return [] if document.nil?
  
  document.xpath('//form').map do |form_tag|
    form = Form.new()
    form.action = form_load_attribute(form_tag, 'action')
    form.method = form_load_attribute(form_tag, 'method', false)  || 'post'
    (form_tag/'input').each do |input_tag|
      form_add_input(form, input_tag)
    end
    (form_tag/'textarea').each do |input_tag|
      form_add_input(form, input_tag)
    end
    (form_tag/'select').each do |select_tag|
      form_add_select(form, select_tag)
    end

    form
  end
end