Skip to content

Commit

Permalink
Ignore <link> tags with rel=preconnect
Browse files Browse the repository at this point in the history
Refactor ignoring logic to take a list.
Closes #202
  • Loading branch information
wjdp committed Jan 28, 2023
1 parent 8c2bfa9 commit c9077f5
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
14 changes: 10 additions & 4 deletions htmltest/check-link.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ import (
"golang.org/x/net/html"
)

// ignoredRels: List of rel values to ignore, dns-prefetch and preconnect are ignored as they are not links to be
// followed rather telling browser we want something on that host, if the root of that host is not valid,
// it's likely not a problem.
var ignoredRels = [...]string{"dns-prefetch", "preconnect"}

func (hT *HTMLTest) checkLink(document *htmldoc.Document, node *html.Node) {
attrs := htmldoc.ExtractAttrs(node.Attr,
[]string{"href", "rel"})
Expand All @@ -27,10 +32,11 @@ func (hT *HTMLTest) checkLink(document *htmldoc.Document, node *html.Node) {
document.State.FaviconPresent = true
}

// Ignore if rel=dns-prefetch, see #40. If we have more cases like this a hashable type should be created and
// checked against.
if attrs["rel"] == "dns-prefetch" {
return
// If rel in IgnoredRels, ignore this link
for _, rel := range ignoredRels {
if attrs["rel"] == rel {
return
}
}

// Create reference
Expand Down
6 changes: 6 additions & 0 deletions htmltest/check-link_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,12 @@ func TestLinkRelDnsPrefetch(t *testing.T) {
tExpectIssueCount(t, hT, 0)
}

func TestLinkRelPreconnect(t *testing.T) {
// ignores links with rel="preconnect"
hT := tTestFile("fixtures/links/link_rel_preconnect.html")
tExpectIssueCount(t, hT, 0)
}

func TestAnchorPre(t *testing.T) {
// catches broken links when inside pre or code tags
hT := tTestFileOpts("fixtures/links/anchors_in_pre.html",
Expand Down
3 changes: 3 additions & 0 deletions htmltest/fixtures/links/link_rel_preconnect.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<head>
<link rel="preconnect" href="https://www.googletagmanager.com" />
</head>

0 comments on commit c9077f5

Please sign in to comment.