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

@ -79,7 +79,7 @@ agent.get("#{web page only logged-in users can see}"
### search node
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
puts page.css("h1").first.inner_text

View File

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

View File

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

View File

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

View File

@ -3,9 +3,9 @@ class MechanizeCr::FormContent::Field
getter name : String
getter type : 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
@name = node.fetch("name", "")
@value = value || node.fetch("value", nil)

View File

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

View File

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

View File

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

View File

@ -1,4 +1,5 @@
require "myhtml"
require "lexbor"
# This is a fake node used when sending post request.
class Node < Hash(String,String)
def css(str)
@ -11,7 +12,7 @@ class Node < Hash(String,String)
end
# This is a real Node got from html.
struct Myhtml::Node
struct Lexbor::Node
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)
end
def parser : Myhtml::Parser
@parser ||= Myhtml::Parser.new(@body)
def parser : Lexbor::Parser
@parser ||= Lexbor::Parser.new(@body)
end
def title