ref: 9d5388d05404585cffbf9092b83301e507110a7d
parent: 9f87586267ec5fe82546f189c0e19f1abc3281ae
author: Philip Silva <philip.silva@protonmail.com>
date: Sun Mar 14 17:36:34 EDT 2021
handle rem in media queries
--- a/style/stylesheets.go
+++ b/style/stylesheets.go
@@ -22,8 +22,8 @@
var availableFontNames []string
var log *logger.Logger
-var rMinWidth = regexp.MustCompile(`min-width: (\d+)px`)
-var rMaxWidth = regexp.MustCompile(`max-width: (\d+)px`)
+var rMinWidth = regexp.MustCompile(`min-width: (\d+)(px|em|rem)`)
+var rMaxWidth = regexp.MustCompile(`max-width: (\d+)(px|em|rem)`)
const FontBaseSize = 11.0
var WindowWidth = 1280
@@ -179,20 +179,24 @@
continue
}
if rMaxWidth.MatchString(r.Prelude) {
- maxWidth, err := strconv.Atoi(rMaxWidth.FindStringSubmatch(r.Prelude)[1])
+ m := rMaxWidth.FindStringSubmatch(r.Prelude)
+ l := m[1]+m[2]
+ maxWidth, _, err := length(l)
if err != nil {
return nil, fmt.Errorf("atoi: %w", err)
}
- if windowWidth > maxWidth {
+ if float64(windowWidth) > maxWidth {
continue
}
}
if rMinWidth.MatchString(r.Prelude) {
- minWidth, err := strconv.Atoi(rMinWidth.FindStringSubmatch(r.Prelude)[1])
+ m := rMinWidth.FindStringSubmatch(r.Prelude)
+ l := m[1]+m[2]
+ minWidth, _, err := length(l)
if err != nil {
return nil, fmt.Errorf("atoi: %w", err)
}
- if windowWidth < minWidth {
+ if float64(windowWidth) < minWidth {
continue
}
}
@@ -552,13 +556,14 @@
switch unit {
case "px":
- case "em":
+ case "em", "rem":
+ // TODO: distinguish between em and rem
f *= FontBaseSize
case "vw":
f *= float64(WindowWidth) / 100.0
case "vh":
f *= float64(WindowHeight) / 100.0
- case "%", "rem":
+ case "%":
f = 0
default:
return f, unit, fmt.Errorf("unknown suffix: %v", l)