Skip to content

Commit c9077f5

Browse files
committed
Ignore <link> tags with rel=preconnect
Refactor ignoring logic to take a list. Closes #202
1 parent 8c2bfa9 commit c9077f5

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

htmltest/check-link.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ import (
1616
"golang.org/x/net/html"
1717
)
1818

19+
// ignoredRels: List of rel values to ignore, dns-prefetch and preconnect are ignored as they are not links to be
20+
// followed rather telling browser we want something on that host, if the root of that host is not valid,
21+
// it's likely not a problem.
22+
var ignoredRels = [...]string{"dns-prefetch", "preconnect"}
23+
1924
func (hT *HTMLTest) checkLink(document *htmldoc.Document, node *html.Node) {
2025
attrs := htmldoc.ExtractAttrs(node.Attr,
2126
[]string{"href", "rel"})
@@ -27,10 +32,11 @@ func (hT *HTMLTest) checkLink(document *htmldoc.Document, node *html.Node) {
2732
document.State.FaviconPresent = true
2833
}
2934

30-
// Ignore if rel=dns-prefetch, see #40. If we have more cases like this a hashable type should be created and
31-
// checked against.
32-
if attrs["rel"] == "dns-prefetch" {
33-
return
35+
// If rel in IgnoredRels, ignore this link
36+
for _, rel := range ignoredRels {
37+
if attrs["rel"] == rel {
38+
return
39+
}
3440
}
3541

3642
// Create reference

htmltest/check-link_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,12 @@ func TestLinkRelDnsPrefetch(t *testing.T) {
690690
tExpectIssueCount(t, hT, 0)
691691
}
692692

693+
func TestLinkRelPreconnect(t *testing.T) {
694+
// ignores links with rel="preconnect"
695+
hT := tTestFile("fixtures/links/link_rel_preconnect.html")
696+
tExpectIssueCount(t, hT, 0)
697+
}
698+
693699
func TestAnchorPre(t *testing.T) {
694700
// catches broken links when inside pre or code tags
695701
hT := tTestFileOpts("fixtures/links/anchors_in_pre.html",
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<head>
2+
<link rel="preconnect" href="https://www.googletagmanager.com" />
3+
</head>

0 commit comments

Comments
 (0)