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

Find returns an element when passed an invalid xpath #52

Closed
iambodi opened this issue Sep 14, 2022 · 3 comments
Closed

Find returns an element when passed an invalid xpath #52

iambodi opened this issue Sep 14, 2022 · 3 comments
Labels

Comments

@iambodi
Copy link

iambodi commented Sep 14, 2022

package main

import (
	"fmt"
	"strings"

	"github.com/antchfx/htmlquery"
)

const html = `<html>
<body>
<div>
    <ul id="food">
        <li>avocado</li>
    </ul>
</div>
</body>
</html>`

const xpath = `//*[contains(@id,"food")]//*[contains(@id,"food")]//*[contains(text(),"avocado")]`

func main() {
	doc, err := htmlquery.Parse(strings.NewReader(html))
	if err != nil {
		panic(err)
	}

	list := htmlquery.Find(doc, xpath)

	node := list[0]
	fmt.Println(node.Data, node.FirstChild.Data, node.Parent.Data)
}

Hi,

I have an issue with the Find method. When passed an invalid xpath (confirmed by the HTML inspector of Chrome and Firefox), it returns a node.

On playground: https://go.dev/play/p/3Z6jtSNsYfx

Thank you !

@zhengchun zhengchun added the bug label Sep 14, 2022
@zhengchun
Copy link
Contributor

the second //*[contains(@id,"food")] including current self node when searching matches node, the correct should only process child of matched node by the first //*[contains(@id,"food")], not self. That is why your xpath can return a matched node.

@iambodi
Copy link
Author

iambodi commented Sep 19, 2022

Thank you for your quick response !
Ok, so just to be sure : this is a bug, right ? If so, do you think it could be fixed in the near future ? I took a look at the code but couldn’t find what’s causing this behavior.

@zhengchun
Copy link
Contributor

checkout antchfx/xpath@ba368a6 to fix.

You can re-open it if not fix.

zhengchun added a commit to antchfx/xpath that referenced this issue Apr 2, 2024
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