Skip to content

Commit

Permalink
Refactor (#237)
Browse files Browse the repository at this point in the history
* Refactor

* Refactor

* Add unit tests

* Refactor

* Add test
  • Loading branch information
raviqqe authored Aug 1, 2022
1 parent ff097ab commit 82068a2
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
15 changes: 8 additions & 7 deletions link_finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,20 @@ func (f linkFinder) Find(n *html.Node, base *url.URL) map[string]error {
for _, s := range ss {
s := strings.TrimSpace(s)

if s == "" {
continue
}

u, err := url.Parse(s)
if err != nil {
ls[s] = err
continue
}
fullurl := base.ResolveReference(u).String()

if s == "" || f.isLinkExcluded(fullurl) || len(f.includedPatterns) > 0 && !f.isLinkIncluded(fullurl) {
continue
}
s = base.ResolveReference(u).String()

if _, ok := validSchemes[u.Scheme]; ok {
ls[fullurl] = nil
if _, ok := validSchemes[u.Scheme]; ok && !f.isLinkExcluded(s) && f.isLinkIncluded(s) {
ls[s] = nil
}
}
}
Expand Down Expand Up @@ -92,7 +93,7 @@ func (f linkFinder) isLinkExcluded(u string) bool {
}

func (f linkFinder) isLinkIncluded(u string) bool {
return f.matches(u, f.includedPatterns)
return len(f.includedPatterns) == 0 || f.matches(u, f.includedPatterns)
}

func (f linkFinder) matches(u string, rs []*regexp.Regexp) bool {
Expand Down
27 changes: 27 additions & 0 deletions link_finder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func TestLinkFinderFindLinks(t *testing.T) {
linkCount int
}{
{``, 0},
{`<a href="" />`, 0},
{`<a href="/" />`, 1},
{`<a href="/foo" />`, 1},
// TODO: Test <frame> tag.
Expand Down Expand Up @@ -170,6 +171,32 @@ func TestLinkFinderIsLinkIncluded(t *testing.T) {
}
}

func TestLinkFinderExcludeEntireUrl(t *testing.T) {
b, err := url.Parse("http://foo.com")
assert.Nil(t, err)

n, err := html.Parse(strings.NewReader(htmlWithBody(`<a href="/bar" />`)))
assert.Nil(t, err)

rs, err := compileRegexps([]string{"foo"})
assert.Nil(t, err)

assert.Equal(t, map[string]error{}, newLinkFinder(rs, nil).Find(n, b))
}

func TestLinkFinderIncludeEntireUrl(t *testing.T) {
b, err := url.Parse("http://foo.com")
assert.Nil(t, err)

n, err := html.Parse(strings.NewReader(htmlWithBody(`<a href="/bar" />`)))
assert.Nil(t, err)

rs, err := compileRegexps([]string{"foo"})
assert.Nil(t, err)

assert.Equal(t, map[string]error{"http://foo.com/bar": nil}, newLinkFinder(nil, rs).Find(n, b))
}

func TestLinkFinderFindLinkInSrcSet(t *testing.T) {
b, err := url.Parse("http://foo.com")
assert.Nil(t, err)
Expand Down

0 comments on commit 82068a2

Please sign in to comment.