anaskhan96/soup

Navigating to Parent

joekinley opened this issue · 2 comments

In order for proper selection it would be awesome to be able to navigate to the current elements Parent, then keep going through siblings and all.

Right now it is quite hard to properly find what I am looking for from a strict top-down view.

Good idea. Will add something along the lines of Root.Parent to access the parent element

sijms commented

I make a function you can review it if it is ok you can add to your code

func (r Root) FindParent(tagName string) Root {
	parent := r.Pointer.Parent
	if parent == nil {
		return Root{Pointer: parent}
	}
	rParent := Root{Pointer: parent, NodeValue: parent.Data}
	if strings.ToLower(parent.Data) == strings.ToLower(tagName) {
		return rParent
	}
	return rParent.FindParent(tagName)
}