Skip to content

Commit

Permalink
Allow empty attributes in tags
Browse files Browse the repository at this point in the history
  • Loading branch information
stanislas-m authored and markbates committed Jan 2, 2018
1 parent 82570c5 commit 75a7ff0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
10 changes: 8 additions & 2 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,14 @@ func (o Options) String() string {
var tmp = make([]string, 2)
for k, v := range o {
tmp[0] = template.HTMLEscaper(k)
tmp[1] = fmt.Sprintf("\"%s\"", template.HTMLEscaper(v))
out = append(out, strings.Join(tmp, "="))
if v != nil {
tmp[1] = fmt.Sprintf("\"%s\"", template.HTMLEscaper(v))
out = append(out, strings.Join(tmp, "="))
} else {
// nil attribute value is interpreted as empty attribute notation
// https://www.w3.org/TR/html5/syntax.html#elements-attributes
out = append(out, tmp[0])
}
}
sort.Strings(out)
return strings.Join(out, " ")
Expand Down
10 changes: 10 additions & 0 deletions options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,13 @@ func Test_Options_String_Escaped(t *testing.T) {
s := o.String()
r.Equal(`<b>="<p>"`, s)
}

func Test_Options_String_Empty_Attribute(t *testing.T) {
r := require.New(t)
o := tags.Options{
"value": "Mark",
"checked": nil,
}
s := o.String()
r.Equal(`checked value="Mark"`, s)
}

0 comments on commit 75a7ff0

Please sign in to comment.