Skip to content

Commit

Permalink
Add support for single quoted attrs
Browse files Browse the repository at this point in the history
  • Loading branch information
goring committed Aug 7, 2024
1 parent b443f17 commit 3a5e7a0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
17 changes: 14 additions & 3 deletions elem.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,22 @@ func (e *Element) renderAttrTo(attrName string, builder *strings.Builder) {
}
} else {
// regular attribute has a name and a value
attrVal := e.Attrs[attrName]

// A necessary check to to avoid adding extra quotes around values that are already single-quoted
// An example is '{"quantity": 5}'
isSingleQuoted := strings.HasPrefix(attrVal, "'") && strings.HasSuffix(attrVal, "'")

builder.WriteString(` `)
builder.WriteString(attrName)
builder.WriteString(`="`)
builder.WriteString(e.Attrs[attrName])
builder.WriteString(`"`)
builder.WriteString(`=`)
if !isSingleQuoted {
builder.WriteString(`"`)
}
builder.WriteString(attrVal)
if !isSingleQuoted {
builder.WriteString(`"`)
}
}
}

Expand Down
9 changes: 9 additions & 0 deletions elements_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -687,3 +687,12 @@ func TestCSS(t *testing.T) {
el := CSS(cssContent)
assert.Equal(t, expected, el.Render())
}

func TestSingleQuote(t *testing.T) {
expected := `<div data-values='{"quantity": 5}'></div>`
el := Div(attrs.Props{
"data-values": `'{"quantity": 5}'`,
})
actual := el.Render()
assert.Equal(t, expected, actual)
}

0 comments on commit 3a5e7a0

Please sign in to comment.