2019-02-20 01:10:18 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-02-21 04:24:05 +02:00
|
|
|
func (iterator *Iterator) Next(ctx context.Context) (value core.Value, key core.Value, err error) {
|
2019-02-20 01:10:18 +02:00
|
|
|
if iterator.node.Length() > iterator.pos {
|
|
|
|
idx := iterator.pos
|
2019-02-21 04:24:05 +02:00
|
|
|
val := iterator.node.GetChildNode(ctx, idx)
|
2019-02-20 01:10:18 +02:00
|
|
|
|
|
|
|
iterator.pos++
|
|
|
|
|
|
|
|
return val, idx, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return values.None, values.None, nil
|
|
|
|
}
|