all repos — rel2abs @ 2fcd126dea001db07fde887da91745586af517e9

go library to convert all relative urls in a html document to absolute ones

Fix href not getting modified
Anirudh Oppiliappan x@icyphox.sh
Wed, 29 Sep 2021 21:51:46 +0530
commit

2fcd126dea001db07fde887da91745586af517e9

parent

d12532c24732837934a5e9987e7614764c01ca93

1 files changed, 9 insertions(+), 10 deletions(-)

jump to
M rel2abs.gorel2abs.go

@@ -6,25 +6,24 @@ "fmt"

"net/url" "golang.org/x/net/html" + "golang.org/x/net/html/atom" ) func rel2abs(n *html.Node, nurl *url.URL) error { - if n.Type == html.ElementNode && n.Data == "a" { - for _, a := range n.Attr { - if a.Key == "href" { - rel, err := url.Parse(a.Val) - fmt.Println("rel:", rel) + if n.Type == html.ElementNode && n.DataAtom == atom.A { + for i := range n.Attr { + if n.Attr[i].Key == "href" { + rel, err := url.Parse(n.Attr[i].Val) if err != nil { return fmt.Errorf("relative url: %w\n", err) } - a.Val = nurl.ResolveReference(rel).String() - fmt.Println("resolved:", a.Val) + n.Attr[i].Val = nurl.ResolveReference(rel).String() } } - for c := n.FirstChild; c != nil; c = c.NextSibling { - rel2abs(c, nurl) - } + } + for c := n.FirstChild; c != nil; c = c.NextSibling { + rel2abs(c, nurl) } return nil }