Skip to content

Commit

Permalink
DataURI: fix introduced bug: if original data URI is shorter, use the…
Browse files Browse the repository at this point in the history
… original and not the decoded, related to #282
  • Loading branch information
tdewolff committed Jan 8, 2020
1 parent 315e0a7 commit 0c951d4
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
5 changes: 3 additions & 2 deletions common.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func Mediatype(b []byte) []byte {

// DataURI minifies a data URI and calls a minifier by the specified mediatype. Specifications: https://www.ietf.org/rfc/rfc2397.txt.
func DataURI(m *M, dataURI []byte) []byte {
origData := parse.Copy(dataURI)
mediatype, data, err := parse.DataURI(dataURI)
if err != nil {
return dataURI
Expand All @@ -52,8 +53,8 @@ func DataURI(m *M, dataURI []byte) []byte {
break
}
}
if len(dataURI) < base64Len && len(dataURI) < asciiLen {
return dataURI
if len(origData) < base64Len && len(origData) < asciiLen {
return origData
}
if base64Len < asciiLen {
encoded := make([]byte, base64Len-len(";base64"))
Expand Down
2 changes: 1 addition & 1 deletion common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestDataURI(t *testing.T) {
{"data:text/x,<?xx?>", "data:text/x,%3C%3Fxx%3F%3E"},
{"data:text/other,\"<\u2318", "data:text/other,%22%3C%E2%8C%98"},
{"data:text/other,\"<\u2318>", "data:text/other;base64,IjzijJg+"},
{`data:text/svg+xml,<svg height="100" width="100"><circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" /></svg>`, `data:text/svg+xml,<svg height="100" width="100"><circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" /></svg>`},
{`data:text/svg+xml,%3Csvg height="100" width="100"><circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" /></svg>`, `data:text/svg+xml,%3Csvg height="100" width="100"><circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" /></svg>`},
}
m := New()
m.AddFunc("text/x", func(_ *M, w io.Writer, r io.Reader, _ map[string]string) error {
Expand Down

0 comments on commit 0c951d4

Please sign in to comment.