switch to Lexbor parser

This commit is contained in:
Anton Maminov 2021-08-14 12:20:15 +03:00
parent 9dd4273f2c
commit 30c1f97114
10 changed files with 26 additions and 25 deletions

View File

@ -47,7 +47,7 @@ page = agent.post("http://example.com/", query: query)
### add query params, request_headers ### add query params, request_headers
You can add any query parameters and headers to requests. You can add any query parameters and headers to requests.
```crystal ```crystal
require "mechanize" require "mechanize"
@ -79,7 +79,7 @@ agent.get("#{web page only logged-in users can see}"
### search node ### search node
You can use css selector to search html nodes by using `#css` method. You can use css selector to search html nodes by using `#css` method.
This method is from [myhtml](https://github.com/sparklemotion/mechanize), so if you want to explore more, please refer the repository. This method is from [lexbor](https://github.com/kostya/lexbor), so if you want to explore more, please refer the repository.
```crystal ```crystal
puts page.css("h1").first.inner_text puts page.css("h1").first.inner_text

View File

@ -9,8 +9,8 @@ crystal: 1.0.0
license: MIT license: MIT
dependencies: dependencies:
myhtml: lexbor:
github: kostya/myhtml github: kostya/lexbor
development_dependencies: development_dependencies:
webmock: webmock:

View File

@ -11,7 +11,7 @@ require "./utils/element_matcher"
class MechanizeCr::Form class MechanizeCr::Form
include MechanzeCr::ElementMatcher include MechanzeCr::ElementMatcher
getter node : Node | Myhtml::Node getter node : Node | Lexbor::Node
getter fields : Array(FormContent::Field) getter fields : Array(FormContent::Field)
getter checkboxes : Array(FormContent::CheckBox) getter checkboxes : Array(FormContent::CheckBox)
getter radiobuttons : Array(FormContent::RadioButton) getter radiobuttons : Array(FormContent::RadioButton)
@ -23,7 +23,7 @@ class MechanizeCr::Form
getter page : Page? getter page : Page?
property action : String property action : String
def initialize(node : Node | Myhtml::Node, page : Page? = nil) def initialize(node : Node | Lexbor::Node, page : Page? = nil)
@enctype = node.fetch("enctype", "application/x-www-form-urlencoded") @enctype = node.fetch("enctype", "application/x-www-form-urlencoded")
@node = node @node = node
@fields = Array(FormContent::Field).new @fields = Array(FormContent::Field).new
@ -64,7 +64,7 @@ class MechanizeCr::Form
private def parse private def parse
@node.css("input").not_nil!.each do |html_node| @node.css("input").not_nil!.each do |html_node|
html_node = html_node.as(Myhtml::Node) html_node = html_node.as(Lexbor::Node)
type = (html_node["type"] || "text").downcase type = (html_node["type"] || "text").downcase
case type case type
when "checkbox" when "checkbox"
@ -92,13 +92,13 @@ class MechanizeCr::Form
# Find all textarea tags # Find all textarea tags
@node.css("textarea").each do |node| @node.css("textarea").each do |node|
node = node.as(Myhtml::Node) node = node.as(Lexbor::Node)
next if node["name"].empty? next if node["name"].empty?
@fields << FormContent::Textarea.new(node, node.inner_text) @fields << FormContent::Textarea.new(node, node.inner_text)
end end
@node.css("button").each do |node| @node.css("button").each do |node|
node = node.as(Myhtml::Node) node = node.as(Lexbor::Node)
type = node.fetch("type", "submit").downcase type = node.fetch("type", "submit").downcase
next if type == "reset" next if type == "reset"
@buttons << FormContent::Button.new(node, @node) @buttons << FormContent::Button.new(node, @node)
@ -106,7 +106,7 @@ class MechanizeCr::Form
# Find all select tags # Find all select tags
@node.css("select").each do |node| @node.css("select").each do |node|
node = node.as(Myhtml::Node) node = node.as(Lexbor::Node)
next if node["name"].empty? next if node["name"].empty?
if node.has_key?("multiple") if node.has_key?("multiple")
@selectboxes << FormContent::MultiSelectList.new(node) @selectboxes << FormContent::MultiSelectList.new(node)

View File

@ -1,6 +1,6 @@
class MechanizeCr::FormContent::Button < MechanizeCr::FormContent::Field class MechanizeCr::FormContent::Button < MechanizeCr::FormContent::Field
getter form_node : Node | Myhtml::Node getter form_node : Node | Lexbor::Node
def initialize(node : Node | Myhtml::Node, form_node : Node | Myhtml::Node, value=nil) def initialize(node : Node | Lexbor::Node, form_node : Node | Lexbor::Node, value=nil)
@form_node = form_node @form_node = form_node
super(node, value) super(node, value)
end end

View File

@ -3,9 +3,9 @@ class MechanizeCr::FormContent::Field
getter name : String getter name : String
getter type : String getter type : String
getter raw_value : String? getter raw_value : String?
getter node : Node | Myhtml::Node getter node : Node | Lexbor::Node
def initialize(node : Node | Myhtml::Node, value=nil) def initialize(node : Node | Lexbor::Node, value=nil)
@node = node @node = node
@name = node.fetch("name", "") @name = node.fetch("name", "")
@value = value || node.fetch("value", nil) @value = value || node.fetch("value", nil)
@ -17,12 +17,12 @@ class MechanizeCr::FormContent::Field
[@name, @value || ""] [@name, @value || ""]
end end
# returns DOM 'id' value # returns DOM 'id' value
def dom_id def dom_id
node.fetch("id", "") node.fetch("id", "")
end end
# returns DOM 'class' value # returns DOM 'class' value
def dom_class def dom_class
node.fetch("class", "") node.fetch("class", "")
end end

View File

@ -1,13 +1,13 @@
require "./option" require "./option"
class MechanizeCr::FormContent::MultiSelectList class MechanizeCr::FormContent::MultiSelectList
getter node : Myhtml::Node getter node : Lexbor::Node
getter name : String getter name : String
getter type : String getter type : String
property values : Array(String) property values : Array(String)
property options : Array(FormContent::Option) property options : Array(FormContent::Option)
def initialize(node : Myhtml::Node) def initialize(node : Lexbor::Node)
@node = node @node = node
@name = node.fetch("name", "") @name = node.fetch("name", "")
@type = node.fetch("type", "") @type = node.fetch("type", "")

View File

@ -1,6 +1,6 @@
class MechanizeCr::FormContent::Option class MechanizeCr::FormContent::Option
getter select_list : FormContent::MultiSelectList getter select_list : FormContent::MultiSelectList
getter node : Myhtml::Node getter node : Lexbor::Node
getter text : String getter text : String
getter value : String getter value : String
getter selected : Bool getter selected : Bool

View File

@ -1,7 +1,7 @@
class MechanizeCr::FormContent::RadioButton < MechanizeCr::FormContent::Field class MechanizeCr::FormContent::RadioButton < MechanizeCr::FormContent::Field
property :checked, :form property :checked, :form
def initialize(node : Node | Myhtml::Node, form : Form) def initialize(node : Node | Lexbor::Node, form : Form)
@checked = !!node.fetch("checked", nil) @checked = !!node.fetch("checked", nil)
@form = form @form = form
super(node) super(node)

View File

@ -1,5 +1,6 @@
require "myhtml" require "lexbor"
# This is a fake node used when sending post request.
# This is a fake node used when sending post request.
class Node < Hash(String,String) class Node < Hash(String,String)
def css(str) def css(str)
[] of Hash(String,String) [] of Hash(String,String)
@ -11,7 +12,7 @@ class Node < Hash(String,String)
end end
# This is a real Node got from html. # This is a real Node got from html.
struct Myhtml::Node struct Lexbor::Node
delegate :[], to: attributes delegate :[], to: attributes
delegate :[]=, to: attributes delegate :[]=, to: attributes
delegate :[]?, to: attributes delegate :[]?, to: attributes

View File

@ -12,8 +12,8 @@ class MechanizeCr::Page < MechanizeCr::File
super(uri, response, body, code) super(uri, response, body, code)
end end
def parser : Myhtml::Parser def parser : Lexbor::Parser
@parser ||= Myhtml::Parser.new(@body) @parser ||= Lexbor::Parser.new(@body)
end end
def title def title