1
0
mirror of https://github.com/MontFerret/ferret.git synced 2025-01-04 03:02:02 +02:00

Added GetParentElement to HTMLElement struct (#571)

* Added GetParentElement to HTMLElement struct

* Fixed linter issue

* Fixed formatting
This commit is contained in:
Tim Voronov 2020-11-20 23:09:12 -05:00 committed by GitHub
parent 7eed93721c
commit 8629b57fa7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 47 additions and 3 deletions

View File

@ -0,0 +1,9 @@
LET doc = DOCUMENT(@lab.cdn.dynamic + "/#/lists", { driver:"cdp" })
LET child = ELEMENT(doc, ".track-list")
LET parent = child.parentElement
T::NOT::NONE(parent)
T::EQ(parent.attributes.id, "tracks")
RETURN NONE

View File

@ -0,0 +1,10 @@
LET url = @lab.cdn.static + '/list.html'
LET doc = DOCUMENT(url)
LET child = ELEMENT(doc, ".track-list")
LET parent = child.parentElement
T::NOT::NONE(parent)
T::EQ(parent.attributes.id, "tracks")
RETURN NONE

View File

@ -575,15 +575,19 @@ func (el *HTMLElement) GetChildNode(ctx context.Context, idx values.Int) (core.V
)
}
func (el *HTMLElement) GetParentElement(ctx context.Context) (core.Value, error) {
return el.evalAndGetElement(ctx, templates.GetParent())
}
func (el *HTMLElement) GetPreviousElementSibling(ctx context.Context) (core.Value, error) {
return el.getSibling(ctx, templates.GetPreviousElementSibling())
return el.evalAndGetElement(ctx, templates.GetPreviousElementSibling())
}
func (el *HTMLElement) GetNextElementSibling(ctx context.Context) (core.Value, error) {
return el.getSibling(ctx, templates.GetNextElementSibling())
return el.evalAndGetElement(ctx, templates.GetNextElementSibling())
}
func (el *HTMLElement) getSibling(ctx context.Context, expr string) (core.Value, error) {
func (el *HTMLElement) evalAndGetElement(ctx context.Context, expr string) (core.Value, error) {
if el.IsDetached() {
return values.None, drivers.ErrDetached
}

View File

@ -0,0 +1,7 @@
package templates
const getParent = "(el) => el.parentElement"
func GetParent() string {
return getParent
}

View File

@ -198,6 +198,8 @@ func GetInElement(ctx context.Context, el drivers.HTMLElement, path []core.Value
return el.GetPreviousElementSibling(ctx)
case "nextElementSibling":
return el.GetNextElementSibling(ctx)
case "parentElement":
return el.GetParentElement(ctx)
default:
return GetInNode(ctx, el, path)
}

View File

@ -497,6 +497,16 @@ func (el *HTMLElement) Iterate(_ context.Context) (core.Iterator, error) {
return common.NewIterator(el)
}
func (el *HTMLElement) GetParentElement(_ context.Context) (core.Value, error) {
parent := el.selection.Parent()
if parent == nil {
return values.None, nil
}
return NewHTMLElement(parent)
}
func (el *HTMLElement) GetPreviousElementSibling(_ context.Context) (core.Value, error) {
sibling := el.selection.Prev()

View File

@ -97,6 +97,8 @@ type (
GetNextElementSibling(ctx context.Context) (core.Value, error)
GetParentElement(ctx context.Context) (core.Value, error)
Click(ctx context.Context, count values.Int) error
ClickBySelector(ctx context.Context, selector values.String, count values.Int) error