From 2813e989a9a8015ad04950aa0dd7e768fe571fae Mon Sep 17 00:00:00 2001 From: Kanezoh Date: Wed, 26 May 2021 14:46:50 +0900 Subject: [PATCH] modeling node --- main.cr | 11 ++++++----- src/mechanize/form.cr | 4 +++- src/mechanize/node.cr | 12 ++++++++++-- src/mechanize/page.cr | 15 ++++++++++++++- 4 files changed, 33 insertions(+), 9 deletions(-) diff --git a/main.cr b/main.cr index c5c64f2..5e3c7b2 100644 --- a/main.cr +++ b/main.cr @@ -2,11 +2,12 @@ require "./src/mechanize.cr" agent = Mechanize.new agent.request_headers = HTTP::Headers{"Foo" => "Bar"} -#params = {"hoge" => "hoge"} -#page = agent.get("http://example.com/", params: params) -query = {"foo" => "foo_value", "bar" => "bar_value"} -page = agent.post("http://example.com/", query: query) +params = {"hoge" => "hoge"} +page = agent.get("https://kowabana.jp/users/sign_in/", params: params) +page.forms +#query = {"foo" => "foo_value", "bar" => "bar_value"} +#page = agent.post("http://example.com/", query: query) #puts page.code -puts page.body +#puts page.body #puts page.css("h1").first.inner_text #puts page.title diff --git a/src/mechanize/form.cr b/src/mechanize/form.cr index a2f2c52..890377b 100644 --- a/src/mechanize/form.cr +++ b/src/mechanize/form.cr @@ -5,13 +5,15 @@ class MechanizeCr::Form getter fields : Array(MechanizeCr::FormContent::Field) getter checkboxes : Array(MechanizeCr::FormContent::CheckBox) getter enctype : String + property action : String def initialize(node : Node | Myhtml::Node) - @enctype = node["enctype"] || "application/x-www-form-urlencoded" + @enctype = node["enctype"]? ? node["enctype"] : "application/x-www-form-urlencoded" @node = node @fields = Array(MechanizeCr::FormContent::Field).new @checkboxes = Array(MechanizeCr::FormContent::CheckBox).new #@action = Mechanize::Util.html_unescape(node['action']) + @action = node["action"] #@method = (node['method'] || 'GET').upcase #@name = node['name'] #@clicked_buttons = [] diff --git a/src/mechanize/node.cr b/src/mechanize/node.cr index 5591286..6b8e50d 100644 --- a/src/mechanize/node.cr +++ b/src/mechanize/node.cr @@ -1,8 +1,16 @@ -# This is a fake node. -# Real node is represented by Myhtml::Node +require "myhtml" +# This is a fake node. class Node < Hash(String,String) def css(str) [] of Hash(String,String) end end + + +# This is a real Node. +struct Myhtml::Node + delegate :[], to: attributes + delegate :[]=, to: attributes + delegate :[]?, to: attributes +end diff --git a/src/mechanize/page.cr b/src/mechanize/page.cr index 75a2447..cb2fede 100644 --- a/src/mechanize/page.cr +++ b/src/mechanize/page.cr @@ -1,4 +1,3 @@ -require "myhtml" require "./file" class MechanizeCr::Page < MechanizeCr::File @@ -15,4 +14,18 @@ class MechanizeCr::Page < MechanizeCr::File def title title = parser.css("title").first.inner_text end + + def forms + #@forms ||= css("form").map do |html_form| + # form = Mechanize::Form.new(html_form, @mech, self) + # form.attributes["action"]# ||= @uri.to_s + # form + #end + + forms = css("form").each do |html_form| + form = MechanizeCr::Form.new(html_form) + puts form.action# ||= @uri.to_s + form + end + end end