Skip to content

Commit

Permalink
Merge pull request #200 from jhunt/ignore-alt-empty
Browse files Browse the repository at this point in the history
  • Loading branch information
wjdp authored Nov 4, 2022
2 parents 3145191 + 39067a6 commit ec73feb
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ htmltest uses a YAML configuration file. Put `.htmltest.yml` in the same directo
| `IgnoreCanonicalBrokenLinks` | When true produces a warning, rather than an error, for broken canonical links. When testing a site which isn't live yet or before publishing a new page canonical links will fail. | `true` |
| `IgnoreExternalBrokenLinks` | When true produces a warning, rather than an error, for broken external links. Useful when testing a site having hundreds of external links. | `false` |
| `IgnoreAltMissing` | Turns off image alt attribute checking. | `false` |
| `IgnoreAltEmpty` | Allows `alt=""` for decorative images. | `false` |
| `IgnoreDirectoryMissingTrailingSlash` | Turns off errors for links to directories without a trailing slash. | `false` |
| `IgnoreSSLVerify` | Turns off x509 errors for self-signed certificates. | `false` |
| `IgnoreTagAttribute` | Specify the ignore attribute. All tags with this attribute will be excluded from every check. | `"data-proofer-ignore"` |
Expand Down
2 changes: 1 addition & 1 deletion htmltest/check-img.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (hT *HTMLTest) checkImg(document *htmldoc.Document, node *html.Node) {
})
} else if htmldoc.AttrPresent(node.Attr, "alt") && !hT.opts.IgnoreAltMissing {
// Following checks require alt attr is present
if len(attrs["alt"]) == 0 {
if len(attrs["alt"]) == 0 && !hT.opts.IgnoreAltEmpty {
// Check alt has length, fail if empty
hT.issueStore.AddIssue(issues.Issue{
Level: issues.LevelError,
Expand Down
15 changes: 15 additions & 0 deletions htmltest/check-img_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,21 @@ func TestImageAltEmpty(t *testing.T) {
tExpectIssue(t, hT, "alt text empty", 1)
}

func TestImageAltIgnoreEmptyWhenMissing(t *testing.T) {
// fails for image with an empty alt attribute
hT := tTestFileOpts("fixtures/images/missingImageAlt.html",
map[string]interface{}{"IgnoreAltEmpty": true})
tExpectIssueCount(t, hT, 1)
tExpectIssue(t, hT, "alt attribute missing", 1)
}

func TestImageAltIgnoreEmptyWhenPresent(t *testing.T) {
// ignores empty alt attribute present for image
hT := tTestFileOpts("fixtures/images/emptyImageAltTextExplicit.html",
map[string]interface{}{"IgnoreAltEmpty": true})
tExpectIssueCount(t, hT, 0)
}

func TestImageAltSpaces(t *testing.T) {
// fails for image with nothing but spaces in alt attribute
hT := tTestFile("fixtures/images/emptyImageAltText.html")
Expand Down
9 changes: 9 additions & 0 deletions htmltest/fixtures/images/emptyImageAltTextExplicit.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@


<body>

<p>Blah blah blah. <img src="./gpl.png" alt=""/> </p>
<p>Blah blah blah. <img src="./gpl.png" alt/> </p>
</body>

</html>
2 changes: 2 additions & 0 deletions htmltest/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type Options struct {
IgnoreCanonicalBrokenLinks bool
IgnoreExternalBrokenLinks bool
IgnoreAltMissing bool
IgnoreAltEmpty bool
IgnoreDirectoryMissingTrailingSlash bool
IgnoreSSLVerify bool
IgnoreTagAttribute string
Expand Down Expand Up @@ -114,6 +115,7 @@ func DefaultOptions() map[string]interface{} {
"IgnoreCanonicalBrokenLinks": true,
"IgnoreExternalBrokenLinks": false,
"IgnoreAltMissing": false,
"IgnoreAltEmpty": false,
"IgnoreDirectoryMissingTrailingSlash": false,
"IgnoreSSLVerify": false,
"IgnoreTagAttribute": "data-proofer-ignore",
Expand Down

0 comments on commit ec73feb

Please sign in to comment.