diff --git a/cstyle/transformers/flex/main.go b/cstyle/transformers/flex/main.go
index 1aa0604..7629b80 100644
--- a/cstyle/transformers/flex/main.go
+++ b/cstyle/transformers/flex/main.go
@@ -7 +6,0 @@ import (
- "grim/lgc"
@@ -14,2 +13,2 @@ func Init() cstyle.Transformer {
- Selector: func(n *element.Node, c *cstyle.CSS) bool {
- return n.Style("flex") != ""
+ Selector: func(n *element.Node) bool {
+ return n.CStyle["flex"] != ""
@@ -18 +17 @@ func Init() cstyle.Transformer {
- flex, err := parseFlex(n.Style("flex"))
+ flex, _ := parseFlex(n.CStyle["flex"])
@@ -20,13 +19,3 @@ func Init() cstyle.Transformer {
- if err != nil {
- panic(lgc.Error(
- lgc.Desc(
- lgc.Red("Error Parsing flex values"),
- lgc.CrossMark("Input: "+lgc.String(n.Style("flex"))),
- err,
- ),
- ))
- }
-
- n.Style("flex-basis", flex.FlexBasis)
- n.Style("flex-grow", flex.FlexGrow)
- n.Style("flex-shrink", flex.FlexShrink)
+ n.CStyle["flex-basis"] = flex.FlexBasis
+ n.CStyle["flex-grow"] = flex.FlexGrow
+ n.CStyle["flex-shrink"] = flex.FlexShrink
package flexprep
import (
"fmt"
"grim/cstyle"
"grim/element"
"grim/lgc"
"strconv"
"strings"
)
func Init() cstyle.Transformer {
return cstyle.Transformer{
Selector: func(n *element.Node, c *cstyle.CSS) bool {
return n.Style("flex") != ""
},
Handler: func(n *element.Node, c *cstyle.CSS) *element.Node {
flex, err := parseFlex(n.Style("flex"))
if err != nil {
panic(lgc.Error(
lgc.Desc(
lgc.Red("Error Parsing flex values"),
lgc.CrossMark("Input: "+lgc.String(n.Style("flex"))),
err,
),
))
}
n.Style("flex-basis", flex.FlexBasis)
n.Style("flex-grow", flex.FlexGrow)
n.Style("flex-shrink", flex.FlexShrink)
return n
},
}
}
type FlexProperties struct {
FlexGrow string
FlexShrink string
FlexBasis string
}
func parseFlex(flex string) (FlexProperties, error) {
parts := strings.Fields(flex)
prop := FlexProperties{
FlexGrow: "1", // default value
FlexShrink: "1", // default value
FlexBasis: "0%", // default value
}
switch len(parts) {
case 1:
if strings.HasSuffix(parts[0], "%") || strings.HasSuffix(parts[0], "px") || strings.HasSuffix(parts[0], "em") {
prop.FlexBasis = parts[0]
} else if _, err := strconv.ParseFloat(parts[0], 64); err == nil {
prop.FlexGrow = parts[0]
prop.FlexShrink = "1"
prop.FlexBasis = "0%"
} else {
return prop, fmt.Errorf("invalid flex value: %s", parts[0])
}
case 2:
prop.FlexGrow = parts[0]
prop.FlexShrink = parts[1]
prop.FlexBasis = "0%"
case 3:
prop.FlexGrow = parts[0]
prop.FlexShrink = parts[1]
prop.FlexBasis = parts[2]
default:
return prop, fmt.Errorf("invalid number of values for flex property")
}
return prop, nil
}