shithub: mycel

Download patch

ref: 1a335e5982b76c5cd7850326ae6b333d7e265e2e
parent: 5598fa5fc7e9fda9610e85fd663b03554fb8915f
author: Philip Silva <philip.silva@protonmail.com>
date: Sun Dec 13 16:58:42 EST 2020

url encoded svgs :-)

--- a/img/img.go
+++ b/img/img.go
@@ -35,18 +35,32 @@
 	}
 	parts := strings.Split(addr, ",")
 	
-	header := strings.Split(parts[0], ";")
-	if ct, err = opossum.NewContentType(header[1]); err != nil {
+	var ctStr string
+	if strings.Contains(parts[0], ";") {
+		header := strings.Split(parts[0], ";")
+		ctStr = header[1]
+	} else {
+		ctStr = parts[0]
+	}
+	if ct, err = opossum.NewContentType(ctStr); err != nil {
 		return nil, ct, err
 	}
 	
-	e := base64.RawStdEncoding
-	if strings.HasSuffix(addr, "=") {
-		e = base64.StdEncoding
+	if strings.Contains(addr, "base64") {
+		e := base64.RawStdEncoding
+		if strings.HasSuffix(addr, "=") {
+			e = base64.StdEncoding
+		}
+		if data, err = e.DecodeString(parts[1]); err != nil {
+			return nil, ct, fmt.Errorf("base64 decode %v src: %w", addr, err)
+		}
+	} else {
+		out, err := url.QueryUnescape(parts[1])
+		if err != nil {
+			return nil, ct, fmt.Errorf("url decode: %w", err)
+		}
+		data = []byte(out)
 	}
-	if data, err = e.DecodeString(parts[1]); err != nil {
-		return nil, ct, fmt.Errorf("decode %v src: %w", addr, err)
-	}
 	return
 }
 
@@ -72,8 +86,10 @@
 	if contentType.IsSvg() {
 		r := bytes.NewReader(data)
 		icon, _ := oksvg.ReadIconStream(r)
-		w := 100
-		h := 100
+		if w == 0 || h == 0 {
+			w = 20
+			h = 20
+		}
 		icon.SetTarget(0, 0, float64(w), float64(h))
 		rgba := image.NewRGBA(image.Rect(0, 0, w, h))
 		icon.Draw(rasterx.NewDasher(w, h, rasterx.NewScannerGV(w, h, rgba, rgba.Bounds())), 1)
@@ -82,9 +98,7 @@
 			return nil, fmt.Errorf("encode: %w", err)
 		}
 		data = buf.Bytes()
-	}
-
-	if w != 0 || h != 0 {
+	} else if w != 0 || h != 0 {
 		image, _, err := image.Decode(bytes.NewReader(data))
 		if err != nil {
 			return nil, fmt.Errorf("decode %v: %w", imgUrl, err)
--- a/img/img_test.go
+++ b/img/img_test.go
@@ -12,7 +12,8 @@
 func TestParseDataUri(t *testing.T) {
 	srcs := []string{"data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP//yH5BAEAAAAALAAAAAABAAEAAAIBRAA7",
 		"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNgYAAAAAMAASsJTYQAAAAASUVORK5CYII=",
-		// svg example from github.com/tigt/mini-svg-data-uri (MIT License, (c) 2018 Taylor Hunt)
+		// svg examples from github.com/tigt/mini-svg-data-uri (MIT License, (c) 2018 Taylor Hunt)
+		"data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 50 50' %3e%3cpath d='M22 38V51L32 32l19-19v12C44 26 43 10 38 0 52 15 49 39 22 38z'/%3e %3c/svg%3e",
 		"data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCA1MCA1MCI+PHBhdGggZD0iTTIyIDM4VjUxTDMyIDMybDE5LTE5djEyQzQ0IDI2IDQzIDEwIDM4IDAgNTIgMTUgNDkgMzkgMjIgMzh6Ii8+PC9zdmc+",
 	}