shithub: mycel

Download patch

ref: 16cd73398af29b1685483a5f15d1ea4cf8048e86
parent: d950385eaaa5e22a8e13744a7b7b12b1c59516f8
author: Philip Silva <[email protected]>
date: Mon Jul 19 13:07:32 EDT 2021

Improve srcset handling

- faster loading due to less processing time

--- a/browser/browser.go
+++ b/browser/browser.go
@@ -22,6 +22,7 @@
 	"github.com/psilva261/opossum/logger"
 	"github.com/psilva261/opossum/nodes"
 	"github.com/psilva261/opossum/style"
+	"math"
 	"os"
 	"strconv"
 	"strings"
@@ -33,8 +34,10 @@
 	_ "image/png"
 )
 
-const debugPrintHtml = false
-const EnterKey = 10
+const (
+	debugPrintHtml = false
+	EnterKey = 10
+)
 
 // cursor based on Clipart from Francesco 'Architetto' Rollandin
 // OpenClipart SVG ID: 163773 from OCAL 0.18 release 16/11/2019
@@ -55,17 +58,22 @@
 	0b00001100, 0b01111000,
 }
 
-var ExperimentalJsInsecure bool
-var EnableNoScriptTag bool
+var (
+	ExperimentalJsInsecure bool
+	EnableNoScriptTag bool
+)
 
-var browser *Browser
-var Style = style.Map{}
-var dui *duit.DUI
-var colorCache = make(map[draw.Color]*draw.Image)
-var imageCache = make(map[string]*draw.Image)
-var scroller *duitx.Scroll
-var display *draw.Display
+var (
+	browser *Browser
+	Style = style.Map{}
+	dui *duit.DUI
+	scroller *duitx.Scroll
+	display *draw.Display
 
+	colorCache = make(map[draw.Color]*draw.Image)
+	imageCache = make(map[string]*draw.Image)
+)
+
 type Label struct {
 	*duitx.Label
 
@@ -259,9 +267,14 @@
 }
 
 func srcSet(n *nodes.Node) (w int, src string) {
-	smallestImg := ""
-	smallestW := 0
+	bestImg := ""
+	bestW := 0
+	idealW := n.Width()
 	scale := 1
+	
+	u := func(wd int) int {
+		return int(math.Abs(float64(wd)-float64(idealW)))
+	}
 
 	if dui != nil {
 		scale = int(dui.Scale(1))
@@ -284,13 +297,13 @@
 		if err != nil {
 			continue
 		}
-		if smallestImg == "" || smallestW > w {
-			smallestImg = src
-			smallestW = w
+		if bestImg == "" || u(bestW) > u(w) {
+			bestImg = src
+			bestW = w
 		}
 	}
 
-	return smallestW, smallestImg
+	return bestW, bestImg
 }
 
 type Element struct {
--- a/browser/browser_test.go
+++ b/browser/browser_test.go
@@ -387,8 +387,8 @@
 
 func TestNewPicture(t *testing.T) {
 	htm := `
-		<picture itemprop="contentUrl">
-			<source srcset="https://example.com/2040 2040w,https://example.com/1880 1880w,https://example.com/1400 1400w" media="(-webkit-min-device-pixel-ratio: 1.25), (min-resolution: 120dpi)">
+	<picture itemprop="contentUrl">
+		<source srcset="https://example.com/2040 2040w,https://example.com/1880 1880w,https://example.com/1400 1400w" media="(-webkit-min-device-pixel-ratio: 1.25), (min-resolution: 120dpi)">
     		<source srcset="https://example.com/1020 1020w,https://example.com/940 940w,https://example.com/700 700w">
     		<img src="https://example.com/465" height="5000" width="7000" loading="lazy">
     	</picture>
@@ -401,6 +401,22 @@
 	p := nt.Find("picture")
 	src := newPicture(p)
 	if src != "https://example.com/700" {
+		t.Error()
+	}
+}
+
+func TestSrcSet(t *testing.T) {
+	htm := `
+		<img width="800" height="429" style="width: 600px" src="/t.jpg" srcset="/t.jpg 800w, /t-300x165.jpg 300w, /t-768x421.jpg 768w, /t-561x308.jpg 561w, /t-364x200.jpg 364w, /t-728x399.jpg 728w, /t-608x334.jpg 608w, /t-758x416.jpg 758w, /t-87x48.jpg 87w, /t-175x96.jpg 175w, /t-313x172.jpg 313w" sizes="(max-width: 800px) 100vw, 800px">
+	`
+	nt, _, err := digestHtm(htm)
+	if err != nil {
+		t.Fatalf("digest: %v", err)
+	}
+	img := nt.Find("img")
+	w, src := srcSet(img)
+	t.Logf("%v, %v, %v", nt.Data(), w, src)
+	if w != 608 || src != "/t-608x334.jpg" {
 		t.Error()
 	}
 }