antchfx / htmlquery

htmlquery is golang XPath package for HTML query.
https://github.com/antchfx/xpath
MIT License
727 stars 73 forks source link

Any plans on supporting object-oriented style nodes? #8

Closed Fawers closed 5 years ago

Fawers commented 5 years ago

As of today, one must write, e.g.,

// let `root` be a *html.Node
htmlquery.SelectAttr(htmlquery.FindOne(root, `.//someelement[@id="some=id"]`), "some-attribute")

in order to retrieve the attribute some-attribute from an element. However, it would be more natural (IMO) and shorter to write, e.g.,

root.FindOne(`.//someelement[@id="some-id"]`).SelectAttr("some-attribute")

I wouldn't mind trying and submitting a PR, as I'm just a Go rookie.


Btw, thanks for the great library! :slightly_smiling_face:

zhengchun commented 5 years ago

Hello,@Fawers, thanks for your suggestion, but the html.Node is another package type, Go can't extend existing types in another package, like as .Net extension methods.

Fawers commented 5 years ago

I understand. Thought it could be done by defining an "alias" to html.Node, but maybe I'm just imagining things?

Thought something in the lines of,

type Node html.Node

func (top *Node) FindOne(expr string) *Node { ... }

I'll give it a shot when I have the time!

Fawers commented 5 years ago

Mm, seems it would require a lot of tweaking. Guess I'll just import htmlquery with a shorter name then. :joy: Thanks!