2018-12-22 06:14:41 +02:00
|
|
|
package http
|
2018-09-18 22:42:38 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2018-10-12 17:58:08 +02:00
|
|
|
"hash/fnv"
|
|
|
|
|
2018-12-22 06:14:41 +02:00
|
|
|
"github.com/MontFerret/ferret/pkg/drivers/common"
|
2018-09-18 22:42:38 +02:00
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/core"
|
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/values"
|
|
|
|
"github.com/PuerkitoBio/goquery"
|
|
|
|
)
|
|
|
|
|
2018-10-05 22:35:08 +02:00
|
|
|
type HTMLElement struct {
|
2018-09-18 22:42:38 +02:00
|
|
|
selection *goquery.Selection
|
|
|
|
attrs *values.Object
|
|
|
|
children *values.Array
|
|
|
|
}
|
|
|
|
|
2018-10-05 22:35:08 +02:00
|
|
|
func NewHTMLElement(node *goquery.Selection) (*HTMLElement, error) {
|
2018-09-18 22:42:38 +02:00
|
|
|
if node == nil {
|
|
|
|
return nil, core.Error(core.ErrMissedArgument, "element selection")
|
|
|
|
}
|
|
|
|
|
2018-10-05 22:35:08 +02:00
|
|
|
return &HTMLElement{node, nil, nil}, nil
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
2018-10-05 22:35:08 +02:00
|
|
|
func (el *HTMLElement) MarshalJSON() ([]byte, error) {
|
2018-09-28 01:05:56 +02:00
|
|
|
return json.Marshal(el.InnerText().String())
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
2018-10-05 22:35:08 +02:00
|
|
|
func (el *HTMLElement) Type() core.Type {
|
|
|
|
return core.HTMLElementType
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
2018-10-05 22:35:08 +02:00
|
|
|
func (el *HTMLElement) String() string {
|
|
|
|
return el.InnerHTML().String()
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
2018-10-05 22:35:08 +02:00
|
|
|
func (el *HTMLElement) Compare(other core.Value) int {
|
2018-09-18 22:42:38 +02:00
|
|
|
switch other.Type() {
|
2018-10-05 22:35:08 +02:00
|
|
|
case core.HTMLElementType:
|
2018-09-18 22:42:38 +02:00
|
|
|
// TODO: complete the comparison
|
|
|
|
return -1
|
|
|
|
default:
|
2018-10-05 22:35:08 +02:00
|
|
|
if other.Type() > core.HTMLElementType {
|
2018-09-18 22:42:38 +02:00
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-05 22:35:08 +02:00
|
|
|
func (el *HTMLElement) Unwrap() interface{} {
|
2018-09-18 22:42:38 +02:00
|
|
|
return el.selection
|
|
|
|
}
|
|
|
|
|
2018-10-05 23:42:28 +02:00
|
|
|
func (el *HTMLElement) Hash() uint64 {
|
2018-09-18 22:42:38 +02:00
|
|
|
str, err := el.selection.Html()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2018-10-05 21:17:22 +02:00
|
|
|
h := fnv.New64a()
|
2018-09-18 22:42:38 +02:00
|
|
|
|
2018-10-05 21:17:22 +02:00
|
|
|
h.Write([]byte(el.Type().String()))
|
|
|
|
h.Write([]byte(":"))
|
|
|
|
h.Write([]byte(str))
|
2018-09-18 22:42:38 +02:00
|
|
|
|
2018-10-05 21:17:22 +02:00
|
|
|
return h.Sum64()
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
2018-10-12 17:58:08 +02:00
|
|
|
func (el *HTMLElement) Copy() core.Value {
|
2018-10-05 22:35:08 +02:00
|
|
|
c, _ := NewHTMLElement(el.selection.Clone())
|
2018-09-27 17:53:26 +02:00
|
|
|
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2018-10-05 22:35:08 +02:00
|
|
|
func (el *HTMLElement) NodeType() values.Int {
|
2018-09-18 22:42:38 +02:00
|
|
|
nodes := el.selection.Nodes
|
|
|
|
|
|
|
|
if len(nodes) == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2018-10-06 01:40:09 +02:00
|
|
|
return values.NewInt(common.ToHTMLType(nodes[0].Type))
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
2018-10-05 22:35:08 +02:00
|
|
|
func (el *HTMLElement) NodeName() values.String {
|
2018-09-18 22:42:38 +02:00
|
|
|
return values.NewString(goquery.NodeName(el.selection))
|
|
|
|
}
|
|
|
|
|
2018-10-05 22:35:08 +02:00
|
|
|
func (el *HTMLElement) Length() values.Int {
|
2018-09-18 22:42:38 +02:00
|
|
|
if el.children == nil {
|
|
|
|
el.children = el.parseChildren()
|
|
|
|
}
|
|
|
|
|
|
|
|
return el.children.Length()
|
|
|
|
}
|
|
|
|
|
2018-10-05 22:35:08 +02:00
|
|
|
func (el *HTMLElement) Value() core.Value {
|
2018-09-18 22:42:38 +02:00
|
|
|
val, ok := el.selection.Attr("value")
|
|
|
|
|
|
|
|
if ok {
|
|
|
|
return values.NewString(val)
|
|
|
|
}
|
|
|
|
|
|
|
|
return values.EmptyString
|
|
|
|
}
|
|
|
|
|
2018-10-05 22:35:08 +02:00
|
|
|
func (el *HTMLElement) InnerText() values.String {
|
2018-09-18 22:42:38 +02:00
|
|
|
return values.NewString(el.selection.Text())
|
|
|
|
}
|
|
|
|
|
2018-10-05 22:35:08 +02:00
|
|
|
func (el *HTMLElement) InnerHTML() values.String {
|
2018-09-18 22:42:38 +02:00
|
|
|
h, err := el.selection.Html()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.EmptyString
|
|
|
|
}
|
|
|
|
|
|
|
|
return values.NewString(h)
|
|
|
|
}
|
|
|
|
|
2018-10-05 22:35:08 +02:00
|
|
|
func (el *HTMLElement) GetAttributes() core.Value {
|
2018-09-18 22:42:38 +02:00
|
|
|
if el.attrs == nil {
|
|
|
|
el.attrs = el.parseAttrs()
|
|
|
|
}
|
|
|
|
|
|
|
|
return el.attrs
|
|
|
|
}
|
|
|
|
|
2018-10-05 22:35:08 +02:00
|
|
|
func (el *HTMLElement) GetAttribute(name values.String) core.Value {
|
2018-09-18 22:42:38 +02:00
|
|
|
v, ok := el.selection.Attr(name.String())
|
|
|
|
|
|
|
|
if ok {
|
|
|
|
return values.NewString(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
return values.None
|
|
|
|
}
|
|
|
|
|
2018-10-05 22:35:08 +02:00
|
|
|
func (el *HTMLElement) GetChildNodes() core.Value {
|
2018-09-18 22:42:38 +02:00
|
|
|
if el.children == nil {
|
|
|
|
el.children = el.parseChildren()
|
|
|
|
}
|
|
|
|
|
|
|
|
return el.children
|
|
|
|
}
|
|
|
|
|
2018-10-05 22:35:08 +02:00
|
|
|
func (el *HTMLElement) GetChildNode(idx values.Int) core.Value {
|
2018-09-18 22:42:38 +02:00
|
|
|
if el.children == nil {
|
|
|
|
el.children = el.parseChildren()
|
|
|
|
}
|
|
|
|
|
|
|
|
return el.children.Get(idx)
|
|
|
|
}
|
|
|
|
|
2018-10-05 22:35:08 +02:00
|
|
|
func (el *HTMLElement) QuerySelector(selector values.String) core.Value {
|
2018-09-18 22:42:38 +02:00
|
|
|
selection := el.selection.Find(selector.String())
|
|
|
|
|
|
|
|
if selection == nil {
|
|
|
|
return values.None
|
|
|
|
}
|
|
|
|
|
2018-10-05 22:35:08 +02:00
|
|
|
res, err := NewHTMLElement(selection)
|
2018-09-18 22:42:38 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.None
|
|
|
|
}
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2018-10-05 22:35:08 +02:00
|
|
|
func (el *HTMLElement) QuerySelectorAll(selector values.String) core.Value {
|
2018-09-18 22:42:38 +02:00
|
|
|
selection := el.selection.Find(selector.String())
|
|
|
|
|
|
|
|
if selection == nil {
|
|
|
|
return values.None
|
|
|
|
}
|
|
|
|
|
|
|
|
arr := values.NewArray(selection.Length())
|
|
|
|
|
|
|
|
selection.Each(func(i int, selection *goquery.Selection) {
|
2018-10-05 22:35:08 +02:00
|
|
|
el, err := NewHTMLElement(selection)
|
2018-09-18 22:42:38 +02:00
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
arr.Push(el)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return arr
|
|
|
|
}
|
|
|
|
|
2018-10-09 02:20:40 +02:00
|
|
|
func (el *HTMLElement) InnerHTMLBySelector(selector values.String) values.String {
|
|
|
|
selection := el.selection.Find(selector.String())
|
|
|
|
|
|
|
|
str, err := selection.Html()
|
|
|
|
|
|
|
|
// TODO: log error
|
|
|
|
if err != nil {
|
|
|
|
return values.EmptyString
|
|
|
|
}
|
|
|
|
|
|
|
|
return values.NewString(str)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (el *HTMLElement) InnerHTMLBySelectorAll(selector values.String) *values.Array {
|
|
|
|
selection := el.selection.Find(selector.String())
|
|
|
|
arr := values.NewArray(selection.Length())
|
|
|
|
|
|
|
|
selection.Each(func(_ int, selection *goquery.Selection) {
|
|
|
|
str, err := selection.Html()
|
|
|
|
|
|
|
|
// TODO: log error
|
|
|
|
if err == nil {
|
|
|
|
arr.Push(values.NewString(str))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return arr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (el *HTMLElement) InnerTextBySelector(selector values.String) values.String {
|
|
|
|
selection := el.selection.Find(selector.String())
|
|
|
|
|
|
|
|
return values.NewString(selection.Text())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (el *HTMLElement) InnerTextBySelectorAll(selector values.String) *values.Array {
|
|
|
|
selection := el.selection.Find(selector.String())
|
|
|
|
arr := values.NewArray(selection.Length())
|
|
|
|
|
|
|
|
selection.Each(func(_ int, selection *goquery.Selection) {
|
|
|
|
arr.Push(values.NewString(selection.Text()))
|
|
|
|
})
|
|
|
|
|
|
|
|
return arr
|
|
|
|
}
|
|
|
|
|
2018-10-13 03:52:27 +02:00
|
|
|
func (el *HTMLElement) CountBySelector(selector values.String) values.Int {
|
|
|
|
selection := el.selection.Find(selector.String())
|
|
|
|
|
|
|
|
if selection == nil {
|
|
|
|
return values.ZeroInt
|
|
|
|
}
|
|
|
|
|
|
|
|
return values.NewInt(selection.Size())
|
|
|
|
}
|
|
|
|
|
2018-12-22 06:14:41 +02:00
|
|
|
func (el *HTMLElement) ExistsBySelector(selector values.String) values.Boolean {
|
|
|
|
selection := el.selection.Closest(selector.String())
|
|
|
|
|
|
|
|
if selection == nil {
|
|
|
|
return values.False
|
|
|
|
}
|
|
|
|
|
|
|
|
return values.True
|
|
|
|
}
|
|
|
|
|
2018-10-05 22:35:08 +02:00
|
|
|
func (el *HTMLElement) parseAttrs() *values.Object {
|
2018-09-18 22:42:38 +02:00
|
|
|
obj := values.NewObject()
|
|
|
|
|
|
|
|
for _, name := range common.Attributes {
|
|
|
|
val, ok := el.selection.Attr(name)
|
|
|
|
|
|
|
|
if ok {
|
|
|
|
obj.Set(values.NewString(name), values.NewString(val))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return obj
|
|
|
|
}
|
|
|
|
|
2018-10-05 22:35:08 +02:00
|
|
|
func (el *HTMLElement) parseChildren() *values.Array {
|
2018-09-18 22:42:38 +02:00
|
|
|
children := el.selection.Children()
|
|
|
|
|
|
|
|
arr := values.NewArray(10)
|
|
|
|
|
|
|
|
children.Each(func(i int, selection *goquery.Selection) {
|
|
|
|
//name := goquery.NodeName(selection)
|
|
|
|
//if name != "#text" && name != "#comment" {
|
2018-10-05 22:35:08 +02:00
|
|
|
// child, err := NewHTMLElement(selection)
|
2018-09-18 22:42:38 +02:00
|
|
|
//
|
|
|
|
// if err == nil {
|
|
|
|
// arr.Push(child)
|
|
|
|
// }
|
|
|
|
//}
|
|
|
|
|
2018-10-05 22:35:08 +02:00
|
|
|
child, err := NewHTMLElement(selection)
|
2018-09-18 22:42:38 +02:00
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
arr.Push(child)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return arr
|
|
|
|
}
|