-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix #1843: generate shorter data urls if possible
- Loading branch information
Showing
9 changed files
with
324 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package helpers | ||
|
||
import ( | ||
"strings" | ||
"unicode/utf8" | ||
) | ||
|
||
// See "scripts/dataurl-escapes.html" for how this was derived | ||
func EncodeStringAsPercentEscapedDataURL(mimeType string, text string) (string, bool) { | ||
hex := "0123456789ABCDEF" | ||
sb := strings.Builder{} | ||
n := len(text) | ||
i := 0 | ||
runStart := 0 | ||
sb.WriteString("data:") | ||
sb.WriteString(mimeType) | ||
sb.WriteByte(',') | ||
|
||
// Scan for trailing characters that need to be escaped | ||
trailingStart := n | ||
for trailingStart > 0 { | ||
if c := text[trailingStart-1]; c > 0x20 || c == '\t' || c == '\n' || c == '\r' { | ||
break | ||
} | ||
trailingStart-- | ||
} | ||
|
||
for i < n { | ||
c, width := utf8.DecodeRuneInString(text[i:]) | ||
|
||
// We can't encode invalid UTF-8 data | ||
if c == utf8.RuneError && width == 1 { | ||
return "", false | ||
} | ||
|
||
// Escape this character if needed | ||
if c == '\t' || c == '\n' || c == '\r' || c == '#' || i >= trailingStart || | ||
(c == '%' && i+2 < n && isHex(text[i+1]) && isHex(text[i+2])) { | ||
if runStart < i { | ||
sb.WriteString(text[runStart:i]) | ||
} | ||
sb.WriteByte('%') | ||
sb.WriteByte(hex[c>>4]) | ||
sb.WriteByte(hex[c&15]) | ||
runStart = i + width | ||
} | ||
|
||
i += width | ||
} | ||
|
||
if runStart < n { | ||
sb.WriteString(text[runStart:]) | ||
} | ||
|
||
return sb.String(), true | ||
} | ||
|
||
func isHex(c byte) bool { | ||
return c >= '0' && c <= '9' || c >= 'a' && c <= 'f' || c >= 'A' && c <= 'F' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package helpers_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/evanw/esbuild/internal/helpers" | ||
) | ||
|
||
func TestEncodeDataURL(t *testing.T) { | ||
check := func(raw string, expected string) { | ||
url, ok := helpers.EncodeStringAsPercentEscapedDataURL("text/plain", raw) | ||
if !ok { | ||
t.Fatalf("Failed to encode %q", raw) | ||
} else if url != expected { | ||
t.Fatalf("Got %q but expected %q", url, expected) | ||
} | ||
} | ||
|
||
for i := 0; i <= 0xFF; i++ { | ||
alwaysEscape := i == '\t' || i == '\r' || i == '\n' || i == '#' | ||
trailingEscape := i <= 0x20 || i == '#' | ||
|
||
if trailingEscape { | ||
check(string(rune(i)), fmt.Sprintf("data:text/plain,%%%02X", i)) | ||
check("foo"+string(rune(i)), fmt.Sprintf("data:text/plain,foo%%%02X", i)) | ||
} else { | ||
check(string(rune(i)), fmt.Sprintf("data:text/plain,%c", i)) | ||
check("foo"+string(rune(i)), fmt.Sprintf("data:text/plain,foo%c", i)) | ||
} | ||
|
||
if alwaysEscape { | ||
check(string(rune(i))+"foo", fmt.Sprintf("data:text/plain,%%%02Xfoo", i)) | ||
} else { | ||
check(string(rune(i))+"foo", fmt.Sprintf("data:text/plain,%cfoo", i)) | ||
} | ||
} | ||
|
||
// Test leading vs. trailing | ||
check(" \t ", "data:text/plain, %09%20") | ||
check(" \n ", "data:text/plain, %0A%20") | ||
check(" \r ", "data:text/plain, %0D%20") | ||
check(" # ", "data:text/plain, %23%20") | ||
check("\x08#\x08", "data:text/plain,\x08%23%08") | ||
|
||
// Only "%" symbols that could form an escape need to be escaped | ||
check("%, %3, %33, %333", "data:text/plain,%, %3, %2533, %25333") | ||
} |
Oops, something went wrong.