diff --git a/doc/todo.txt b/doc/todo.txt index f87a029..ef7682d 100644 --- a/doc/todo.txt +++ b/doc/todo.txt @@ -1,5 +1,4 @@ - fix: use only 1 item content field - etc: test new parser extensively -- etc: readability strips out code comments in https://2ality.com/2021/01/looping-over-arrays.html - fix: loading items (by scrolling down) is glitching while feeds are refreshing diff --git a/src/content/htmlutil/query.go b/src/content/htmlutil/query.go index bf17644..8c88814 100644 --- a/src/content/htmlutil/query.go +++ b/src/content/htmlutil/query.go @@ -32,6 +32,16 @@ func Query(node *html.Node, sel string) []*html.Node { return FindNodes(node, matcher.Match) } +func Closest(node *html.Node, sel string) *html.Node { + matcher := NewMatcher(sel) + for cur := node; cur != nil; cur = cur.Parent { + if matcher.Match(cur) { + return cur + } + } + return nil +} + func NewMatcher(sel string) Matcher { multi := MultiMatch{} parts := strings.Split(sel, ",") diff --git a/src/content/htmlutil/query_test.go b/src/content/htmlutil/query_test.go index 60c88ac..b0ab50e 100644 --- a/src/content/htmlutil/query_test.go +++ b/src/content/htmlutil/query_test.go @@ -62,3 +62,28 @@ func TestQueryMulti(t *testing.T) { t.Fatal("incorrect match") } } + +func TestClosest(t *testing.T) { + html, _ := html.Parse(strings.NewReader(` + + +
+ +