1
0
mirror of https://github.com/MontFerret/ferret.git synced 2024-12-14 11:23:02 +02:00
ferret/pkg/drivers/common/iterator.go
Tim Voronov 6e15846d0f
Added Context to HTML methods (#235)
* Added Context to HTML methods

* Fixed unit tests

* Updated timeout

* Fixed WAIT_CLASS timeout
2019-02-20 21:24:05 -05:00

38 lines
739 B
Go

package common
import (
"context"
"github.com/MontFerret/ferret/pkg/drivers"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
)
type Iterator struct {
node drivers.HTMLElement
pos values.Int
}
func NewIterator(
node drivers.HTMLElement,
) (core.Iterator, error) {
if node == nil {
return nil, core.Error(core.ErrMissedArgument, "result")
}
return &Iterator{node, 0}, nil
}
func (iterator *Iterator) Next(ctx context.Context) (value core.Value, key core.Value, err error) {
if iterator.node.Length() > iterator.pos {
idx := iterator.pos
val := iterator.node.GetChildNode(ctx, idx)
iterator.pos++
return val, idx, nil
}
return values.None, values.None, nil
}