shithub: mycel

Download patch

ref: 9d37d6e32a89ccfc17a18b0d25165b0713a74dc5
parent: 780d55cb042d7d8e892b25c1368355e0358b1969
author: Philip Silva <philip.silva@protonmail.com>
date: Thu May 13 06:02:35 EDT 2021

handle srcset also in <img>

--- a/browser/browser.go
+++ b/browser/browser.go
@@ -205,6 +205,11 @@
 		} else {
 			return nil, fmt.Errorf("img svg %v: %v", xml, err)
 		}
+	} else if n.Data() == "img" {
+		_, s := srcSet(n)
+		if s != "" {
+			src = s
+		}
 	}
 
 	if src == "" {
@@ -240,6 +245,21 @@
 func newPicture(n *nodes.Node) string {
 	smallestImg := ""
 	smallestW := 0
+
+	for _, source := range n.FindAll("source") {
+		w, src := srcSet(source)
+		if src != "" && (smallestImg == "" || smallestW > w) {
+			smallestImg = src
+			smallestW = w
+		}
+	}
+
+	return smallestImg
+}
+
+func srcSet(n *nodes.Node) (w int, src string) {
+	smallestImg := ""
+	smallestW := 0
 	scale := 1
 
 	if dui != nil {
@@ -246,32 +266,30 @@
 		scale = int(dui.Scale(1))
 	}
 
-	for _, source := range n.FindAll("source") {
-		for _, s := range strings.Split(source.Attr("srcset"), ",") {
-			s = strings.TrimSpace(s)
-			tmp := strings.Split(s, " ")
-			src := ""
-			s := ""
-			src = tmp[0]
-			if len(tmp) == 2 {
-				s = tmp[1]
-			}
-			if s == "" || s == fmt.Sprintf("%vx", scale) {
-				return src
-			}
-			s = strings.TrimSuffix(s, "w")
-			w, err := strconv.Atoi(s)
-			if err != nil {
-				continue
-			}
-			if smallestImg == "" || smallestW > w {
-				smallestImg = src
-				smallestW = w
-			}
+	for _, s := range strings.Split(n.Attr("srcset"), ",") {
+		s = strings.TrimSpace(s)
+		tmp := strings.Split(s, " ")
+		src := ""
+		s := ""
+		src = tmp[0]
+		if len(tmp) == 2 {
+			s = tmp[1]
 		}
+		if s == "" || s == fmt.Sprintf("%vx", scale) {
+			return 0, src
+		}
+		s = strings.TrimSuffix(s, "w")
+		w, err := strconv.Atoi(s)
+		if err != nil {
+			continue
+		}
+		if smallestImg == "" || smallestW > w {
+			smallestImg = src
+			smallestW = w
+		}
 	}
 
-	return smallestImg
+	return smallestW, smallestImg
 }
 
 type Element struct {