Repository: raviqqe/muffet (go)

Issue:
Recognize <a href="#name"> as creating a valid link target
Muffet complains `id #mozTocId468501 not found` when it sees my site link to:

http://www.clearskyinstitute.com/xephem/help/xephem.html#mozTocId468501

Yet the link works, jumping to the correct section of the page. It appears that muffet might not yet know about link targets created with `<a href="#name">`? Adding that knowledge would help reduce its noise when run against links whose sites use them. Thanks!

Pull request description:
Recognize names as fragments
Close #66


Patch:
diff --git a/configuration.go b/configuration.go
index f6177dd..f122f64 100644
--- a/configuration.go
+++ b/configuration.go
@@ -3,7 +3,7 @@ package main
 import "time"
 
 const (
-	version                = "1.4.0"
+	version                = "1.5.0"
 	defaultBufferSize      = 4096
 	defaultConcurrency     = 512
 	defaultMaxRedirections = 64
diff --git a/link_fetcher.go b/link_fetcher.go
index ea0bad8..c160dae 100644
--- a/link_fetcher.go
+++ b/link_fetcher.go
@@ -35,7 +35,7 @@ func (f *linkFetcher) Fetch(u string) (int, *page, error) {
 		return 0, nil, err
 	} else if p == nil || f.options.IgnoreFragments || fr == "" {
 		return s, p, nil
-	} else if _, ok := p.IDs()[fr]; !ok {
+	} else if _, ok := p.Fragments()[fr]; !ok {
 		return 0, nil, fmt.Errorf("id #%v not found", fr)
 	}
 
diff --git a/page.go b/page.go
index b29bb1c..2e23d05 100644
--- a/page.go
+++ b/page.go
@@ -5,21 +5,21 @@ import (
 )
 
 type page struct {
-	url   *url.URL
-	ids   map[string]struct{}
-	links map[string]error
+	url       *url.URL
+	fragments map[string]struct{}
+	links     map[string]error
 }
 
-func newPage(u *url.URL, ids map[string]struct{}, links map[string]error) *page {
-	return &page{u, ids, links}
+func newPage(u *url.URL, fragments map[string]struct{}, links map[string]error) *page {
+	return &page{u, fragments, links}
 }
 
 func (p *page) URL() *url.URL {
 	return p.url
 }
 
-func (p *page) IDs() map[string]struct{} {
-	return p.ids
+func (p *page) Fragments() map[string]struct{} {
+	return p.fragments
 }
 
 func (p *page) Links() map[string]error {
diff --git a/page_parser.go b/page_parser.go
index 80d5770..468124c 100644
--- a/page_parser.go
+++ b/page_parser.go
@@ -35,11 +35,15 @@ func (p pageParser) Parse(rawURL string, body []byte) (*page, error) {
 		u.RawQuery = ""
 	}
 
-	ids := map[string]struct{}{}
+	frs := map[string]struct{}{}
 
 	scrape.FindAllNested(n, func(n *html.Node) bool {
 		if s := scrape.Attr(n, "id"); s != "" {
-			ids[s] = struct{}{}
+			frs[s] = struct{}{}
+		}
+
+		if s := scrape.Attr(n, "name"); s != "" {
+			frs[s] = struct{}{}
 		}
 
 		return false
@@ -58,5 +62,5 @@ func (p pageParser) Parse(rawURL string, body []byte) (*page, error) {
 		base = base.ResolveReference(u)
 	}
 
-	return newPage(u, ids, p.linkFinder.Find(n, base)), nil
+	return newPage(u, frs, p.linkFinder.Find(n, base)), nil
 }


Test patch:
diff --git a/checker_test.go b/checker_test.go
index 9383129..7f6729e 100644
--- a/checker_test.go
+++ b/checker_test.go
@@ -21,11 +21,11 @@ func createTestChecker(c *fakeHTTPClient) *checker {
 	)
 }
 
-func createTestPage(t *testing.T, ids map[string]struct{}, links map[string]error) *page {
+func createTestPage(t *testing.T, fragments map[string]struct{}, links map[string]error) *page {
 	u, err := url.Parse("http://foo.com")
 	assert.Nil(t, err)
 
-	return newPage(u, ids, links)
+	return newPage(u, fragments, links)
 }
 
 func TestCheckerCheckOnePage(t *testing.T) {
diff --git a/page_parser_test.go b/page_parser_test.go
index 371a017..5827997 100644
--- a/page_parser_test.go
+++ b/page_parser_test.go
@@ -84,13 +84,31 @@ func TestPageParserResolveLinksWithBlankBaseTag(t *testing.T) {
 	assert.Equal(t, map[string]error{"http://foo.com/bar": nil}, p.Links())
 }
 
-func TestPageParserParseIDs(t *testing.T) {
+func TestPageParserParseID(t *testing.T) {
 	p, err := newPageParser(newLinkFinder(nil), false).Parse(
 		"http://foo.com",
 		[]byte(`<p id="foo" />`),
 	)
 	assert.Nil(t, err)
-	assert.Equal(t, map[string]struct{}{"foo": {}}, p.IDs())
+	assert.Equal(t, map[string]struct{}{"foo": {}}, p.Fragments())
+}
+
+func TestPageParserParseName(t *testing.T) {
+	p, err := newPageParser(newLinkFinder(nil), false).Parse(
+		"http://foo.com",
+		[]byte(`<p name="foo" />`),
+	)
+	assert.Nil(t, err)
+	assert.Equal(t, map[string]struct{}{"foo": {}}, p.Fragments())
+}
+
+func TestPageParserParseIDAndName(t *testing.T) {
+	p, err := newPageParser(newLinkFinder(nil), false).Parse(
+		"http://foo.com",
+		[]byte(`<p id="foo" name="bar" />`),
+	)
+	assert.Nil(t, err)
+	assert.Equal(t, map[string]struct{}{"foo": {}, "bar": {}}, p.Fragments())
 }
 
 func TestPageParserParseLinks(t *testing.T) {