Skip to content

Commit

Permalink
[css] optimize justify- and align- props (for #2)
Browse files Browse the repository at this point in the history
  • Loading branch information
benoitkugler committed Jan 17, 2025
1 parent 281e5a9 commit f5b17fe
Show file tree
Hide file tree
Showing 10 changed files with 539 additions and 373 deletions.
77 changes: 77 additions & 0 deletions css/properties/keywords/keywords.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package keywords

// Keyword efficiently stores CSS keywords
type Keyword uint8

const (
_ Keyword = iota
Auto
Baseline
Center
End
First
FlexEnd
FlexStart
Last
Left
Legacy
Normal
Right
Safe
SelfEnd
SelfStart
SpaceAround
SpaceBetween
SpaceEvenly
Start
Stretch
Unsafe
)

func NewKeyword(s string) Keyword {
switch s {
case "auto":
return Auto
case "baseline":
return Baseline
case "center":
return Center
case "end":
return End
case "first":
return First
case "flex-end":
return FlexEnd
case "flex-start":
return FlexStart
case "last":
return Last
case "left":
return Left
case "legacy":
return Legacy
case "normal":
return Normal
case "right":
return Right
case "safe":
return Safe
case "self-end":
return SelfEnd
case "self-start":
return SelfStart
case "space-around":
return SpaceAround
case "space-between":
return SpaceBetween
case "space-evenly":
return SpaceEvenly
case "start":
return Start
case "stretch":
return Stretch
case "unsafe":
return Unsafe
}
return 0
}
56 changes: 56 additions & 0 deletions css/properties/keywords/keywords_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package keywords

import (
"fmt"
"sort"
"testing"

"github.com/benoitkugler/webrender/utils"
)

func TestKeywordSorted(t *testing.T) {
l := [...]string{
"center", "space-between", "space-around", "space-evenly",
"stretch", "normal", "flex-start", "flex-end",
"start", "end", "left", "right",
"safe", "unsafe",
"center", "start", "end", "flex-start", "flex-end", "left",
"right",
"normal", "stretch", "center", "start", "end", "self-start",
"self-end", "flex-start", "flex-end", "left", "right",
"legacy",
"baseline",
"center", "start", "end", "self-start", "self-end",
"flex-start", "flex-end", "left", "right",
"auto", "normal", "stretch", "center", "start", "end",
"self-start", "self-end", "flex-start", "flex-end", "left",
"right",
"center", "start", "end", "self-start", "self-end",
"flex-start", "flex-end", "left", "right",
"normal", "stretch", "center", "start", "end", "self-start",
"self-end", "flex-start", "flex-end",
"baseline",
"center", "start", "end", "self-start", "self-end",
"flex-start", "flex-end",
"auto", "normal", "stretch", "center", "start", "end",
"self-start", "self-end", "flex-start", "flex-end",
"center", "start", "end", "self-start", "self-end",
"flex-start", "flex-end",
"center", "space-between", "space-around", "space-evenly",
"stretch", "normal", "flex-start", "flex-end",
"start", "end",
"baseline",
"center", "start", "end", "flex-start", "flex-end",
"first", "last",
}

m := utils.NewSet(l[:]...)
var out []string
for l := range m {
out = append(out, l)
}
sort.Strings(out)
for _, s := range out {
fmt.Printf("case %q:\nreturn %s\n", s, s)
}
}
17 changes: 10 additions & 7 deletions css/properties/properties.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package properties

import "github.com/benoitkugler/webrender/css/parser"
import (
"github.com/benoitkugler/webrender/css/parser"
kw "github.com/benoitkugler/webrender/css/properties/keywords"
)

// This file is used to generate typed accessors
//go:generate go run gen/gen.go
Expand Down Expand Up @@ -437,12 +440,12 @@ var InitialValues = Properties{
PGridColumnEnd: GridLine{Tag: Auto},

// CSS Box Alignment Module Level 3 (WD): https://www.w3.org/TR/css-align-3/
PAlignContent: Strings{"normal"},
PAlignItems: Strings{"normal"},
PAlignSelf: Strings{"auto"},
PJustifyContent: Strings{"normal"},
PJustifyItems: Strings{"normal"},
PJustifySelf: Strings{"auto"},
PAlignContent: JustifyOrAlign{kw.Normal},
PAlignItems: JustifyOrAlign{kw.Normal},
PAlignSelf: JustifyOrAlign{kw.Auto},
PJustifyContent: JustifyOrAlign{kw.Normal},
PJustifyItems: JustifyOrAlign{kw.Normal},
PJustifySelf: JustifyOrAlign{kw.Auto},
POrder: Int(0),
PColumnGap: DimOrS{S: "normal"},
PRowGap: DimOrS{S: "normal"},
Expand Down
48 changes: 24 additions & 24 deletions css/properties/props_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions css/properties/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

pa "github.com/benoitkugler/webrender/css/parser"
"github.com/benoitkugler/webrender/css/properties/keywords"
)

// ------------- Top levels types, implementing CssProperty ------------
Expand Down Expand Up @@ -122,6 +123,22 @@ func (ft FontFeature) String() string {
// An empty slice means 'normal'
type FontFeatures []FontFeature

// JustifyOrAlign stores properties for 'justify-*' or 'align-*'
type JustifyOrAlign [2]keywords.Keyword

func (ja JustifyOrAlign) Has(kw keywords.Keyword) bool { return ja[0] == kw || ja[1] == kw }

// Intersects returns true if at least one value in [kws]
// is also in the list.
func (ja JustifyOrAlign) Intersects(kws ...keywords.Keyword) bool {
for _, kw := range kws {
if ja.Has(kw) {
return true
}
}
return false
}

type Page string

// Dimension or "auto" or "cover" or "contain"
Expand Down Expand Up @@ -605,6 +622,7 @@ func (Images) isCssProperty() {}
func (Int) isCssProperty() {}
func (IntString) isCssProperty() {}
func (Marks) isCssProperty() {}
func (JustifyOrAlign) isCssProperty() {}
func (Decorations) isCssProperty() {}
func (NamedString) isCssProperty() {}
func (CounterStyleID) isCssProperty() {}
Expand Down Expand Up @@ -651,6 +669,7 @@ func (Int) isDeclaredValue() {}
func (IntString) isDeclaredValue() {}
func (Limits) isDeclaredValue() {}
func (Marks) isDeclaredValue() {}
func (JustifyOrAlign) isDeclaredValue() {}
func (Decorations) isDeclaredValue() {}
func (NamedString) isDeclaredValue() {}
func (CounterStyleID) isDeclaredValue() {}
Expand Down
Loading

0 comments on commit f5b17fe

Please sign in to comment.