shithub: opossum

Download patch

ref: 50ad979b5dc99c2499a3b80d3318c2a4c6430b28
parent: dab0f519d48d558f12d3366deb2cb207383be69e
author: Philip Silva <philip.silva@protonmail.com>
date: Thu Dec 17 18:34:22 EST 2020

Height()/Width() functions

--- a/browser/browser.go
+++ b/browser/browser.go
@@ -17,7 +17,6 @@
 	"opossum/logger"
 	"opossum/nodes"
 	"opossum/style"
-	"strconv"
 	"strings"
 	"unicode"
 
@@ -147,23 +146,7 @@
 	var i *draw.Image
 	var cached bool
 	if i, cached = imageCache[src]; !cached {
-		var w int
-		var h int
-		wStr, ok := n.Declarations["width"]
-		if ok {
-			w, err = strconv.Atoi(strings.TrimSuffix(wStr.Value, "px"))
-			if err != nil {
-				return nil, fmt.Errorf("atoi: %w", err)
-			}
-		}
-		hStr, ok := n.Declarations["height"]
-		if ok {
-			h, err = strconv.Atoi(strings.TrimSuffix(hStr.Value, "px"))
-			if err != nil {
-				return nil, fmt.Errorf("atoi: %w", err)
-			}
-		}
-		r, err := img.Load(browser, src, w, h)
+		r, err := img.Load(browser, src, n.Width(), n.Height())
 		if err != nil {
 			return nil, fmt.Errorf("load draw image: %w", err)
 		}
@@ -225,18 +208,12 @@
 	if cs.IsDisplayNone() {
 		return nil
 	}
-	var w int
-	var h int
-	wStr, ok := cs.Declarations["width"]
-	if ok {
-		w, _ = strconv.Atoi(strings.TrimSuffix(wStr.Value, "px"))
-	}
-	hStr, ok := cs.Declarations["height"]
-	if ok {
-		h, _ = strconv.Atoi(strings.TrimSuffix(hStr.Value, "px"))
-	}
+
 	var i *draw.Image
 	var err error
+	w := cs.Width()
+	h := cs.Height()
+
 	if w == 0 && h == 0 {
 		return NewElement(ui, cs)
 	}
--- a/style/experimental.go
+++ b/style/experimental.go
@@ -101,8 +101,10 @@
 			log.Printf("bg img not ok")
 			return
 		}
+		w := cs.Width()
+		h := cs.Height()
 		log.Printf("bg img ok")
-		r, err := img.Load(fetcher, imgUrl, 0, 0)
+		r, err := img.Load(fetcher, imgUrl, w, h)
 		if err != nil {
 			log.Errorf("bg img load %v: %v", imgUrl, err)
 			return nil
--- a/style/stylesheets.go
+++ b/style/stylesheets.go
@@ -129,11 +129,7 @@
 		return nil, fmt.Errorf("douceur parse: %w", err)
 	}
 	processRule := func(m map[*html.Node][]*css.Rule, r *css.Rule) (err error) {
-		log.Printf("r: %+v", r)
-		log.Printf("r.Rules: %+v", r.Rules)
-		log.Printf("r.Prelude: %+v", r.Prelude)
 		for _, sel := range r.Selectors {
-			log.Printf("sel=%+v", sel)
 			cs, err := cssSel.Compile(sel.Value)
 			if err != nil {
 				log.Printf("cssSel compile %v: %v", sel.Value, err)
@@ -252,7 +248,7 @@
 	// overwrite with higher prio child props
 	for k, v := range ccs.Declarations {
 		switch k {
-		case "height", "width":
+		/*case "height", "width":
 			parentL, ok := res.Declarations[k]
 			if ok && strings.HasSuffix(v.Value, "%") && strings.HasSuffix(parentL.Value, "px") {
 				parentLNum, err := strconv.Atoi(strings.TrimSuffix(parentL.Value, "px"))
@@ -272,7 +268,7 @@
 				}
 				continue
 			}
-			fallthrough
+			fallthrough*/
 		default:
 			res.Declarations[k] = v
 		}
@@ -502,4 +498,49 @@
 		}
 	}
 	return true // TODO: be more specific
+}
+
+func length(l string) (f float64, unit string, err error) {
+	var s string
+	if s == "auto" {
+		return 0, "px", nil
+	}
+	for _, suffix := range []string{"px", "%", "rem", "em"} {
+		if strings.HasSuffix(l, suffix) {
+			s = strings.TrimSuffix(l, suffix)
+			unit = suffix
+			break
+		}
+	}
+	if unit == "" {
+		return f, unit, fmt.Errorf("unknown suffix: %v", l)
+	}
+	if unit == "px" {
+		f, err = strconv.ParseFloat(s, 64)
+	}
+	return
+}
+
+func (cs Map) Height() int {
+	d, ok := cs.Declarations["height"]
+	if ok {
+		f, _, err := length(d.Value)
+		if err != nil {
+			log.Errorf("cannot parse height: %v", err)
+		}
+		return int(f)
+	}
+	return 0
+}
+
+func (cs Map) Width() int {
+	d, ok := cs.Declarations["width"]
+	if ok {
+		f, _, err := length(d.Value)
+		if err != nil {
+			log.Errorf("cannot parse width: %v", err)
+		}
+		return int(f)
+	}
+	return 0
 }