Skip to content

Commit ec73feb

Browse files
authored
Merge pull request #200 from jhunt/ignore-alt-empty
2 parents 3145191 + 39067a6 commit ec73feb

File tree

5 files changed

+28
-1
lines changed

5 files changed

+28
-1
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ htmltest uses a YAML configuration file. Put `.htmltest.yml` in the same directo
160160
| `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` |
161161
| `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` |
162162
| `IgnoreAltMissing` | Turns off image alt attribute checking. | `false` |
163+
| `IgnoreAltEmpty` | Allows `alt=""` for decorative images. | `false` |
163164
| `IgnoreDirectoryMissingTrailingSlash` | Turns off errors for links to directories without a trailing slash. | `false` |
164165
| `IgnoreSSLVerify` | Turns off x509 errors for self-signed certificates. | `false` |
165166
| `IgnoreTagAttribute` | Specify the ignore attribute. All tags with this attribute will be excluded from every check. | `"data-proofer-ignore"` |

htmltest/check-img.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func (hT *HTMLTest) checkImg(document *htmldoc.Document, node *html.Node) {
3232
})
3333
} else if htmldoc.AttrPresent(node.Attr, "alt") && !hT.opts.IgnoreAltMissing {
3434
// Following checks require alt attr is present
35-
if len(attrs["alt"]) == 0 {
35+
if len(attrs["alt"]) == 0 && !hT.opts.IgnoreAltEmpty {
3636
// Check alt has length, fail if empty
3737
hT.issueStore.AddIssue(issues.Issue{
3838
Level: issues.LevelError,

htmltest/check-img_test.go

+15
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,21 @@ func TestImageAltEmpty(t *testing.T) {
179179
tExpectIssue(t, hT, "alt text empty", 1)
180180
}
181181

182+
func TestImageAltIgnoreEmptyWhenMissing(t *testing.T) {
183+
// fails for image with an empty alt attribute
184+
hT := tTestFileOpts("fixtures/images/missingImageAlt.html",
185+
map[string]interface{}{"IgnoreAltEmpty": true})
186+
tExpectIssueCount(t, hT, 1)
187+
tExpectIssue(t, hT, "alt attribute missing", 1)
188+
}
189+
190+
func TestImageAltIgnoreEmptyWhenPresent(t *testing.T) {
191+
// ignores empty alt attribute present for image
192+
hT := tTestFileOpts("fixtures/images/emptyImageAltTextExplicit.html",
193+
map[string]interface{}{"IgnoreAltEmpty": true})
194+
tExpectIssueCount(t, hT, 0)
195+
}
196+
182197
func TestImageAltSpaces(t *testing.T) {
183198
// fails for image with nothing but spaces in alt attribute
184199
hT := tTestFile("fixtures/images/emptyImageAltText.html")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
3+
<body>
4+
5+
<p>Blah blah blah. <img src="./gpl.png" alt=""/> </p>
6+
<p>Blah blah blah. <img src="./gpl.png" alt/> </p>
7+
</body>
8+
9+
</html>

htmltest/options.go

+2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ type Options struct {
4848
IgnoreCanonicalBrokenLinks bool
4949
IgnoreExternalBrokenLinks bool
5050
IgnoreAltMissing bool
51+
IgnoreAltEmpty bool
5152
IgnoreDirectoryMissingTrailingSlash bool
5253
IgnoreSSLVerify bool
5354
IgnoreTagAttribute string
@@ -114,6 +115,7 @@ func DefaultOptions() map[string]interface{} {
114115
"IgnoreCanonicalBrokenLinks": true,
115116
"IgnoreExternalBrokenLinks": false,
116117
"IgnoreAltMissing": false,
118+
"IgnoreAltEmpty": false,
117119
"IgnoreDirectoryMissingTrailingSlash": false,
118120
"IgnoreSSLVerify": false,
119121
"IgnoreTagAttribute": "data-proofer-ignore",

0 commit comments

Comments
 (0)