shithub: mycel

Download patch

ref: 5d909a48ef984b1f8b412acc65222f74deafe139
parent: bfe62e5957b0791af5a2f0684314b32cc0ee5383
author: Philip Silva <philip.silva@protonmail.com>
date: Sun Dec 13 11:37:45 EST 2020

More flexible bg img url handling

--- a/style/experimental.go
+++ b/style/experimental.go
@@ -65,49 +65,61 @@
 	return draw.Color(uint32(draw.White))
 }
 
+func backgroundImageUrl(decl css.Declaration) (url string, ok bool) {
+	if v := decl.Value; strings.Contains(v, "url(") && strings.Contains(v, ")") {
+		v = strings.ReplaceAll(v, `"`, "")
+		v = strings.ReplaceAll(v, `'`, "")
+		from := strings.Index(v, "url(")
+		if from < 0 {
+			log.Printf("bg img: no url: %v", decl.Value)
+			return
+		}
+		from += len("url(")
+		imgUrl := v[from:]
+		to := strings.Index(imgUrl, ")")
+		if to < 0 {
+			log.Printf("bg img: no ): %v", decl.Value)
+			return
+		}
+		imgUrl = imgUrl[:to]
+		return imgUrl, true
+	} else {
+		log.Printf("bg img: missing ( or ) '%+v'", decl.Value)
+		return
+	}
+}
+
 func (cs Map) backgroundImage() (img *draw.Image) {
-	decl, ok := cs.Declarations["background"]
+	decl, ok := cs.Declarations["background-image"]
+	if !ok {
+		decl, ok = cs.Declarations["background"]
+	}
 	log.Printf("decl=%+v\n", decl)
 	if ok {
+		imgUrl, ok := backgroundImageUrl(decl)
+		if !ok {
+			log.Printf("bg img not ok")
+			return
+		}
 		log.Printf("bg img ok")
-		if strings.Contains(decl.Value, "url(") && strings.Contains(decl.Value, ")") {
-			from := strings.Index(decl.Value, "url(")
-			if from < 0 {
-				log.Printf("bg img: no url: %v", decl.Value)
-				return
-			}
-			from += len("url('")
-			imgUrl := decl.Value[from:]
-			to := strings.Index(imgUrl, ")")
-			if to < 0 {
-				log.Printf("bg img: no ): %v", decl.Value)
-				return
-			}
-			to -= len("'")
-			imgUrl = imgUrl[:to]
-			uri, err := fetcher.LinkedUrl(imgUrl)
-			if err != nil {
-				log.Printf("bg img interpet url: %v", err)
-				return nil
-			}
-			buf, contentType, err := fetcher.Get(uri)
-			if err != nil {
-				log.Printf("bg img get %v (%v): %v", uri, contentType, err)
-				return nil
-			}
-			r := bytes.NewReader(buf)
-			log.Printf("Read %v...", imgUrl)
-			img, err = duit.ReadImage(dui.Display, r)
-			if err != nil {
-				log.Printf("bg read image: %v", err)
-				return
-			}
-			return img
-		} else {
-			log.Printf("bg img: missing fixes '%+v'", decl.Value)
+		uri, err := fetcher.LinkedUrl(imgUrl)
+		if err != nil {
+			log.Errorf("bg img interpret url: %v", err)
+			return nil
 		}
-	} else {
-		log.Printf("bg img not ok")
+		buf, contentType, err := fetcher.Get(uri)
+		if err != nil {
+			log.Errorf("bg img get %v (%v): %v", uri, contentType, err)
+			return nil
+		}
+		r := bytes.NewReader(buf)
+		log.Printf("Read %v...", imgUrl)
+		img, err = duit.ReadImage(dui.Display, r)
+		if err != nil {
+			log.Errorf("bg read image: %v", err)
+			return
+		}
+		return img
 	}
 	return
 }
\ No newline at end of file
--- /dev/null
+++ b/style/experimental_test.go
@@ -1,0 +1,23 @@
+package style
+
+import (
+	"github.com/chris-ramon/douceur/css"
+	"testing"
+)
+
+func TestBackgroundImageUrl(t *testing.T) {
+	suffix := ""
+	for _, quote := range []string{"", "'", `"`} {
+		url := "/foo.png"
+		decl := css.Declaration{
+			Value: "url(" + quote + url + quote + ")" + suffix,
+		}
+		imgUrl, ok := backgroundImageUrl(decl)
+		if !ok {
+			t.Fatalf("not ok")
+		}
+		if imgUrl != url {
+			t.Fatalf("expected %+v but got %+v", url, imgUrl)
+		}
+	}
+}