1
0
mirror of https://github.com/MontFerret/ferret.git synced 2025-12-01 22:19:32 +02:00

Added possibility to use FOR loop in ternary expression

This commit is contained in:
Tim Voronov
2018-09-27 19:05:56 -04:00
parent ef29241aa6
commit 8b2e210317
20 changed files with 1175 additions and 561 deletions

View File

@@ -15,7 +15,6 @@ import (
"github.com/mafredri/cdp/protocol/page"
"github.com/mafredri/cdp/rpcc"
"github.com/pkg/errors"
"strings"
"sync"
"time"
)
@@ -25,7 +24,7 @@ type HtmlDocument struct {
conn *rpcc.Conn
client *cdp.Client
events *events.EventBroker
url string
url values.String
element *HtmlElement
}
@@ -132,7 +131,7 @@ func NewHtmlDocument(
doc.url = ""
if root.BaseURL != nil {
doc.url = *root.BaseURL
doc.url = values.NewString(*root.BaseURL)
}
broker.AddEventListener("load", func(_ interface{}) {
@@ -154,7 +153,7 @@ func NewHtmlDocument(
doc.url = ""
if updated.BaseURL != nil {
doc.url = *updated.BaseURL
doc.url = values.NewString(*updated.BaseURL)
}
})
@@ -176,7 +175,7 @@ func (doc *HtmlDocument) String() string {
doc.Lock()
defer doc.Unlock()
return doc.url
return doc.url.String()
}
func (doc *HtmlDocument) Unwrap() interface{} {
@@ -213,7 +212,7 @@ func (doc *HtmlDocument) Compare(other core.Value) int {
case core.HtmlDocumentType:
other := other.(*HtmlDocument)
return strings.Compare(doc.url, other.url)
return doc.url.Compare(other.url)
default:
if other.Type() > core.HtmlDocumentType {
return -1
@@ -320,6 +319,10 @@ func (doc *HtmlDocument) QuerySelectorAll(selector values.String) core.Value {
return doc.element.QuerySelectorAll(selector)
}
func (doc *HtmlDocument) Url() core.Value {
return doc.url
}
func (doc *HtmlDocument) ClickBySelector(selector values.String) (values.Boolean, error) {
res, err := eval.Eval(
doc.client,

View File

@@ -164,11 +164,17 @@ func (el *HtmlElement) MarshalJSON() ([]byte, error) {
el.Lock()
defer el.Unlock()
return json.Marshal(el.innerHtml)
val, err := el.innerText.Value()
if err != nil {
return nil, err
}
return json.Marshal(val.String())
}
func (el *HtmlElement) String() string {
return el.value.String()
return el.InnerHtml().String()
}
func (el *HtmlElement) Compare(other core.Value) int {
@@ -372,6 +378,13 @@ func (el *HtmlElement) Click() (values.Boolean, error) {
return events.DispatchEvent(ctx, el.client, el.id, "click")
}
func (el *HtmlElement) Input(value core.Value, timeout values.Int) error {
ctx, cancel := contextWithTimeout()
defer cancel()
return el.client.DOM.SetAttributeValue(ctx, dom.NewSetAttributeValueArgs(el.id, "value", value.String()))
}
func (el *HtmlElement) IsConnected() values.Boolean {
el.Lock()
defer el.Unlock()

View File

@@ -2,12 +2,13 @@ package static
import (
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
"github.com/PuerkitoBio/goquery"
)
type HtmlDocument struct {
*HtmlElement
url string
url values.String
}
func NewHtmlDocument(
@@ -28,7 +29,7 @@ func NewHtmlDocument(
return nil, err
}
return &HtmlDocument{el, url}, nil
return &HtmlDocument{el, values.NewString(url)}, nil
}
func (el *HtmlDocument) Type() core.Type {
@@ -38,8 +39,9 @@ func (el *HtmlDocument) Type() core.Type {
func (el *HtmlDocument) Compare(other core.Value) int {
switch other.Type() {
case core.HtmlDocumentType:
// TODO: complete the comparison
return -1
otherDoc := other.(values.HtmlDocument)
return el.url.Compare(otherDoc.Url())
default:
if other.Type() > core.HtmlDocumentType {
return -1
@@ -48,3 +50,7 @@ func (el *HtmlDocument) Compare(other core.Value) int {
return 1
}
}
func (el *HtmlDocument) Url() core.Value {
return el.url
}

View File

@@ -24,13 +24,7 @@ func NewHtmlElement(node *goquery.Selection) (*HtmlElement, error) {
}
func (el *HtmlElement) MarshalJSON() ([]byte, error) {
html, err := el.selection.Html()
if err != nil {
return nil, err
}
return json.Marshal(html)
return json.Marshal(el.InnerText().String())
}
func (el *HtmlElement) Type() core.Type {
@@ -38,7 +32,7 @@ func (el *HtmlElement) Type() core.Type {
}
func (el *HtmlElement) String() string {
return el.selection.Text()
return el.InnerHtml().String()
}
func (el *HtmlElement) Compare(other core.Value) int {