Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FindOne with expression containing groups only works the first time #56

Closed
JWAlberty opened this issue Jan 10, 2023 · 2 comments
Closed
Labels

Comments

@JWAlberty
Copy link

JWAlberty commented Jan 10, 2023

If I do an expression like (//*/div) it works the first time but subsequent calls with the same expression return nil. They always return the same result however if i simply drop the paranthesis. This is problematic because I need the paranthesis do pick an element by its index (//*/div)[1]. This seems to have been introduced in the fix for #42 which was done in version 1.2.0 of the xpath library that htmlquery uses.
Here's a sample piece of code where it fails.

package main

import (
	"fmt"
	"strings"

	"github.com/antchfx/htmlquery"
	"golang.org/x/net/html"
)

func main() {
	fmt.Printf("Run 1: %#v\n", getDiv(`(//*/div)`)) // works
	fmt.Printf("Run 2: %#v\n", getDiv(`(//*/div)`)) // fails

	fmt.Printf("Run 1: %#v\n", getDiv(`//*/div`)) // works
	fmt.Printf("Run 2: %#v\n", getDiv(`//*/div`)) // also works
}

func getDiv(selector string) *html.Node {
	s := `<html><head></head><body><div>a</div></body>`
	doc, err := htmlquery.Parse(strings.NewReader(s))
	if err != nil {
		panic(err)
	}
	return htmlquery.FindOne(doc, selector)
}
@zhengchun zhengchun added the bug label Jan 11, 2023
@zhengchun
Copy link
Contributor

Thanks for report. Just update your xpath package to v1.2.2 to fix this.

https://github.com/antchfx/xpath/releases/tag/v1.2.2

@JWAlberty
Copy link
Author

@zhengchun thank you. Just tested 1.2.2 in my project and can confirm v1.2.2 fixes it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants