Skip to content

Commit

Permalink
Merge pull request #58 from wawandco/skipping-non-html
Browse files Browse the repository at this point in the history
Skipping id check when ID is empty
  • Loading branch information
paganotoni authored Aug 20, 2020
2 parents bf1d6aa + a03f596 commit a9720cc
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 19 deletions.
28 changes: 11 additions & 17 deletions reviewers/alt_required.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,12 @@ func (at AltRequired) Review(path string, page io.Reader) ([]Fault, error) {
break
}

if tt == html.StartTagToken || tt == html.SelfClosingTagToken {
token := z.Token()

if !at.tagRequiresAlt(token) || at.hasAlt(token) {
continue
}
token := z.Token()
if !at.tagRequiresAlt(token) || at.hasAlt(token) {
continue
}

if tt == html.StartTagToken || tt == html.SelfClosingTagToken {
result = append(result, Fault{
Reviewer: at.ReviewerName(),
Line: token.Line,
Expand All @@ -47,12 +46,10 @@ func (at AltRequired) Review(path string, page io.Reader) ([]Fault, error) {
}

func (at AltRequired) tagRequiresAlt(token html.Token) bool {
if token.DataAtom.String() == "img" {
switch token.DataAtom.String() {
case "img":
return true
}

// input[type=image]
if token.DataAtom.String() == "input" {
case "input":
for _, attr := range token.Attr {
if attr.Key != "type" || strings.ToLower(attr.Val) != "image" {
continue
Expand All @@ -62,20 +59,17 @@ func (at AltRequired) tagRequiresAlt(token html.Token) bool {
}

return false
}

// area[href]
if token.DataAtom.String() == "area" {
case "area":
for _, attr := range token.Attr {
if attr.Key == "href" {
return true
}
}

return true
default:
return false
}

return false
}

func (at AltRequired) hasAlt(token html.Token) bool {
Expand Down
2 changes: 1 addition & 1 deletion reviewers/att_no_white_spaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (at AttrNoWhiteSpaces) Review(path string, page io.Reader) ([]Fault, error)
continue
}

exp := regexp.MustCompile(`[^\s]+(\s+\=\s*|\s*\=\s+)`)
exp := regexp.MustCompile(`\S+(\s+=\s*|\s*=\s+)`)
if exp.MatchString(line) {
result = append(result, Fault{
Reviewer: at.ReviewerName(),
Expand Down
7 changes: 6 additions & 1 deletion reviewers/attr_id_unique.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
type AttrIDUnique struct{}

func (at AttrIDUnique) ReviewerName() string {
return "attribute/alt-required"
return "attribute/id-unique"
}

func (at AttrIDUnique) Accepts(filePath string) bool {
Expand All @@ -31,8 +31,13 @@ func (at AttrIDUnique) Review(path string, page io.Reader) ([]Fault, error) {
token := z.Token()

ID := at.tagID(token)
if ID == "" {
continue
}

if ID != "" && !IDs[ID] {
IDs[ID] = true

continue
}

Expand Down
11 changes: 11 additions & 0 deletions reviewers/attr_id_unique_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ func Test_AttrIDUnique_Review(t *testing.T) {
`,
},

{
name: "no fault empty",
content: `
<img src="test.png" />
<img src="test.png" />
<img src="test.png" />
<img src="test.png" />
<img src="test.png" />
`,
},

{
name: "Faults",
content: `
Expand Down

0 comments on commit a9720cc

Please sign in to comment.