From 1657129ab78f769a10ac4d32a1f158d5cc80f3e8 Mon Sep 17 00:00:00 2001 From: Stuart George Date: Thu, 6 Jun 2024 09:35:26 +0100 Subject: [PATCH] create separate Props.CSS func --- CHANGELOG.md | 5 +++++ style.go | 32 +++++++++++++++++++++----------- style_test.go | 19 +++++++++++++++++++ 3 files changed, 45 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ef14cfc..1b40d63 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,11 @@ All notable changes to this project will be documented in this file. +### v0.1.2 + +- Move the `Props` CSS writing into a `Props.CSS` func, so it can be used in isolation of the `Style` struct. + - This allows for the `Props` CSS to be used in other contexts, such as in a `style` attribute in HTML. + ### v0.1.1 Added new props: diff --git a/style.go b/style.go index a11e729..4c32289 100644 --- a/style.go +++ b/style.go @@ -118,19 +118,15 @@ type ( } ) -// CSS writes the CSS representation of the style to the writer. -func (s *Style) CSS(w io.Writer) error { - propsValue := reflect.ValueOf(s.Props) - propsType := reflect.TypeOf(s.Props) - - if _, err := fmt.Fprintf(w, "%s{", s.Selector); err != nil { - return err - } +// CSS writes the CSS representation of the props to the writer. +func (p *Props) CSS(w io.Writer) error { + value := reflect.ValueOf(*p) + typ := reflect.TypeOf(*p) // Iterate over the fields of the Props struct and write the CSS properties to the writer. - for i := 0; i < propsValue.NumField(); i++ { - fieldValue := propsValue.Field(i) - fieldType := propsType.Field(i) + for i := 0; i < value.NumField(); i++ { + fieldValue := value.Field(i) + fieldType := typ.Field(i) if fieldValue.IsZero() { continue @@ -145,6 +141,20 @@ func (s *Style) CSS(w io.Writer) error { } } + return nil +} + +// CSS writes the CSS representation of the style to the writer. +func (s *Style) CSS(w io.Writer) error { + if _, err := fmt.Fprintf(w, "%s{", s.Selector); err != nil { + return err + } + + // Write the standard properties to the writer. + if err := s.Props.CSS(w); err != nil { + return err + } + // Write the custom properties to the writer. for _, prop := range s.CustomProps { if _, err := fmt.Fprintf(w, "%s:%s;", prop.Attr, prop.Value); err != nil { diff --git a/style_test.go b/style_test.go index 56fd00f..3efcb46 100644 --- a/style_test.go +++ b/style_test.go @@ -20,6 +20,25 @@ func runTest(t *testing.T, st *Style, expected string) { } } +func TestProps_CSS(t *testing.T) { + testCases := map[Props]string{ + {}: "", + {BackgroundColor: props.ColorRGBA(0, 0, 0, 255)}: "background-color:rgba(0,0,0,1.00);", + {Margin: props.UnitPx(10), Padding: props.UnitPx(10)}: "margin:10px;padding:10px;", + } + for prop, expected := range testCases { + var buf bytes.Buffer + err := prop.CSS(&buf) + if err != nil { + t.Errorf("unexpected error: %v", err) + } + + if buf.String() != expected { + t.Errorf("expected %q, got %q", expected, buf.String()) + } + } +} + func TestStyle_Empty(t *testing.T) { st := &Style{Selector: ".test", Props: Props{}} css := ".test{}"