Skip to content

Commit

Permalink
Optimize the selector function
Browse files Browse the repository at this point in the history
  • Loading branch information
wspl committed Mar 2, 2017
1 parent 44ab040 commit 0a87c0f
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions fun.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type Fun struct {
Name string
Params []string

Document *goquery.Document
Selection *goquery.Selection
Result string

Expand Down Expand Up @@ -64,41 +65,47 @@ func (f *Fun) PageBody() (*goquery.Document, error) {
return goquery.NewDocumentFromReader(r)
}

func (f *Fun) InitSelector() error {
if f.Node.IsArray || f.Node.IndentLen == 0 || f.Node.Page != nil {
func (f *Fun) InitSelector(root bool) error {
var baseSel *goquery.Selection

if f.Node.Page != nil {
doc, err := f.PageBody()
if err != nil {
return err
}
bud := PowerfulFind(doc.Selection, f.Params[0])
f.Document = doc
baseSel = f.Document.Selection
} else {
f.Node.ParentNode.Fun.Invoke()
baseSel = f.Node.ParentNode.Fun.Selection
}

// overflow current page
if len(bud.Nodes) > f.Node.Index {
f.Selection = PowerfulFind(doc.Selection, f.Params[0]).Eq(f.Node.Index)
if f.Node.IsArray {
bundle := PowerfulFind(baseSel, f.Params[0])
if len(bundle.Nodes) > f.Node.Index {
f.Selection = PowerfulFind(baseSel, f.Params[0]).Eq(f.Node.Index)
} else {
// overflow current page
f.Node.Page.Inc()
f.Node.Reset()
f.InitSelector()
f.InitSelector(root)
}
} else {
_, err := f.Node.ParentNode.Fun.Invoke()
if err != nil {
return err
}
if len(f.Params) > 0 {
f.Selection = PowerfulFind(f.Node.ParentNode.Fun.Selection, f.Params[0]).Eq(f.Node.Index)
f.Selection = PowerfulFind(baseSel, f.Params[0]).Eq(f.Node.Index)
} else {
f.Selection = f.Node.ParentNode.Fun.Selection.Eq(f.Node.Index)
f.Selection = baseSel.Eq(f.Node.Index)
}
}

return nil
}

func (f *Fun) Invoke() (string, error) {
var err error
switch f.Name {
case "$":
err = f.InitSelector()
err = f.InitSelector(false)
case "attr":
f.Result, _ = f.PrevFun.Selection.Attr(f.Params[0])
case "text":
Expand Down

0 comments on commit 0a87c0f

Please sign in to comment.